WebGLShaders.js 34 KB

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