WebGLShaders.js 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204
  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 int combine;",
  39. "#endif"
  40. ].join("\n"),
  41. envmap_fragment: [
  42. "#ifdef USE_ENVMAP",
  43. "vec4 cubeColor = textureCube( envMap, vec3( -vReflect.x, vReflect.yz ) );",
  44. "if ( combine == 1 ) {",
  45. //"gl_FragColor = mix( gl_FragColor, cubeColor, reflectivity );",
  46. "gl_FragColor = vec4( mix( gl_FragColor.xyz, cubeColor.xyz, reflectivity ), opacity );",
  47. "} else {",
  48. "gl_FragColor = gl_FragColor * cubeColor;",
  49. "}",
  50. "#endif"
  51. ].join("\n"),
  52. envmap_pars_vertex: [
  53. "#ifdef USE_ENVMAP",
  54. "varying vec3 vReflect;",
  55. "uniform float refractionRatio;",
  56. "uniform bool useRefract;",
  57. "#endif"
  58. ].join("\n"),
  59. envmap_vertex : [
  60. "#ifdef USE_ENVMAP",
  61. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  62. "vec3 nWorld = mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal;",
  63. "if ( useRefract ) {",
  64. "vReflect = refract( normalize( mPosition.xyz - cameraPosition ), normalize( nWorld.xyz ), refractionRatio );",
  65. "} else {",
  66. "vReflect = reflect( normalize( mPosition.xyz - cameraPosition ), normalize( nWorld.xyz ) );",
  67. "}",
  68. "#endif"
  69. ].join("\n"),
  70. // COLOR MAP (particles)
  71. map_particle_pars_fragment: [
  72. "#ifdef USE_MAP",
  73. "uniform sampler2D map;",
  74. "#endif"
  75. ].join("\n"),
  76. map_particle_fragment: [
  77. "#ifdef USE_MAP",
  78. "gl_FragColor = gl_FragColor * texture2D( map, gl_PointCoord );",
  79. "#endif"
  80. ].join("\n"),
  81. // COLOR MAP (triangles)
  82. map_pars_fragment: [
  83. "#ifdef USE_MAP",
  84. "varying vec2 vUv;",
  85. "uniform sampler2D map;",
  86. "#endif"
  87. ].join("\n"),
  88. map_pars_vertex: [
  89. "#ifdef USE_MAP",
  90. "varying vec2 vUv;",
  91. "#endif"
  92. ].join("\n"),
  93. map_fragment: [
  94. "#ifdef USE_MAP",
  95. "gl_FragColor = gl_FragColor * texture2D( map, vUv );",
  96. "#endif"
  97. ].join("\n"),
  98. map_vertex: [
  99. "#ifdef USE_MAP",
  100. "vUv = uv;",
  101. "#endif"
  102. ].join("\n"),
  103. // LIGHT MAP
  104. lightmap_pars_fragment: [
  105. "#ifdef USE_LIGHTMAP",
  106. "varying vec2 vUv2;",
  107. "uniform sampler2D lightMap;",
  108. "#endif"
  109. ].join("\n"),
  110. lightmap_pars_vertex: [
  111. "#ifdef USE_LIGHTMAP",
  112. "varying vec2 vUv2;",
  113. "#endif"
  114. ].join("\n"),
  115. lightmap_fragment: [
  116. "#ifdef USE_LIGHTMAP",
  117. "gl_FragColor = gl_FragColor * texture2D( lightMap, vUv2 );",
  118. "#endif"
  119. ].join("\n"),
  120. lightmap_vertex: [
  121. "#ifdef USE_LIGHTMAP",
  122. "vUv2 = uv2;",
  123. "#endif"
  124. ].join("\n"),
  125. lights_pars_vertex: [
  126. "uniform bool enableLighting;",
  127. "uniform vec3 ambientLightColor;",
  128. "#if MAX_DIR_LIGHTS > 0",
  129. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  130. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  131. "#endif",
  132. "#if MAX_POINT_LIGHTS > 0",
  133. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  134. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  135. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  136. "#ifdef PHONG",
  137. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  138. "#endif",
  139. "#endif"
  140. ].join("\n"),
  141. // LIGHTS
  142. lights_vertex: [
  143. "if ( !enableLighting ) {",
  144. "vLightWeighting = vec3( 1.0 );",
  145. "} else {",
  146. "vLightWeighting = ambientLightColor;",
  147. "#if MAX_DIR_LIGHTS > 0",
  148. "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  149. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  150. "float directionalLightWeighting = max( dot( transformedNormal, normalize( lDirection.xyz ) ), 0.0 );",
  151. "vLightWeighting += directionalLightColor[ i ] * directionalLightWeighting;",
  152. "}",
  153. "#endif",
  154. "#if MAX_POINT_LIGHTS > 0",
  155. "for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",
  156. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  157. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  158. "float lDistance = 1.0;",
  159. "if ( pointLightDistance[ i ] > 0.0 )",
  160. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  161. "lVector = normalize( lVector );",
  162. "float pointLightWeighting = max( dot( transformedNormal, lVector ), 0.0 );",
  163. "vLightWeighting += pointLightColor[ i ] * pointLightWeighting * lDistance;",
  164. "#ifdef PHONG",
  165. "vPointLight[ i ] = vec4( lVector, lDistance );",
  166. "#endif",
  167. "}",
  168. "#endif",
  169. "}"
  170. ].join("\n"),
  171. lights_pars_fragment: [
  172. "#if MAX_DIR_LIGHTS > 0",
  173. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  174. "#endif",
  175. "#if MAX_POINT_LIGHTS > 0",
  176. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  177. "#endif",
  178. "varying vec3 vViewPosition;",
  179. "varying vec3 vNormal;"
  180. ].join("\n"),
  181. lights_fragment: [
  182. "vec3 normal = normalize( vNormal );",
  183. "vec3 viewPosition = normalize( vViewPosition );",
  184. "vec4 mColor = vec4( diffuse, opacity );",
  185. "vec4 mSpecular = vec4( specular, opacity );",
  186. "#if MAX_POINT_LIGHTS > 0",
  187. "vec4 pointDiffuse = vec4( 0.0 );",
  188. "vec4 pointSpecular = vec4( 0.0 );",
  189. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  190. "vec3 pointVector = normalize( vPointLight[ i ].xyz );",
  191. "vec3 pointHalfVector = normalize( vPointLight[ i ].xyz + vViewPosition );",
  192. "float pointDistance = vPointLight[ i ].w;",
  193. "float pointDotNormalHalf = dot( normal, pointHalfVector );",
  194. "float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
  195. "float pointSpecularWeight = 0.0;",
  196. "if ( pointDotNormalHalf >= 0.0 )",
  197. "pointSpecularWeight = pow( pointDotNormalHalf, shininess );",
  198. "pointDiffuse += mColor * pointDiffuseWeight * pointDistance;",
  199. "pointSpecular += mSpecular * pointSpecularWeight * pointDistance;",
  200. "}",
  201. "#endif",
  202. "#if MAX_DIR_LIGHTS > 0",
  203. "vec4 dirDiffuse = vec4( 0.0 );",
  204. "vec4 dirSpecular = vec4( 0.0 );" ,
  205. "for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  206. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  207. "vec3 dirVector = normalize( lDirection.xyz );",
  208. "vec3 dirHalfVector = normalize( lDirection.xyz + vViewPosition );",
  209. "float dirDotNormalHalf = dot( normal, dirHalfVector );",
  210. "float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  211. "float dirSpecularWeight = 0.0;",
  212. "if ( dirDotNormalHalf >= 0.0 )",
  213. "dirSpecularWeight = pow( dirDotNormalHalf, shininess );",
  214. "dirDiffuse += mColor * dirDiffuseWeight;",
  215. "dirSpecular += mSpecular * dirSpecularWeight;",
  216. "}",
  217. "#endif",
  218. "vec4 totalLight = vec4( ambient, opacity );",
  219. "#if MAX_DIR_LIGHTS > 0",
  220. "totalLight += dirDiffuse + dirSpecular;",
  221. "#endif",
  222. "#if MAX_POINT_LIGHTS > 0",
  223. "totalLight += pointDiffuse + pointSpecular;",
  224. "#endif",
  225. "gl_FragColor = gl_FragColor * totalLight;"
  226. ].join("\n"),
  227. // VERTEX COLORS
  228. color_pars_fragment: [
  229. "#ifdef USE_COLOR",
  230. "varying vec3 vColor;",
  231. "#endif"
  232. ].join("\n"),
  233. color_fragment: [
  234. "#ifdef USE_COLOR",
  235. "gl_FragColor = gl_FragColor * vec4( vColor, opacity );",
  236. "#endif"
  237. ].join("\n"),
  238. color_pars_vertex: [
  239. "#ifdef USE_COLOR",
  240. "varying vec3 vColor;",
  241. "#endif"
  242. ].join("\n"),
  243. color_vertex: [
  244. "#ifdef USE_COLOR",
  245. "vColor = color;",
  246. "#endif"
  247. ].join("\n"),
  248. // skinning
  249. skinning_pars_vertex: [
  250. "#ifdef USE_SKINNING",
  251. "uniform mat4 boneGlobalMatrices[ MAX_BONES ];",
  252. "#endif"
  253. ].join("\n"),
  254. skinning_vertex: [
  255. "#ifdef USE_SKINNING",
  256. "gl_Position = ( boneGlobalMatrices[ int( skinIndex.x ) ] * skinVertexA ) * skinWeight.x;",
  257. "gl_Position += ( boneGlobalMatrices[ int( skinIndex.y ) ] * skinVertexB ) * skinWeight.y;",
  258. // this doesn't work, no idea why
  259. //"gl_Position = projectionMatrix * cameraInverseMatrix * objectMatrix * gl_Position;",
  260. "gl_Position = projectionMatrix * viewMatrix * objectMatrix * gl_Position;",
  261. "#endif"
  262. ].join("\n"),
  263. // morphing
  264. morphtarget_pars_vertex: [
  265. "#ifdef USE_MORPHTARGETS",
  266. "uniform float morphTargetInfluences[ 8 ];",
  267. "#endif"
  268. ].join("\n"),
  269. morphtarget_vertex: [
  270. "#ifdef USE_MORPHTARGETS",
  271. "vec3 morphed = vec3( 0.0, 0.0, 0.0 );",
  272. "morphed += ( morphTarget0 - position ) * morphTargetInfluences[ 0 ];",
  273. "morphed += ( morphTarget1 - position ) * morphTargetInfluences[ 1 ];",
  274. "morphed += ( morphTarget2 - position ) * morphTargetInfluences[ 2 ];",
  275. "morphed += ( morphTarget3 - position ) * morphTargetInfluences[ 3 ];",
  276. "morphed += ( morphTarget4 - position ) * morphTargetInfluences[ 4 ];",
  277. "morphed += ( morphTarget5 - position ) * morphTargetInfluences[ 5 ];",
  278. "morphed += ( morphTarget6 - position ) * morphTargetInfluences[ 6 ];",
  279. "morphed += ( morphTarget7 - position ) * morphTargetInfluences[ 7 ];",
  280. "morphed += position;",
  281. "gl_Position = projectionMatrix * modelViewMatrix * vec4( morphed, 1.0 );",
  282. "#endif"
  283. ].join("\n"),
  284. default_vertex : [
  285. "#ifndef USE_MORPHTARGETS",
  286. "#ifndef USE_SKINNING",
  287. "gl_Position = projectionMatrix * mvPosition;",
  288. "#endif",
  289. "#endif"
  290. ].join("\n")
  291. };
  292. THREE.UniformsLib = {
  293. common: {
  294. "diffuse" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  295. "opacity" : { type: "f", value: 1.0 },
  296. "map" : { type: "t", value: 0, texture: null },
  297. "lightMap" : { type: "t", value: 2, texture: null },
  298. "envMap" : { type: "t", value: 1, texture: null },
  299. "useRefract" : { type: "i", value: 0 },
  300. "reflectivity" : { type: "f", value: 1.0 },
  301. "refractionRatio" : { type: "f", value: 0.98 },
  302. "combine" : { type: "i", value: 0 },
  303. "fogDensity" : { type: "f", value: 0.00025 },
  304. "fogNear" : { type: "f", value: 1 },
  305. "fogFar" : { type: "f", value: 2000 },
  306. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) },
  307. "morphTargetInfluences" : { type: "f", value: 0 }
  308. },
  309. lights: {
  310. "enableLighting" : { type: "i", value: 1 },
  311. "ambientLightColor" : { type: "fv", value: [] },
  312. "directionalLightDirection" : { type: "fv", value: [] },
  313. "directionalLightColor" : { type: "fv", value: [] },
  314. "pointLightColor" : { type: "fv", value: [] },
  315. "pointLightPosition" : { type: "fv", value: [] },
  316. "pointLightDistance" : { type: "fv1", value: [] }
  317. },
  318. particle: {
  319. "psColor" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  320. "opacity" : { type: "f", value: 1.0 },
  321. "size" : { type: "f", value: 1.0 },
  322. "scale" : { type: "f", value: 1.0 },
  323. "map" : { type: "t", value: 0, texture: null },
  324. "fogDensity" : { type: "f", value: 0.00025 },
  325. "fogNear" : { type: "f", value: 1 },
  326. "fogFar" : { type: "f", value: 2000 },
  327. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  328. }
  329. };
  330. THREE.ShaderLib = {
  331. 'lensFlareVertexTexture': {
  332. vertexShader: [
  333. "uniform vec3 screenPosition;",
  334. "uniform vec2 scale;",
  335. "uniform float rotation;",
  336. "uniform int renderType;",
  337. "uniform sampler2D occlusionMap;",
  338. "attribute vec2 position;",
  339. "attribute vec2 UV;",
  340. "varying vec2 vUV;",
  341. "varying float vVisibility;",
  342. "void main(void)",
  343. "{",
  344. "vUV = UV;",
  345. "vec2 pos = position;",
  346. "if( renderType == 2 ) {",
  347. "float visibility = texture2D( occlusionMap, vec2( 0.1, 0.1 )).a +",
  348. "texture2D( occlusionMap, vec2( 0.5, 0.1 )).a +",
  349. "texture2D( occlusionMap, vec2( 0.9, 0.1 )).a +",
  350. "texture2D( occlusionMap, vec2( 0.9, 0.5 )).a +",
  351. "texture2D( occlusionMap, vec2( 0.9, 0.9 )).a +",
  352. "texture2D( occlusionMap, vec2( 0.5, 0.9 )).a +",
  353. "texture2D( occlusionMap, vec2( 0.1, 0.9 )).a +",
  354. "texture2D( occlusionMap, vec2( 0.1, 0.5 )).a +",
  355. "texture2D( occlusionMap, vec2( 0.5, 0.5 )).a;",
  356. "vVisibility = ( 1.0 - visibility / 9.0 );",
  357. "pos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;",
  358. "pos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;",
  359. "}",
  360. "gl_Position = vec4(( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );",
  361. "}"
  362. ].join( "\n" ),
  363. fragmentShader: [
  364. "#ifdef GL_ES",
  365. "precision highp float;",
  366. "#endif",
  367. "uniform sampler2D map;",
  368. "uniform float opacity;",
  369. "uniform int renderType;",
  370. "varying vec2 vUV;",
  371. "varying float vVisibility;",
  372. "void main( void )",
  373. "{",
  374. // pink square
  375. "if( renderType == 0 ) {",
  376. "gl_FragColor = vec4( texture2D( map, vUV ).rgb, 0.0 );",
  377. // "gl_FragColor = vec4( 1.0, 0.0, 1.0, 0.0 );",
  378. // restore
  379. "} else if( renderType == 1 ) {",
  380. "gl_FragColor = texture2D( map, vUV );",
  381. // flare
  382. "} else {",
  383. "vec4 color = texture2D( map, vUV );",
  384. "color.a *= opacity * vVisibility;",
  385. "gl_FragColor = color;",
  386. "}",
  387. "}"
  388. ].join( "\n" )
  389. },
  390. 'lensFlare': {
  391. vertexShader: [
  392. "uniform vec3 screenPosition;",
  393. "uniform vec2 scale;",
  394. "uniform float rotation;",
  395. "uniform int renderType;",
  396. "attribute vec2 position;",
  397. "attribute vec2 UV;",
  398. "varying vec2 vUV;",
  399. "void main(void)",
  400. "{",
  401. "vUV = UV;",
  402. "vec2 pos = position;",
  403. "if( renderType == 2 ) {",
  404. "pos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;",
  405. "pos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;",
  406. "}",
  407. "gl_Position = vec4(( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );",
  408. "}"
  409. ].join( "\n" ),
  410. fragmentShader: [
  411. "#ifdef GL_ES",
  412. "precision highp float;",
  413. "#endif",
  414. "uniform sampler2D map;",
  415. "uniform sampler2D occlusionMap;",
  416. "uniform float opacity;",
  417. "uniform int renderType;",
  418. "varying vec2 vUV;",
  419. "void main( void )",
  420. "{",
  421. // pink square
  422. "if( renderType == 0 ) {",
  423. "gl_FragColor = vec4( texture2D( map, vUV ).rgb, 0.0 );",
  424. // restore
  425. "} else if( renderType == 1 ) {",
  426. "gl_FragColor = texture2D( map, vUV );",
  427. // flare
  428. "} else {",
  429. "float visibility = texture2D( occlusionMap, vec2( 0.5, 0.1 )).a +",
  430. "texture2D( occlusionMap, vec2( 0.9, 0.5 )).a +",
  431. "texture2D( occlusionMap, vec2( 0.5, 0.9 )).a +",
  432. "texture2D( occlusionMap, vec2( 0.1, 0.5 )).a;",
  433. "visibility = ( 1.0 - visibility / 4.0 );",
  434. "vec4 color = texture2D( map, vUV );",
  435. "color.a *= opacity * visibility;",
  436. "gl_FragColor = color;",
  437. "}",
  438. "}"
  439. ].join( "\n" )
  440. },
  441. 'sprite': {
  442. vertexShader: [
  443. "uniform int useScreenCoordinates;",
  444. "uniform int affectedByDistance;",
  445. "uniform vec3 screenPosition;",
  446. "uniform mat4 modelViewMatrix;",
  447. "uniform mat4 projectionMatrix;",
  448. "uniform float rotation;",
  449. "uniform vec2 scale;",
  450. "uniform vec2 alignment;",
  451. "uniform vec2 uvOffset;",
  452. "uniform vec2 uvScale;",
  453. "attribute vec2 position;",
  454. "attribute vec2 uv;",
  455. "varying vec2 vUV;",
  456. "void main(void)",
  457. "{",
  458. "vUV = uvOffset + uv * uvScale;",
  459. "vec2 alignedPosition = position + alignment;",
  460. "vec2 rotatedPosition;",
  461. "rotatedPosition.x = ( cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y ) * scale.x;",
  462. "rotatedPosition.y = ( sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y ) * scale.y;",
  463. "vec4 finalPosition;",
  464. "if( useScreenCoordinates != 0 ) {",
  465. "finalPosition = vec4( screenPosition.xy + rotatedPosition, screenPosition.z, 1.0 );",
  466. "} else {",
  467. "finalPosition = projectionMatrix * modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );",
  468. "finalPosition.xy += rotatedPosition * ( affectedByDistance == 1 ? 1.0 : finalPosition.z );",
  469. "}",
  470. "gl_Position = finalPosition;",
  471. "}"
  472. ].join( "\n" ),
  473. fragmentShader: [
  474. "#ifdef GL_ES",
  475. "precision highp float;",
  476. "#endif",
  477. "uniform sampler2D map;",
  478. "uniform float opacity;",
  479. "varying vec2 vUV;",
  480. "void main( void )",
  481. "{",
  482. "vec4 color = texture2D( map, vUV );",
  483. "color.a *= opacity;",
  484. "gl_FragColor = color;",
  485. // "gl_FragColor = vec4( 1.0, 0.0, 1.0, 1.0 );",
  486. "}"
  487. ].join( "\n" )
  488. },
  489. 'shadowPost': {
  490. vertexShader: [
  491. "uniform mat4 projectionMatrix;",
  492. "attribute vec3 position;",
  493. "void main(void)",
  494. "{",
  495. "gl_Position = projectionMatrix * vec4( position, 1.0 );",
  496. "}"
  497. ].join( "\n" ),
  498. fragmentShader: [
  499. "#ifdef GL_ES",
  500. "precision highp float;",
  501. "#endif",
  502. "uniform float darkness;",
  503. "void main( void )",
  504. "{",
  505. "gl_FragColor = vec4( 0, 0, 0, darkness );",
  506. "}"
  507. ].join( "\n" )
  508. },
  509. 'shadowVolumeDynamic': {
  510. uniforms: { "directionalLightDirection": { type: "fv", value: [] }},
  511. vertexShader: [
  512. "uniform vec3 directionalLightDirection;",
  513. "void main() {",
  514. "vec4 pos = objectMatrix * vec4( position, 1.0 );",
  515. "vec3 norm = mat3( objectMatrix[0].xyz, objectMatrix[1].xyz, objectMatrix[2].xyz ) * normal;",
  516. "vec4 extruded = vec4( directionalLightDirection * 5000.0 * step( 0.0, dot( directionalLightDirection, norm )), 0.0 );",
  517. "gl_Position = projectionMatrix * viewMatrix * ( pos + extruded );",
  518. "}"
  519. ].join( "\n" ),
  520. fragmentShader: [
  521. "void main() {",
  522. "gl_FragColor = vec4( 1, 1, 1, 1 );",
  523. "}"
  524. ].join( "\n" )
  525. },
  526. 'depth': {
  527. uniforms: { "mNear": { type: "f", value: 1.0 },
  528. "mFar" : { type: "f", value: 2000.0 },
  529. "opacity" : { type: "f", value: 1.0 }
  530. },
  531. fragmentShader: [
  532. "uniform float mNear;",
  533. "uniform float mFar;",
  534. "uniform float opacity;",
  535. "void main() {",
  536. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  537. "float color = 1.0 - smoothstep( mNear, mFar, depth );",
  538. "gl_FragColor = vec4( vec3( color ), opacity );",
  539. "}"
  540. ].join("\n"),
  541. vertexShader: [
  542. "void main() {",
  543. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  544. "}"
  545. ].join("\n")
  546. },
  547. 'normal': {
  548. uniforms: { "opacity" : { type: "f", value: 1.0 } },
  549. fragmentShader: [
  550. "uniform float opacity;",
  551. "varying vec3 vNormal;",
  552. "void main() {",
  553. "gl_FragColor = vec4( 0.5 * normalize( vNormal ) + 0.5, opacity );",
  554. "}"
  555. ].join("\n"),
  556. vertexShader: [
  557. "varying vec3 vNormal;",
  558. "void main() {",
  559. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  560. "vNormal = normalize( normalMatrix * normal );",
  561. "gl_Position = projectionMatrix * mvPosition;",
  562. "}"
  563. ].join("\n")
  564. },
  565. 'basic': {
  566. uniforms: THREE.UniformsLib[ "common" ],
  567. fragmentShader: [
  568. "uniform vec3 diffuse;",
  569. "uniform float opacity;",
  570. THREE.ShaderChunk[ "color_pars_fragment" ],
  571. THREE.ShaderChunk[ "map_pars_fragment" ],
  572. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  573. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  574. THREE.ShaderChunk[ "fog_pars_fragment" ],
  575. "void main() {",
  576. "gl_FragColor = vec4( diffuse, opacity );",
  577. THREE.ShaderChunk[ "map_fragment" ],
  578. THREE.ShaderChunk[ "lightmap_fragment" ],
  579. THREE.ShaderChunk[ "color_fragment" ],
  580. THREE.ShaderChunk[ "envmap_fragment" ],
  581. THREE.ShaderChunk[ "fog_fragment" ],
  582. "}"
  583. ].join("\n"),
  584. vertexShader: [
  585. THREE.ShaderChunk[ "map_pars_vertex" ],
  586. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  587. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  588. THREE.ShaderChunk[ "color_pars_vertex" ],
  589. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  590. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  591. "void main() {",
  592. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  593. THREE.ShaderChunk[ "map_vertex" ],
  594. THREE.ShaderChunk[ "lightmap_vertex" ],
  595. THREE.ShaderChunk[ "envmap_vertex" ],
  596. THREE.ShaderChunk[ "color_vertex" ],
  597. THREE.ShaderChunk[ "skinning_vertex" ],
  598. THREE.ShaderChunk[ "morphtarget_vertex" ],
  599. THREE.ShaderChunk[ "default_vertex" ],
  600. "}"
  601. ].join("\n")
  602. },
  603. 'lambert': {
  604. uniforms: Uniforms.merge( [ THREE.UniformsLib[ "common" ],
  605. THREE.UniformsLib[ "lights" ] ] ),
  606. fragmentShader: [
  607. "uniform vec3 diffuse;",
  608. "uniform float opacity;",
  609. "varying vec3 vLightWeighting;",
  610. THREE.ShaderChunk[ "color_pars_fragment" ],
  611. THREE.ShaderChunk[ "map_pars_fragment" ],
  612. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  613. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  614. THREE.ShaderChunk[ "fog_pars_fragment" ],
  615. "void main() {",
  616. "gl_FragColor = vec4( diffuse, opacity );",
  617. "gl_FragColor = gl_FragColor * vec4( vLightWeighting, 1.0 );",
  618. THREE.ShaderChunk[ "map_fragment" ],
  619. THREE.ShaderChunk[ "lightmap_fragment" ],
  620. THREE.ShaderChunk[ "color_fragment" ],
  621. THREE.ShaderChunk[ "envmap_fragment" ],
  622. THREE.ShaderChunk[ "fog_fragment" ],
  623. "}"
  624. ].join("\n"),
  625. vertexShader: [
  626. "varying vec3 vLightWeighting;",
  627. THREE.ShaderChunk[ "map_pars_vertex" ],
  628. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  629. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  630. THREE.ShaderChunk[ "lights_pars_vertex" ],
  631. THREE.ShaderChunk[ "color_pars_vertex" ],
  632. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  633. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  634. "void main() {",
  635. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  636. THREE.ShaderChunk[ "map_vertex" ],
  637. THREE.ShaderChunk[ "lightmap_vertex" ],
  638. THREE.ShaderChunk[ "envmap_vertex" ],
  639. THREE.ShaderChunk[ "color_vertex" ],
  640. "vec3 transformedNormal = normalize( normalMatrix * normal );",
  641. THREE.ShaderChunk[ "lights_vertex" ],
  642. THREE.ShaderChunk[ "skinning_vertex" ],
  643. THREE.ShaderChunk[ "morphtarget_vertex" ],
  644. THREE.ShaderChunk[ "default_vertex" ],
  645. "}"
  646. ].join("\n")
  647. },
  648. 'phong': {
  649. uniforms: Uniforms.merge( [ THREE.UniformsLib[ "common" ],
  650. THREE.UniformsLib[ "lights" ],
  651. { "ambient" : { type: "c", value: new THREE.Color( 0x050505 ) },
  652. "specular" : { type: "c", value: new THREE.Color( 0x111111 ) },
  653. "shininess": { type: "f", value: 30 }
  654. }
  655. ] ),
  656. fragmentShader: [
  657. "uniform vec3 diffuse;",
  658. "uniform float opacity;",
  659. "uniform vec3 ambient;",
  660. "uniform vec3 specular;",
  661. "uniform float shininess;",
  662. "varying vec3 vLightWeighting;",
  663. THREE.ShaderChunk[ "color_pars_fragment" ],
  664. THREE.ShaderChunk[ "map_pars_fragment" ],
  665. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  666. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  667. THREE.ShaderChunk[ "fog_pars_fragment" ],
  668. THREE.ShaderChunk[ "lights_pars_fragment" ],
  669. "void main() {",
  670. "gl_FragColor = vec4( vLightWeighting, 1.0 );",
  671. THREE.ShaderChunk[ "lights_fragment" ],
  672. THREE.ShaderChunk[ "map_fragment" ],
  673. THREE.ShaderChunk[ "lightmap_fragment" ],
  674. THREE.ShaderChunk[ "color_fragment" ],
  675. THREE.ShaderChunk[ "envmap_fragment" ],
  676. THREE.ShaderChunk[ "fog_fragment" ],
  677. "}"
  678. ].join("\n"),
  679. vertexShader: [
  680. "#define PHONG",
  681. "varying vec3 vLightWeighting;",
  682. "varying vec3 vViewPosition;",
  683. "varying vec3 vNormal;",
  684. THREE.ShaderChunk[ "map_pars_vertex" ],
  685. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  686. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  687. THREE.ShaderChunk[ "lights_pars_vertex" ],
  688. THREE.ShaderChunk[ "color_pars_vertex" ],
  689. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  690. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  691. "void main() {",
  692. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  693. THREE.ShaderChunk[ "map_vertex" ],
  694. THREE.ShaderChunk[ "lightmap_vertex" ],
  695. THREE.ShaderChunk[ "envmap_vertex" ],
  696. THREE.ShaderChunk[ "color_vertex" ],
  697. "#ifndef USE_ENVMAP",
  698. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  699. "#endif",
  700. "vViewPosition = cameraPosition - mPosition.xyz;",
  701. "vec3 transformedNormal = normalize( normalMatrix * normal );",
  702. "vNormal = transformedNormal;",
  703. THREE.ShaderChunk[ "lights_vertex" ],
  704. THREE.ShaderChunk[ "skinning_vertex" ],
  705. THREE.ShaderChunk[ "morphtarget_vertex" ],
  706. THREE.ShaderChunk[ "default_vertex" ],
  707. "}"
  708. ].join("\n")
  709. },
  710. 'particle_basic': {
  711. uniforms: THREE.UniformsLib[ "particle" ],
  712. fragmentShader: [
  713. "uniform vec3 psColor;",
  714. "uniform float opacity;",
  715. THREE.ShaderChunk[ "color_pars_fragment" ],
  716. THREE.ShaderChunk[ "map_particle_pars_fragment" ],
  717. THREE.ShaderChunk[ "fog_pars_fragment" ],
  718. "void main() {",
  719. "gl_FragColor = vec4( psColor, opacity );",
  720. THREE.ShaderChunk[ "map_particle_fragment" ],
  721. THREE.ShaderChunk[ "color_fragment" ],
  722. THREE.ShaderChunk[ "fog_fragment" ],
  723. "}"
  724. ].join("\n"),
  725. vertexShader: [
  726. "uniform float size;",
  727. "uniform float scale;",
  728. THREE.ShaderChunk[ "color_pars_vertex" ],
  729. "void main() {",
  730. THREE.ShaderChunk[ "color_vertex" ],
  731. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  732. "#ifdef USE_SIZEATTENUATION",
  733. "gl_PointSize = size * ( scale / length( mvPosition.xyz ) );",
  734. "#else",
  735. "gl_PointSize = size;",
  736. "#endif",
  737. "gl_Position = projectionMatrix * mvPosition;",
  738. "}"
  739. ].join("\n")
  740. }
  741. };