ShaderDeferred.js 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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. "areaLight" : {
  496. uniforms: {
  497. samplerNormalDepth: { type: "t", value: null },
  498. samplerColor: { type: "t", value: null },
  499. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  500. matView: { type: "m4", value: new THREE.Matrix4() },
  501. viewWidth: { type: "f", value: 800 },
  502. viewHeight: { type: "f", value: 600 },
  503. lightPosition: { type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
  504. lightColor: { type: "c", value: new THREE.Color( 0x000000 ) },
  505. lightIntensity: { type: "f", value: 1.0 },
  506. lightRight: { type: "v3", value: new THREE.Vector3( 1, 0, 0 ) },
  507. lightNormal: { type: "v3", value: new THREE.Vector3( 0, -1, 0 ) },
  508. lightWidth: { type: "f", value: 1.0 },
  509. lightHeight: { type: "f", value: 1.0 }
  510. },
  511. fragmentShader : [
  512. "varying vec3 lightPosVS;",
  513. "varying vec3 lightNormalVS;",
  514. "varying vec3 lightRightVS;",
  515. "uniform sampler2D samplerColor;",
  516. "uniform sampler2D samplerNormalDepth;",
  517. "uniform float lightWidth;",
  518. "uniform float lightHeight;",
  519. "uniform float lightIntensity;",
  520. "uniform vec3 lightColor;",
  521. "uniform float viewHeight;",
  522. "uniform float viewWidth;",
  523. "uniform mat4 matProjInverse;",
  524. THREE.DeferredShaderChunk[ "unpackFloat" ],
  525. "vec3 projectOnPlane( vec3 point, vec3 planeCenter, vec3 planeNorm ) {",
  526. "return point - dot( point - planeCenter, planeNorm ) * planeNorm;",
  527. "}",
  528. "bool sideOfPlane( vec3 point, vec3 planeCenter, vec3 planeNorm ) {",
  529. "return ( dot( point - planeCenter, planeNorm ) >= 0.0 );",
  530. "}",
  531. "vec3 linePlaneIntersect( vec3 lp, vec3 lv, vec3 pc, vec3 pn ) {",
  532. "return lp + lv * ( dot( pn, pc - lp ) / dot( pn, lv ) );",
  533. "}",
  534. "float calculateAttenuation( float dist ) {",
  535. "float constAtten = 1.5;",
  536. "float linAtten = 0.5;",
  537. "float quadAtten = 0.1;",
  538. "return ( 1.0 / ( constAtten + linAtten * dist + quadAtten * dist * dist ) );",
  539. "}",
  540. "void main() {",
  541. THREE.DeferredShaderChunk[ "computeVertexPositionVS" ],
  542. THREE.DeferredShaderChunk[ "computeNormal" ],
  543. THREE.DeferredShaderChunk[ "unpackColorMap" ],
  544. "float w = lightWidth;",
  545. "float h = lightHeight;",
  546. "vec3 lightUpVS = normalize( cross( lightRightVS, lightNormalVS ) );",
  547. "vec3 proj = projectOnPlane( vertexPositionVS.xyz, lightPosVS, lightNormalVS );",
  548. "vec3 dir = proj - lightPosVS;",
  549. "vec2 diagonal = vec2( dot( dir, lightRightVS ), dot( dir, lightUpVS ) );",
  550. "vec2 nearest2D = vec2( clamp( diagonal.x, -w, w ), clamp( diagonal.y, -h, h ) );",
  551. "vec3 nearestPointInside = vec3( lightPosVS ) + ( lightRightVS * nearest2D.x + lightUpVS * nearest2D.y );",
  552. "float dist = distance( vertexPositionVS.xyz, nearestPointInside );",
  553. "float attenuation = calculateAttenuation( dist );",
  554. "vec3 lightDir = normalize( nearestPointInside - vertexPositionVS.xyz );",
  555. "vec3 color = vec3( 0.0 );",
  556. "float NdotL = dot( lightNormalVS, -lightDir );",
  557. "if ( NdotL != 0.0 && sideOfPlane( vertexPositionVS.xyz, lightPosVS, lightNormalVS ) ) {",
  558. "color.xyz = vec3( lightColor * attenuation * NdotL * 1.5 );",
  559. "} else {",
  560. "color.xyz = vec3( 0.0 );",
  561. "}",
  562. "gl_FragColor = vec4( color, 1.0 );",
  563. "}"
  564. ].join("\n"),
  565. vertexShader : [
  566. "varying vec3 lightPosVS;",
  567. "varying vec3 lightNormalVS;",
  568. "varying vec3 lightRightVS;",
  569. "uniform vec3 lightPosition;",
  570. "uniform vec3 lightNormal;",
  571. "uniform vec3 lightRight;",
  572. "uniform mat4 matView;",
  573. "void main() {",
  574. "vec4 pos = vec4( sign( position.xy ), 0.0, 1.0 );",
  575. "gl_Position = pos;",
  576. "lightNormalVS = normalize( mat3( matView ) * lightNormal );",
  577. "lightRightVS = normalize( mat3( matView ) * lightRight ); ",
  578. "lightPosVS = vec3( matView * vec4( lightPosition, 1.0 ) );",
  579. "}"
  580. ].join("\n")
  581. },
  582. "emissiveLight" : {
  583. uniforms: {
  584. samplerColor: { type: "t", value: null },
  585. viewWidth: { type: "f", value: 800 },
  586. viewHeight: { type: "f", value: 600 },
  587. },
  588. fragmentShader : [
  589. "uniform sampler2D samplerColor;",
  590. "uniform float viewHeight;",
  591. "uniform float viewWidth;",
  592. THREE.DeferredShaderChunk[ "unpackFloat" ],
  593. "void main() {",
  594. "vec2 texCoord = gl_FragCoord.xy / vec2( viewWidth, viewHeight );",
  595. "vec4 colorMap = texture2D( samplerColor, texCoord );",
  596. "vec3 emissiveColor = float_to_vec3( abs( colorMap.w ) );",
  597. "gl_FragColor = vec4( emissiveColor, 1.0 );",
  598. "}"
  599. ].join("\n"),
  600. vertexShader : [
  601. "void main() { ",
  602. // full screen quad proxy
  603. "gl_Position = vec4( sign( position.xy ), 0.0, 1.0 );",
  604. "}"
  605. ].join("\n")
  606. }
  607. };