ShaderDeferred.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @author MPanknin / http://www.redplant.de/
  4. *
  5. */
  6. THREE.ShaderDeferred = {
  7. "clipDepth" : {
  8. uniforms: { },
  9. fragmentShader : [
  10. "varying vec4 clipPos;",
  11. "void main() {",
  12. "gl_FragColor = vec4( clipPos.z / clipPos.w, 1.0, 1.0, 1.0 );",
  13. "}"
  14. ].join("\n"),
  15. vertexShader : [
  16. "varying vec4 clipPos;",
  17. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  18. "void main() {",
  19. THREE.ShaderChunk[ "morphtarget_vertex" ],
  20. THREE.ShaderChunk[ "default_vertex" ],
  21. "clipPos = gl_Position;",
  22. "}"
  23. ].join("\n")
  24. },
  25. "normals" : {
  26. uniforms: { },
  27. fragmentShader : [
  28. "varying vec3 normalView;",
  29. "void main() {",
  30. "gl_FragColor = vec4( vec3( normalView * 0.5 + 0.5 ), 1.0 );",
  31. "}"
  32. ].join("\n"),
  33. vertexShader : [
  34. "varying vec3 normalView;",
  35. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  36. "void main() {",
  37. THREE.ShaderChunk[ "morphnormal_vertex" ],
  38. THREE.ShaderChunk[ "morphtarget_vertex" ],
  39. THREE.ShaderChunk[ "default_vertex" ],
  40. "vec3 objectNormal;",
  41. "#if !defined( USE_SKINNING ) && defined( USE_MORPHNORMALS )",
  42. "objectNormal = morphedNormal;",
  43. "#endif",
  44. "#if !defined( USE_SKINNING ) && ! defined( USE_MORPHNORMALS )",
  45. "objectNormal = normal;",
  46. "#endif",
  47. "normalView = normalize( normalMatrix * objectNormal );",
  48. "}"
  49. ].join("\n")
  50. },
  51. "bump" : {
  52. uniforms: {
  53. bumpMap: { type: "t", value: null },
  54. bumpScale: { type: "f", value: 1 },
  55. offsetRepeat: { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) }
  56. },
  57. fragmentShader : [
  58. "#extension GL_OES_standard_derivatives : enable\n",
  59. "varying vec3 normalView;",
  60. "varying vec2 vUv;",
  61. "varying vec3 vViewPosition;",
  62. "uniform sampler2D bumpMap;",
  63. "uniform float bumpScale;",
  64. // Derivative maps - bump mapping unparametrized surfaces by Morten Mikkelsen
  65. // http://mmikkelsen3d.blogspot.sk/2011/07/derivative-maps.html
  66. // Evaluate the derivative of the height w.r.t. screen-space using forward differencing (listing 2)
  67. "vec2 dHdxy_fwd() {",
  68. "vec2 dSTdx = dFdx( vUv );",
  69. "vec2 dSTdy = dFdy( vUv );",
  70. "float Hll = bumpScale * texture2D( bumpMap, vUv ).x;",
  71. "float dBx = bumpScale * texture2D( bumpMap, vUv + dSTdx ).x - Hll;",
  72. "float dBy = bumpScale * texture2D( bumpMap, vUv + dSTdy ).x - Hll;",
  73. "return vec2( dBx, dBy );",
  74. "}",
  75. "vec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy ) {",
  76. "vec3 vSigmaX = dFdx( surf_pos );",
  77. "vec3 vSigmaY = dFdy( surf_pos );",
  78. "vec3 vN = surf_norm;", // normalized
  79. "vec3 R1 = cross( vSigmaY, vN );",
  80. "vec3 R2 = cross( vN, vSigmaX );",
  81. "float fDet = dot( vSigmaX, R1 );",
  82. "vec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 );",
  83. "return normalize( abs( fDet ) * surf_norm - vGrad );",
  84. "}",
  85. "void main() {",
  86. "vec3 normal = normalize( normalView );",
  87. "normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );",
  88. "gl_FragColor = vec4( vec3( normal * 0.5 + 0.5 ), 1.0 );",
  89. "}"
  90. ].join("\n"),
  91. vertexShader : [
  92. "varying vec3 normalView;",
  93. "varying vec2 vUv;",
  94. "varying vec3 vViewPosition;",
  95. "uniform vec4 offsetRepeat;",
  96. "void main() {",
  97. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  98. "gl_Position = projectionMatrix * mvPosition;",
  99. "normalView = normalize( normalMatrix * normal );",
  100. "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
  101. "vViewPosition = -mvPosition.xyz;",
  102. "}"
  103. ].join("\n")
  104. },
  105. "unlit" : {
  106. uniforms: {
  107. samplerDepth: { type: "t", value: null },
  108. viewWidth: { type: "f", value: 800 },
  109. viewHeight: { type: "f", value: 600 },
  110. lightColor: { type: "v3", value: new THREE.Vector3( 0, 0, 0 ) }
  111. },
  112. fragmentShader : [
  113. "varying vec4 clipPos;",
  114. "uniform sampler2D samplerDepth;",
  115. "uniform float viewHeight;",
  116. "uniform float viewWidth;",
  117. "uniform vec3 lightColor;",
  118. "void main() {",
  119. "vec2 texCoord = gl_FragCoord.xy / vec2( viewWidth, viewHeight );",
  120. "float z = texture2D( samplerDepth, texCoord ).x;",
  121. "vec4 color = vec4( lightColor, 1.0 );",
  122. "float depth = clipPos.z / clipPos.w;",
  123. "if( depth > z && z > 0.0 ) color.w = 0.0;",
  124. "gl_FragColor = color;",
  125. "}"
  126. ].join("\n"),
  127. vertexShader : [
  128. "varying vec4 clipPos;",
  129. "void main() {",
  130. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  131. "clipPos = gl_Position;",
  132. "}"
  133. ].join("\n")
  134. },
  135. "composite" : {
  136. uniforms: {
  137. samplerLightBuffer: { type: "t", value: null },
  138. samplerEmitter: { type: "t", value: null }
  139. },
  140. fragmentShader : [
  141. "varying vec2 texCoord;",
  142. "uniform sampler2D samplerLightBuffer;",
  143. "uniform sampler2D samplerEmitter;",
  144. "uniform vec3 lightPos;",
  145. "void main() {",
  146. "vec3 color = texture2D( samplerLightBuffer, texCoord ).xyz;",
  147. "vec3 emitter = texture2D( samplerEmitter, texCoord ).xyz;",
  148. "if ( emitter != vec3( 0.0 ) ) {",
  149. "gl_FragColor = vec4( emitter, 1.0 );",
  150. "} else {",
  151. "gl_FragColor = vec4( sqrt( color ), 1.0 );",
  152. "}",
  153. "}"
  154. ].join("\n"),
  155. vertexShader : [
  156. "varying vec2 texCoord;",
  157. "void main() {",
  158. "vec4 pos = vec4( sign( position.xy ), 0.0, 1.0 );",
  159. "texCoord = pos.xy * vec2( 0.5 ) + 0.5;",
  160. "gl_Position = pos;",
  161. "}"
  162. ].join("\n")
  163. },
  164. "light" : {
  165. uniforms: {
  166. samplerLightBuffer: { type: "t", value: null },
  167. samplerNormals: { type: "t", value: null },
  168. samplerDepth: { type: "t", value: null },
  169. samplerColor: { type: "t", value: null },
  170. matView: { type: "m4", value: new THREE.Matrix4() },
  171. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  172. viewWidth: { type: "f", value: 800 },
  173. viewHeight: { type: "f", value: 600 },
  174. lightPos: { type: "v3", value: new THREE.Vector3( 0, 0, 0 ) },
  175. lightColor: { type: "v3", value: new THREE.Vector3( 0, 0, 0 ) },
  176. lightIntensity: { type: "f", value: 1.0 },
  177. lightRadius: { type: "f", value: 1.0 }
  178. },
  179. fragmentShader : [
  180. "varying vec3 lightView;",
  181. "varying vec4 clipPos;",
  182. "uniform sampler2D samplerColor;",
  183. "uniform sampler2D samplerDepth;",
  184. "uniform sampler2D samplerNormals;",
  185. "uniform sampler2D samplerLightBuffer;",
  186. "uniform float lightRadius;",
  187. "uniform float lightIntensity;",
  188. "uniform float viewHeight;",
  189. "uniform float viewWidth;",
  190. "uniform vec3 lightColor;",
  191. "uniform mat4 matProjInverse;",
  192. "void main() {",
  193. "vec2 texCoord = gl_FragCoord.xy / vec2( viewWidth, viewHeight );",
  194. "float z = texture2D( samplerDepth, texCoord ).x;",
  195. "float lightZ = clipPos.z / clipPos.w;",
  196. /*
  197. "if ( z == 0.0 ) {",
  198. "gl_FragColor = vec4( vec3( 0.0 ), 1.0 );",
  199. "return;",
  200. "}",
  201. */
  202. "if ( z == 0.0 || lightZ > z ) discard;",
  203. "float x = texCoord.x * 2.0 - 1.0;",
  204. "float y = texCoord.y * 2.0 - 1.0;",
  205. "vec4 projectedPos = vec4( x, y, z, 1.0 );",
  206. "vec4 viewPos = matProjInverse * projectedPos;",
  207. "viewPos.xyz /= viewPos.w;",
  208. "viewPos.w = 1.0;",
  209. "vec3 lightDir = lightView - viewPos.xyz;",
  210. "float dist = length( lightDir );",
  211. "if ( dist > lightRadius ) discard;",
  212. "lightDir = normalize( lightDir );",
  213. "float cutoff = 0.3;",
  214. "float denom = dist/lightRadius + 1.0;",
  215. "float attenuation = 1.0 / ( denom * denom );",
  216. "attenuation = ( attenuation - cutoff ) / ( 1.0 - cutoff );",
  217. "attenuation = max( attenuation, 0.0 );",
  218. "attenuation *= attenuation;",
  219. "vec3 normal = texture2D( samplerNormals, texCoord ).xyz * 2.0 - 1.0;",
  220. // wrap around lighting
  221. "float diffuseFull = max( dot( normal, lightDir ), 0.0 );",
  222. "float diffuseHalf = max( 0.5 + 0.5 * dot( normal, lightDir ), 0.0 );",
  223. "const vec3 wrapRGB = vec3( 0.6, 0.2, 0.2 );",
  224. "vec3 diffuse = mix( vec3 ( diffuseFull ), vec3( diffuseHalf ), wrapRGB );",
  225. // simple lighting
  226. //"float diffuseFull = max( dot( normal, lightDir ), 0.0 );",
  227. //"vec3 diffuse = vec3 ( diffuseFull );",
  228. // specular
  229. "const float shininess = 75.0;",
  230. "const float specularIntensity = 0.4;",
  231. "vec3 halfVector = normalize( lightDir - normalize( viewPos.xyz ) );",
  232. "float dotNormalHalf = max( dot( normal, halfVector ), 0.0 );",
  233. // simple specular
  234. //"vec3 specular = specularIntensity * max( pow( dotNormalHalf, shininess ), 0.0 ) * diffuse;",
  235. // physically based specular
  236. "vec3 specularColor = specularIntensity * vec3( 0.312 );",
  237. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  238. "vec3 schlick = specularColor + vec3( 1.0 - specularColor ) * pow( 1.0 - dot( lightDir, halfVector ), 5.0 );",
  239. "vec3 specular = schlick * max( pow( dotNormalHalf, shininess ), 0.0 ) * diffuse * specularNormalization;",
  240. // color
  241. "vec4 albedo = texture2D( samplerColor, texCoord );",
  242. // combine
  243. "vec4 color = vec4( 0.0 );",
  244. "color.xyz = albedo.xyz * lightColor * lightIntensity;",
  245. "color.w = attenuation;",
  246. //"gl_FragColor = color * vec4( diffuse + specular, 1.0 );",
  247. "gl_FragColor = color * vec4( diffuse, 1.0 ) + vec4( lightColor * lightIntensity * specular, attenuation );",
  248. "}"
  249. ].join("\n"),
  250. vertexShader : [
  251. "varying vec3 lightView;",
  252. "varying vec4 clipPos;",
  253. "uniform vec3 lightPos;",
  254. "uniform mat4 matView;",
  255. "void main() { ",
  256. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  257. "gl_Position = projectionMatrix * mvPosition;",
  258. "lightView = vec3( matView * vec4( lightPos, 1.0 ) );",
  259. "clipPos = gl_Position;",
  260. "}"
  261. ].join("\n")
  262. }
  263. };