WebGLShaders.js 26 KB

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