ShaderDeferred.js 26 KB

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