WebGLShaders.js 46 KB

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