WebGLShaders.js 48 KB

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