ShaderDeferred.js 26 KB

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