ShaderDeferred.js 11 KB

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