WebGLShaders.js 49 KB

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