WebGLShaders.js 56 KB

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