WebGLShaders.js 35 KB

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