ShaderDeferred.js 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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. "void main() {",
  270. "vec3 color = texture2D( samplerLight, texCoord ).xyz;",
  271. "gl_FragColor = vec4( brightness * sqrt( color ), 1.0 );",
  272. "}"
  273. ].join("\n"),
  274. vertexShader : [
  275. "varying vec2 texCoord;",
  276. "void main() {",
  277. "vec4 pos = vec4( sign( position.xy ), 0.0, 1.0 );",
  278. "texCoord = pos.xy * vec2( 0.5 ) + 0.5;",
  279. "gl_Position = pos;",
  280. "}"
  281. ].join("\n")
  282. },
  283. "pointLight" : {
  284. uniforms: {
  285. samplerNormalDepth: { type: "t", value: null },
  286. samplerColor: { type: "t", value: null },
  287. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  288. viewWidth: { type: "f", value: 800 },
  289. viewHeight: { type: "f", value: 600 },
  290. lightPositionVS:{ type: "v3", value: new THREE.Vector3( 0, 0, 0 ) },
  291. lightColor: { type: "c", value: new THREE.Color( 0x000000 ) },
  292. lightIntensity: { type: "f", value: 1.0 },
  293. lightRadius: { type: "f", value: 1.0 }
  294. },
  295. fragmentShader : [
  296. "uniform sampler2D samplerColor;",
  297. "uniform sampler2D samplerNormalDepth;",
  298. "uniform float lightRadius;",
  299. "uniform float lightIntensity;",
  300. "uniform float viewHeight;",
  301. "uniform float viewWidth;",
  302. "uniform vec3 lightColor;",
  303. "uniform vec3 lightPositionVS;",
  304. "uniform mat4 matProjInverse;",
  305. THREE.DeferredShaderChunk[ "unpackFloat" ],
  306. "void main() {",
  307. THREE.DeferredShaderChunk[ "computeVertexPositionVS" ],
  308. // bail out early when pixel outside of light sphere
  309. "vec3 lightVector = lightPositionVS - vertexPositionVS.xyz;",
  310. "float distance = length( lightVector );",
  311. "if ( distance > lightRadius ) discard;",
  312. THREE.DeferredShaderChunk[ "computeNormal" ],
  313. THREE.DeferredShaderChunk[ "unpackColorMap" ],
  314. // compute light
  315. "lightVector = normalize( lightVector );",
  316. THREE.DeferredShaderChunk[ "computeDiffuse" ],
  317. THREE.DeferredShaderChunk[ "computeSpecular" ],
  318. // combine
  319. "float cutoff = 0.3;",
  320. "float denom = distance / lightRadius + 1.0;",
  321. "float attenuation = 1.0 / ( denom * denom );",
  322. "attenuation = ( attenuation - cutoff ) / ( 1.0 - cutoff );",
  323. "attenuation = max( attenuation, 0.0 );",
  324. "attenuation *= attenuation;",
  325. THREE.DeferredShaderChunk[ "combine" ],
  326. "}"
  327. ].join("\n"),
  328. vertexShader : [
  329. "void main() { ",
  330. // sphere proxy needs real position
  331. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  332. "gl_Position = projectionMatrix * mvPosition;",
  333. "}"
  334. ].join("\n")
  335. },
  336. "spotLight" : {
  337. uniforms: {
  338. samplerNormalDepth: { type: "t", value: null },
  339. samplerColor: { type: "t", value: null },
  340. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  341. viewWidth: { type: "f", value: 800 },
  342. viewHeight: { type: "f", value: 600 },
  343. lightPositionVS :{ type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
  344. lightDirectionVS:{ type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
  345. lightColor: { type: "c", value: new THREE.Color( 0x000000 ) },
  346. lightIntensity: { type: "f", value: 1.0 },
  347. lightDistance: { type: "f", value: 1.0 },
  348. lightAngle: { type: "f", value: 1.0 }
  349. },
  350. fragmentShader : [
  351. "uniform vec3 lightPositionVS;",
  352. "uniform vec3 lightDirectionVS;",
  353. "uniform sampler2D samplerColor;",
  354. "uniform sampler2D samplerNormalDepth;",
  355. "uniform float viewHeight;",
  356. "uniform float viewWidth;",
  357. "uniform float lightAngle;",
  358. "uniform float lightIntensity;",
  359. "uniform vec3 lightColor;",
  360. "uniform mat4 matProjInverse;",
  361. THREE.DeferredShaderChunk[ "unpackFloat" ],
  362. "void main() {",
  363. THREE.DeferredShaderChunk[ "computeVertexPositionVS" ],
  364. THREE.DeferredShaderChunk[ "computeNormal" ],
  365. THREE.DeferredShaderChunk[ "unpackColorMap" ],
  366. // compute light
  367. "vec3 lightVector = normalize( lightPositionVS.xyz - vertexPositionVS.xyz );",
  368. "float rho = dot( lightDirectionVS, lightVector );",
  369. "float rhoMax = cos( lightAngle * 0.5 );",
  370. "if ( rho <= rhoMax ) discard;",
  371. "float theta = rhoMax + 0.0001;",
  372. "float phi = rhoMax + 0.05;",
  373. "float falloff = 4.0;",
  374. "float spot = 0.0;",
  375. "if ( rho >= phi ) {",
  376. "spot = 1.0;",
  377. "} else if ( rho <= theta ) {",
  378. "spot = 0.0;",
  379. "} else { ",
  380. "spot = pow( ( rho - theta ) / ( phi - theta ), falloff );",
  381. "}",
  382. THREE.DeferredShaderChunk[ "computeDiffuse" ],
  383. "diffuse *= spot;",
  384. THREE.DeferredShaderChunk[ "computeSpecular" ],
  385. // combine
  386. "const float attenuation = 1.0;",
  387. THREE.DeferredShaderChunk[ "combine" ],
  388. "}"
  389. ].join("\n"),
  390. vertexShader : [
  391. "void main() { ",
  392. // full screen quad proxy
  393. "gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  394. "}"
  395. ].join("\n")
  396. },
  397. "directionalLight" : {
  398. uniforms: {
  399. samplerNormalDepth: { type: "t", value: null },
  400. samplerColor: { type: "t", value: null },
  401. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  402. viewWidth: { type: "f", value: 800 },
  403. viewHeight: { type: "f", value: 600 },
  404. lightDirectionVS: { type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
  405. lightColor: { type: "c", value: new THREE.Color( 0x000000 ) },
  406. lightIntensity: { type: "f", value: 1.0 }
  407. },
  408. fragmentShader : [
  409. "uniform sampler2D samplerColor;",
  410. "uniform sampler2D samplerNormalDepth;",
  411. "uniform float lightRadius;",
  412. "uniform float lightIntensity;",
  413. "uniform float viewHeight;",
  414. "uniform float viewWidth;",
  415. "uniform vec3 lightColor;",
  416. "uniform vec3 lightDirectionVS;",
  417. "uniform mat4 matProjInverse;",
  418. THREE.DeferredShaderChunk[ "unpackFloat" ],
  419. "void main() {",
  420. THREE.DeferredShaderChunk[ "computeVertexPositionVS" ],
  421. THREE.DeferredShaderChunk[ "computeNormal" ],
  422. THREE.DeferredShaderChunk[ "unpackColorMap" ],
  423. // compute light
  424. "vec3 lightVector = lightDirectionVS;",
  425. THREE.DeferredShaderChunk[ "computeDiffuse" ],
  426. THREE.DeferredShaderChunk[ "computeSpecular" ],
  427. // combine
  428. "const float attenuation = 1.0;",
  429. THREE.DeferredShaderChunk[ "combine" ],
  430. "}"
  431. ].join("\n"),
  432. vertexShader : [
  433. "void main() { ",
  434. // full screen quad proxy
  435. "gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  436. "}"
  437. ].join("\n")
  438. },
  439. "hemisphereLight" : {
  440. uniforms: {
  441. samplerNormalDepth: { type: "t", value: null },
  442. samplerColor: { type: "t", value: null },
  443. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  444. viewWidth: { type: "f", value: 800 },
  445. viewHeight: { type: "f", value: 600 },
  446. lightDirectionVS: { type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
  447. lightColorSky: { type: "c", value: new THREE.Color( 0x000000 ) },
  448. lightColorGround: { type: "c", value: new THREE.Color( 0x000000 ) },
  449. lightIntensity: { type: "f", value: 1.0 }
  450. },
  451. fragmentShader : [
  452. "uniform sampler2D samplerColor;",
  453. "uniform sampler2D samplerNormalDepth;",
  454. "uniform float lightRadius;",
  455. "uniform float lightIntensity;",
  456. "uniform float viewHeight;",
  457. "uniform float viewWidth;",
  458. "uniform vec3 lightColorSky;",
  459. "uniform vec3 lightColorGround;",
  460. "uniform vec3 lightDirectionVS;",
  461. "uniform mat4 matProjInverse;",
  462. THREE.DeferredShaderChunk[ "unpackFloat" ],
  463. "void main() {",
  464. THREE.DeferredShaderChunk[ "computeVertexPositionVS" ],
  465. THREE.DeferredShaderChunk[ "computeNormal" ],
  466. THREE.DeferredShaderChunk[ "unpackColorMap" ],
  467. // compute light
  468. "vec3 lightVector = lightDirectionVS;",
  469. // diffuse
  470. "float dotProduct = dot( normal, lightVector );",
  471. "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
  472. "vec3 hemiColor = mix( lightColorGround, lightColorSky, hemiDiffuseWeight );",
  473. "vec3 diffuse = hemiColor;",
  474. // specular (sky light)
  475. "vec3 hemiHalfVectorSky = normalize( lightVector - vertexPositionVS.xyz );",
  476. "float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;",
  477. "float hemiSpecularWeightSky = max( pow( hemiDotNormalHalfSky, shininess ), 0.0 );",
  478. // specular (ground light)
  479. "vec3 lVectorGround = -lightVector;",
  480. "vec3 hemiHalfVectorGround = normalize( lVectorGround - vertexPositionVS.xyz );",
  481. "float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;",
  482. "float hemiSpecularWeightGround = max( pow( hemiDotNormalHalfGround, shininess ), 0.0 );",
  483. "float dotProductGround = dot( normal, lVectorGround );",
  484. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  485. "vec3 schlickSky = specularColor + vec3( 1.0 - specularColor ) * pow( 1.0 - dot( lightVector, hemiHalfVectorSky ), 5.0 );",
  486. "vec3 schlickGround = specularColor + vec3( 1.0 - specularColor ) * pow( 1.0 - dot( lVectorGround, hemiHalfVectorGround ), 5.0 );",
  487. "vec3 specular = hemiColor * specularNormalization * ( schlickSky * hemiSpecularWeightSky * max( dotProduct, 0.0 ) + schlickGround * hemiSpecularWeightGround * max( dotProductGround, 0.0 ) );",
  488. // combine
  489. "gl_FragColor = vec4( lightIntensity * ( albedo * diffuse + specular ), 1.0 );",
  490. "}"
  491. ].join("\n"),
  492. vertexShader : [
  493. "void main() { ",
  494. // full screen quad proxy
  495. "gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  496. "}"
  497. ].join("\n")
  498. },
  499. "areaLight" : {
  500. uniforms: {
  501. samplerNormalDepth: { type: "t", value: null },
  502. samplerColor: { type: "t", value: null },
  503. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  504. viewWidth: { type: "f", value: 800 },
  505. viewHeight: { type: "f", value: 600 },
  506. lightPositionVS: { type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
  507. lightNormalVS: { type: "v3", value: new THREE.Vector3( 0, -1, 0 ) },
  508. lightRightVS: { type: "v3", value: new THREE.Vector3( 1, 0, 0 ) },
  509. lightUpVS: { type: "v3", value: new THREE.Vector3( 1, 0, 0 ) },
  510. lightColor: { type: "c", value: new THREE.Color( 0x000000 ) },
  511. lightIntensity: { type: "f", value: 1.0 },
  512. lightWidth: { type: "f", value: 1.0 },
  513. lightHeight: { type: "f", value: 1.0 },
  514. constantAttenuation: { type: "f", value: 1.5 },
  515. linearAttenuation: { type: "f", value: 0.5 },
  516. quadraticAttenuation: { type: "f", value: 0.1 }
  517. },
  518. fragmentShader : [
  519. "uniform vec3 lightPositionVS;",
  520. "uniform vec3 lightNormalVS;",
  521. "uniform vec3 lightRightVS;",
  522. "uniform vec3 lightUpVS;",
  523. "uniform sampler2D samplerColor;",
  524. "uniform sampler2D samplerNormalDepth;",
  525. "uniform float lightWidth;",
  526. "uniform float lightHeight;",
  527. "uniform float constantAttenuation;",
  528. "uniform float linearAttenuation;",
  529. "uniform float quadraticAttenuation;",
  530. "uniform float lightIntensity;",
  531. "uniform vec3 lightColor;",
  532. "uniform float viewHeight;",
  533. "uniform float viewWidth;",
  534. "uniform mat4 matProjInverse;",
  535. THREE.DeferredShaderChunk[ "unpackFloat" ],
  536. "vec3 projectOnPlane( vec3 point, vec3 planeCenter, vec3 planeNorm ) {",
  537. "return point - dot( point - planeCenter, planeNorm ) * planeNorm;",
  538. "}",
  539. "bool sideOfPlane( vec3 point, vec3 planeCenter, vec3 planeNorm ) {",
  540. "return ( dot( point - planeCenter, planeNorm ) >= 0.0 );",
  541. "}",
  542. "vec3 linePlaneIntersect( vec3 lp, vec3 lv, vec3 pc, vec3 pn ) {",
  543. "return lp + lv * ( dot( pn, pc - lp ) / dot( pn, lv ) );",
  544. "}",
  545. "float calculateAttenuation( float dist ) {",
  546. "return ( 1.0 / ( constantAttenuation + linearAttenuation * dist + quadraticAttenuation * dist * dist ) );",
  547. "}",
  548. "void main() {",
  549. THREE.DeferredShaderChunk[ "computeVertexPositionVS" ],
  550. THREE.DeferredShaderChunk[ "computeNormal" ],
  551. THREE.DeferredShaderChunk[ "unpackColorMap" ],
  552. "float w = lightWidth;",
  553. "float h = lightHeight;",
  554. "vec3 proj = projectOnPlane( vertexPositionVS.xyz, lightPositionVS, lightNormalVS );",
  555. "vec3 dir = proj - lightPositionVS;",
  556. "vec2 diagonal = vec2( dot( dir, lightRightVS ), dot( dir, lightUpVS ) );",
  557. "vec2 nearest2D = vec2( clamp( diagonal.x, -w, w ), clamp( diagonal.y, -h, h ) );",
  558. "vec3 nearestPointInside = vec3( lightPositionVS ) + ( lightRightVS * nearest2D.x + lightUpVS * nearest2D.y );",
  559. "vec3 lightDir = normalize( nearestPointInside - vertexPositionVS.xyz );",
  560. "float NdotL = max( dot( lightNormalVS, -lightDir ), 0.0 );",
  561. "float NdotL2 = max( dot( normal, lightDir ), 0.0 );",
  562. //"if ( NdotL2 * NdotL > 0.0 && sideOfPlane( vertexPositionVS.xyz, lightPositionVS, lightNormalVS ) ) {",
  563. "if ( NdotL2 * NdotL > 0.0 ) {",
  564. // diffuse
  565. "vec3 diffuse = vec3( sqrt( NdotL * NdotL2 ) );",
  566. // specular
  567. "vec3 specular = vec3( 0.0 );",
  568. "vec3 R = reflect( normalize( -vertexPositionVS.xyz ), normal );",
  569. "vec3 E = linePlaneIntersect( vertexPositionVS.xyz, R, vec3( lightPositionVS ), lightNormalVS );",
  570. "float specAngle = dot( R, lightNormalVS );",
  571. "if ( specAngle > 0.0 ) {",
  572. "vec3 dirSpec = E - vec3( lightPositionVS );",
  573. "vec2 dirSpec2D = vec2( dot( dirSpec, lightRightVS ), dot( dirSpec, lightUpVS ) );",
  574. "vec2 nearestSpec2D = vec2( clamp( dirSpec2D.x, -w, w ), clamp( dirSpec2D.y, -h, h ) );",
  575. "float specFactor = 1.0 - clamp( length( nearestSpec2D - dirSpec2D ) * 0.05 * shininess, 0.0, 1.0 );",
  576. "specular = specularColor * specFactor * specAngle * diffuse;",
  577. "}",
  578. // combine
  579. "float dist = distance( vertexPositionVS.xyz, nearestPointInside );",
  580. "float attenuation = calculateAttenuation( dist );",
  581. THREE.DeferredShaderChunk[ "combine" ],
  582. "} else {",
  583. "discard;",
  584. "}",
  585. "}"
  586. ].join("\n"),
  587. vertexShader : [
  588. "void main() {",
  589. // full screen quad proxy
  590. "gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  591. "}"
  592. ].join("\n")
  593. },
  594. "emissiveLight" : {
  595. uniforms: {
  596. samplerColor: { type: "t", value: null },
  597. viewWidth: { type: "f", value: 800 },
  598. viewHeight: { type: "f", value: 600 },
  599. },
  600. fragmentShader : [
  601. "uniform sampler2D samplerColor;",
  602. "uniform float viewHeight;",
  603. "uniform float viewWidth;",
  604. THREE.DeferredShaderChunk[ "unpackFloat" ],
  605. "void main() {",
  606. "vec2 texCoord = gl_FragCoord.xy / vec2( viewWidth, viewHeight );",
  607. "vec4 colorMap = texture2D( samplerColor, texCoord );",
  608. "vec3 emissiveColor = float_to_vec3( abs( colorMap.w ) );",
  609. "gl_FragColor = vec4( emissiveColor, 1.0 );",
  610. "}"
  611. ].join("\n"),
  612. vertexShader : [
  613. "void main() { ",
  614. // full screen quad proxy
  615. "gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  616. "}"
  617. ].join("\n")
  618. }
  619. };