ShaderDeferred.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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.ShaderDeferred = {
  8. "color" : {
  9. uniforms: THREE.UniformsUtils.merge( [
  10. THREE.UniformsLib[ "common" ],
  11. THREE.UniformsLib[ "fog" ],
  12. THREE.UniformsLib[ "shadowmap" ],
  13. {
  14. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  15. "specular" : { type: "c", value: new THREE.Color( 0x111111 ) },
  16. "shininess": { type: "f", value: 30 },
  17. "wrapAround": { type: "f", value: 1 }
  18. }
  19. ] ),
  20. fragmentShader : [
  21. "uniform vec3 diffuse;",
  22. "uniform vec3 specular;",
  23. "uniform vec3 emissive;",
  24. "uniform float shininess;",
  25. "uniform float wrapAround;",
  26. THREE.ShaderChunk[ "color_pars_fragment" ],
  27. THREE.ShaderChunk[ "map_pars_fragment" ],
  28. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  29. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  30. THREE.ShaderChunk[ "fog_pars_fragment" ],
  31. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  32. THREE.ShaderChunk[ "specularmap_pars_fragment" ],
  33. "const float unit = 255.0/256.0;",
  34. "float vec3_to_float( vec3 data ) {",
  35. "highp float compressed = fract( data.x * unit ) + floor( data.y * unit * 255.0 ) + floor( data.z * unit * 255.0 ) * 255.0;",
  36. "return compressed;",
  37. "}",
  38. "void main() {",
  39. "const float opacity = 1.0;",
  40. "gl_FragColor = vec4( diffuse, opacity );",
  41. THREE.ShaderChunk[ "map_fragment" ],
  42. THREE.ShaderChunk[ "alphatest_fragment" ],
  43. THREE.ShaderChunk[ "specularmap_fragment" ],
  44. THREE.ShaderChunk[ "lightmap_fragment" ],
  45. THREE.ShaderChunk[ "color_fragment" ],
  46. THREE.ShaderChunk[ "envmap_fragment" ],
  47. THREE.ShaderChunk[ "shadowmap_fragment" ],
  48. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  49. THREE.ShaderChunk[ "fog_fragment" ],
  50. //
  51. "const float compressionScale = 0.999;",
  52. // diffuse color
  53. "gl_FragColor.x = vec3_to_float( compressionScale * gl_FragColor.xyz );",
  54. // specular color
  55. "gl_FragColor.y = vec3_to_float( compressionScale * specular );",
  56. // shininess
  57. "gl_FragColor.z = wrapAround * shininess;",
  58. // emissive color
  59. "#ifdef USE_MAP",
  60. "gl_FragColor.w = vec3_to_float( compressionScale * emissive * texelColor.xyz );",
  61. "#else",
  62. "gl_FragColor.w = vec3_to_float( compressionScale * emissive );",
  63. "#endif",
  64. "}"
  65. ].join("\n"),
  66. vertexShader : [
  67. THREE.ShaderChunk[ "map_pars_vertex" ],
  68. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  69. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  70. THREE.ShaderChunk[ "color_pars_vertex" ],
  71. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  72. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  73. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  74. "void main() {",
  75. THREE.ShaderChunk[ "map_vertex" ],
  76. THREE.ShaderChunk[ "lightmap_vertex" ],
  77. THREE.ShaderChunk[ "color_vertex" ],
  78. "#ifdef USE_ENVMAP",
  79. THREE.ShaderChunk[ "morphnormal_vertex" ],
  80. THREE.ShaderChunk[ "skinbase_vertex" ],
  81. THREE.ShaderChunk[ "skinnormal_vertex" ],
  82. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  83. "#endif",
  84. THREE.ShaderChunk[ "morphtarget_vertex" ],
  85. THREE.ShaderChunk[ "skinning_vertex" ],
  86. THREE.ShaderChunk[ "default_vertex" ],
  87. THREE.ShaderChunk[ "worldpos_vertex" ],
  88. THREE.ShaderChunk[ "envmap_vertex" ],
  89. THREE.ShaderChunk[ "shadowmap_vertex" ],
  90. "}"
  91. ].join("\n")
  92. },
  93. "normalDepth" : {
  94. uniforms: {
  95. bumpMap: { type: "t", value: null },
  96. bumpScale: { type: "f", value: 1 },
  97. offsetRepeat: { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) }
  98. },
  99. fragmentShader : [
  100. "#ifdef USE_BUMPMAP",
  101. "#extension GL_OES_standard_derivatives : enable\n",
  102. "varying vec2 vUv;",
  103. "varying vec3 vViewPosition;",
  104. THREE.ShaderChunk[ "bumpmap_pars_fragment" ],
  105. "#endif",
  106. "varying vec3 normalView;",
  107. "varying vec4 clipPos;",
  108. "void main() {",
  109. "vec3 normal = normalize( normalView );",
  110. "#ifdef USE_BUMPMAP",
  111. "normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );",
  112. "#endif",
  113. "gl_FragColor.xyz = normal * 0.5 + 0.5;",
  114. "gl_FragColor.w = clipPos.z / clipPos.w;",
  115. "}"
  116. ].join("\n"),
  117. vertexShader : [
  118. "varying vec3 normalView;",
  119. "varying vec4 clipPos;",
  120. "#ifdef USE_BUMPMAP",
  121. "varying vec2 vUv;",
  122. "varying vec3 vViewPosition;",
  123. "uniform vec4 offsetRepeat;",
  124. "#endif",
  125. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  126. "void main() {",
  127. THREE.ShaderChunk[ "morphnormal_vertex" ],
  128. THREE.ShaderChunk[ "morphtarget_vertex" ],
  129. THREE.ShaderChunk[ "default_vertex" ],
  130. "vec3 objectNormal;",
  131. "#if !defined( USE_SKINNING ) && defined( USE_MORPHNORMALS )",
  132. "objectNormal = morphedNormal;",
  133. "#endif",
  134. "#if !defined( USE_SKINNING ) && ! defined( USE_MORPHNORMALS )",
  135. "objectNormal = normal;",
  136. "#endif",
  137. "normalView = normalize( normalMatrix * objectNormal );",
  138. "#ifdef USE_BUMPMAP",
  139. "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
  140. "vViewPosition = -mvPosition.xyz;",
  141. "#endif",
  142. "clipPos = gl_Position;",
  143. "}"
  144. ].join("\n")
  145. },
  146. "composite" : {
  147. uniforms: {
  148. samplerLight: { type: "t", value: null },
  149. brightness: { type: "f", value: 1 }
  150. },
  151. fragmentShader : [
  152. "varying vec2 texCoord;",
  153. "uniform sampler2D samplerLight;",
  154. "uniform float brightness;",
  155. "void main() {",
  156. "vec3 color = texture2D( samplerLight, texCoord ).xyz;",
  157. "gl_FragColor = vec4( brightness * sqrt( color ), 1.0 );",
  158. "}"
  159. ].join("\n"),
  160. vertexShader : [
  161. "varying vec2 texCoord;",
  162. "void main() {",
  163. "vec4 pos = vec4( sign( position.xy ), 0.0, 1.0 );",
  164. "texCoord = pos.xy * vec2( 0.5 ) + 0.5;",
  165. "gl_Position = pos;",
  166. "}"
  167. ].join("\n")
  168. },
  169. "pointLight" : {
  170. uniforms: {
  171. samplerNormalDepth: { type: "t", value: null },
  172. samplerColor: { type: "t", value: null },
  173. matView: { type: "m4", value: new THREE.Matrix4() },
  174. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  175. viewWidth: { type: "f", value: 800 },
  176. viewHeight: { type: "f", value: 600 },
  177. lightPos: { type: "v3", value: new THREE.Vector3( 0, 0, 0 ) },
  178. lightColor: { type: "c", value: new THREE.Color( 0x000000 ) },
  179. lightIntensity: { type: "f", value: 1.0 },
  180. lightRadius: { type: "f", value: 1.0 }
  181. },
  182. fragmentShader : [
  183. "varying vec3 lightView;",
  184. "varying vec4 clipPos;",
  185. "uniform sampler2D samplerColor;",
  186. "uniform sampler2D samplerNormalDepth;",
  187. "uniform float lightRadius;",
  188. "uniform float lightIntensity;",
  189. "uniform float viewHeight;",
  190. "uniform float viewWidth;",
  191. "uniform vec3 lightColor;",
  192. "uniform mat4 matProjInverse;",
  193. "vec3 float_to_vec3( float data ) {",
  194. "vec3 uncompressed;",
  195. "uncompressed.x = fract( data );",
  196. "float zInt = floor( data / 255.0 );",
  197. "uncompressed.z = fract( zInt / 255.0 );",
  198. "uncompressed.y = fract( floor( data - ( zInt * 255.0 ) ) / 255.0 );",
  199. "return uncompressed;",
  200. "}",
  201. "void main() {",
  202. "vec2 texCoord = gl_FragCoord.xy / vec2( viewWidth, viewHeight );",
  203. "vec4 normalDepth = texture2D( samplerNormalDepth, texCoord );",
  204. "float z = normalDepth.w;",
  205. "float lightZ = clipPos.z / clipPos.w;",
  206. "if ( z == 0.0 || lightZ > z ) discard;",
  207. "float x = texCoord.x * 2.0 - 1.0;",
  208. "float y = texCoord.y * 2.0 - 1.0;",
  209. "vec4 projectedPos = vec4( x, y, z, 1.0 );",
  210. "vec4 viewPos = matProjInverse * projectedPos;",
  211. "viewPos.xyz /= viewPos.w;",
  212. "viewPos.w = 1.0;",
  213. "vec3 lightDir = lightView - viewPos.xyz;",
  214. "float dist = length( lightDir );",
  215. "if ( dist > lightRadius ) discard;",
  216. "lightDir = normalize( lightDir );",
  217. "float cutoff = 0.3;",
  218. "float denom = dist/lightRadius + 1.0;",
  219. "float attenuation = 1.0 / ( denom * denom );",
  220. "attenuation = ( attenuation - cutoff ) / ( 1.0 - cutoff );",
  221. "attenuation = max( attenuation, 0.0 );",
  222. "attenuation *= attenuation;",
  223. // normal
  224. "vec3 normal = normalDepth.xyz * 2.0 - 1.0;",
  225. // color
  226. "vec4 colorMap = texture2D( samplerColor, texCoord );",
  227. "vec3 albedo = float_to_vec3( abs( colorMap.x ) );",
  228. "vec3 specularColor = float_to_vec3( abs( colorMap.y ) );",
  229. "float shininess = abs( colorMap.z );",
  230. "float wrapAround = sign( colorMap.z );",
  231. // light
  232. "vec3 diffuse;",
  233. "float diffuseFull = max( dot( normal, lightDir ), 0.0 );",
  234. "if ( wrapAround < 0.0 ) {",
  235. // wrap around lighting
  236. "float diffuseHalf = max( 0.5 + 0.5 * dot( normal, lightDir ), 0.0 );",
  237. "const vec3 wrapRGB = vec3( 0.6, 0.2, 0.2 );",
  238. "diffuse = mix( vec3( diffuseFull ), vec3( diffuseHalf ), wrapRGB );",
  239. "} else {",
  240. // simple lighting
  241. "diffuse = vec3( diffuseFull );",
  242. "}",
  243. // specular
  244. "vec3 halfVector = normalize( lightDir - normalize( viewPos.xyz ) );",
  245. "float dotNormalHalf = max( dot( normal, halfVector ), 0.0 );",
  246. // simple specular
  247. //"vec3 specular = specularIntensity * max( pow( dotNormalHalf, shininess ), 0.0 ) * diffuse;",
  248. // physically based specular
  249. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  250. "vec3 schlick = specularColor + vec3( 1.0 - specularColor ) * pow( 1.0 - dot( lightDir, halfVector ), 5.0 );",
  251. "vec3 specular = schlick * max( pow( dotNormalHalf, shininess ), 0.0 ) * diffuse * specularNormalization;",
  252. // combine
  253. "vec3 light = lightIntensity * lightColor;",
  254. "#ifdef ADDITIVE_SPECULAR",
  255. "gl_FragColor = vec4( albedo * light * diffuse, attenuation ) + vec4( light * specular, attenuation );",
  256. "#else",
  257. "gl_FragColor = vec4( albedo * light * ( diffuse + specular ), attenuation );",
  258. "#endif",
  259. "}"
  260. ].join("\n"),
  261. vertexShader : [
  262. "varying vec3 lightView;",
  263. "varying vec4 clipPos;",
  264. "uniform vec3 lightPos;",
  265. "uniform mat4 matView;",
  266. "void main() { ",
  267. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  268. "gl_Position = projectionMatrix * mvPosition;",
  269. "lightView = vec3( matView * vec4( lightPos, 1.0 ) );",
  270. "clipPos = gl_Position;",
  271. "}"
  272. ].join("\n")
  273. },
  274. "directionalLight" : {
  275. uniforms: {
  276. samplerNormalDepth: { type: "t", value: null },
  277. samplerColor: { type: "t", value: null },
  278. matView: { type: "m4", value: new THREE.Matrix4() },
  279. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  280. viewWidth: { type: "f", value: 800 },
  281. viewHeight: { type: "f", value: 600 },
  282. lightDir: { type: "v3", value: new THREE.Vector3( 0, 1, 0 ) },
  283. lightColor: { type: "c", value: new THREE.Color( 0x000000 ) },
  284. lightIntensity: { type: "f", value: 1.0 }
  285. },
  286. fragmentShader : [
  287. "varying vec3 lightView;",
  288. "varying vec4 clipPos;",
  289. "uniform sampler2D samplerColor;",
  290. "uniform sampler2D samplerNormalDepth;",
  291. "uniform float lightRadius;",
  292. "uniform float lightIntensity;",
  293. "uniform float viewHeight;",
  294. "uniform float viewWidth;",
  295. "uniform vec3 lightColor;",
  296. "uniform mat4 matProjInverse;",
  297. "vec3 float_to_vec3( float data ) {",
  298. "vec3 uncompressed;",
  299. "uncompressed.x = fract( data );",
  300. "float zInt = floor( data / 255.0 );",
  301. "uncompressed.z = fract( zInt / 255.0 );",
  302. "uncompressed.y = fract( floor( data - ( zInt * 255.0 ) ) / 255.0 );",
  303. "return uncompressed;",
  304. "}",
  305. "void main() {",
  306. "vec2 texCoord = gl_FragCoord.xy / vec2( viewWidth, viewHeight );",
  307. "vec4 normalDepth = texture2D( samplerNormalDepth, texCoord );",
  308. "float z = normalDepth.w;",
  309. "if ( z == 0.0 ) discard;",
  310. "float x = texCoord.x * 2.0 - 1.0;",
  311. "float y = texCoord.y * 2.0 - 1.0;",
  312. "vec4 projectedPos = vec4( x, y, z, 1.0 );",
  313. "vec4 viewPos = matProjInverse * projectedPos;",
  314. "viewPos.xyz /= viewPos.w;",
  315. "viewPos.w = 1.0;",
  316. "vec3 lightDir = normalize( lightView );",
  317. // normal
  318. "vec3 normal = normalDepth.xyz * 2.0 - 1.0;",
  319. // color
  320. "vec4 colorMap = texture2D( samplerColor, texCoord );",
  321. "vec3 albedo = float_to_vec3( abs( colorMap.x ) );",
  322. "vec3 specularColor = float_to_vec3( abs( colorMap.y ) );",
  323. "float shininess = colorMap.z;",
  324. // wrap around lighting
  325. "float diffuseFull = max( dot( normal, lightDir ), 0.0 );",
  326. "float diffuseHalf = max( 0.5 + 0.5 * dot( normal, lightDir ), 0.0 );",
  327. "const vec3 wrapRGB = vec3( 0.2, 0.2, 0.2 );",
  328. "vec3 diffuse = mix( vec3 ( diffuseFull ), vec3( diffuseHalf ), wrapRGB );",
  329. // simple lighting
  330. //"float diffuseFull = max( dot( normal, lightDir ), 0.0 );",
  331. //"vec3 diffuse = vec3 ( diffuseFull );",
  332. // specular
  333. "vec3 halfVector = normalize( lightDir + normalize( viewPos.xyz ) );",
  334. "float dotNormalHalf = max( dot( normal, halfVector ), 0.0 );",
  335. // simple specular
  336. //"vec3 specular = specularIntensity * max( pow( dotNormalHalf, shininess ), 0.0 ) * diffuse;",
  337. // physically based specular
  338. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  339. "vec3 schlick = specularColor + vec3( 1.0 - specularColor ) * pow( 1.0 - dot( lightDir, halfVector ), 5.0 );",
  340. "vec3 specular = schlick * max( pow( dotNormalHalf, shininess ), 0.0 ) * diffuse * specularNormalization;",
  341. // combine
  342. "vec3 light = lightIntensity * lightColor;",
  343. "#ifdef ADDITIVE_SPECULAR",
  344. "gl_FragColor = vec4( albedo * light * diffuse, 1.0 ) + vec4( light * specular, 1.0 );",
  345. "#else",
  346. "gl_FragColor = vec4( albedo * light * ( diffuse + specular ), 1.0 );",
  347. "#endif",
  348. "}"
  349. ].join("\n"),
  350. vertexShader : [
  351. "varying vec3 lightView;",
  352. "varying vec4 clipPos;",
  353. "uniform vec3 lightDir;",
  354. "uniform mat4 matView;",
  355. "void main() { ",
  356. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  357. "gl_Position = projectionMatrix * mvPosition;",
  358. "lightView = vec3( matView * vec4( lightDir, 0.0 ) );",
  359. "clipPos = gl_Position;",
  360. "}"
  361. ].join("\n")
  362. },
  363. "emissiveLight" : {
  364. uniforms: {
  365. samplerColor: { type: "t", value: null },
  366. viewWidth: { type: "f", value: 800 },
  367. viewHeight: { type: "f", value: 600 },
  368. },
  369. fragmentShader : [
  370. "uniform sampler2D samplerColor;",
  371. "uniform float viewHeight;",
  372. "uniform float viewWidth;",
  373. "vec3 float_to_vec3( float data ) {",
  374. "vec3 uncompressed;",
  375. "uncompressed.x = fract( data );",
  376. "float zInt = floor( data / 255.0 );",
  377. "uncompressed.z = fract( zInt / 255.0 );",
  378. "uncompressed.y = fract( floor( data - ( zInt * 255.0 ) ) / 255.0 );",
  379. "return uncompressed;",
  380. "}",
  381. "void main() {",
  382. "vec2 texCoord = gl_FragCoord.xy / vec2( viewWidth, viewHeight );",
  383. "vec4 colorMap = texture2D( samplerColor, texCoord );",
  384. "vec3 emissiveColor = float_to_vec3( abs( colorMap.w ) );",
  385. "gl_FragColor = vec4( emissiveColor, 1.0 );",
  386. "}"
  387. ].join("\n"),
  388. vertexShader : [
  389. "void main() { ",
  390. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  391. "gl_Position = projectionMatrix * mvPosition;",
  392. "}"
  393. ].join("\n")
  394. }
  395. };