WebGLShaders.js 48 KB

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