PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/js/ShaderDeferred.js

https://github.com/hschamberlin/three.js
JavaScript | 1104 lines | 612 code | 445 blank | 47 comment | 0 complexity | ba40742c2e8984a733b82bc41d9255c0 MD5 | raw file
Possible License(s): MIT, Apache-2.0
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author MPanknin / http://www.redplant.de/
  4. * @author benaadams / http://blog.illyriad.co.uk/
  5. *
  6. */
  7. THREE.DeferredShaderChunk = {
  8. // decode float to vec3
  9. unpackFloat: [
  10. "vec3 float_to_vec3( float data ) {",
  11. "vec3 uncompressed;",
  12. "uncompressed.x = fract( data );",
  13. "float zInt = floor( data / 255.0 );",
  14. "uncompressed.z = fract( zInt / 255.0 );",
  15. "uncompressed.y = fract( floor( data - ( zInt * 255.0 ) ) / 255.0 );",
  16. "return uncompressed;",
  17. "}"
  18. ].join("\n"),
  19. computeVertexPositionVS: [
  20. "vec2 texCoord = gl_FragCoord.xy / vec2( viewWidth, viewHeight );",
  21. "vec4 normalDepth = texture2D( samplerNormalDepth, texCoord );",
  22. "float z = normalDepth.w;",
  23. "if ( z == 0.0 ) discard;",
  24. "vec2 xy = texCoord * 2.0 - 1.0;",
  25. "vec4 vertexPositionProjected = vec4( xy, z, 1.0 );",
  26. "vec4 vertexPositionVS = matProjInverse * vertexPositionProjected;",
  27. "vertexPositionVS.xyz /= vertexPositionVS.w;",
  28. "vertexPositionVS.w = 1.0;"
  29. ].join("\n"),
  30. computeNormal: [
  31. "vec3 normal = normalDepth.xyz * 2.0 - 1.0;"
  32. ].join("\n"),
  33. unpackColorMap: [
  34. "vec4 colorMap = texture2D( samplerColor, texCoord );",
  35. "vec3 albedo = float_to_vec3( abs( colorMap.x ) );",
  36. "vec3 specularColor = float_to_vec3( abs( colorMap.y ) );",
  37. "float shininess = abs( colorMap.z );",
  38. "float wrapAround = sign( colorMap.z );",
  39. "float additiveSpecular = sign( colorMap.y );"
  40. ].join("\n"),
  41. computeDiffuse: [
  42. "float dotProduct = dot( normal, lightVector );",
  43. "float diffuseFull = max( dotProduct, 0.0 );",
  44. "vec3 diffuse;",
  45. "if ( wrapAround < 0.0 ) {",
  46. // wrap around lighting
  47. "float diffuseHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
  48. "const vec3 wrapRGB = vec3( 1.0, 1.0, 1.0 );",
  49. "diffuse = mix( vec3( diffuseFull ), vec3( diffuseHalf ), wrapRGB );",
  50. "} else {",
  51. // simple lighting
  52. "diffuse = vec3( diffuseFull );",
  53. "}"
  54. ].join("\n"),
  55. computeSpecular: [
  56. "vec3 halfVector = normalize( lightVector - normalize( vertexPositionVS.xyz ) );",
  57. "float dotNormalHalf = max( dot( normal, halfVector ), 0.0 );",
  58. // simple specular
  59. //"vec3 specular = specularColor * max( pow( dotNormalHalf, shininess ), 0.0 ) * diffuse;",
  60. // physically based specular
  61. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  62. "vec3 schlick = specularColor + vec3( 1.0 - specularColor ) * pow( 1.0 - dot( lightVector, halfVector ), 5.0 );",
  63. "vec3 specular = schlick * max( pow( dotNormalHalf, shininess ), 0.0 ) * diffuse * specularNormalization;"
  64. ].join("\n"),
  65. combine: [
  66. "vec3 light = lightIntensity * lightColor;",
  67. "gl_FragColor = vec4( light * ( albedo * diffuse + specular ), attenuation );"
  68. ].join("\n")
  69. };
  70. THREE.ShaderDeferred = {
  71. "color" : {
  72. uniforms: THREE.UniformsUtils.merge( [
  73. THREE.UniformsLib[ "common" ],
  74. THREE.UniformsLib[ "fog" ],
  75. THREE.UniformsLib[ "shadowmap" ],
  76. {
  77. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  78. "specular" : { type: "c", value: new THREE.Color( 0x111111 ) },
  79. "shininess": { type: "f", value: 30 },
  80. "wrapAround": { type: "f", value: 1 },
  81. "additiveSpecular": { type: "f", value: 1 },
  82. "samplerNormalDepth": { type: "t", value: null },
  83. "viewWidth": { type: "f", value: 800 },
  84. "viewHeight": { type: "f", value: 600 }
  85. }
  86. ] ),
  87. fragmentShader : [
  88. "uniform vec3 diffuse;",
  89. "uniform vec3 specular;",
  90. "uniform vec3 emissive;",
  91. "uniform float shininess;",
  92. "uniform float wrapAround;",
  93. "uniform float additiveSpecular;",
  94. THREE.ShaderChunk[ "color_pars_fragment" ],
  95. THREE.ShaderChunk[ "map_pars_fragment" ],
  96. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  97. "#ifdef USE_ENVMAP",
  98. "varying vec3 vWorldPosition;",
  99. "uniform float reflectivity;",
  100. "uniform samplerCube envMap;",
  101. "uniform float flipEnvMap;",
  102. "uniform int combine;",
  103. "uniform bool useRefract;",
  104. "uniform float refractionRatio;",
  105. "uniform sampler2D samplerNormalDepth;",
  106. "uniform float viewHeight;",
  107. "uniform float viewWidth;",
  108. "#endif",
  109. THREE.ShaderChunk[ "fog_pars_fragment" ],
  110. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  111. THREE.ShaderChunk[ "specularmap_pars_fragment" ],
  112. "const float unit = 255.0/256.0;",
  113. "float vec3_to_float( vec3 data ) {",
  114. "highp float compressed = fract( data.x * unit ) + floor( data.y * unit * 255.0 ) + floor( data.z * unit * 255.0 ) * 255.0;",
  115. "return compressed;",
  116. "}",
  117. "void main() {",
  118. "const float opacity = 1.0;",
  119. "gl_FragColor = vec4( diffuse, opacity );",
  120. THREE.ShaderChunk[ "map_fragment" ],
  121. THREE.ShaderChunk[ "alphatest_fragment" ],
  122. THREE.ShaderChunk[ "specularmap_fragment" ],
  123. THREE.ShaderChunk[ "lightmap_fragment" ],
  124. THREE.ShaderChunk[ "color_fragment" ],
  125. "#ifdef USE_ENVMAP",
  126. "vec2 texCoord = gl_FragCoord.xy / vec2( viewWidth, viewHeight );",
  127. "vec4 normalDepth = texture2D( samplerNormalDepth, texCoord );",
  128. "vec3 normal = normalDepth.xyz * 2.0 - 1.0;",
  129. "vec3 reflectVec;",
  130. "vec3 cameraToVertex = normalize( vWorldPosition - cameraPosition );",
  131. "if ( useRefract ) {",
  132. "reflectVec = refract( cameraToVertex, normal, refractionRatio );",
  133. "} else { ",
  134. "reflectVec = reflect( cameraToVertex, normal );",
  135. "}",
  136. "#ifdef DOUBLE_SIDED",
  137. "float flipNormal = ( -1.0 + 2.0 * float( gl_FrontFacing ) );",
  138. "vec4 cubeColor = textureCube( envMap, flipNormal * vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );",
  139. "#else",
  140. "vec4 cubeColor = textureCube( envMap, vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );",
  141. "#endif",
  142. "#ifdef GAMMA_INPUT",
  143. "cubeColor.xyz *= cubeColor.xyz;",
  144. "#endif",
  145. "if ( combine == 1 ) {",
  146. "gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularStrength * reflectivity );",
  147. "} else if ( combine == 2 ) {",
  148. "gl_FragColor.xyz += cubeColor.xyz * specularStrength * reflectivity;",
  149. "} else {",
  150. "gl_FragColor.xyz = mix( gl_FragColor.xyz, gl_FragColor.xyz * cubeColor.xyz, specularStrength * reflectivity );",
  151. "}",
  152. "#endif",
  153. THREE.ShaderChunk[ "shadowmap_fragment" ],
  154. THREE.ShaderChunk[ "fog_fragment" ],
  155. //
  156. "const float compressionScale = 0.999;",
  157. //
  158. "vec3 diffuseMapColor;",
  159. "#ifdef USE_MAP",
  160. "diffuseMapColor = texelColor.xyz;",
  161. "#else",
  162. "diffuseMapColor = vec3( 1.0 );",
  163. "#endif",
  164. // diffuse color
  165. "gl_FragColor.x = vec3_to_float( compressionScale * gl_FragColor.xyz );",
  166. // specular color
  167. "if ( additiveSpecular < 0.0 ) {",
  168. "gl_FragColor.y = vec3_to_float( compressionScale * specular );",
  169. "} else {",
  170. "gl_FragColor.y = vec3_to_float( compressionScale * specular * diffuseMapColor );",
  171. "}",
  172. "gl_FragColor.y *= additiveSpecular;",
  173. // shininess
  174. "gl_FragColor.z = wrapAround * shininess;",
  175. // emissive color
  176. "#ifdef USE_COLOR",
  177. "gl_FragColor.w = vec3_to_float( compressionScale * emissive * diffuseMapColor * vColor );",
  178. "#else",
  179. "gl_FragColor.w = vec3_to_float( compressionScale * emissive * diffuseMapColor );",
  180. "#endif",
  181. "}"
  182. ].join("\n"),
  183. vertexShader : [
  184. THREE.ShaderChunk[ "map_pars_vertex" ],
  185. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  186. THREE.ShaderChunk[ "color_pars_vertex" ],
  187. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  188. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  189. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  190. "#ifdef USE_ENVMAP",
  191. "varying vec3 vWorldPosition;",
  192. "#endif",
  193. "void main() {",
  194. THREE.ShaderChunk[ "map_vertex" ],
  195. THREE.ShaderChunk[ "lightmap_vertex" ],
  196. THREE.ShaderChunk[ "color_vertex" ],
  197. THREE.ShaderChunk[ "skinbase_vertex" ],
  198. THREE.ShaderChunk[ "morphtarget_vertex" ],
  199. THREE.ShaderChunk[ "skinning_vertex" ],
  200. THREE.ShaderChunk[ "default_vertex" ],
  201. THREE.ShaderChunk[ "worldpos_vertex" ],
  202. THREE.ShaderChunk[ "shadowmap_vertex" ],
  203. "#ifdef USE_ENVMAP",
  204. "vWorldPosition = worldPosition.xyz;",
  205. "#endif",
  206. "}"
  207. ].join("\n")
  208. },
  209. "normalDepth" : {
  210. uniforms: {
  211. bumpMap: { type: "t", value: null },
  212. bumpScale: { type: "f", value: 1 },
  213. offsetRepeat: { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) }
  214. },
  215. fragmentShader : [
  216. "#ifdef USE_BUMPMAP",
  217. "#extension GL_OES_standard_derivatives : enable\n",
  218. "varying vec2 vUv;",
  219. "varying vec3 vViewPosition;",
  220. THREE.ShaderChunk[ "bumpmap_pars_fragment" ],
  221. "#endif",
  222. "varying vec3 normalView;",
  223. "varying vec4 clipPos;",
  224. "void main() {",
  225. "vec3 normal = normalize( normalView );",
  226. "#ifdef USE_BUMPMAP",
  227. "normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );",
  228. "#endif",
  229. "gl_FragColor.xyz = normal * 0.5 + 0.5;",
  230. "gl_FragColor.w = clipPos.z / clipPos.w;",
  231. "}"
  232. ].join("\n"),
  233. vertexShader : [
  234. "varying vec3 normalView;",
  235. "varying vec4 clipPos;",
  236. "#ifdef USE_BUMPMAP",
  237. "varying vec2 vUv;",
  238. "varying vec3 vViewPosition;",
  239. "uniform vec4 offsetRepeat;",
  240. "#endif",
  241. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  242. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  243. "void main() {",
  244. THREE.ShaderChunk[ "morphnormal_vertex" ],
  245. THREE.ShaderChunk[ "skinbase_vertex" ],
  246. THREE.ShaderChunk[ "skinnormal_vertex" ],
  247. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  248. THREE.ShaderChunk[ "morphtarget_vertex" ],
  249. THREE.ShaderChunk[ "skinning_vertex" ],
  250. THREE.ShaderChunk[ "default_vertex" ],
  251. "normalView = normalize( normalMatrix * objectNormal );",
  252. "#ifdef USE_BUMPMAP",
  253. "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
  254. "vViewPosition = -mvPosition.xyz;",
  255. "#endif",
  256. "clipPos = gl_Position;",
  257. "}"
  258. ].join("\n")
  259. },
  260. "composite" : {
  261. uniforms: {
  262. samplerLight: { type: "t", value: null },
  263. brightness: { type: "f", value: 1 }
  264. },
  265. fragmentShader : [
  266. "varying vec2 texCoord;",
  267. "uniform sampler2D samplerLight;",
  268. "uniform float brightness;",
  269. // tonemapping operators
  270. // based on John Hable's HLSL snippets
  271. // from http://filmicgames.com/archives/75
  272. "#ifdef TONEMAP_UNCHARTED",
  273. "const float A = 0.15;",
  274. "const float B = 0.50;",
  275. "const float C = 0.10;",
  276. "const float D = 0.20;",
  277. "const float E = 0.02;",
  278. "const float F = 0.30;",
  279. "const float W = 11.2;",
  280. "vec3 Uncharted2Tonemap( vec3 x ) {",
  281. "return ( ( x * ( A * x + C * B ) + D * E ) / ( x * ( A * x + B ) + D * F ) ) - E / F;",
  282. "}",
  283. "#endif",
  284. "void main() {",
  285. "vec3 inColor = texture2D( samplerLight, texCoord ).xyz;",
  286. "inColor *= brightness;",
  287. "vec3 outColor;",
  288. "#if defined( TONEMAP_SIMPLE )",
  289. "outColor = sqrt( inColor );",
  290. "#elif defined( TONEMAP_LINEAR )",
  291. // simple linear to gamma conversion
  292. "outColor = pow( inColor, vec3( 1.0 / 2.2 ) );",
  293. "#elif defined( TONEMAP_REINHARD )",
  294. // Reinhard operator
  295. "inColor = inColor / ( 1.0 + inColor );",
  296. "outColor = pow( inColor, vec3( 1.0 / 2.2 ) );",
  297. "#elif defined( TONEMAP_FILMIC )",
  298. // filmic operator by Jim Hejl and Richard Burgess-Dawson
  299. "vec3 x = max( vec3( 0.0 ), inColor - 0.004 );",
  300. "outColor = ( x * ( 6.2 * x + 0.5 ) ) / ( x * ( 6.2 * x + 1.7 ) + 0.06 );",
  301. "#elif defined( TONEMAP_UNCHARTED )",
  302. // tonemapping operator from Uncharted 2 by John Hable
  303. "float ExposureBias = 2.0;",
  304. "vec3 curr = Uncharted2Tonemap( ExposureBias * inColor );",
  305. "vec3 whiteScale = vec3( 1.0 ) / Uncharted2Tonemap( vec3( W ) );",
  306. "vec3 color = curr * whiteScale;",
  307. "outColor = pow( color, vec3( 1.0 / 2.2 ) );",
  308. "#else",
  309. "outColor = inColor;",
  310. "#endif",
  311. "gl_FragColor = vec4( outColor, 1.0 );",
  312. "}"
  313. ].join("\n"),
  314. vertexShader : [
  315. "varying vec2 texCoord;",
  316. "void main() {",
  317. "vec4 pos = vec4( sign( position.xy ), 0.0, 1.0 );",
  318. "texCoord = pos.xy * vec2( 0.5 ) + 0.5;",
  319. "gl_Position = pos;",
  320. "}"
  321. ].join("\n")
  322. },
  323. "pointLight" : {
  324. uniforms: {
  325. samplerNormalDepth: { type: "t", value: null },
  326. samplerColor: { type: "t", value: null },
  327. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  328. viewWidth: { type: "f", value: 800 },
  329. viewHeight: { type: "f", value: 600 },
  330. lightPositionVS:{ type: "v3", value: new THREE.Vector3( 0, 0, 0 ) },
  331. lightColor: { type: "c", value: new THREE.Color( 0x000000 ) },
  332. lightIntensity: { type: "f", value: 1.0 },
  333. lightRadius: { type: "f", value: 1.0 }
  334. },
  335. fragmentShader : [
  336. "uniform sampler2D samplerColor;",
  337. "uniform sampler2D samplerNormalDepth;",
  338. "uniform float lightRadius;",
  339. "uniform float lightIntensity;",
  340. "uniform float viewHeight;",
  341. "uniform float viewWidth;",
  342. "uniform vec3 lightColor;",
  343. "uniform vec3 lightPositionVS;",
  344. "uniform mat4 matProjInverse;",
  345. THREE.DeferredShaderChunk[ "unpackFloat" ],
  346. "void main() {",
  347. THREE.DeferredShaderChunk[ "computeVertexPositionVS" ],
  348. // bail out early when pixel outside of light sphere
  349. "vec3 lightVector = lightPositionVS - vertexPositionVS.xyz;",
  350. "float distance = length( lightVector );",
  351. "if ( distance > lightRadius ) discard;",
  352. THREE.DeferredShaderChunk[ "computeNormal" ],
  353. THREE.DeferredShaderChunk[ "unpackColorMap" ],
  354. // compute light
  355. "lightVector = normalize( lightVector );",
  356. THREE.DeferredShaderChunk[ "computeDiffuse" ],
  357. THREE.DeferredShaderChunk[ "computeSpecular" ],
  358. // combine
  359. "float cutoff = 0.3;",
  360. "float denom = distance / lightRadius + 1.0;",
  361. "float attenuation = 1.0 / ( denom * denom );",
  362. "attenuation = ( attenuation - cutoff ) / ( 1.0 - cutoff );",
  363. "attenuation = max( attenuation, 0.0 );",
  364. "attenuation *= attenuation;",
  365. THREE.DeferredShaderChunk[ "combine" ],
  366. "}"
  367. ].join("\n"),
  368. vertexShader : [
  369. "void main() { ",
  370. // sphere proxy needs real position
  371. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  372. "gl_Position = projectionMatrix * mvPosition;",
  373. "}"
  374. ].join("\n")
  375. },
  376. "spotLight" : {
  377. uniforms: {
  378. samplerNormalDepth: { type: "t", value: null },
  379. samplerColor: { type: "t", value: null },
  380. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  381. viewWidth: { type: "f", value: 800 },
  382. viewHeight: { type: "f", value: 600 },
  383. lightPositionVS :{ type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
  384. lightDirectionVS:{ type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
  385. lightColor: { type: "c", value: new THREE.Color( 0x000000 ) },
  386. lightIntensity: { type: "f", value: 1.0 },
  387. lightDistance: { type: "f", value: 1.0 },
  388. lightAngle: { type: "f", value: 1.0 }
  389. },
  390. fragmentShader : [
  391. "uniform vec3 lightPositionVS;",
  392. "uniform vec3 lightDirectionVS;",
  393. "uniform sampler2D samplerColor;",
  394. "uniform sampler2D samplerNormalDepth;",
  395. "uniform float viewHeight;",
  396. "uniform float viewWidth;",
  397. "uniform float lightAngle;",
  398. "uniform float lightIntensity;",
  399. "uniform vec3 lightColor;",
  400. "uniform mat4 matProjInverse;",
  401. THREE.DeferredShaderChunk[ "unpackFloat" ],
  402. "void main() {",
  403. THREE.DeferredShaderChunk[ "computeVertexPositionVS" ],
  404. THREE.DeferredShaderChunk[ "computeNormal" ],
  405. THREE.DeferredShaderChunk[ "unpackColorMap" ],
  406. // compute light
  407. "vec3 lightVector = normalize( lightPositionVS.xyz - vertexPositionVS.xyz );",
  408. "float rho = dot( lightDirectionVS, lightVector );",
  409. "float rhoMax = cos( lightAngle * 0.5 );",
  410. "if ( rho <= rhoMax ) discard;",
  411. "float theta = rhoMax + 0.0001;",
  412. "float phi = rhoMax + 0.05;",
  413. "float falloff = 4.0;",
  414. "float spot = 0.0;",
  415. "if ( rho >= phi ) {",
  416. "spot = 1.0;",
  417. "} else if ( rho <= theta ) {",
  418. "spot = 0.0;",
  419. "} else { ",
  420. "spot = pow( ( rho - theta ) / ( phi - theta ), falloff );",
  421. "}",
  422. THREE.DeferredShaderChunk[ "computeDiffuse" ],
  423. "diffuse *= spot;",
  424. THREE.DeferredShaderChunk[ "computeSpecular" ],
  425. // combine
  426. "const float attenuation = 1.0;",
  427. THREE.DeferredShaderChunk[ "combine" ],
  428. "}"
  429. ].join("\n"),
  430. vertexShader : [
  431. "void main() { ",
  432. // full screen quad proxy
  433. "gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  434. "}"
  435. ].join("\n")
  436. },
  437. "directionalLight" : {
  438. uniforms: {
  439. samplerNormalDepth: { type: "t", value: null },
  440. samplerColor: { type: "t", value: null },
  441. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  442. viewWidth: { type: "f", value: 800 },
  443. viewHeight: { type: "f", value: 600 },
  444. lightDirectionVS: { type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
  445. lightColor: { type: "c", value: new THREE.Color( 0x000000 ) },
  446. lightIntensity: { type: "f", value: 1.0 }
  447. },
  448. fragmentShader : [
  449. "uniform sampler2D samplerColor;",
  450. "uniform sampler2D samplerNormalDepth;",
  451. "uniform float lightRadius;",
  452. "uniform float lightIntensity;",
  453. "uniform float viewHeight;",
  454. "uniform float viewWidth;",
  455. "uniform vec3 lightColor;",
  456. "uniform vec3 lightDirectionVS;",
  457. "uniform mat4 matProjInverse;",
  458. THREE.DeferredShaderChunk[ "unpackFloat" ],
  459. "void main() {",
  460. THREE.DeferredShaderChunk[ "computeVertexPositionVS" ],
  461. THREE.DeferredShaderChunk[ "computeNormal" ],
  462. THREE.DeferredShaderChunk[ "unpackColorMap" ],
  463. // compute light
  464. "vec3 lightVector = lightDirectionVS;",
  465. THREE.DeferredShaderChunk[ "computeDiffuse" ],
  466. THREE.DeferredShaderChunk[ "computeSpecular" ],
  467. // combine
  468. "const float attenuation = 1.0;",
  469. THREE.DeferredShaderChunk[ "combine" ],
  470. "}"
  471. ].join("\n"),
  472. vertexShader : [
  473. "void main() { ",
  474. // full screen quad proxy
  475. "gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  476. "}"
  477. ].join("\n")
  478. },
  479. "hemisphereLight" : {
  480. uniforms: {
  481. samplerNormalDepth: { type: "t", value: null },
  482. samplerColor: { type: "t", value: null },
  483. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  484. viewWidth: { type: "f", value: 800 },
  485. viewHeight: { type: "f", value: 600 },
  486. lightDirectionVS: { type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
  487. lightColorSky: { type: "c", value: new THREE.Color( 0x000000 ) },
  488. lightColorGround: { type: "c", value: new THREE.Color( 0x000000 ) },
  489. lightIntensity: { type: "f", value: 1.0 }
  490. },
  491. fragmentShader : [
  492. "uniform sampler2D samplerColor;",
  493. "uniform sampler2D samplerNormalDepth;",
  494. "uniform float lightRadius;",
  495. "uniform float lightIntensity;",
  496. "uniform float viewHeight;",
  497. "uniform float viewWidth;",
  498. "uniform vec3 lightColorSky;",
  499. "uniform vec3 lightColorGround;",
  500. "uniform vec3 lightDirectionVS;",
  501. "uniform mat4 matProjInverse;",
  502. THREE.DeferredShaderChunk[ "unpackFloat" ],
  503. "void main() {",
  504. THREE.DeferredShaderChunk[ "computeVertexPositionVS" ],
  505. THREE.DeferredShaderChunk[ "computeNormal" ],
  506. THREE.DeferredShaderChunk[ "unpackColorMap" ],
  507. // compute light
  508. "vec3 lightVector = lightDirectionVS;",
  509. // diffuse
  510. "float dotProduct = dot( normal, lightVector );",
  511. "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
  512. "vec3 hemiColor = mix( lightColorGround, lightColorSky, hemiDiffuseWeight );",
  513. "vec3 diffuse = hemiColor;",
  514. // specular (sky light)
  515. "vec3 hemiHalfVectorSky = normalize( lightVector - vertexPositionVS.xyz );",
  516. "float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;",
  517. "float hemiSpecularWeightSky = max( pow( hemiDotNormalHalfSky, shininess ), 0.0 );",
  518. // specular (ground light)
  519. "vec3 lVectorGround = -lightVector;",
  520. "vec3 hemiHalfVectorGround = normalize( lVectorGround - vertexPositionVS.xyz );",
  521. "float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;",
  522. "float hemiSpecularWeightGround = max( pow( hemiDotNormalHalfGround, shininess ), 0.0 );",
  523. "float dotProductGround = dot( normal, lVectorGround );",
  524. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  525. "vec3 schlickSky = specularColor + vec3( 1.0 - specularColor ) * pow( 1.0 - dot( lightVector, hemiHalfVectorSky ), 5.0 );",
  526. "vec3 schlickGround = specularColor + vec3( 1.0 - specularColor ) * pow( 1.0 - dot( lVectorGround, hemiHalfVectorGround ), 5.0 );",
  527. "vec3 specular = hemiColor * specularNormalization * ( schlickSky * hemiSpecularWeightSky * max( dotProduct, 0.0 ) + schlickGround * hemiSpecularWeightGround * max( dotProductGround, 0.0 ) );",
  528. // combine
  529. "gl_FragColor = vec4( lightIntensity * ( albedo * diffuse + specular ), 1.0 );",
  530. "}"
  531. ].join("\n"),
  532. vertexShader : [
  533. "void main() { ",
  534. // full screen quad proxy
  535. "gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  536. "}"
  537. ].join("\n")
  538. },
  539. "areaLight" : {
  540. uniforms: {
  541. samplerNormalDepth: { type: "t", value: null },
  542. samplerColor: { type: "t", value: null },
  543. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  544. viewWidth: { type: "f", value: 800 },
  545. viewHeight: { type: "f", value: 600 },
  546. lightPositionVS: { type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
  547. lightNormalVS: { type: "v3", value: new THREE.Vector3( 0, -1, 0 ) },
  548. lightRightVS: { type: "v3", value: new THREE.Vector3( 1, 0, 0 ) },
  549. lightUpVS: { type: "v3", value: new THREE.Vector3( 1, 0, 0 ) },
  550. lightColor: { type: "c", value: new THREE.Color( 0x000000 ) },
  551. lightIntensity: { type: "f", value: 1.0 },
  552. lightWidth: { type: "f", value: 1.0 },
  553. lightHeight: { type: "f", value: 1.0 },
  554. constantAttenuation: { type: "f", value: 1.5 },
  555. linearAttenuation: { type: "f", value: 0.5 },
  556. quadraticAttenuation: { type: "f", value: 0.1 }
  557. },
  558. fragmentShader : [
  559. "uniform vec3 lightPositionVS;",
  560. "uniform vec3 lightNormalVS;",
  561. "uniform vec3 lightRightVS;",
  562. "uniform vec3 lightUpVS;",
  563. "uniform sampler2D samplerColor;",
  564. "uniform sampler2D samplerNormalDepth;",
  565. "uniform float lightWidth;",
  566. "uniform float lightHeight;",
  567. "uniform float constantAttenuation;",
  568. "uniform float linearAttenuation;",
  569. "uniform float quadraticAttenuation;",
  570. "uniform float lightIntensity;",
  571. "uniform vec3 lightColor;",
  572. "uniform float viewHeight;",
  573. "uniform float viewWidth;",
  574. "uniform mat4 matProjInverse;",
  575. THREE.DeferredShaderChunk[ "unpackFloat" ],
  576. "vec3 projectOnPlane( vec3 point, vec3 planeCenter, vec3 planeNorm ) {",
  577. "return point - dot( point - planeCenter, planeNorm ) * planeNorm;",
  578. "}",
  579. "bool sideOfPlane( vec3 point, vec3 planeCenter, vec3 planeNorm ) {",
  580. "return ( dot( point - planeCenter, planeNorm ) >= 0.0 );",
  581. "}",
  582. "vec3 linePlaneIntersect( vec3 lp, vec3 lv, vec3 pc, vec3 pn ) {",
  583. "return lp + lv * ( dot( pn, pc - lp ) / dot( pn, lv ) );",
  584. "}",
  585. "float calculateAttenuation( float dist ) {",
  586. "return ( 1.0 / ( constantAttenuation + linearAttenuation * dist + quadraticAttenuation * dist * dist ) );",
  587. "}",
  588. "void main() {",
  589. THREE.DeferredShaderChunk[ "computeVertexPositionVS" ],
  590. THREE.DeferredShaderChunk[ "computeNormal" ],
  591. THREE.DeferredShaderChunk[ "unpackColorMap" ],
  592. "float w = lightWidth;",
  593. "float h = lightHeight;",
  594. "vec3 proj = projectOnPlane( vertexPositionVS.xyz, lightPositionVS, lightNormalVS );",
  595. "vec3 dir = proj - lightPositionVS;",
  596. "vec2 diagonal = vec2( dot( dir, lightRightVS ), dot( dir, lightUpVS ) );",
  597. "vec2 nearest2D = vec2( clamp( diagonal.x, -w, w ), clamp( diagonal.y, -h, h ) );",
  598. "vec3 nearestPointInside = vec3( lightPositionVS ) + ( lightRightVS * nearest2D.x + lightUpVS * nearest2D.y );",
  599. "vec3 lightDir = normalize( nearestPointInside - vertexPositionVS.xyz );",
  600. "float NdotL = max( dot( lightNormalVS, -lightDir ), 0.0 );",
  601. "float NdotL2 = max( dot( normal, lightDir ), 0.0 );",
  602. //"if ( NdotL2 * NdotL > 0.0 && sideOfPlane( vertexPositionVS.xyz, lightPositionVS, lightNormalVS ) ) {",
  603. "if ( NdotL2 * NdotL > 0.0 ) {",
  604. // diffuse
  605. "vec3 diffuse = vec3( sqrt( NdotL * NdotL2 ) );",
  606. // specular
  607. "vec3 specular = vec3( 0.0 );",
  608. "vec3 R = reflect( normalize( -vertexPositionVS.xyz ), normal );",
  609. "vec3 E = linePlaneIntersect( vertexPositionVS.xyz, R, vec3( lightPositionVS ), lightNormalVS );",
  610. "float specAngle = dot( R, lightNormalVS );",
  611. "if ( specAngle > 0.0 ) {",
  612. "vec3 dirSpec = E - vec3( lightPositionVS );",
  613. "vec2 dirSpec2D = vec2( dot( dirSpec, lightRightVS ), dot( dirSpec, lightUpVS ) );",
  614. "vec2 nearestSpec2D = vec2( clamp( dirSpec2D.x, -w, w ), clamp( dirSpec2D.y, -h, h ) );",
  615. "float specFactor = 1.0 - clamp( length( nearestSpec2D - dirSpec2D ) * 0.05 * shininess, 0.0, 1.0 );",
  616. "specular = specularColor * specFactor * specAngle * diffuse;",
  617. "}",
  618. // combine
  619. "float dist = distance( vertexPositionVS.xyz, nearestPointInside );",
  620. "float attenuation = calculateAttenuation( dist );",
  621. THREE.DeferredShaderChunk[ "combine" ],
  622. "} else {",
  623. "discard;",
  624. "}",
  625. "}"
  626. ].join("\n"),
  627. vertexShader : [
  628. "void main() {",
  629. // full screen quad proxy
  630. "gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  631. "}"
  632. ].join("\n")
  633. },
  634. "emissiveLight" : {
  635. uniforms: {
  636. samplerColor: { type: "t", value: null },
  637. viewWidth: { type: "f", value: 800 },
  638. viewHeight: { type: "f", value: 600 },
  639. },
  640. fragmentShader : [
  641. "uniform sampler2D samplerColor;",
  642. "uniform float viewHeight;",
  643. "uniform float viewWidth;",
  644. THREE.DeferredShaderChunk[ "unpackFloat" ],
  645. "void main() {",
  646. "vec2 texCoord = gl_FragCoord.xy / vec2( viewWidth, viewHeight );",
  647. "vec4 colorMap = texture2D( samplerColor, texCoord );",
  648. "vec3 emissiveColor = float_to_vec3( abs( colorMap.w ) );",
  649. "gl_FragColor = vec4( emissiveColor, 1.0 );",
  650. "}"
  651. ].join("\n"),
  652. vertexShader : [
  653. "void main() { ",
  654. // full screen quad proxy
  655. "gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  656. "}"
  657. ].join("\n")
  658. }
  659. };