ShaderDeferred.js 26 KB

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