WebGLShaders.js 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  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. "vec4 cubeColor = textureCube( envMap, vec3( flipEnvMap * vReflect.x, vReflect.yz ) );",
  45. "#ifdef GAMMA_INPUT",
  46. "cubeColor.xyz *= cubeColor.xyz;",
  47. "#endif",
  48. "if ( combine == 1 ) {",
  49. "gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, reflectivity );",
  50. "} else {",
  51. "gl_FragColor.xyz = gl_FragColor.xyz * cubeColor.xyz;",
  52. "}",
  53. "#endif"
  54. ].join("\n"),
  55. envmap_pars_vertex: [
  56. "#ifdef USE_ENVMAP",
  57. "varying vec3 vReflect;",
  58. "uniform float refractionRatio;",
  59. "uniform bool useRefract;",
  60. "#endif"
  61. ].join("\n"),
  62. envmap_vertex : [
  63. "#ifdef USE_ENVMAP",
  64. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  65. "vec3 nWorld = mat3( objectMatrix[ 0 ].xyz, objectMatrix[ 1 ].xyz, objectMatrix[ 2 ].xyz ) * normal;",
  66. "if ( useRefract ) {",
  67. "vReflect = refract( normalize( mPosition.xyz - cameraPosition ), normalize( nWorld.xyz ), refractionRatio );",
  68. "} else {",
  69. "vReflect = reflect( normalize( mPosition.xyz - cameraPosition ), normalize( nWorld.xyz ) );",
  70. "}",
  71. "#endif"
  72. ].join("\n"),
  73. // COLOR MAP (particles)
  74. map_particle_pars_fragment: [
  75. "#ifdef USE_MAP",
  76. "uniform sampler2D map;",
  77. "#endif"
  78. ].join("\n"),
  79. map_particle_fragment: [
  80. "#ifdef USE_MAP",
  81. "gl_FragColor = gl_FragColor * texture2D( map, gl_PointCoord );",
  82. "#endif"
  83. ].join("\n"),
  84. // COLOR MAP (triangles)
  85. map_pars_vertex: [
  86. "#ifdef USE_MAP",
  87. "varying vec2 vUv;",
  88. "uniform vec4 offsetRepeat;",
  89. "#endif"
  90. ].join("\n"),
  91. map_pars_fragment: [
  92. "#ifdef USE_MAP",
  93. "varying vec2 vUv;",
  94. "uniform sampler2D map;",
  95. "#endif"
  96. ].join("\n"),
  97. map_vertex: [
  98. "#ifdef USE_MAP",
  99. "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
  100. "#endif"
  101. ].join("\n"),
  102. map_fragment: [
  103. "#ifdef USE_MAP",
  104. "#ifdef GAMMA_INPUT",
  105. "vec4 texelColor = texture2D( map, vUv );",
  106. "texelColor.xyz *= texelColor.xyz;",
  107. "gl_FragColor = gl_FragColor * texelColor;",
  108. "#else",
  109. "gl_FragColor = gl_FragColor * texture2D( map, vUv );",
  110. "#endif",
  111. "#endif"
  112. ].join("\n"),
  113. // LIGHT MAP
  114. lightmap_pars_fragment: [
  115. "#ifdef USE_LIGHTMAP",
  116. "varying vec2 vUv2;",
  117. "uniform sampler2D lightMap;",
  118. "#endif"
  119. ].join("\n"),
  120. lightmap_pars_vertex: [
  121. "#ifdef USE_LIGHTMAP",
  122. "varying vec2 vUv2;",
  123. "#endif"
  124. ].join("\n"),
  125. lightmap_fragment: [
  126. "#ifdef USE_LIGHTMAP",
  127. "gl_FragColor = gl_FragColor * texture2D( lightMap, vUv2 );",
  128. "#endif"
  129. ].join("\n"),
  130. lightmap_vertex: [
  131. "#ifdef USE_LIGHTMAP",
  132. "vUv2 = uv2;",
  133. "#endif"
  134. ].join("\n"),
  135. // LIGHTS LAMBERT
  136. lights_lambert_pars_vertex: [
  137. "uniform vec3 ambient;",
  138. "uniform vec3 diffuse;",
  139. "uniform vec3 ambientLightColor;",
  140. "#if MAX_DIR_LIGHTS > 0",
  141. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  142. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  143. "#endif",
  144. "#if MAX_POINT_LIGHTS > 0",
  145. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  146. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  147. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  148. "#endif"
  149. ].join("\n"),
  150. lights_lambert_vertex: [
  151. "vLightWeighting = vec3( 0.0 );",
  152. "#if MAX_DIR_LIGHTS > 0",
  153. "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
  154. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  155. "float directionalLightWeighting = max( dot( transformedNormal, normalize( lDirection.xyz ) ), 0.0 );",
  156. "vLightWeighting += directionalLightColor[ i ] * directionalLightWeighting;",
  157. "}",
  158. "#endif",
  159. "#if MAX_POINT_LIGHTS > 0",
  160. "for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  161. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  162. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  163. "float lDistance = 1.0;",
  164. "if ( pointLightDistance[ i ] > 0.0 )",
  165. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  166. "lVector = normalize( lVector );",
  167. "float pointLightWeighting = max( dot( transformedNormal, lVector ), 0.0 );",
  168. "vLightWeighting += pointLightColor[ i ] * pointLightWeighting * lDistance;",
  169. "}",
  170. "#endif",
  171. "vLightWeighting = vLightWeighting * diffuse + ambient * ambientLightColor;",
  172. ].join("\n"),
  173. // LIGHTS PHONG
  174. lights_phong_pars_vertex: [
  175. "#if MAX_POINT_LIGHTS > 0",
  176. "#ifndef PHONG_PER_PIXEL",
  177. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  178. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  179. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  180. "#endif",
  181. "#endif"
  182. ].join("\n"),
  183. lights_phong_vertex: [
  184. "#if MAX_POINT_LIGHTS > 0",
  185. "#ifndef PHONG_PER_PIXEL",
  186. "for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  187. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  188. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  189. "float lDistance = 1.0;",
  190. "if ( pointLightDistance[ i ] > 0.0 )",
  191. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  192. "lVector = normalize( lVector );",
  193. "vPointLight[ i ] = vec4( lVector, lDistance );",
  194. "}",
  195. "#endif",
  196. "#endif"
  197. ].join("\n"),
  198. lights_phong_pars_fragment: [
  199. "uniform vec3 ambientLightColor;",
  200. "#if MAX_DIR_LIGHTS > 0",
  201. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  202. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  203. "#endif",
  204. "#if MAX_POINT_LIGHTS > 0",
  205. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  206. "#ifdef PHONG_PER_PIXEL",
  207. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  208. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  209. "#else",
  210. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  211. "#endif",
  212. "#endif",
  213. "varying vec3 vViewPosition;",
  214. "varying vec3 vNormal;"
  215. ].join("\n"),
  216. lights_phong_fragment: [
  217. "vec3 normal = normalize( vNormal );",
  218. "vec3 viewPosition = normalize( vViewPosition );",
  219. "#if MAX_POINT_LIGHTS > 0",
  220. "vec3 pointDiffuse = vec3( 0.0 );",
  221. "vec3 pointSpecular = vec3( 0.0 );",
  222. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  223. "#ifdef PHONG_PER_PIXEL",
  224. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  225. "vec3 lVector = lPosition.xyz + vViewPosition.xyz;",
  226. "float lDistance = 1.0;",
  227. "if ( pointLightDistance[ i ] > 0.0 )",
  228. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  229. "lVector = normalize( lVector );",
  230. "#else",
  231. "vec3 lVector = normalize( vPointLight[ i ].xyz );",
  232. "float lDistance = vPointLight[ i ].w;",
  233. "#endif",
  234. "vec3 pointHalfVector = normalize( lVector + viewPosition );",
  235. "float pointDistance = lDistance;",
  236. "float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  237. "float pointDiffuseWeight = max( dot( normal, lVector ), 0.0 );",
  238. "float pointSpecularWeight = pow( pointDotNormalHalf, shininess );",
  239. "#ifdef PHYSICALLY_BASED_SHADING",
  240. "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( dot( lVector, pointHalfVector ), 5.0 );",
  241. "pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * pointDistance;",
  242. "#else",
  243. "pointSpecular += specular * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * pointDistance;",
  244. "#endif",
  245. "pointDiffuse += diffuse * pointLightColor[ i ] * pointDiffuseWeight * pointDistance;",
  246. "}",
  247. "#endif",
  248. "#if MAX_DIR_LIGHTS > 0",
  249. "vec3 dirDiffuse = vec3( 0.0 );",
  250. "vec3 dirSpecular = vec3( 0.0 );" ,
  251. "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
  252. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  253. "vec3 dirVector = normalize( lDirection.xyz );",
  254. "vec3 dirHalfVector = normalize( lDirection.xyz + viewPosition );",
  255. "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  256. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  257. "float dirSpecularWeight = pow( dirDotNormalHalf, shininess );",
  258. "#ifdef PHYSICALLY_BASED_SHADING",
  259. /*
  260. // fresnel term from skin shader
  261. "const float F0 = 0.128;",
  262. "float base = 1.0 - dot( viewPosition, dirHalfVector );",
  263. "float exponential = pow( base, 5.0 );",
  264. "float fresnel = exponential + F0 * ( 1.0 - exponential );",
  265. */
  266. /*
  267. // fresnel term from fresnel shader
  268. "const float mFresnelBias = 0.08;",
  269. "const float mFresnelScale = 0.3;",
  270. "const float mFresnelPower = 5.0;",
  271. "float fresnel = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( -viewPosition ), normal ), mFresnelPower );",
  272. */
  273. // normalization factor
  274. //float specularNormalization = ( shininess + 2.0 ) / 8.0;
  275. //"dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization * fresnel;",
  276. "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( dot( dirVector, dirHalfVector ), 5.0 );",
  277. "dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight;",
  278. "#else",
  279. "dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight;",
  280. "#endif",
  281. "dirDiffuse += diffuse * directionalLightColor[ i ] * dirDiffuseWeight;",
  282. "}",
  283. "#endif",
  284. "vec3 totalDiffuse = vec3( 0.0 );",
  285. "vec3 totalSpecular = vec3( 0.0 );",
  286. "#if MAX_DIR_LIGHTS > 0",
  287. "totalDiffuse += dirDiffuse;",
  288. "totalSpecular += dirSpecular;",
  289. "#endif",
  290. "#if MAX_POINT_LIGHTS > 0",
  291. "totalDiffuse += pointDiffuse;",
  292. "totalSpecular += pointSpecular;",
  293. "#endif",
  294. "#ifdef METAL",
  295. "gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * ambient + totalSpecular );",
  296. "#else",
  297. "gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * ambient ) + totalSpecular;",
  298. "#endif"
  299. ].join("\n"),
  300. // VERTEX COLORS
  301. color_pars_fragment: [
  302. "#ifdef USE_COLOR",
  303. "varying vec3 vColor;",
  304. "#endif"
  305. ].join("\n"),
  306. color_fragment: [
  307. "#ifdef USE_COLOR",
  308. "gl_FragColor = gl_FragColor * vec4( vColor, opacity );",
  309. "#endif"
  310. ].join("\n"),
  311. color_pars_vertex: [
  312. "#ifdef USE_COLOR",
  313. "varying vec3 vColor;",
  314. "#endif"
  315. ].join("\n"),
  316. color_vertex: [
  317. "#ifdef USE_COLOR",
  318. "#ifdef GAMMA_INPUT",
  319. "vColor = color * color;",
  320. "#else",
  321. "vColor = color;",
  322. "#endif",
  323. "#endif"
  324. ].join("\n"),
  325. // SKINNING
  326. skinning_pars_vertex: [
  327. "#ifdef USE_SKINNING",
  328. "uniform mat4 boneGlobalMatrices[ MAX_BONES ];",
  329. "#endif"
  330. ].join("\n"),
  331. skinning_vertex: [
  332. "#ifdef USE_SKINNING",
  333. "gl_Position = ( boneGlobalMatrices[ int( skinIndex.x ) ] * skinVertexA ) * skinWeight.x;",
  334. "gl_Position += ( boneGlobalMatrices[ int( skinIndex.y ) ] * skinVertexB ) * skinWeight.y;",
  335. // this doesn't work, no idea why
  336. //"gl_Position = projectionMatrix * cameraInverseMatrix * objectMatrix * gl_Position;",
  337. "gl_Position = projectionMatrix * viewMatrix * objectMatrix * gl_Position;",
  338. "#endif"
  339. ].join("\n"),
  340. // MORPHING
  341. morphtarget_pars_vertex: [
  342. "#ifdef USE_MORPHTARGETS",
  343. "uniform float morphTargetInfluences[ 8 ];",
  344. "#endif"
  345. ].join("\n"),
  346. morphtarget_vertex: [
  347. "#ifdef USE_MORPHTARGETS",
  348. "vec3 morphed = vec3( 0.0, 0.0, 0.0 );",
  349. "morphed += ( morphTarget0 - position ) * morphTargetInfluences[ 0 ];",
  350. "morphed += ( morphTarget1 - position ) * morphTargetInfluences[ 1 ];",
  351. "morphed += ( morphTarget2 - position ) * morphTargetInfluences[ 2 ];",
  352. "morphed += ( morphTarget3 - position ) * morphTargetInfluences[ 3 ];",
  353. "morphed += ( morphTarget4 - position ) * morphTargetInfluences[ 4 ];",
  354. "morphed += ( morphTarget5 - position ) * morphTargetInfluences[ 5 ];",
  355. "morphed += ( morphTarget6 - position ) * morphTargetInfluences[ 6 ];",
  356. "morphed += ( morphTarget7 - position ) * morphTargetInfluences[ 7 ];",
  357. "morphed += position;",
  358. "gl_Position = projectionMatrix * modelViewMatrix * vec4( morphed, 1.0 );",
  359. "#endif"
  360. ].join("\n"),
  361. default_vertex : [
  362. "#ifndef USE_MORPHTARGETS",
  363. "#ifndef USE_SKINNING",
  364. "gl_Position = projectionMatrix * mvPosition;",
  365. "#endif",
  366. "#endif"
  367. ].join("\n"),
  368. // SHADOW MAP
  369. // based on SpiderGL shadow map and Fabien Sanglard's GLSL shadow mapping examples
  370. // http://spidergl.org/example.php?id=6
  371. // http://fabiensanglard.net/shadowmapping
  372. shadowmap_pars_fragment: [
  373. "#ifdef USE_SHADOWMAP",
  374. "uniform sampler2D shadowMap[ MAX_SHADOWS ];",
  375. "uniform float shadowDarkness;",
  376. "uniform float shadowBias;",
  377. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  378. "float unpackDepth( const in vec4 rgba_depth ) {",
  379. "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 );",
  380. "float depth = dot( rgba_depth, bit_shift );",
  381. "return depth;",
  382. "}",
  383. "#endif"
  384. ].join("\n"),
  385. shadowmap_fragment: [
  386. "#ifdef USE_SHADOWMAP",
  387. "#ifdef SHADOWMAP_SOFT",
  388. "const float xPixelOffset = 1.0 / SHADOWMAP_WIDTH;",
  389. "const float yPixelOffset = 1.0 / SHADOWMAP_HEIGHT;",
  390. "#endif",
  391. "vec3 shadowColor = vec3( 1.0 );",
  392. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  393. "vec3 shadowCoord = vShadowCoord[ i ].xyz / vShadowCoord[ i ].w;",
  394. "shadowCoord.z += shadowBias;",
  395. "if ( shadowCoord.x >= 0.0 && shadowCoord.x <= 1.0 && shadowCoord.y >= 0.0 && shadowCoord.y <= 1.0 ) {",
  396. "#ifdef SHADOWMAP_SOFT",
  397. // Percentage-close filtering
  398. // (9 pixel kernel)
  399. // http://fabiensanglard.net/shadowmappingPCF/
  400. "float shadow = 0.0;",
  401. "for ( float y = -1.25; y <= 1.25; y += 1.25 )",
  402. "for ( float x = -1.25; x <= 1.25; x += 1.25 ) {",
  403. "vec4 rgbaDepth = texture2D( shadowMap[ i ], vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy );",
  404. // doesn't seem to produce any noticeable visual difference compared to simple "texture2D" lookup
  405. //"vec4 rgbaDepth = texture2DProj( shadowMap[ i ], vec4( vShadowCoord[ i ].w * ( vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy ), 0.05, vShadowCoord[ i ].w ) );",
  406. "float fDepth = unpackDepth( rgbaDepth );",
  407. "if ( fDepth < shadowCoord.z )",
  408. "shadow += 1.0;",
  409. "}",
  410. "shadow /= 9.0;",
  411. "shadowColor = shadowColor * vec3( ( 1.0 - shadowDarkness * shadow ) );",
  412. "#else",
  413. "vec4 rgbaDepth = texture2D( shadowMap[ i ], shadowCoord.xy );",
  414. "float fDepth = unpackDepth( rgbaDepth );",
  415. "if ( fDepth < shadowCoord.z )",
  416. // spot with multiple shadows is darker
  417. "shadowColor = shadowColor * vec3( shadowDarkness );",
  418. // spot with multiple shadows has the same color as single shadow spot
  419. //"shadowColor = min( shadowColor, vec3( shadowDarkness ) );",
  420. "#endif",
  421. "}",
  422. // uncomment to see light frustum boundaries
  423. //"if ( !( shadowCoord.x >= 0.0 && shadowCoord.x <= 1.0 && shadowCoord.y >= 0.0 && shadowCoord.y <= 1.0 ) )",
  424. // "gl_FragColor.xyz = gl_FragColor.xyz * vec3( 1.0, 0.0, 0.0 );",
  425. "}",
  426. "#ifdef GAMMA_OUTPUT",
  427. "shadowColor *= shadowColor;",
  428. "#endif",
  429. "gl_FragColor.xyz = gl_FragColor.xyz * shadowColor;",
  430. "#endif"
  431. ].join("\n"),
  432. shadowmap_pars_vertex: [
  433. "#ifdef USE_SHADOWMAP",
  434. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  435. "uniform mat4 shadowMatrix[ MAX_SHADOWS ];",
  436. "#endif"
  437. ].join("\n"),
  438. shadowmap_vertex: [
  439. "#ifdef USE_SHADOWMAP",
  440. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  441. "vShadowCoord[ i ] = shadowMatrix[ i ] * objectMatrix * vec4( position, 1.0 );",
  442. "}",
  443. "#endif"
  444. ].join("\n"),
  445. // ALPHATEST
  446. alphatest_fragment: [
  447. "#ifdef ALPHATEST",
  448. "if ( gl_FragColor.a < ALPHATEST ) discard;",
  449. "#endif"
  450. ].join("\n"),
  451. // LINEAR SPACE
  452. linear_to_gamma_fragment: [
  453. "#ifdef GAMMA_OUTPUT",
  454. "gl_FragColor.xyz = sqrt( gl_FragColor.xyz );",
  455. "#endif"
  456. ].join("\n"),
  457. };
  458. THREE.UniformsUtils = {
  459. merge: function ( uniforms ) {
  460. var u, p, tmp, merged = {};
  461. for ( u = 0; u < uniforms.length; u++ ) {
  462. tmp = this.clone( uniforms[ u ] );
  463. for ( p in tmp ) {
  464. merged[ p ] = tmp[ p ];
  465. }
  466. }
  467. return merged;
  468. },
  469. clone: function ( uniforms_src ) {
  470. var u, p, parameter, parameter_src, uniforms_dst = {};
  471. for ( u in uniforms_src ) {
  472. uniforms_dst[ u ] = {};
  473. for ( p in uniforms_src[ u ] ) {
  474. parameter_src = uniforms_src[ u ][ p ];
  475. if ( parameter_src instanceof THREE.Color ||
  476. parameter_src instanceof THREE.Vector2 ||
  477. parameter_src instanceof THREE.Vector3 ||
  478. parameter_src instanceof THREE.Vector4 ||
  479. parameter_src instanceof THREE.Matrix4 ||
  480. parameter_src instanceof THREE.Texture ) {
  481. uniforms_dst[ u ][ p ] = parameter_src.clone();
  482. } else if ( parameter_src instanceof Array ) {
  483. uniforms_dst[ u ][ p ] = parameter_src.slice();
  484. } else {
  485. uniforms_dst[ u ][ p ] = parameter_src;
  486. }
  487. }
  488. }
  489. return uniforms_dst;
  490. }
  491. };
  492. THREE.UniformsLib = {
  493. common: {
  494. "diffuse" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  495. "opacity" : { type: "f", value: 1.0 },
  496. "map" : { type: "t", value: 0, texture: null },
  497. "offsetRepeat" : { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) },
  498. "lightMap" : { type: "t", value: 2, texture: null },
  499. "envMap" : { type: "t", value: 1, texture: null },
  500. "flipEnvMap" : { type: "f", value: -1 },
  501. "useRefract" : { type: "i", value: 0 },
  502. "reflectivity" : { type: "f", value: 1.0 },
  503. "refractionRatio" : { type: "f", value: 0.98 },
  504. "combine" : { type: "i", value: 0 },
  505. "morphTargetInfluences" : { type: "f", value: 0 }
  506. },
  507. fog : {
  508. "fogDensity" : { type: "f", value: 0.00025 },
  509. "fogNear" : { type: "f", value: 1 },
  510. "fogFar" : { type: "f", value: 2000 },
  511. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  512. },
  513. lights: {
  514. "ambientLightColor" : { type: "fv", value: [] },
  515. "directionalLightDirection" : { type: "fv", value: [] },
  516. "directionalLightColor" : { type: "fv", value: [] },
  517. "pointLightColor" : { type: "fv", value: [] },
  518. "pointLightPosition" : { type: "fv", value: [] },
  519. "pointLightDistance" : { type: "fv1", value: [] }
  520. },
  521. particle: {
  522. "psColor" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  523. "opacity" : { type: "f", value: 1.0 },
  524. "size" : { type: "f", value: 1.0 },
  525. "scale" : { type: "f", value: 1.0 },
  526. "map" : { type: "t", value: 0, texture: null },
  527. "fogDensity" : { type: "f", value: 0.00025 },
  528. "fogNear" : { type: "f", value: 1 },
  529. "fogFar" : { type: "f", value: 2000 },
  530. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  531. },
  532. shadowmap: {
  533. "shadowMap": { type: "tv", value: 6, texture: [] },
  534. "shadowMatrix" : { type: "m4v", value: [] },
  535. "shadowBias" : { type: "f", value: 0.0039 },
  536. "shadowDarkness": { type: "f", value: 0.2 }
  537. }
  538. };
  539. THREE.ShaderLib = {
  540. 'sprite': {
  541. vertexShader: [
  542. "uniform int useScreenCoordinates;",
  543. "uniform int affectedByDistance;",
  544. "uniform vec3 screenPosition;",
  545. "uniform mat4 modelViewMatrix;",
  546. "uniform mat4 projectionMatrix;",
  547. "uniform float rotation;",
  548. "uniform vec2 scale;",
  549. "uniform vec2 alignment;",
  550. "uniform vec2 uvOffset;",
  551. "uniform vec2 uvScale;",
  552. "attribute vec2 position;",
  553. "attribute vec2 uv;",
  554. "varying vec2 vUV;",
  555. "void main() {",
  556. "vUV = uvOffset + uv * uvScale;",
  557. "vec2 alignedPosition = position + alignment;",
  558. "vec2 rotatedPosition;",
  559. "rotatedPosition.x = ( cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y ) * scale.x;",
  560. "rotatedPosition.y = ( sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y ) * scale.y;",
  561. "vec4 finalPosition;",
  562. "if( useScreenCoordinates != 0 ) {",
  563. "finalPosition = vec4( screenPosition.xy + rotatedPosition, screenPosition.z, 1.0 );",
  564. "} else {",
  565. "finalPosition = projectionMatrix * modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );",
  566. "finalPosition.xy += rotatedPosition * ( affectedByDistance == 1 ? 1.0 : finalPosition.z );",
  567. "}",
  568. "gl_Position = finalPosition;",
  569. "}"
  570. ].join( "\n" ),
  571. fragmentShader: [
  572. "#ifdef GL_ES",
  573. "precision highp float;",
  574. "#endif",
  575. "uniform vec3 color;",
  576. "uniform sampler2D map;",
  577. "uniform float opacity;",
  578. "varying vec2 vUV;",
  579. "void main() {",
  580. "vec4 texture = texture2D( map, vUV );",
  581. "gl_FragColor = vec4( color * texture.xyz, texture.a * opacity );",
  582. "}"
  583. ].join( "\n" )
  584. },
  585. 'depth': {
  586. uniforms: {
  587. "mNear": { type: "f", value: 1.0 },
  588. "mFar" : { type: "f", value: 2000.0 },
  589. "opacity" : { type: "f", value: 1.0 }
  590. },
  591. vertexShader: [
  592. "void main() {",
  593. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  594. "}"
  595. ].join("\n"),
  596. fragmentShader: [
  597. "uniform float mNear;",
  598. "uniform float mFar;",
  599. "uniform float opacity;",
  600. "void main() {",
  601. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  602. "float color = 1.0 - smoothstep( mNear, mFar, depth );",
  603. "gl_FragColor = vec4( vec3( color ), opacity );",
  604. "}"
  605. ].join("\n")
  606. },
  607. 'normal': {
  608. uniforms: {
  609. "opacity" : { type: "f", value: 1.0 }
  610. },
  611. vertexShader: [
  612. "varying vec3 vNormal;",
  613. "void main() {",
  614. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  615. "vNormal = normalize( normalMatrix * normal );",
  616. "gl_Position = projectionMatrix * mvPosition;",
  617. "}"
  618. ].join("\n"),
  619. fragmentShader: [
  620. "uniform float opacity;",
  621. "varying vec3 vNormal;",
  622. "void main() {",
  623. "gl_FragColor = vec4( 0.5 * normalize( vNormal ) + 0.5, opacity );",
  624. "}"
  625. ].join("\n")
  626. },
  627. 'basic': {
  628. uniforms: THREE.UniformsUtils.merge( [
  629. THREE.UniformsLib[ "common" ],
  630. THREE.UniformsLib[ "fog" ],
  631. THREE.UniformsLib[ "shadowmap" ]
  632. ] ),
  633. vertexShader: [
  634. THREE.ShaderChunk[ "map_pars_vertex" ],
  635. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  636. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  637. THREE.ShaderChunk[ "color_pars_vertex" ],
  638. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  639. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  640. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  641. "void main() {",
  642. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  643. THREE.ShaderChunk[ "map_vertex" ],
  644. THREE.ShaderChunk[ "lightmap_vertex" ],
  645. THREE.ShaderChunk[ "envmap_vertex" ],
  646. THREE.ShaderChunk[ "color_vertex" ],
  647. THREE.ShaderChunk[ "skinning_vertex" ],
  648. THREE.ShaderChunk[ "morphtarget_vertex" ],
  649. THREE.ShaderChunk[ "default_vertex" ],
  650. THREE.ShaderChunk[ "shadowmap_vertex" ],
  651. "}"
  652. ].join("\n"),
  653. fragmentShader: [
  654. "uniform vec3 diffuse;",
  655. "uniform float opacity;",
  656. THREE.ShaderChunk[ "color_pars_fragment" ],
  657. THREE.ShaderChunk[ "map_pars_fragment" ],
  658. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  659. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  660. THREE.ShaderChunk[ "fog_pars_fragment" ],
  661. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  662. "void main() {",
  663. "gl_FragColor = vec4( diffuse, opacity );",
  664. THREE.ShaderChunk[ "map_fragment" ],
  665. THREE.ShaderChunk[ "alphatest_fragment" ],
  666. THREE.ShaderChunk[ "lightmap_fragment" ],
  667. THREE.ShaderChunk[ "color_fragment" ],
  668. THREE.ShaderChunk[ "envmap_fragment" ],
  669. THREE.ShaderChunk[ "shadowmap_fragment" ],
  670. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  671. THREE.ShaderChunk[ "fog_fragment" ],
  672. "}"
  673. ].join("\n")
  674. },
  675. 'lambert': {
  676. uniforms: THREE.UniformsUtils.merge( [
  677. THREE.UniformsLib[ "common" ],
  678. THREE.UniformsLib[ "fog" ],
  679. THREE.UniformsLib[ "lights" ],
  680. THREE.UniformsLib[ "shadowmap" ],
  681. {
  682. "ambient" : { type: "c", value: new THREE.Color( 0x050505 ) }
  683. }
  684. ] ),
  685. vertexShader: [
  686. "varying vec3 vLightWeighting;",
  687. THREE.ShaderChunk[ "map_pars_vertex" ],
  688. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  689. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  690. THREE.ShaderChunk[ "lights_lambert_pars_vertex" ],
  691. THREE.ShaderChunk[ "color_pars_vertex" ],
  692. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  693. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  694. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  695. "void main() {",
  696. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  697. THREE.ShaderChunk[ "map_vertex" ],
  698. THREE.ShaderChunk[ "lightmap_vertex" ],
  699. THREE.ShaderChunk[ "envmap_vertex" ],
  700. THREE.ShaderChunk[ "color_vertex" ],
  701. "vec3 transformedNormal = normalize( normalMatrix * normal );",
  702. THREE.ShaderChunk[ "lights_lambert_vertex" ],
  703. THREE.ShaderChunk[ "skinning_vertex" ],
  704. THREE.ShaderChunk[ "morphtarget_vertex" ],
  705. THREE.ShaderChunk[ "default_vertex" ],
  706. THREE.ShaderChunk[ "shadowmap_vertex" ],
  707. "}"
  708. ].join("\n"),
  709. fragmentShader: [
  710. "uniform float opacity;",
  711. "varying vec3 vLightWeighting;",
  712. THREE.ShaderChunk[ "color_pars_fragment" ],
  713. THREE.ShaderChunk[ "map_pars_fragment" ],
  714. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  715. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  716. THREE.ShaderChunk[ "fog_pars_fragment" ],
  717. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  718. "void main() {",
  719. "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );",
  720. THREE.ShaderChunk[ "map_fragment" ],
  721. THREE.ShaderChunk[ "alphatest_fragment" ],
  722. "gl_FragColor.xyz = gl_FragColor.xyz * vLightWeighting;",
  723. THREE.ShaderChunk[ "lightmap_fragment" ],
  724. THREE.ShaderChunk[ "color_fragment" ],
  725. THREE.ShaderChunk[ "envmap_fragment" ],
  726. THREE.ShaderChunk[ "shadowmap_fragment" ],
  727. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  728. THREE.ShaderChunk[ "fog_fragment" ],
  729. "}"
  730. ].join("\n")
  731. },
  732. 'phong': {
  733. uniforms: THREE.UniformsUtils.merge( [
  734. THREE.UniformsLib[ "common" ],
  735. THREE.UniformsLib[ "fog" ],
  736. THREE.UniformsLib[ "lights" ],
  737. THREE.UniformsLib[ "shadowmap" ],
  738. {
  739. "ambient" : { type: "c", value: new THREE.Color( 0x050505 ) },
  740. "specular" : { type: "c", value: new THREE.Color( 0x111111 ) },
  741. "shininess": { type: "f", value: 30 }
  742. }
  743. ] ),
  744. vertexShader: [
  745. "varying vec3 vViewPosition;",
  746. "varying vec3 vNormal;",
  747. THREE.ShaderChunk[ "map_pars_vertex" ],
  748. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  749. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  750. THREE.ShaderChunk[ "lights_phong_pars_vertex" ],
  751. THREE.ShaderChunk[ "color_pars_vertex" ],
  752. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  753. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  754. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  755. "void main() {",
  756. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  757. THREE.ShaderChunk[ "map_vertex" ],
  758. THREE.ShaderChunk[ "lightmap_vertex" ],
  759. THREE.ShaderChunk[ "envmap_vertex" ],
  760. THREE.ShaderChunk[ "color_vertex" ],
  761. "#ifndef USE_ENVMAP",
  762. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  763. "#endif",
  764. "vViewPosition = -mvPosition.xyz;",
  765. "vec3 transformedNormal = normalMatrix * normal;",
  766. "vNormal = transformedNormal;",
  767. THREE.ShaderChunk[ "lights_phong_vertex" ],
  768. THREE.ShaderChunk[ "skinning_vertex" ],
  769. THREE.ShaderChunk[ "morphtarget_vertex" ],
  770. THREE.ShaderChunk[ "default_vertex" ],
  771. THREE.ShaderChunk[ "shadowmap_vertex" ],
  772. "}"
  773. ].join("\n"),
  774. fragmentShader: [
  775. "uniform vec3 diffuse;",
  776. "uniform float opacity;",
  777. "uniform vec3 ambient;",
  778. "uniform vec3 specular;",
  779. "uniform float shininess;",
  780. THREE.ShaderChunk[ "color_pars_fragment" ],
  781. THREE.ShaderChunk[ "map_pars_fragment" ],
  782. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  783. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  784. THREE.ShaderChunk[ "fog_pars_fragment" ],
  785. THREE.ShaderChunk[ "lights_phong_pars_fragment" ],
  786. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  787. "void main() {",
  788. "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );",
  789. THREE.ShaderChunk[ "map_fragment" ],
  790. THREE.ShaderChunk[ "alphatest_fragment" ],
  791. THREE.ShaderChunk[ "lights_phong_fragment" ],
  792. THREE.ShaderChunk[ "lightmap_fragment" ],
  793. THREE.ShaderChunk[ "color_fragment" ],
  794. THREE.ShaderChunk[ "envmap_fragment" ],
  795. THREE.ShaderChunk[ "shadowmap_fragment" ],
  796. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  797. THREE.ShaderChunk[ "fog_fragment" ],
  798. "}"
  799. ].join("\n")
  800. },
  801. 'particle_basic': {
  802. uniforms: THREE.UniformsUtils.merge( [
  803. THREE.UniformsLib[ "particle" ],
  804. THREE.UniformsLib[ "shadowmap" ]
  805. ] ),
  806. vertexShader: [
  807. "uniform float size;",
  808. "uniform float scale;",
  809. THREE.ShaderChunk[ "color_pars_vertex" ],
  810. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  811. "void main() {",
  812. THREE.ShaderChunk[ "color_vertex" ],
  813. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  814. "#ifdef USE_SIZEATTENUATION",
  815. "gl_PointSize = size * ( scale / length( mvPosition.xyz ) );",
  816. "#else",
  817. "gl_PointSize = size;",
  818. "#endif",
  819. "gl_Position = projectionMatrix * mvPosition;",
  820. THREE.ShaderChunk[ "shadowmap_vertex" ],
  821. "}"
  822. ].join("\n"),
  823. fragmentShader: [
  824. "uniform vec3 psColor;",
  825. "uniform float opacity;",
  826. THREE.ShaderChunk[ "color_pars_fragment" ],
  827. THREE.ShaderChunk[ "map_particle_pars_fragment" ],
  828. THREE.ShaderChunk[ "fog_pars_fragment" ],
  829. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  830. "void main() {",
  831. "gl_FragColor = vec4( psColor, opacity );",
  832. THREE.ShaderChunk[ "map_particle_fragment" ],
  833. THREE.ShaderChunk[ "alphatest_fragment" ],
  834. THREE.ShaderChunk[ "color_fragment" ],
  835. THREE.ShaderChunk[ "shadowmap_fragment" ],
  836. THREE.ShaderChunk[ "fog_fragment" ],
  837. "}"
  838. ].join("\n")
  839. },
  840. // Depth encoding into RGBA texture
  841. // based on SpiderGL shadow map example
  842. // http://spidergl.org/example.php?id=6
  843. // originally from
  844. // http://www.gamedev.net/topic/442138-packing-a-float-into-a-a8r8g8b8-texture-shader/page__whichpage__1%25EF%25BF%25BD
  845. // see also here:
  846. // http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
  847. 'depthRGBA': {
  848. uniforms: {},
  849. vertexShader: [
  850. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  851. "void main() {",
  852. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  853. THREE.ShaderChunk[ "morphtarget_vertex" ],
  854. THREE.ShaderChunk[ "default_vertex" ],
  855. "}"
  856. ].join("\n"),
  857. fragmentShader: [
  858. "vec4 pack_depth( const in float depth ) {",
  859. "const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );",
  860. "const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );",
  861. "vec4 res = fract( depth * bit_shift );",
  862. "res -= res.xxyz * bit_mask;",
  863. "return res;",
  864. "}",
  865. "void main() {",
  866. "gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z );",
  867. //"gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z / gl_FragCoord.w );",
  868. //"float z = ( ( gl_FragCoord.z / gl_FragCoord.w ) - 3.0 ) / ( 4000.0 - 3.0 );",
  869. //"gl_FragData[ 0 ] = pack_depth( z );",
  870. //"gl_FragData[ 0 ] = vec4( z, z, z, 1.0 );",
  871. "}"
  872. ].join("\n")
  873. }
  874. };