2
0

ShaderDeferred.js 26 KB

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