ShaderDeferred.js 26 KB

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