WebGLShaders.js 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author mikael emtinger / http://gomo.se/
  5. */
  6. THREE.ShaderChunk = {
  7. // FOG
  8. fog_pars_fragment: [
  9. "#ifdef USE_FOG",
  10. "uniform vec3 fogColor;",
  11. "#ifdef FOG_EXP2",
  12. "uniform float fogDensity;",
  13. "#else",
  14. "uniform float fogNear;",
  15. "uniform float fogFar;",
  16. "#endif",
  17. "#endif"
  18. ].join("\n"),
  19. fog_fragment: [
  20. "#ifdef USE_FOG",
  21. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  22. "#ifdef FOG_EXP2",
  23. "const float LOG2 = 1.442695;",
  24. "float fogFactor = exp2( - fogDensity * fogDensity * depth * depth * LOG2 );",
  25. "fogFactor = 1.0 - clamp( fogFactor, 0.0, 1.0 );",
  26. "#else",
  27. "float fogFactor = smoothstep( fogNear, fogFar, depth );",
  28. "#endif",
  29. "gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );",
  30. "#endif"
  31. ].join("\n"),
  32. // ENVIRONMENT MAP
  33. envmap_pars_fragment: [
  34. "#ifdef USE_ENVMAP",
  35. "uniform float reflectivity;",
  36. "uniform samplerCube envMap;",
  37. "uniform float flipEnvMap;",
  38. "uniform int combine;",
  39. "#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP )",
  40. "uniform bool useRefract;",
  41. "uniform float refractionRatio;",
  42. "#else",
  43. "varying vec3 vReflect;",
  44. "#endif",
  45. "#endif"
  46. ].join("\n"),
  47. envmap_fragment: [
  48. "#ifdef USE_ENVMAP",
  49. "vec3 reflectVec;",
  50. "#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP )",
  51. "vec3 cameraToVertex = normalize( vWorldPosition - cameraPosition );",
  52. "if ( useRefract ) {",
  53. "reflectVec = refract( cameraToVertex, normal, refractionRatio );",
  54. "} else { ",
  55. "reflectVec = reflect( cameraToVertex, normal );",
  56. "}",
  57. "#else",
  58. "reflectVec = vReflect;",
  59. "#endif",
  60. "#ifdef DOUBLE_SIDED",
  61. "float flipNormal = ( -1.0 + 2.0 * float( gl_FrontFacing ) );",
  62. "vec4 cubeColor = textureCube( envMap, flipNormal * vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );",
  63. "#else",
  64. "vec4 cubeColor = textureCube( envMap, vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );",
  65. "#endif",
  66. "#ifdef GAMMA_INPUT",
  67. "cubeColor.xyz *= cubeColor.xyz;",
  68. "#endif",
  69. "if ( combine == 1 ) {",
  70. "gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularStrength * reflectivity );",
  71. "} else if ( combine == 2 ) {",
  72. "gl_FragColor.xyz += cubeColor.xyz * specularStrength * reflectivity;",
  73. "} else {",
  74. "gl_FragColor.xyz = mix( gl_FragColor.xyz, gl_FragColor.xyz * cubeColor.xyz, specularStrength * reflectivity );",
  75. "}",
  76. "#endif"
  77. ].join("\n"),
  78. envmap_pars_vertex: [
  79. "#if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP )",
  80. "varying vec3 vReflect;",
  81. "uniform float refractionRatio;",
  82. "uniform bool useRefract;",
  83. "#endif"
  84. ].join("\n"),
  85. worldpos_vertex : [
  86. "#if defined( USE_ENVMAP ) || defined( PHONG ) || defined( LAMBERT ) || defined ( USE_SHADOWMAP )",
  87. "#ifdef USE_SKINNING",
  88. "vec4 worldPosition = modelMatrix * skinned;",
  89. "#endif",
  90. "#if defined( USE_MORPHTARGETS ) && ! defined( USE_SKINNING )",
  91. "vec4 worldPosition = modelMatrix * vec4( morphed, 1.0 );",
  92. "#endif",
  93. "#if ! defined( USE_MORPHTARGETS ) && ! defined( USE_SKINNING )",
  94. "vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  95. "#endif",
  96. "#endif"
  97. ].join("\n"),
  98. envmap_vertex : [
  99. "#if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP )",
  100. "vec3 worldNormal = mat3( modelMatrix[ 0 ].xyz, modelMatrix[ 1 ].xyz, modelMatrix[ 2 ].xyz ) * objectNormal;",
  101. "worldNormal = normalize( worldNormal );",
  102. "vec3 cameraToVertex = normalize( worldPosition.xyz - cameraPosition );",
  103. "if ( useRefract ) {",
  104. "vReflect = refract( cameraToVertex, worldNormal, refractionRatio );",
  105. "} else {",
  106. "vReflect = reflect( cameraToVertex, worldNormal );",
  107. "}",
  108. "#endif"
  109. ].join("\n"),
  110. // COLOR MAP (particles)
  111. map_particle_pars_fragment: [
  112. "#ifdef USE_MAP",
  113. "uniform sampler2D map;",
  114. "#endif"
  115. ].join("\n"),
  116. map_particle_fragment: [
  117. "#ifdef USE_MAP",
  118. "gl_FragColor = gl_FragColor * texture2D( map, vec2( gl_PointCoord.x, 1.0 - gl_PointCoord.y ) );",
  119. "#endif"
  120. ].join("\n"),
  121. // COLOR MAP (triangles)
  122. map_pars_vertex: [
  123. "#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP )",
  124. "varying vec2 vUv;",
  125. "uniform vec4 offsetRepeat;",
  126. "#endif"
  127. ].join("\n"),
  128. map_pars_fragment: [
  129. "#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP )",
  130. "varying vec2 vUv;",
  131. "#endif",
  132. "#ifdef USE_MAP",
  133. "uniform sampler2D map;",
  134. "#endif"
  135. ].join("\n"),
  136. map_vertex: [
  137. "#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP )",
  138. "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
  139. "#endif"
  140. ].join("\n"),
  141. map_fragment: [
  142. "#ifdef USE_MAP",
  143. "#ifdef GAMMA_INPUT",
  144. "vec4 texelColor = texture2D( map, vUv );",
  145. "texelColor.xyz *= texelColor.xyz;",
  146. "gl_FragColor = gl_FragColor * texelColor;",
  147. "#else",
  148. "gl_FragColor = gl_FragColor * texture2D( map, vUv );",
  149. "#endif",
  150. "#endif"
  151. ].join("\n"),
  152. // LIGHT MAP
  153. lightmap_pars_fragment: [
  154. "#ifdef USE_LIGHTMAP",
  155. "varying vec2 vUv2;",
  156. "uniform sampler2D lightMap;",
  157. "#endif"
  158. ].join("\n"),
  159. lightmap_pars_vertex: [
  160. "#ifdef USE_LIGHTMAP",
  161. "varying vec2 vUv2;",
  162. "#endif"
  163. ].join("\n"),
  164. lightmap_fragment: [
  165. "#ifdef USE_LIGHTMAP",
  166. "gl_FragColor = gl_FragColor * texture2D( lightMap, vUv2 );",
  167. "#endif"
  168. ].join("\n"),
  169. lightmap_vertex: [
  170. "#ifdef USE_LIGHTMAP",
  171. "vUv2 = uv2;",
  172. "#endif"
  173. ].join("\n"),
  174. // BUMP MAP
  175. bumpmap_pars_fragment: [
  176. "#ifdef USE_BUMPMAP",
  177. "uniform sampler2D bumpMap;",
  178. "uniform float bumpScale;",
  179. // Derivative maps - bump mapping unparametrized surfaces by Morten Mikkelsen
  180. // http://mmikkelsen3d.blogspot.sk/2011/07/derivative-maps.html
  181. // Evaluate the derivative of the height w.r.t. screen-space using forward differencing (listing 2)
  182. "vec2 dHdxy_fwd() {",
  183. "vec2 dSTdx = dFdx( vUv );",
  184. "vec2 dSTdy = dFdy( vUv );",
  185. "float Hll = bumpScale * texture2D( bumpMap, vUv ).x;",
  186. "float dBx = bumpScale * texture2D( bumpMap, vUv + dSTdx ).x - Hll;",
  187. "float dBy = bumpScale * texture2D( bumpMap, vUv + dSTdy ).x - Hll;",
  188. "return vec2( dBx, dBy );",
  189. "}",
  190. "vec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy ) {",
  191. "vec3 vSigmaX = dFdx( surf_pos );",
  192. "vec3 vSigmaY = dFdy( surf_pos );",
  193. "vec3 vN = surf_norm;", // normalized
  194. "vec3 R1 = cross( vSigmaY, vN );",
  195. "vec3 R2 = cross( vN, vSigmaX );",
  196. "float fDet = dot( vSigmaX, R1 );",
  197. "vec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 );",
  198. "return normalize( abs( fDet ) * surf_norm - vGrad );",
  199. "}",
  200. "#endif"
  201. ].join("\n"),
  202. // NORMAL MAP
  203. normalmap_pars_fragment: [
  204. "#ifdef USE_NORMALMAP",
  205. "uniform sampler2D normalMap;",
  206. "uniform vec2 normalScale;",
  207. // Per-Pixel Tangent Space Normal Mapping
  208. // http://hacksoflife.blogspot.ch/2009/11/per-pixel-tangent-space-normal-mapping.html
  209. "vec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm ) {",
  210. "vec3 q0 = dFdx( eye_pos.xyz );",
  211. "vec3 q1 = dFdy( eye_pos.xyz );",
  212. "vec2 st0 = dFdx( vUv.st );",
  213. "vec2 st1 = dFdy( vUv.st );",
  214. "vec3 S = normalize( q0 * st1.t - q1 * st0.t );",
  215. "vec3 T = normalize( -q0 * st1.s + q1 * st0.s );",
  216. "vec3 N = normalize( surf_norm );",
  217. "vec3 mapN = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;",
  218. "mapN.xy = normalScale * mapN.xy;",
  219. "mat3 tsn = mat3( S, T, N );",
  220. "return normalize( tsn * mapN );",
  221. "}",
  222. "#endif"
  223. ].join("\n"),
  224. // SPECULAR MAP
  225. specularmap_pars_fragment: [
  226. "#ifdef USE_SPECULARMAP",
  227. "uniform sampler2D specularMap;",
  228. "#endif"
  229. ].join("\n"),
  230. specularmap_fragment: [
  231. "float specularStrength;",
  232. "#ifdef USE_SPECULARMAP",
  233. "vec4 texelSpecular = texture2D( specularMap, vUv );",
  234. "specularStrength = texelSpecular.r;",
  235. "#else",
  236. "specularStrength = 1.0;",
  237. "#endif"
  238. ].join("\n"),
  239. // LIGHTS LAMBERT
  240. lights_lambert_pars_vertex: [
  241. "uniform vec3 ambient;",
  242. "uniform vec3 diffuse;",
  243. "uniform vec3 emissive;",
  244. "uniform vec3 ambientLightColor;",
  245. "#if MAX_DIR_LIGHTS > 0",
  246. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  247. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  248. "#endif",
  249. "#if MAX_HEMI_LIGHTS > 0",
  250. "uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];",
  251. "uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];",
  252. "uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];",
  253. "#endif",
  254. "#if MAX_POINT_LIGHTS > 0",
  255. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  256. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  257. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  258. "#endif",
  259. "#if MAX_SPOT_LIGHTS > 0",
  260. "uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];",
  261. "uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];",
  262. "uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];",
  263. "uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
  264. "uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];",
  265. "uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];",
  266. "#endif",
  267. "#ifdef WRAP_AROUND",
  268. "uniform vec3 wrapRGB;",
  269. "#endif"
  270. ].join("\n"),
  271. lights_lambert_vertex: [
  272. "vLightFront = vec3( 0.0 );",
  273. "#ifdef DOUBLE_SIDED",
  274. "vLightBack = vec3( 0.0 );",
  275. "#endif",
  276. "transformedNormal = normalize( transformedNormal );",
  277. "#if MAX_DIR_LIGHTS > 0",
  278. "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
  279. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  280. "vec3 dirVector = normalize( lDirection.xyz );",
  281. "float dotProduct = dot( transformedNormal, dirVector );",
  282. "vec3 directionalLightWeighting = vec3( max( dotProduct, 0.0 ) );",
  283. "#ifdef DOUBLE_SIDED",
  284. "vec3 directionalLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
  285. "#ifdef WRAP_AROUND",
  286. "vec3 directionalLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
  287. "#endif",
  288. "#endif",
  289. "#ifdef WRAP_AROUND",
  290. "vec3 directionalLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
  291. "directionalLightWeighting = mix( directionalLightWeighting, directionalLightWeightingHalf, wrapRGB );",
  292. "#ifdef DOUBLE_SIDED",
  293. "directionalLightWeightingBack = mix( directionalLightWeightingBack, directionalLightWeightingHalfBack, wrapRGB );",
  294. "#endif",
  295. "#endif",
  296. "vLightFront += directionalLightColor[ i ] * directionalLightWeighting;",
  297. "#ifdef DOUBLE_SIDED",
  298. "vLightBack += directionalLightColor[ i ] * directionalLightWeightingBack;",
  299. "#endif",
  300. "}",
  301. "#endif",
  302. "#if MAX_POINT_LIGHTS > 0",
  303. "for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  304. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  305. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  306. "float lDistance = 1.0;",
  307. "if ( pointLightDistance[ i ] > 0.0 )",
  308. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  309. "lVector = normalize( lVector );",
  310. "float dotProduct = dot( transformedNormal, lVector );",
  311. "vec3 pointLightWeighting = vec3( max( dotProduct, 0.0 ) );",
  312. "#ifdef DOUBLE_SIDED",
  313. "vec3 pointLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
  314. "#ifdef WRAP_AROUND",
  315. "vec3 pointLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
  316. "#endif",
  317. "#endif",
  318. "#ifdef WRAP_AROUND",
  319. "vec3 pointLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
  320. "pointLightWeighting = mix( pointLightWeighting, pointLightWeightingHalf, wrapRGB );",
  321. "#ifdef DOUBLE_SIDED",
  322. "pointLightWeightingBack = mix( pointLightWeightingBack, pointLightWeightingHalfBack, wrapRGB );",
  323. "#endif",
  324. "#endif",
  325. "vLightFront += pointLightColor[ i ] * pointLightWeighting * lDistance;",
  326. "#ifdef DOUBLE_SIDED",
  327. "vLightBack += pointLightColor[ i ] * pointLightWeightingBack * lDistance;",
  328. "#endif",
  329. "}",
  330. "#endif",
  331. "#if MAX_SPOT_LIGHTS > 0",
  332. "for( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",
  333. "vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );",
  334. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  335. "float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - worldPosition.xyz ) );",
  336. "if ( spotEffect > spotLightAngleCos[ i ] ) {",
  337. "spotEffect = max( pow( spotEffect, spotLightExponent[ i ] ), 0.0 );",
  338. "float lDistance = 1.0;",
  339. "if ( spotLightDistance[ i ] > 0.0 )",
  340. "lDistance = 1.0 - min( ( length( lVector ) / spotLightDistance[ i ] ), 1.0 );",
  341. "lVector = normalize( lVector );",
  342. "float dotProduct = dot( transformedNormal, lVector );",
  343. "vec3 spotLightWeighting = vec3( max( dotProduct, 0.0 ) );",
  344. "#ifdef DOUBLE_SIDED",
  345. "vec3 spotLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
  346. "#ifdef WRAP_AROUND",
  347. "vec3 spotLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
  348. "#endif",
  349. "#endif",
  350. "#ifdef WRAP_AROUND",
  351. "vec3 spotLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
  352. "spotLightWeighting = mix( spotLightWeighting, spotLightWeightingHalf, wrapRGB );",
  353. "#ifdef DOUBLE_SIDED",
  354. "spotLightWeightingBack = mix( spotLightWeightingBack, spotLightWeightingHalfBack, wrapRGB );",
  355. "#endif",
  356. "#endif",
  357. "vLightFront += spotLightColor[ i ] * spotLightWeighting * lDistance * spotEffect;",
  358. "#ifdef DOUBLE_SIDED",
  359. "vLightBack += spotLightColor[ i ] * spotLightWeightingBack * lDistance * spotEffect;",
  360. "#endif",
  361. "}",
  362. "}",
  363. "#endif",
  364. "#if MAX_HEMI_LIGHTS > 0",
  365. "for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {",
  366. "vec4 lDirection = viewMatrix * vec4( hemisphereLightDirection[ i ], 0.0 );",
  367. "vec3 lVector = normalize( lDirection.xyz );",
  368. "float dotProduct = dot( transformedNormal, lVector );",
  369. "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
  370. "float hemiDiffuseWeightBack = -0.5 * dotProduct + 0.5;",
  371. "vLightFront += mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
  372. "#ifdef DOUBLE_SIDED",
  373. "vLightBack += mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeightBack );",
  374. "#endif",
  375. "}",
  376. "#endif",
  377. "vLightFront = vLightFront * diffuse + ambient * ambientLightColor + emissive;",
  378. "#ifdef DOUBLE_SIDED",
  379. "vLightBack = vLightBack * diffuse + ambient * ambientLightColor + emissive;",
  380. "#endif"
  381. ].join("\n"),
  382. // LIGHTS PHONG
  383. lights_phong_pars_vertex: [
  384. "#ifndef PHONG_PER_PIXEL",
  385. "#if MAX_POINT_LIGHTS > 0",
  386. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  387. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  388. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  389. "#endif",
  390. "#if MAX_SPOT_LIGHTS > 0",
  391. "uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];",
  392. "uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
  393. "varying vec4 vSpotLight[ MAX_SPOT_LIGHTS ];",
  394. "#endif",
  395. "#endif",
  396. "#if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP )",
  397. "varying vec3 vWorldPosition;",
  398. "#endif"
  399. ].join("\n"),
  400. lights_phong_vertex: [
  401. "#ifndef PHONG_PER_PIXEL",
  402. "#if MAX_POINT_LIGHTS > 0",
  403. "for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  404. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  405. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  406. "float lDistance = 1.0;",
  407. "if ( pointLightDistance[ i ] > 0.0 )",
  408. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  409. "vPointLight[ i ] = vec4( lVector, lDistance );",
  410. "}",
  411. "#endif",
  412. "#if MAX_SPOT_LIGHTS > 0",
  413. "for( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",
  414. "vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );",
  415. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  416. "float lDistance = 1.0;",
  417. "if ( spotLightDistance[ i ] > 0.0 )",
  418. "lDistance = 1.0 - min( ( length( lVector ) / spotLightDistance[ i ] ), 1.0 );",
  419. "vSpotLight[ i ] = vec4( lVector, lDistance );",
  420. "}",
  421. "#endif",
  422. "#endif",
  423. "#if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP )",
  424. "vWorldPosition = worldPosition.xyz;",
  425. "#endif"
  426. ].join("\n"),
  427. lights_phong_pars_fragment: [
  428. "uniform vec3 ambientLightColor;",
  429. "#if MAX_DIR_LIGHTS > 0",
  430. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  431. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  432. "#endif",
  433. "#if MAX_HEMI_LIGHTS > 0",
  434. "uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];",
  435. "uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];",
  436. "uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];",
  437. "#endif",
  438. "#if MAX_POINT_LIGHTS > 0",
  439. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  440. "#ifdef PHONG_PER_PIXEL",
  441. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  442. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  443. "#else",
  444. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  445. "#endif",
  446. "#endif",
  447. "#if MAX_SPOT_LIGHTS > 0",
  448. "uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];",
  449. "uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];",
  450. "uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];",
  451. "uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];",
  452. "uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];",
  453. "#ifdef PHONG_PER_PIXEL",
  454. "uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
  455. "#else",
  456. "varying vec4 vSpotLight[ MAX_SPOT_LIGHTS ];",
  457. "#endif",
  458. "#endif",
  459. "#if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP )",
  460. "varying vec3 vWorldPosition;",
  461. "#endif",
  462. "#ifdef WRAP_AROUND",
  463. "uniform vec3 wrapRGB;",
  464. "#endif",
  465. "varying vec3 vViewPosition;",
  466. "varying vec3 vNormal;"
  467. ].join("\n"),
  468. lights_phong_fragment: [
  469. "vec3 normal = normalize( vNormal );",
  470. "vec3 viewPosition = normalize( vViewPosition );",
  471. "#ifdef DOUBLE_SIDED",
  472. "normal = normal * ( -1.0 + 2.0 * float( gl_FrontFacing ) );",
  473. "#endif",
  474. "#ifdef USE_NORMALMAP",
  475. "normal = perturbNormal2Arb( -viewPosition, normal );",
  476. "#elif defined( USE_BUMPMAP )",
  477. "normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );",
  478. "#endif",
  479. "#if MAX_POINT_LIGHTS > 0",
  480. "vec3 pointDiffuse = vec3( 0.0 );",
  481. "vec3 pointSpecular = vec3( 0.0 );",
  482. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  483. "#ifdef PHONG_PER_PIXEL",
  484. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  485. "vec3 lVector = lPosition.xyz + vViewPosition.xyz;",
  486. "float lDistance = 1.0;",
  487. "if ( pointLightDistance[ i ] > 0.0 )",
  488. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  489. "lVector = normalize( lVector );",
  490. "#else",
  491. "vec3 lVector = normalize( vPointLight[ i ].xyz );",
  492. "float lDistance = vPointLight[ i ].w;",
  493. "#endif",
  494. // diffuse
  495. "float dotProduct = dot( normal, lVector );",
  496. "#ifdef WRAP_AROUND",
  497. "float pointDiffuseWeightFull = max( dotProduct, 0.0 );",
  498. "float pointDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
  499. "vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",
  500. "#else",
  501. "float pointDiffuseWeight = max( dotProduct, 0.0 );",
  502. "#endif",
  503. "pointDiffuse += diffuse * pointLightColor[ i ] * pointDiffuseWeight * lDistance;",
  504. // specular
  505. "vec3 pointHalfVector = normalize( lVector + viewPosition );",
  506. "float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  507. "float pointSpecularWeight = specularStrength * max( pow( pointDotNormalHalf, shininess ), 0.0 );",
  508. "#ifdef PHYSICALLY_BASED_SHADING",
  509. // 2.0 => 2.0001 is hack to work around ANGLE bug
  510. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  511. "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVector, pointHalfVector ), 5.0 );",
  512. "pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * lDistance * specularNormalization;",
  513. "#else",
  514. "pointSpecular += specular * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * lDistance;",
  515. "#endif",
  516. "}",
  517. "#endif",
  518. "#if MAX_SPOT_LIGHTS > 0",
  519. "vec3 spotDiffuse = vec3( 0.0 );",
  520. "vec3 spotSpecular = vec3( 0.0 );",
  521. "for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",
  522. "#ifdef PHONG_PER_PIXEL",
  523. "vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );",
  524. "vec3 lVector = lPosition.xyz + vViewPosition.xyz;",
  525. "float lDistance = 1.0;",
  526. "if ( spotLightDistance[ i ] > 0.0 )",
  527. "lDistance = 1.0 - min( ( length( lVector ) / spotLightDistance[ i ] ), 1.0 );",
  528. "lVector = normalize( lVector );",
  529. "#else",
  530. "vec3 lVector = normalize( vSpotLight[ i ].xyz );",
  531. "float lDistance = vSpotLight[ i ].w;",
  532. "#endif",
  533. "float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - vWorldPosition ) );",
  534. "if ( spotEffect > spotLightAngleCos[ i ] ) {",
  535. "spotEffect = max( pow( spotEffect, spotLightExponent[ i ] ), 0.0 );",
  536. // diffuse
  537. "float dotProduct = dot( normal, lVector );",
  538. "#ifdef WRAP_AROUND",
  539. "float spotDiffuseWeightFull = max( dotProduct, 0.0 );",
  540. "float spotDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
  541. "vec3 spotDiffuseWeight = mix( vec3 ( spotDiffuseWeightFull ), vec3( spotDiffuseWeightHalf ), wrapRGB );",
  542. "#else",
  543. "float spotDiffuseWeight = max( dotProduct, 0.0 );",
  544. "#endif",
  545. "spotDiffuse += diffuse * spotLightColor[ i ] * spotDiffuseWeight * lDistance * spotEffect;",
  546. // specular
  547. "vec3 spotHalfVector = normalize( lVector + viewPosition );",
  548. "float spotDotNormalHalf = max( dot( normal, spotHalfVector ), 0.0 );",
  549. "float spotSpecularWeight = specularStrength * max( pow( spotDotNormalHalf, shininess ), 0.0 );",
  550. "#ifdef PHYSICALLY_BASED_SHADING",
  551. // 2.0 => 2.0001 is hack to work around ANGLE bug
  552. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  553. "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVector, spotHalfVector ), 5.0 );",
  554. "spotSpecular += schlick * spotLightColor[ i ] * spotSpecularWeight * spotDiffuseWeight * lDistance * specularNormalization * spotEffect;",
  555. "#else",
  556. "spotSpecular += specular * spotLightColor[ i ] * spotSpecularWeight * spotDiffuseWeight * lDistance * spotEffect;",
  557. "#endif",
  558. "}",
  559. "}",
  560. "#endif",
  561. "#if MAX_DIR_LIGHTS > 0",
  562. "vec3 dirDiffuse = vec3( 0.0 );",
  563. "vec3 dirSpecular = vec3( 0.0 );" ,
  564. "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
  565. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  566. "vec3 dirVector = normalize( lDirection.xyz );",
  567. // diffuse
  568. "float dotProduct = dot( normal, dirVector );",
  569. "#ifdef WRAP_AROUND",
  570. "float dirDiffuseWeightFull = max( dotProduct, 0.0 );",
  571. "float dirDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
  572. "vec3 dirDiffuseWeight = mix( vec3( dirDiffuseWeightFull ), vec3( dirDiffuseWeightHalf ), wrapRGB );",
  573. "#else",
  574. "float dirDiffuseWeight = max( dotProduct, 0.0 );",
  575. "#endif",
  576. "dirDiffuse += diffuse * directionalLightColor[ i ] * dirDiffuseWeight;",
  577. // specular
  578. "vec3 dirHalfVector = normalize( dirVector + viewPosition );",
  579. "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  580. "float dirSpecularWeight = specularStrength * max( pow( dirDotNormalHalf, shininess ), 0.0 );",
  581. "#ifdef PHYSICALLY_BASED_SHADING",
  582. /*
  583. // fresnel term from skin shader
  584. "const float F0 = 0.128;",
  585. "float base = 1.0 - dot( viewPosition, dirHalfVector );",
  586. "float exponential = pow( base, 5.0 );",
  587. "float fresnel = exponential + F0 * ( 1.0 - exponential );",
  588. */
  589. /*
  590. // fresnel term from fresnel shader
  591. "const float mFresnelBias = 0.08;",
  592. "const float mFresnelScale = 0.3;",
  593. "const float mFresnelPower = 5.0;",
  594. "float fresnel = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( -viewPosition ), normal ), mFresnelPower );",
  595. */
  596. // 2.0 => 2.0001 is hack to work around ANGLE bug
  597. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  598. //"dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization * fresnel;",
  599. "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( dirVector, dirHalfVector ), 5.0 );",
  600. "dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",
  601. "#else",
  602. "dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight;",
  603. "#endif",
  604. "}",
  605. "#endif",
  606. "#if MAX_HEMI_LIGHTS > 0",
  607. "vec3 hemiDiffuse = vec3( 0.0 );",
  608. "vec3 hemiSpecular = vec3( 0.0 );" ,
  609. "for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {",
  610. "vec4 lDirection = viewMatrix * vec4( hemisphereLightDirection[ i ], 0.0 );",
  611. "vec3 lVector = normalize( lDirection.xyz );",
  612. // diffuse
  613. "float dotProduct = dot( normal, lVector );",
  614. "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
  615. "vec3 hemiColor = mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
  616. "hemiDiffuse += diffuse * hemiColor;",
  617. // specular (sky light)
  618. "vec3 hemiHalfVectorSky = normalize( lVector + viewPosition );",
  619. "float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;",
  620. "float hemiSpecularWeightSky = specularStrength * max( pow( hemiDotNormalHalfSky, shininess ), 0.0 );",
  621. // specular (ground light)
  622. "vec3 lVectorGround = -lVector;",
  623. "vec3 hemiHalfVectorGround = normalize( lVectorGround + viewPosition );",
  624. "float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;",
  625. "float hemiSpecularWeightGround = specularStrength * max( pow( hemiDotNormalHalfGround, shininess ), 0.0 );",
  626. "#ifdef PHYSICALLY_BASED_SHADING",
  627. "float dotProductGround = dot( normal, lVectorGround );",
  628. // 2.0 => 2.0001 is hack to work around ANGLE bug
  629. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  630. "vec3 schlickSky = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVector, hemiHalfVectorSky ), 5.0 );",
  631. "vec3 schlickGround = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVectorGround, hemiHalfVectorGround ), 5.0 );",
  632. "hemiSpecular += hemiColor * specularNormalization * ( schlickSky * hemiSpecularWeightSky * max( dotProduct, 0.0 ) + schlickGround * hemiSpecularWeightGround * max( dotProductGround, 0.0 ) );",
  633. "#else",
  634. "hemiSpecular += specular * hemiColor * ( hemiSpecularWeightSky + hemiSpecularWeightGround ) * hemiDiffuseWeight;",
  635. "#endif",
  636. "}",
  637. "#endif",
  638. "vec3 totalDiffuse = vec3( 0.0 );",
  639. "vec3 totalSpecular = vec3( 0.0 );",
  640. "#if MAX_DIR_LIGHTS > 0",
  641. "totalDiffuse += dirDiffuse;",
  642. "totalSpecular += dirSpecular;",
  643. "#endif",
  644. "#if MAX_HEMI_LIGHTS > 0",
  645. "totalDiffuse += hemiDiffuse;",
  646. "totalSpecular += hemiSpecular;",
  647. "#endif",
  648. "#if MAX_POINT_LIGHTS > 0",
  649. "totalDiffuse += pointDiffuse;",
  650. "totalSpecular += pointSpecular;",
  651. "#endif",
  652. "#if MAX_SPOT_LIGHTS > 0",
  653. "totalDiffuse += spotDiffuse;",
  654. "totalSpecular += spotSpecular;",
  655. "#endif",
  656. "#ifdef METAL",
  657. "gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient + totalSpecular );",
  658. "#else",
  659. "gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient ) + totalSpecular;",
  660. "#endif"
  661. ].join("\n"),
  662. // VERTEX COLORS
  663. color_pars_fragment: [
  664. "#ifdef USE_COLOR",
  665. "varying vec3 vColor;",
  666. "#endif"
  667. ].join("\n"),
  668. color_fragment: [
  669. "#ifdef USE_COLOR",
  670. "gl_FragColor = gl_FragColor * vec4( vColor, opacity );",
  671. "#endif"
  672. ].join("\n"),
  673. color_pars_vertex: [
  674. "#ifdef USE_COLOR",
  675. "varying vec3 vColor;",
  676. "#endif"
  677. ].join("\n"),
  678. color_vertex: [
  679. "#ifdef USE_COLOR",
  680. "#ifdef GAMMA_INPUT",
  681. "vColor = color * color;",
  682. "#else",
  683. "vColor = color;",
  684. "#endif",
  685. "#endif"
  686. ].join("\n"),
  687. // SKINNING
  688. skinning_pars_vertex: [
  689. "#ifdef USE_SKINNING",
  690. "#ifdef BONE_TEXTURE",
  691. "uniform sampler2D boneTexture;",
  692. "mat4 getBoneMatrix( const in float i ) {",
  693. "float j = i * 4.0;",
  694. "float x = mod( j, N_BONE_PIXEL_X );",
  695. "float y = floor( j / N_BONE_PIXEL_X );",
  696. "const float dx = 1.0 / N_BONE_PIXEL_X;",
  697. "const float dy = 1.0 / N_BONE_PIXEL_Y;",
  698. "y = dy * ( y + 0.5 );",
  699. "vec4 v1 = texture2D( boneTexture, vec2( dx * ( x + 0.5 ), y ) );",
  700. "vec4 v2 = texture2D( boneTexture, vec2( dx * ( x + 1.5 ), y ) );",
  701. "vec4 v3 = texture2D( boneTexture, vec2( dx * ( x + 2.5 ), y ) );",
  702. "vec4 v4 = texture2D( boneTexture, vec2( dx * ( x + 3.5 ), y ) );",
  703. "mat4 bone = mat4( v1, v2, v3, v4 );",
  704. "return bone;",
  705. "}",
  706. "#else",
  707. "uniform mat4 boneGlobalMatrices[ MAX_BONES ];",
  708. "mat4 getBoneMatrix( const in float i ) {",
  709. "mat4 bone = boneGlobalMatrices[ int(i) ];",
  710. "return bone;",
  711. "}",
  712. "#endif",
  713. "#endif"
  714. ].join("\n"),
  715. skinbase_vertex: [
  716. "#ifdef USE_SKINNING",
  717. "mat4 boneMatX = getBoneMatrix( skinIndex.x );",
  718. "mat4 boneMatY = getBoneMatrix( skinIndex.y );",
  719. "#endif"
  720. ].join("\n"),
  721. skinning_vertex: [
  722. "#ifdef USE_SKINNING",
  723. "#ifdef USE_MORPHTARGETS",
  724. "vec4 skinVertex = vec4( morphed, 1.0 );",
  725. "#else",
  726. "vec4 skinVertex = vec4( position, 1.0 );",
  727. "#endif",
  728. "vec4 skinned = boneMatX * skinVertex * skinWeight.x;",
  729. "skinned += boneMatY * skinVertex * skinWeight.y;",
  730. "#endif"
  731. ].join("\n"),
  732. // MORPHING
  733. morphtarget_pars_vertex: [
  734. "#ifdef USE_MORPHTARGETS",
  735. "#ifndef USE_MORPHNORMALS",
  736. "uniform float morphTargetInfluences[ 8 ];",
  737. "#else",
  738. "uniform float morphTargetInfluences[ 4 ];",
  739. "#endif",
  740. "#endif"
  741. ].join("\n"),
  742. morphtarget_vertex: [
  743. "#ifdef USE_MORPHTARGETS",
  744. "vec3 morphed = vec3( 0.0 );",
  745. "morphed += ( morphTarget0 - position ) * morphTargetInfluences[ 0 ];",
  746. "morphed += ( morphTarget1 - position ) * morphTargetInfluences[ 1 ];",
  747. "morphed += ( morphTarget2 - position ) * morphTargetInfluences[ 2 ];",
  748. "morphed += ( morphTarget3 - position ) * morphTargetInfluences[ 3 ];",
  749. "#ifndef USE_MORPHNORMALS",
  750. "morphed += ( morphTarget4 - position ) * morphTargetInfluences[ 4 ];",
  751. "morphed += ( morphTarget5 - position ) * morphTargetInfluences[ 5 ];",
  752. "morphed += ( morphTarget6 - position ) * morphTargetInfluences[ 6 ];",
  753. "morphed += ( morphTarget7 - position ) * morphTargetInfluences[ 7 ];",
  754. "#endif",
  755. "morphed += position;",
  756. "#endif"
  757. ].join("\n"),
  758. default_vertex : [
  759. "vec4 mvPosition;",
  760. "#ifdef USE_SKINNING",
  761. "mvPosition = modelViewMatrix * skinned;",
  762. "#endif",
  763. "#if !defined( USE_SKINNING ) && defined( USE_MORPHTARGETS )",
  764. "mvPosition = modelViewMatrix * vec4( morphed, 1.0 );",
  765. "#endif",
  766. "#if !defined( USE_SKINNING ) && ! defined( USE_MORPHTARGETS )",
  767. "mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  768. "#endif",
  769. "gl_Position = projectionMatrix * mvPosition;"
  770. ].join("\n"),
  771. morphnormal_vertex: [
  772. "#ifdef USE_MORPHNORMALS",
  773. "vec3 morphedNormal = vec3( 0.0 );",
  774. "morphedNormal += ( morphNormal0 - normal ) * morphTargetInfluences[ 0 ];",
  775. "morphedNormal += ( morphNormal1 - normal ) * morphTargetInfluences[ 1 ];",
  776. "morphedNormal += ( morphNormal2 - normal ) * morphTargetInfluences[ 2 ];",
  777. "morphedNormal += ( morphNormal3 - normal ) * morphTargetInfluences[ 3 ];",
  778. "morphedNormal += normal;",
  779. "#endif"
  780. ].join("\n"),
  781. skinnormal_vertex: [
  782. "#ifdef USE_SKINNING",
  783. "mat4 skinMatrix = skinWeight.x * boneMatX;",
  784. "skinMatrix += skinWeight.y * boneMatY;",
  785. "#ifdef USE_MORPHNORMALS",
  786. "vec4 skinnedNormal = skinMatrix * vec4( morphedNormal, 0.0 );",
  787. "#else",
  788. "vec4 skinnedNormal = skinMatrix * vec4( normal, 0.0 );",
  789. "#endif",
  790. "#endif"
  791. ].join("\n"),
  792. defaultnormal_vertex: [
  793. "vec3 objectNormal;",
  794. "#ifdef USE_SKINNING",
  795. "objectNormal = skinnedNormal.xyz;",
  796. "#endif",
  797. "#if !defined( USE_SKINNING ) && defined( USE_MORPHNORMALS )",
  798. "objectNormal = morphedNormal;",
  799. "#endif",
  800. "#if !defined( USE_SKINNING ) && ! defined( USE_MORPHNORMALS )",
  801. "objectNormal = normal;",
  802. "#endif",
  803. "#ifdef FLIP_SIDED",
  804. "objectNormal = -objectNormal;",
  805. "#endif",
  806. "vec3 transformedNormal = normalMatrix * objectNormal;"
  807. ].join("\n"),
  808. // SHADOW MAP
  809. // based on SpiderGL shadow map and Fabien Sanglard's GLSL shadow mapping examples
  810. // http://spidergl.org/example.php?id=6
  811. // http://fabiensanglard.net/shadowmapping
  812. shadowmap_pars_fragment: [
  813. "#ifdef USE_SHADOWMAP",
  814. "uniform sampler2D shadowMap[ MAX_SHADOWS ];",
  815. "uniform vec2 shadowMapSize[ MAX_SHADOWS ];",
  816. "uniform float shadowDarkness[ MAX_SHADOWS ];",
  817. "uniform float shadowBias[ MAX_SHADOWS ];",
  818. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  819. "float unpackDepth( const in vec4 rgba_depth ) {",
  820. "const vec4 bit_shift = vec4( 1.0 / ( 256.0 * 256.0 * 256.0 ), 1.0 / ( 256.0 * 256.0 ), 1.0 / 256.0, 1.0 );",
  821. "float depth = dot( rgba_depth, bit_shift );",
  822. "return depth;",
  823. "}",
  824. "#endif"
  825. ].join("\n"),
  826. shadowmap_fragment: [
  827. "#ifdef USE_SHADOWMAP",
  828. "#ifdef SHADOWMAP_DEBUG",
  829. "vec3 frustumColors[3];",
  830. "frustumColors[0] = vec3( 1.0, 0.5, 0.0 );",
  831. "frustumColors[1] = vec3( 0.0, 1.0, 0.8 );",
  832. "frustumColors[2] = vec3( 0.0, 0.5, 1.0 );",
  833. "#endif",
  834. "#ifdef SHADOWMAP_CASCADE",
  835. "int inFrustumCount = 0;",
  836. "#endif",
  837. "float fDepth;",
  838. "vec3 shadowColor = vec3( 1.0 );",
  839. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  840. "vec3 shadowCoord = vShadowCoord[ i ].xyz / vShadowCoord[ i ].w;",
  841. // "if ( something && something )" breaks ATI OpenGL shader compiler
  842. // "if ( all( something, something ) )" using this instead
  843. "bvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 );",
  844. "bool inFrustum = all( inFrustumVec );",
  845. // don't shadow pixels outside of light frustum
  846. // use just first frustum (for cascades)
  847. // don't shadow pixels behind far plane of light frustum
  848. "#ifdef SHADOWMAP_CASCADE",
  849. "inFrustumCount += int( inFrustum );",
  850. "bvec3 frustumTestVec = bvec3( inFrustum, inFrustumCount == 1, shadowCoord.z <= 1.0 );",
  851. "#else",
  852. "bvec2 frustumTestVec = bvec2( inFrustum, shadowCoord.z <= 1.0 );",
  853. "#endif",
  854. "bool frustumTest = all( frustumTestVec );",
  855. "if ( frustumTest ) {",
  856. "shadowCoord.z += shadowBias[ i ];",
  857. "#ifdef SHADOWMAP_SOFT",
  858. // Percentage-close filtering
  859. // (9 pixel kernel)
  860. // http://fabiensanglard.net/shadowmappingPCF/
  861. "float shadow = 0.0;",
  862. /*
  863. // nested loops breaks shader compiler / validator on some ATI cards when using OpenGL
  864. // must enroll loop manually
  865. "for ( float y = -1.25; y <= 1.25; y += 1.25 )",
  866. "for ( float x = -1.25; x <= 1.25; x += 1.25 ) {",
  867. "vec4 rgbaDepth = texture2D( shadowMap[ i ], vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy );",
  868. // doesn't seem to produce any noticeable visual difference compared to simple "texture2D" lookup
  869. //"vec4 rgbaDepth = texture2DProj( shadowMap[ i ], vec4( vShadowCoord[ i ].w * ( vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy ), 0.05, vShadowCoord[ i ].w ) );",
  870. "float fDepth = unpackDepth( rgbaDepth );",
  871. "if ( fDepth < shadowCoord.z )",
  872. "shadow += 1.0;",
  873. "}",
  874. "shadow /= 9.0;",
  875. */
  876. "const float shadowDelta = 1.0 / 9.0;",
  877. "float xPixelOffset = 1.0 / shadowMapSize[ i ].x;",
  878. "float yPixelOffset = 1.0 / shadowMapSize[ i ].y;",
  879. "float dx0 = -1.0 * xPixelOffset;",
  880. "float dy0 = -1.0 * yPixelOffset;",
  881. "float dx1 = 1.0 * xPixelOffset;",
  882. "float dy1 = 1.0 * yPixelOffset;",
  883. "mat3 shadowKernel;",
  884. "mat3 depthKernel;",
  885. "depthKernel[0][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );",
  886. "if ( depthKernel[0][0] < shadowCoord.z ) shadowKernel[0][0] = 0.25;",
  887. "else shadowKernel[0][0] = 0.0;",
  888. "depthKernel[0][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );",
  889. "if ( depthKernel[0][1] < shadowCoord.z ) shadowKernel[0][1] = 0.25;",
  890. "else shadowKernel[0][1] = 0.0;",
  891. "depthKernel[0][2] = unpackDepth( texture2D( shadowMap[ i], shadowCoord.xy + vec2( dx0, dy1 ) ) );",
  892. "if ( depthKernel[0][2] < shadowCoord.z ) shadowKernel[0][2] = 0.25;",
  893. "else shadowKernel[0][2] = 0.0;",
  894. "depthKernel[1][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );",
  895. "if ( depthKernel[1][0] < shadowCoord.z ) shadowKernel[1][0] = 0.25;",
  896. "else shadowKernel[1][0] = 0.0;",
  897. "depthKernel[1][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );",
  898. "if ( depthKernel[1][1] < shadowCoord.z ) shadowKernel[1][1] = 0.25;",
  899. "else shadowKernel[1][1] = 0.0;",
  900. "depthKernel[1][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );",
  901. "if ( depthKernel[1][2] < shadowCoord.z ) shadowKernel[1][2] = 0.25;",
  902. "else shadowKernel[1][2] = 0.0;",
  903. "depthKernel[2][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );",
  904. "if ( depthKernel[2][0] < shadowCoord.z ) shadowKernel[2][0] = 0.25;",
  905. "else shadowKernel[2][0] = 0.0;",
  906. "depthKernel[2][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );",
  907. "if ( depthKernel[2][1] < shadowCoord.z ) shadowKernel[2][1] = 0.25;",
  908. "else shadowKernel[2][1] = 0.0;",
  909. "depthKernel[2][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );",
  910. "if ( depthKernel[2][2] < shadowCoord.z ) shadowKernel[2][2] = 0.25;",
  911. "else shadowKernel[2][2] = 0.0;",
  912. "vec2 fractionalCoord = 1.0 - fract(shadowCoord.xy * shadowMapSize[i].xy );",
  913. "shadowKernel[0] = mix( shadowKernel[1], shadowKernel[0], fractionalCoord.x );",
  914. "shadowKernel[1] = mix( shadowKernel[2], shadowKernel[1], fractionalCoord.x );",
  915. "vec4 shadowValues;",
  916. "shadowValues.x = mix(shadowKernel[0][1], shadowKernel[0][0], fractionalCoord.y );",
  917. "shadowValues.y = mix(shadowKernel[0][2], shadowKernel[0][1], fractionalCoord.y );",
  918. "shadowValues.z = mix(shadowKernel[1][1], shadowKernel[1][0], fractionalCoord.y );",
  919. "shadowValues.w = mix(shadowKernel[1][2], shadowKernel[1][1], fractionalCoord.y );",
  920. "shadow = dot(shadowValues, vec4(1.0));",
  921. "shadowColor = shadowColor * vec3( ( 1.0 - shadowDarkness[ i ] * shadow ) );",
  922. "#else",
  923. "vec4 rgbaDepth = texture2D( shadowMap[ i ], shadowCoord.xy );",
  924. "float fDepth = unpackDepth( rgbaDepth );",
  925. "if ( fDepth < shadowCoord.z )",
  926. // spot with multiple shadows is darker
  927. "shadowColor = shadowColor * vec3( 1.0 - shadowDarkness[ i ] );",
  928. // spot with multiple shadows has the same color as single shadow spot
  929. //"shadowColor = min( shadowColor, vec3( shadowDarkness[ i ] ) );",
  930. "#endif",
  931. "}",
  932. "#ifdef SHADOWMAP_DEBUG",
  933. "#ifdef SHADOWMAP_CASCADE",
  934. "if ( inFrustum && inFrustumCount == 1 ) gl_FragColor.xyz *= frustumColors[ i ];",
  935. "#else",
  936. "if ( inFrustum ) gl_FragColor.xyz *= frustumColors[ i ];",
  937. "#endif",
  938. "#endif",
  939. "}",
  940. "#ifdef GAMMA_OUTPUT",
  941. "shadowColor *= shadowColor;",
  942. "#endif",
  943. "gl_FragColor.xyz = gl_FragColor.xyz * shadowColor;",
  944. "#endif"
  945. ].join("\n"),
  946. shadowmap_pars_vertex: [
  947. "#ifdef USE_SHADOWMAP",
  948. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  949. "uniform mat4 shadowMatrix[ MAX_SHADOWS ];",
  950. "#endif"
  951. ].join("\n"),
  952. shadowmap_vertex: [
  953. "#ifdef USE_SHADOWMAP",
  954. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  955. "vShadowCoord[ i ] = shadowMatrix[ i ] * worldPosition;",
  956. "}",
  957. "#endif"
  958. ].join("\n"),
  959. // ALPHATEST
  960. alphatest_fragment: [
  961. "#ifdef ALPHATEST",
  962. "if ( gl_FragColor.a < ALPHATEST ) discard;",
  963. "#endif"
  964. ].join("\n"),
  965. // LINEAR SPACE
  966. linear_to_gamma_fragment: [
  967. "#ifdef GAMMA_OUTPUT",
  968. "gl_FragColor.xyz = sqrt( gl_FragColor.xyz );",
  969. "#endif"
  970. ].join("\n")
  971. };
  972. THREE.UniformsUtils = {
  973. merge: function ( uniforms ) {
  974. var u, p, tmp, merged = {};
  975. for ( u = 0; u < uniforms.length; u ++ ) {
  976. tmp = this.clone( uniforms[ u ] );
  977. for ( p in tmp ) {
  978. merged[ p ] = tmp[ p ];
  979. }
  980. }
  981. return merged;
  982. },
  983. clone: function ( uniforms_src ) {
  984. var u, p, parameter, parameter_src, uniforms_dst = {};
  985. for ( u in uniforms_src ) {
  986. uniforms_dst[ u ] = {};
  987. for ( p in uniforms_src[ u ] ) {
  988. parameter_src = uniforms_src[ u ][ p ];
  989. if ( parameter_src instanceof THREE.Color ||
  990. parameter_src instanceof THREE.Vector2 ||
  991. parameter_src instanceof THREE.Vector3 ||
  992. parameter_src instanceof THREE.Vector4 ||
  993. parameter_src instanceof THREE.Matrix4 ||
  994. parameter_src instanceof THREE.Texture ) {
  995. uniforms_dst[ u ][ p ] = parameter_src.clone();
  996. } else if ( parameter_src instanceof Array ) {
  997. uniforms_dst[ u ][ p ] = parameter_src.slice();
  998. } else {
  999. uniforms_dst[ u ][ p ] = parameter_src;
  1000. }
  1001. }
  1002. }
  1003. return uniforms_dst;
  1004. }
  1005. };
  1006. THREE.UniformsLib = {
  1007. common: {
  1008. "diffuse" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  1009. "opacity" : { type: "f", value: 1.0 },
  1010. "map" : { type: "t", value: null },
  1011. "offsetRepeat" : { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) },
  1012. "lightMap" : { type: "t", value: null },
  1013. "specularMap" : { type: "t", value: null },
  1014. "envMap" : { type: "t", value: null },
  1015. "flipEnvMap" : { type: "f", value: -1 },
  1016. "useRefract" : { type: "i", value: 0 },
  1017. "reflectivity" : { type: "f", value: 1.0 },
  1018. "refractionRatio" : { type: "f", value: 0.98 },
  1019. "combine" : { type: "i", value: 0 },
  1020. "morphTargetInfluences" : { type: "f", value: 0 }
  1021. },
  1022. bump: {
  1023. "bumpMap" : { type: "t", value: null },
  1024. "bumpScale" : { type: "f", value: 1 }
  1025. },
  1026. normalmap: {
  1027. "normalMap" : { type: "t", value: null },
  1028. "normalScale" : { type: "v2", value: new THREE.Vector2( 1, 1 ) }
  1029. },
  1030. fog : {
  1031. "fogDensity" : { type: "f", value: 0.00025 },
  1032. "fogNear" : { type: "f", value: 1 },
  1033. "fogFar" : { type: "f", value: 2000 },
  1034. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  1035. },
  1036. lights: {
  1037. "ambientLightColor" : { type: "fv", value: [] },
  1038. "directionalLightDirection" : { type: "fv", value: [] },
  1039. "directionalLightColor" : { type: "fv", value: [] },
  1040. "hemisphereLightDirection" : { type: "fv", value: [] },
  1041. "hemisphereLightSkyColor" : { type: "fv", value: [] },
  1042. "hemisphereLightGroundColor" : { type: "fv", value: [] },
  1043. "pointLightColor" : { type: "fv", value: [] },
  1044. "pointLightPosition" : { type: "fv", value: [] },
  1045. "pointLightDistance" : { type: "fv1", value: [] },
  1046. "spotLightColor" : { type: "fv", value: [] },
  1047. "spotLightPosition" : { type: "fv", value: [] },
  1048. "spotLightDirection" : { type: "fv", value: [] },
  1049. "spotLightDistance" : { type: "fv1", value: [] },
  1050. "spotLightAngleCos" : { type: "fv1", value: [] },
  1051. "spotLightExponent" : { type: "fv1", value: [] }
  1052. },
  1053. particle: {
  1054. "psColor" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  1055. "opacity" : { type: "f", value: 1.0 },
  1056. "size" : { type: "f", value: 1.0 },
  1057. "scale" : { type: "f", value: 1.0 },
  1058. "map" : { type: "t", value: null },
  1059. "fogDensity" : { type: "f", value: 0.00025 },
  1060. "fogNear" : { type: "f", value: 1 },
  1061. "fogFar" : { type: "f", value: 2000 },
  1062. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  1063. },
  1064. shadowmap: {
  1065. "shadowMap": { type: "tv", value: [] },
  1066. "shadowMapSize": { type: "v2v", value: [] },
  1067. "shadowBias" : { type: "fv1", value: [] },
  1068. "shadowDarkness": { type: "fv1", value: [] },
  1069. "shadowMatrix" : { type: "m4v", value: [] }
  1070. }
  1071. };
  1072. THREE.ShaderLib = {
  1073. 'depth': {
  1074. uniforms: {
  1075. "mNear": { type: "f", value: 1.0 },
  1076. "mFar" : { type: "f", value: 2000.0 },
  1077. "opacity" : { type: "f", value: 1.0 }
  1078. },
  1079. vertexShader: [
  1080. "void main() {",
  1081. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  1082. "}"
  1083. ].join("\n"),
  1084. fragmentShader: [
  1085. "uniform float mNear;",
  1086. "uniform float mFar;",
  1087. "uniform float opacity;",
  1088. "void main() {",
  1089. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  1090. "float color = 1.0 - smoothstep( mNear, mFar, depth );",
  1091. "gl_FragColor = vec4( vec3( color ), opacity );",
  1092. "}"
  1093. ].join("\n")
  1094. },
  1095. 'normal': {
  1096. uniforms: {
  1097. "opacity" : { type: "f", value: 1.0 }
  1098. },
  1099. vertexShader: [
  1100. "varying vec3 vNormal;",
  1101. "void main() {",
  1102. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  1103. "vNormal = normalize( normalMatrix * normal );",
  1104. "gl_Position = projectionMatrix * mvPosition;",
  1105. "}"
  1106. ].join("\n"),
  1107. fragmentShader: [
  1108. "uniform float opacity;",
  1109. "varying vec3 vNormal;",
  1110. "void main() {",
  1111. "gl_FragColor = vec4( 0.5 * normalize( vNormal ) + 0.5, opacity );",
  1112. "}"
  1113. ].join("\n")
  1114. },
  1115. 'basic': {
  1116. uniforms: THREE.UniformsUtils.merge( [
  1117. THREE.UniformsLib[ "common" ],
  1118. THREE.UniformsLib[ "fog" ],
  1119. THREE.UniformsLib[ "shadowmap" ]
  1120. ] ),
  1121. vertexShader: [
  1122. THREE.ShaderChunk[ "map_pars_vertex" ],
  1123. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  1124. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  1125. THREE.ShaderChunk[ "color_pars_vertex" ],
  1126. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  1127. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  1128. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  1129. "void main() {",
  1130. THREE.ShaderChunk[ "map_vertex" ],
  1131. THREE.ShaderChunk[ "lightmap_vertex" ],
  1132. THREE.ShaderChunk[ "color_vertex" ],
  1133. "#ifdef USE_ENVMAP",
  1134. THREE.ShaderChunk[ "morphnormal_vertex" ],
  1135. THREE.ShaderChunk[ "skinbase_vertex" ],
  1136. THREE.ShaderChunk[ "skinnormal_vertex" ],
  1137. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  1138. "#endif",
  1139. THREE.ShaderChunk[ "morphtarget_vertex" ],
  1140. THREE.ShaderChunk[ "skinning_vertex" ],
  1141. THREE.ShaderChunk[ "default_vertex" ],
  1142. THREE.ShaderChunk[ "worldpos_vertex" ],
  1143. THREE.ShaderChunk[ "envmap_vertex" ],
  1144. THREE.ShaderChunk[ "shadowmap_vertex" ],
  1145. "}"
  1146. ].join("\n"),
  1147. fragmentShader: [
  1148. "uniform vec3 diffuse;",
  1149. "uniform float opacity;",
  1150. THREE.ShaderChunk[ "color_pars_fragment" ],
  1151. THREE.ShaderChunk[ "map_pars_fragment" ],
  1152. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  1153. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  1154. THREE.ShaderChunk[ "fog_pars_fragment" ],
  1155. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  1156. THREE.ShaderChunk[ "specularmap_pars_fragment" ],
  1157. "void main() {",
  1158. "gl_FragColor = vec4( diffuse, opacity );",
  1159. THREE.ShaderChunk[ "map_fragment" ],
  1160. THREE.ShaderChunk[ "alphatest_fragment" ],
  1161. THREE.ShaderChunk[ "specularmap_fragment" ],
  1162. THREE.ShaderChunk[ "lightmap_fragment" ],
  1163. THREE.ShaderChunk[ "color_fragment" ],
  1164. THREE.ShaderChunk[ "envmap_fragment" ],
  1165. THREE.ShaderChunk[ "shadowmap_fragment" ],
  1166. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  1167. THREE.ShaderChunk[ "fog_fragment" ],
  1168. "}"
  1169. ].join("\n")
  1170. },
  1171. 'lambert': {
  1172. uniforms: THREE.UniformsUtils.merge( [
  1173. THREE.UniformsLib[ "common" ],
  1174. THREE.UniformsLib[ "fog" ],
  1175. THREE.UniformsLib[ "lights" ],
  1176. THREE.UniformsLib[ "shadowmap" ],
  1177. {
  1178. "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) },
  1179. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  1180. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  1181. }
  1182. ] ),
  1183. vertexShader: [
  1184. "#define LAMBERT",
  1185. "varying vec3 vLightFront;",
  1186. "#ifdef DOUBLE_SIDED",
  1187. "varying vec3 vLightBack;",
  1188. "#endif",
  1189. THREE.ShaderChunk[ "map_pars_vertex" ],
  1190. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  1191. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  1192. THREE.ShaderChunk[ "lights_lambert_pars_vertex" ],
  1193. THREE.ShaderChunk[ "color_pars_vertex" ],
  1194. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  1195. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  1196. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  1197. "void main() {",
  1198. THREE.ShaderChunk[ "map_vertex" ],
  1199. THREE.ShaderChunk[ "lightmap_vertex" ],
  1200. THREE.ShaderChunk[ "color_vertex" ],
  1201. THREE.ShaderChunk[ "morphnormal_vertex" ],
  1202. THREE.ShaderChunk[ "skinbase_vertex" ],
  1203. THREE.ShaderChunk[ "skinnormal_vertex" ],
  1204. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  1205. THREE.ShaderChunk[ "morphtarget_vertex" ],
  1206. THREE.ShaderChunk[ "skinning_vertex" ],
  1207. THREE.ShaderChunk[ "default_vertex" ],
  1208. THREE.ShaderChunk[ "worldpos_vertex" ],
  1209. THREE.ShaderChunk[ "envmap_vertex" ],
  1210. THREE.ShaderChunk[ "lights_lambert_vertex" ],
  1211. THREE.ShaderChunk[ "shadowmap_vertex" ],
  1212. "}"
  1213. ].join("\n"),
  1214. fragmentShader: [
  1215. "uniform float opacity;",
  1216. "varying vec3 vLightFront;",
  1217. "#ifdef DOUBLE_SIDED",
  1218. "varying vec3 vLightBack;",
  1219. "#endif",
  1220. THREE.ShaderChunk[ "color_pars_fragment" ],
  1221. THREE.ShaderChunk[ "map_pars_fragment" ],
  1222. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  1223. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  1224. THREE.ShaderChunk[ "fog_pars_fragment" ],
  1225. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  1226. THREE.ShaderChunk[ "specularmap_pars_fragment" ],
  1227. "void main() {",
  1228. "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );",
  1229. THREE.ShaderChunk[ "map_fragment" ],
  1230. THREE.ShaderChunk[ "alphatest_fragment" ],
  1231. THREE.ShaderChunk[ "specularmap_fragment" ],
  1232. "#ifdef DOUBLE_SIDED",
  1233. //"float isFront = float( gl_FrontFacing );",
  1234. //"gl_FragColor.xyz *= isFront * vLightFront + ( 1.0 - isFront ) * vLightBack;",
  1235. "if ( gl_FrontFacing )",
  1236. "gl_FragColor.xyz *= vLightFront;",
  1237. "else",
  1238. "gl_FragColor.xyz *= vLightBack;",
  1239. "#else",
  1240. "gl_FragColor.xyz *= vLightFront;",
  1241. "#endif",
  1242. THREE.ShaderChunk[ "lightmap_fragment" ],
  1243. THREE.ShaderChunk[ "color_fragment" ],
  1244. THREE.ShaderChunk[ "envmap_fragment" ],
  1245. THREE.ShaderChunk[ "shadowmap_fragment" ],
  1246. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  1247. THREE.ShaderChunk[ "fog_fragment" ],
  1248. "}"
  1249. ].join("\n")
  1250. },
  1251. 'phong': {
  1252. uniforms: THREE.UniformsUtils.merge( [
  1253. THREE.UniformsLib[ "common" ],
  1254. THREE.UniformsLib[ "bump" ],
  1255. THREE.UniformsLib[ "normalmap" ],
  1256. THREE.UniformsLib[ "fog" ],
  1257. THREE.UniformsLib[ "lights" ],
  1258. THREE.UniformsLib[ "shadowmap" ],
  1259. {
  1260. "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) },
  1261. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  1262. "specular" : { type: "c", value: new THREE.Color( 0x111111 ) },
  1263. "shininess": { type: "f", value: 30 },
  1264. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  1265. }
  1266. ] ),
  1267. vertexShader: [
  1268. "#define PHONG",
  1269. "varying vec3 vViewPosition;",
  1270. "varying vec3 vNormal;",
  1271. THREE.ShaderChunk[ "map_pars_vertex" ],
  1272. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  1273. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  1274. THREE.ShaderChunk[ "lights_phong_pars_vertex" ],
  1275. THREE.ShaderChunk[ "color_pars_vertex" ],
  1276. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  1277. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  1278. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  1279. "void main() {",
  1280. THREE.ShaderChunk[ "map_vertex" ],
  1281. THREE.ShaderChunk[ "lightmap_vertex" ],
  1282. THREE.ShaderChunk[ "color_vertex" ],
  1283. THREE.ShaderChunk[ "morphnormal_vertex" ],
  1284. THREE.ShaderChunk[ "skinbase_vertex" ],
  1285. THREE.ShaderChunk[ "skinnormal_vertex" ],
  1286. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  1287. "vNormal = normalize( transformedNormal );",
  1288. THREE.ShaderChunk[ "morphtarget_vertex" ],
  1289. THREE.ShaderChunk[ "skinning_vertex" ],
  1290. THREE.ShaderChunk[ "default_vertex" ],
  1291. "vViewPosition = -mvPosition.xyz;",
  1292. THREE.ShaderChunk[ "worldpos_vertex" ],
  1293. THREE.ShaderChunk[ "envmap_vertex" ],
  1294. THREE.ShaderChunk[ "lights_phong_vertex" ],
  1295. THREE.ShaderChunk[ "shadowmap_vertex" ],
  1296. "}"
  1297. ].join("\n"),
  1298. fragmentShader: [
  1299. "uniform vec3 diffuse;",
  1300. "uniform float opacity;",
  1301. "uniform vec3 ambient;",
  1302. "uniform vec3 emissive;",
  1303. "uniform vec3 specular;",
  1304. "uniform float shininess;",
  1305. THREE.ShaderChunk[ "color_pars_fragment" ],
  1306. THREE.ShaderChunk[ "map_pars_fragment" ],
  1307. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  1308. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  1309. THREE.ShaderChunk[ "fog_pars_fragment" ],
  1310. THREE.ShaderChunk[ "lights_phong_pars_fragment" ],
  1311. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  1312. THREE.ShaderChunk[ "bumpmap_pars_fragment" ],
  1313. THREE.ShaderChunk[ "normalmap_pars_fragment" ],
  1314. THREE.ShaderChunk[ "specularmap_pars_fragment" ],
  1315. "void main() {",
  1316. "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );",
  1317. THREE.ShaderChunk[ "map_fragment" ],
  1318. THREE.ShaderChunk[ "alphatest_fragment" ],
  1319. THREE.ShaderChunk[ "specularmap_fragment" ],
  1320. THREE.ShaderChunk[ "lights_phong_fragment" ],
  1321. THREE.ShaderChunk[ "lightmap_fragment" ],
  1322. THREE.ShaderChunk[ "color_fragment" ],
  1323. THREE.ShaderChunk[ "envmap_fragment" ],
  1324. THREE.ShaderChunk[ "shadowmap_fragment" ],
  1325. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  1326. THREE.ShaderChunk[ "fog_fragment" ],
  1327. "}"
  1328. ].join("\n")
  1329. },
  1330. 'particle_basic': {
  1331. uniforms: THREE.UniformsUtils.merge( [
  1332. THREE.UniformsLib[ "particle" ],
  1333. THREE.UniformsLib[ "shadowmap" ]
  1334. ] ),
  1335. vertexShader: [
  1336. "uniform float size;",
  1337. "uniform float scale;",
  1338. THREE.ShaderChunk[ "color_pars_vertex" ],
  1339. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  1340. "void main() {",
  1341. THREE.ShaderChunk[ "color_vertex" ],
  1342. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  1343. "#ifdef USE_SIZEATTENUATION",
  1344. "gl_PointSize = size * ( scale / length( mvPosition.xyz ) );",
  1345. "#else",
  1346. "gl_PointSize = size;",
  1347. "#endif",
  1348. "gl_Position = projectionMatrix * mvPosition;",
  1349. THREE.ShaderChunk[ "worldpos_vertex" ],
  1350. THREE.ShaderChunk[ "shadowmap_vertex" ],
  1351. "}"
  1352. ].join("\n"),
  1353. fragmentShader: [
  1354. "uniform vec3 psColor;",
  1355. "uniform float opacity;",
  1356. THREE.ShaderChunk[ "color_pars_fragment" ],
  1357. THREE.ShaderChunk[ "map_particle_pars_fragment" ],
  1358. THREE.ShaderChunk[ "fog_pars_fragment" ],
  1359. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  1360. "void main() {",
  1361. "gl_FragColor = vec4( psColor, opacity );",
  1362. THREE.ShaderChunk[ "map_particle_fragment" ],
  1363. THREE.ShaderChunk[ "alphatest_fragment" ],
  1364. THREE.ShaderChunk[ "color_fragment" ],
  1365. THREE.ShaderChunk[ "shadowmap_fragment" ],
  1366. THREE.ShaderChunk[ "fog_fragment" ],
  1367. "}"
  1368. ].join("\n")
  1369. },
  1370. 'dashed': {
  1371. uniforms: THREE.UniformsUtils.merge( [
  1372. THREE.UniformsLib[ "common" ],
  1373. THREE.UniformsLib[ "fog" ],
  1374. {
  1375. "scale": { type: "f", value: 1 },
  1376. "dashSize": { type: "f", value: 1 },
  1377. "totalSize": { type: "f", value: 2 }
  1378. }
  1379. ] ),
  1380. vertexShader: [
  1381. "uniform float scale;",
  1382. "attribute float lineDistance;",
  1383. "varying float vLineDistance;",
  1384. THREE.ShaderChunk[ "color_pars_vertex" ],
  1385. "void main() {",
  1386. THREE.ShaderChunk[ "color_vertex" ],
  1387. "vLineDistance = scale * lineDistance;",
  1388. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  1389. "gl_Position = projectionMatrix * mvPosition;",
  1390. "}"
  1391. ].join("\n"),
  1392. fragmentShader: [
  1393. "uniform vec3 diffuse;",
  1394. "uniform float opacity;",
  1395. "uniform float dashSize;",
  1396. "uniform float totalSize;",
  1397. "varying float vLineDistance;",
  1398. THREE.ShaderChunk[ "color_pars_fragment" ],
  1399. THREE.ShaderChunk[ "fog_pars_fragment" ],
  1400. "void main() {",
  1401. "if ( mod( vLineDistance, totalSize ) > dashSize ) {",
  1402. "discard;",
  1403. "}",
  1404. "gl_FragColor = vec4( diffuse, opacity );",
  1405. THREE.ShaderChunk[ "color_fragment" ],
  1406. THREE.ShaderChunk[ "fog_fragment" ],
  1407. "}"
  1408. ].join("\n")
  1409. },
  1410. // Depth encoding into RGBA texture
  1411. // based on SpiderGL shadow map example
  1412. // http://spidergl.org/example.php?id=6
  1413. // originally from
  1414. // http://www.gamedev.net/topic/442138-packing-a-float-into-a-a8r8g8b8-texture-shader/page__whichpage__1%25EF%25BF%25BD
  1415. // see also here:
  1416. // http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
  1417. 'depthRGBA': {
  1418. uniforms: {},
  1419. vertexShader: [
  1420. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  1421. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  1422. "void main() {",
  1423. THREE.ShaderChunk[ "skinbase_vertex" ],
  1424. THREE.ShaderChunk[ "morphtarget_vertex" ],
  1425. THREE.ShaderChunk[ "skinning_vertex" ],
  1426. THREE.ShaderChunk[ "default_vertex" ],
  1427. "}"
  1428. ].join("\n"),
  1429. fragmentShader: [
  1430. "vec4 pack_depth( const in float depth ) {",
  1431. "const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );",
  1432. "const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );",
  1433. "vec4 res = fract( depth * bit_shift );",
  1434. "res -= res.xxyz * bit_mask;",
  1435. "return res;",
  1436. "}",
  1437. "void main() {",
  1438. "gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z );",
  1439. //"gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z / gl_FragCoord.w );",
  1440. //"float z = ( ( gl_FragCoord.z / gl_FragCoord.w ) - 3.0 ) / ( 4000.0 - 3.0 );",
  1441. //"gl_FragData[ 0 ] = pack_depth( z );",
  1442. //"gl_FragData[ 0 ] = vec4( z, z, z, 1.0 );",
  1443. "}"
  1444. ].join("\n")
  1445. }
  1446. };