ShaderDeferred.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  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. "gl_FragColor.w = vec3_to_float( compressionScale * emissive * diffuseMapColor );",
  177. "}"
  178. ].join("\n"),
  179. vertexShader : [
  180. THREE.ShaderChunk[ "map_pars_vertex" ],
  181. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  182. THREE.ShaderChunk[ "color_pars_vertex" ],
  183. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  184. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  185. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  186. "#ifdef USE_ENVMAP",
  187. "varying vec3 vWorldPosition;",
  188. "#endif",
  189. "void main() {",
  190. THREE.ShaderChunk[ "map_vertex" ],
  191. THREE.ShaderChunk[ "lightmap_vertex" ],
  192. THREE.ShaderChunk[ "color_vertex" ],
  193. THREE.ShaderChunk[ "skinbase_vertex" ],
  194. THREE.ShaderChunk[ "morphtarget_vertex" ],
  195. THREE.ShaderChunk[ "skinning_vertex" ],
  196. THREE.ShaderChunk[ "default_vertex" ],
  197. THREE.ShaderChunk[ "worldpos_vertex" ],
  198. THREE.ShaderChunk[ "shadowmap_vertex" ],
  199. "#ifdef USE_ENVMAP",
  200. "vWorldPosition = worldPosition.xyz;",
  201. "#endif",
  202. "}"
  203. ].join("\n")
  204. },
  205. "normalDepth" : {
  206. uniforms: {
  207. bumpMap: { type: "t", value: null },
  208. bumpScale: { type: "f", value: 1 },
  209. offsetRepeat: { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) }
  210. },
  211. fragmentShader : [
  212. "#ifdef USE_BUMPMAP",
  213. "#extension GL_OES_standard_derivatives : enable\n",
  214. "varying vec2 vUv;",
  215. "varying vec3 vViewPosition;",
  216. THREE.ShaderChunk[ "bumpmap_pars_fragment" ],
  217. "#endif",
  218. "varying vec3 normalView;",
  219. "varying vec4 clipPos;",
  220. "void main() {",
  221. "vec3 normal = normalize( normalView );",
  222. "#ifdef USE_BUMPMAP",
  223. "normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );",
  224. "#endif",
  225. "gl_FragColor.xyz = normal * 0.5 + 0.5;",
  226. "gl_FragColor.w = clipPos.z / clipPos.w;",
  227. "}"
  228. ].join("\n"),
  229. vertexShader : [
  230. "varying vec3 normalView;",
  231. "varying vec4 clipPos;",
  232. "#ifdef USE_BUMPMAP",
  233. "varying vec2 vUv;",
  234. "varying vec3 vViewPosition;",
  235. "uniform vec4 offsetRepeat;",
  236. "#endif",
  237. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  238. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  239. "void main() {",
  240. THREE.ShaderChunk[ "morphnormal_vertex" ],
  241. THREE.ShaderChunk[ "skinbase_vertex" ],
  242. THREE.ShaderChunk[ "skinnormal_vertex" ],
  243. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  244. THREE.ShaderChunk[ "morphtarget_vertex" ],
  245. THREE.ShaderChunk[ "skinning_vertex" ],
  246. THREE.ShaderChunk[ "default_vertex" ],
  247. "normalView = normalize( normalMatrix * objectNormal );",
  248. "#ifdef USE_BUMPMAP",
  249. "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
  250. "vViewPosition = -mvPosition.xyz;",
  251. "#endif",
  252. "clipPos = gl_Position;",
  253. "}"
  254. ].join("\n")
  255. },
  256. "composite" : {
  257. uniforms: {
  258. samplerLight: { type: "t", value: null },
  259. brightness: { type: "f", value: 1 }
  260. },
  261. fragmentShader : [
  262. "varying vec2 texCoord;",
  263. "uniform sampler2D samplerLight;",
  264. "uniform float brightness;",
  265. "void main() {",
  266. "vec3 color = texture2D( samplerLight, texCoord ).xyz;",
  267. "gl_FragColor = vec4( brightness * sqrt( color ), 1.0 );",
  268. "}"
  269. ].join("\n"),
  270. vertexShader : [
  271. "varying vec2 texCoord;",
  272. "void main() {",
  273. "vec4 pos = vec4( sign( position.xy ), 0.0, 1.0 );",
  274. "texCoord = pos.xy * vec2( 0.5 ) + 0.5;",
  275. "gl_Position = pos;",
  276. "}"
  277. ].join("\n")
  278. },
  279. "pointLight" : {
  280. uniforms: {
  281. samplerNormalDepth: { type: "t", value: null },
  282. samplerColor: { type: "t", value: null },
  283. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  284. viewWidth: { type: "f", value: 800 },
  285. viewHeight: { type: "f", value: 600 },
  286. lightPositionVS:{ type: "v3", value: new THREE.Vector3( 0, 0, 0 ) },
  287. lightColor: { type: "c", value: new THREE.Color( 0x000000 ) },
  288. lightIntensity: { type: "f", value: 1.0 },
  289. lightRadius: { type: "f", value: 1.0 }
  290. },
  291. fragmentShader : [
  292. "uniform sampler2D samplerColor;",
  293. "uniform sampler2D samplerNormalDepth;",
  294. "uniform float lightRadius;",
  295. "uniform float lightIntensity;",
  296. "uniform float viewHeight;",
  297. "uniform float viewWidth;",
  298. "uniform vec3 lightColor;",
  299. "uniform vec3 lightPositionVS;",
  300. "uniform mat4 matProjInverse;",
  301. THREE.DeferredShaderChunk[ "unpackFloat" ],
  302. "void main() {",
  303. THREE.DeferredShaderChunk[ "computeVertexPositionVS" ],
  304. // bail out early when pixel outside of light sphere
  305. "vec3 lightVector = lightPositionVS - vertexPositionVS.xyz;",
  306. "float distance = length( lightVector );",
  307. "if ( distance > lightRadius ) discard;",
  308. THREE.DeferredShaderChunk[ "computeNormal" ],
  309. THREE.DeferredShaderChunk[ "unpackColorMap" ],
  310. // compute light
  311. "lightVector = normalize( lightVector );",
  312. THREE.DeferredShaderChunk[ "computeDiffuse" ],
  313. THREE.DeferredShaderChunk[ "computeSpecular" ],
  314. // combine
  315. "float cutoff = 0.3;",
  316. "float denom = distance / lightRadius + 1.0;",
  317. "float attenuation = 1.0 / ( denom * denom );",
  318. "attenuation = ( attenuation - cutoff ) / ( 1.0 - cutoff );",
  319. "attenuation = max( attenuation, 0.0 );",
  320. "attenuation *= attenuation;",
  321. THREE.DeferredShaderChunk[ "combine" ],
  322. "}"
  323. ].join("\n"),
  324. vertexShader : [
  325. "void main() { ",
  326. // sphere proxy needs real position
  327. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  328. "gl_Position = projectionMatrix * mvPosition;",
  329. "}"
  330. ].join("\n")
  331. },
  332. "spotLight" : {
  333. uniforms: {
  334. samplerNormalDepth: { type: "t", value: null },
  335. samplerColor: { type: "t", value: null },
  336. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  337. viewWidth: { type: "f", value: 800 },
  338. viewHeight: { type: "f", value: 600 },
  339. lightPositionVS :{ type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
  340. lightDirectionVS:{ type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
  341. lightColor: { type: "c", value: new THREE.Color( 0x000000 ) },
  342. lightIntensity: { type: "f", value: 1.0 },
  343. lightDistance: { type: "f", value: 1.0 },
  344. lightAngle: { type: "f", value: 1.0 }
  345. },
  346. fragmentShader : [
  347. "uniform vec3 lightPositionVS;",
  348. "uniform vec3 lightDirectionVS;",
  349. "uniform sampler2D samplerColor;",
  350. "uniform sampler2D samplerNormalDepth;",
  351. "uniform float viewHeight;",
  352. "uniform float viewWidth;",
  353. "uniform float lightAngle;"+
  354. "uniform float lightIntensity;"+
  355. "uniform vec3 lightColor;",
  356. "uniform mat4 matProjInverse;",
  357. THREE.DeferredShaderChunk[ "unpackFloat" ],
  358. "void main() {",
  359. THREE.DeferredShaderChunk[ "computeVertexPositionVS" ],
  360. THREE.DeferredShaderChunk[ "computeNormal" ],
  361. THREE.DeferredShaderChunk[ "unpackColorMap" ],
  362. // compute light
  363. "vec3 lightVector = normalize( lightPositionVS.xyz - vertexPositionVS.xyz );",
  364. "float rho = dot( lightDirectionVS, lightVector );",
  365. "float rhoMax = cos( lightAngle * 0.5 );",
  366. "if ( rho <= rhoMax ) discard;",
  367. "float theta = rhoMax + 0.0001;",
  368. "float phi = rhoMax + 0.05;",
  369. "float falloff = 4.0;",
  370. "float spot = 0.0;",
  371. "if ( rho >= phi ) {",
  372. "spot = 1.0;",
  373. "} else if ( rho <= theta ) {",
  374. "spot = 0.0;",
  375. "} else { ",
  376. "spot = pow( ( rho - theta ) / ( phi - theta ), falloff );",
  377. "}",
  378. THREE.DeferredShaderChunk[ "computeDiffuse" ],
  379. "diffuse *= spot;",
  380. THREE.DeferredShaderChunk[ "computeSpecular" ],
  381. // combine
  382. "const float attenuation = 1.0;",
  383. THREE.DeferredShaderChunk[ "combine" ],
  384. "}"
  385. ].join("\n"),
  386. vertexShader : [
  387. "void main() { ",
  388. // full screen quad proxy
  389. "gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  390. "}"
  391. ].join("\n")
  392. },
  393. "directionalLight" : {
  394. uniforms: {
  395. samplerNormalDepth: { type: "t", value: null },
  396. samplerColor: { type: "t", value: null },
  397. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  398. viewWidth: { type: "f", value: 800 },
  399. viewHeight: { type: "f", value: 600 },
  400. lightDirectionVS: { type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
  401. lightColor: { type: "c", value: new THREE.Color( 0x000000 ) },
  402. lightIntensity: { type: "f", value: 1.0 }
  403. },
  404. fragmentShader : [
  405. "uniform sampler2D samplerColor;",
  406. "uniform sampler2D samplerNormalDepth;",
  407. "uniform float lightRadius;",
  408. "uniform float lightIntensity;",
  409. "uniform float viewHeight;",
  410. "uniform float viewWidth;",
  411. "uniform vec3 lightColor;",
  412. "uniform vec3 lightDirectionVS;",
  413. "uniform mat4 matProjInverse;",
  414. THREE.DeferredShaderChunk[ "unpackFloat" ],
  415. "void main() {",
  416. THREE.DeferredShaderChunk[ "computeVertexPositionVS" ],
  417. THREE.DeferredShaderChunk[ "computeNormal" ],
  418. THREE.DeferredShaderChunk[ "unpackColorMap" ],
  419. // compute light
  420. "vec3 lightVector = lightDirectionVS;",
  421. THREE.DeferredShaderChunk[ "computeDiffuse" ],
  422. THREE.DeferredShaderChunk[ "computeSpecular" ],
  423. // combine
  424. "const float attenuation = 1.0;",
  425. THREE.DeferredShaderChunk[ "combine" ],
  426. "}"
  427. ].join("\n"),
  428. vertexShader : [
  429. "void main() { ",
  430. // full screen quad proxy
  431. "gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  432. "}"
  433. ].join("\n")
  434. },
  435. "hemisphereLight" : {
  436. uniforms: {
  437. samplerNormalDepth: { type: "t", value: null },
  438. samplerColor: { type: "t", value: null },
  439. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  440. viewWidth: { type: "f", value: 800 },
  441. viewHeight: { type: "f", value: 600 },
  442. lightDirectionVS: { type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
  443. lightColorSky: { type: "c", value: new THREE.Color( 0x000000 ) },
  444. lightColorGround: { type: "c", value: new THREE.Color( 0x000000 ) },
  445. lightIntensity: { type: "f", value: 1.0 }
  446. },
  447. fragmentShader : [
  448. "uniform sampler2D samplerColor;",
  449. "uniform sampler2D samplerNormalDepth;",
  450. "uniform float lightRadius;",
  451. "uniform float lightIntensity;",
  452. "uniform float viewHeight;",
  453. "uniform float viewWidth;",
  454. "uniform vec3 lightColorSky;",
  455. "uniform vec3 lightColorGround;",
  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. // diffuse
  466. "float dotProduct = dot( normal, lightVector );",
  467. "float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
  468. "vec3 hemiColor = mix( lightColorGround, lightColorSky, hemiDiffuseWeight );",
  469. "vec3 diffuse = hemiColor;",
  470. // specular (sky light)
  471. "vec3 hemiHalfVectorSky = normalize( lightVector - vertexPositionVS.xyz );",
  472. "float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;",
  473. "float hemiSpecularWeightSky = max( pow( hemiDotNormalHalfSky, shininess ), 0.0 );",
  474. // specular (ground light)
  475. "vec3 lVectorGround = -lightVector;",
  476. "vec3 hemiHalfVectorGround = normalize( lVectorGround - vertexPositionVS.xyz );",
  477. "float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;",
  478. "float hemiSpecularWeightGround = max( pow( hemiDotNormalHalfGround, shininess ), 0.0 );",
  479. "float dotProductGround = dot( normal, lVectorGround );",
  480. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  481. "vec3 schlickSky = specularColor + vec3( 1.0 - specularColor ) * pow( 1.0 - dot( lightVector, hemiHalfVectorSky ), 5.0 );",
  482. "vec3 schlickGround = specularColor + vec3( 1.0 - specularColor ) * pow( 1.0 - dot( lVectorGround, hemiHalfVectorGround ), 5.0 );",
  483. "vec3 specular = hemiColor * specularNormalization * ( schlickSky * hemiSpecularWeightSky * max( dotProduct, 0.0 ) + schlickGround * hemiSpecularWeightGround * max( dotProductGround, 0.0 ) );",
  484. // combine
  485. "gl_FragColor = vec4( lightIntensity * ( albedo * diffuse + specular ), 1.0 );",
  486. "}"
  487. ].join("\n"),
  488. vertexShader : [
  489. "void main() { ",
  490. // full screen quad proxy
  491. "gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  492. "}"
  493. ].join("\n")
  494. },
  495. "emissiveLight" : {
  496. uniforms: {
  497. samplerColor: { type: "t", value: null },
  498. viewWidth: { type: "f", value: 800 },
  499. viewHeight: { type: "f", value: 600 },
  500. },
  501. fragmentShader : [
  502. "uniform sampler2D samplerColor;",
  503. "uniform float viewHeight;",
  504. "uniform float viewWidth;",
  505. THREE.DeferredShaderChunk[ "unpackFloat" ],
  506. "void main() {",
  507. "vec2 texCoord = gl_FragCoord.xy / vec2( viewWidth, viewHeight );",
  508. "vec4 colorMap = texture2D( samplerColor, texCoord );",
  509. "vec3 emissiveColor = float_to_vec3( abs( colorMap.w ) );",
  510. "gl_FragColor = vec4( emissiveColor, 1.0 );",
  511. "}"
  512. ].join("\n"),
  513. vertexShader : [
  514. "void main() { ",
  515. // full screen quad proxy
  516. "gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  517. "}"
  518. ].join("\n")
  519. }
  520. };