ShaderDeferred.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. THREE.ShaderChunk[ "bumpmap_pars_fragment" ],
  63. "void main() {",
  64. "vec3 normal = normalize( normalView );",
  65. "normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );",
  66. "gl_FragColor = vec4( vec3( normal * 0.5 + 0.5 ), 1.0 );",
  67. "}"
  68. ].join("\n"),
  69. vertexShader : [
  70. "varying vec3 normalView;",
  71. "varying vec2 vUv;",
  72. "varying vec3 vViewPosition;",
  73. "uniform vec4 offsetRepeat;",
  74. "void main() {",
  75. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  76. "gl_Position = projectionMatrix * mvPosition;",
  77. "normalView = normalize( normalMatrix * normal );",
  78. "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
  79. "vViewPosition = -mvPosition.xyz;",
  80. "}"
  81. ].join("\n")
  82. },
  83. "unlit" : {
  84. uniforms: {
  85. samplerDepth: { type: "t", value: null },
  86. viewWidth: { type: "f", value: 800 },
  87. viewHeight: { type: "f", value: 600 },
  88. lightColor: { type: "v3", value: new THREE.Vector3( 0, 0, 0 ) }
  89. },
  90. fragmentShader : [
  91. "varying vec4 clipPos;",
  92. "uniform sampler2D samplerDepth;",
  93. "uniform float viewHeight;",
  94. "uniform float viewWidth;",
  95. "uniform vec3 lightColor;",
  96. "void main() {",
  97. "vec2 texCoord = gl_FragCoord.xy / vec2( viewWidth, viewHeight );",
  98. "float z = texture2D( samplerDepth, texCoord ).x;",
  99. "vec4 color = vec4( lightColor, 1.0 );",
  100. "float depth = clipPos.z / clipPos.w;",
  101. "if( depth > z && z > 0.0 ) color.w = 0.0;",
  102. "gl_FragColor = color;",
  103. "}"
  104. ].join("\n"),
  105. vertexShader : [
  106. "varying vec4 clipPos;",
  107. "void main() {",
  108. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  109. "clipPos = gl_Position;",
  110. "}"
  111. ].join("\n")
  112. },
  113. "composite" : {
  114. uniforms: {
  115. samplerLightBuffer: { type: "t", value: null },
  116. samplerEmitter: { type: "t", value: null }
  117. },
  118. fragmentShader : [
  119. "varying vec2 texCoord;",
  120. "uniform sampler2D samplerLightBuffer;",
  121. "uniform sampler2D samplerEmitter;",
  122. "uniform vec3 lightPos;",
  123. "void main() {",
  124. "vec3 color = texture2D( samplerLightBuffer, texCoord ).xyz;",
  125. "vec3 emitter = texture2D( samplerEmitter, texCoord ).xyz;",
  126. "if ( emitter != vec3( 0.0 ) ) {",
  127. "gl_FragColor = vec4( emitter, 1.0 );",
  128. "} else {",
  129. "gl_FragColor = vec4( sqrt( color ), 1.0 );",
  130. "}",
  131. "}"
  132. ].join("\n"),
  133. vertexShader : [
  134. "varying vec2 texCoord;",
  135. "void main() {",
  136. "vec4 pos = vec4( sign( position.xy ), 0.0, 1.0 );",
  137. "texCoord = pos.xy * vec2( 0.5 ) + 0.5;",
  138. "gl_Position = pos;",
  139. "}"
  140. ].join("\n")
  141. },
  142. "light" : {
  143. uniforms: {
  144. samplerLightBuffer: { type: "t", value: null },
  145. samplerNormals: { type: "t", value: null },
  146. samplerDepth: { type: "t", value: null },
  147. samplerColor: { type: "t", value: null },
  148. matView: { type: "m4", value: new THREE.Matrix4() },
  149. matProjInverse: { type: "m4", value: new THREE.Matrix4() },
  150. viewWidth: { type: "f", value: 800 },
  151. viewHeight: { type: "f", value: 600 },
  152. lightPos: { type: "v3", value: new THREE.Vector3( 0, 0, 0 ) },
  153. lightColor: { type: "v3", value: new THREE.Vector3( 0, 0, 0 ) },
  154. lightIntensity: { type: "f", value: 1.0 },
  155. lightRadius: { type: "f", value: 1.0 }
  156. },
  157. fragmentShader : [
  158. "varying vec3 lightView;",
  159. "varying vec4 clipPos;",
  160. "uniform sampler2D samplerColor;",
  161. "uniform sampler2D samplerDepth;",
  162. "uniform sampler2D samplerNormals;",
  163. "uniform sampler2D samplerLightBuffer;",
  164. "uniform float lightRadius;",
  165. "uniform float lightIntensity;",
  166. "uniform float viewHeight;",
  167. "uniform float viewWidth;",
  168. "uniform vec3 lightColor;",
  169. "uniform mat4 matProjInverse;",
  170. "void main() {",
  171. "vec2 texCoord = gl_FragCoord.xy / vec2( viewWidth, viewHeight );",
  172. "float z = texture2D( samplerDepth, texCoord ).x;",
  173. "float lightZ = clipPos.z / clipPos.w;",
  174. /*
  175. "if ( z == 0.0 ) {",
  176. "gl_FragColor = vec4( vec3( 0.0 ), 1.0 );",
  177. "return;",
  178. "}",
  179. */
  180. "if ( z == 0.0 || lightZ > z ) discard;",
  181. "float x = texCoord.x * 2.0 - 1.0;",
  182. "float y = texCoord.y * 2.0 - 1.0;",
  183. "vec4 projectedPos = vec4( x, y, z, 1.0 );",
  184. "vec4 viewPos = matProjInverse * projectedPos;",
  185. "viewPos.xyz /= viewPos.w;",
  186. "viewPos.w = 1.0;",
  187. "vec3 lightDir = lightView - viewPos.xyz;",
  188. "float dist = length( lightDir );",
  189. "if ( dist > lightRadius ) discard;",
  190. "lightDir = normalize( lightDir );",
  191. "float cutoff = 0.3;",
  192. "float denom = dist/lightRadius + 1.0;",
  193. "float attenuation = 1.0 / ( denom * denom );",
  194. "attenuation = ( attenuation - cutoff ) / ( 1.0 - cutoff );",
  195. "attenuation = max( attenuation, 0.0 );",
  196. "attenuation *= attenuation;",
  197. "vec3 normal = texture2D( samplerNormals, texCoord ).xyz * 2.0 - 1.0;",
  198. // wrap around lighting
  199. "float diffuseFull = max( dot( normal, lightDir ), 0.0 );",
  200. "float diffuseHalf = max( 0.5 + 0.5 * dot( normal, lightDir ), 0.0 );",
  201. "const vec3 wrapRGB = vec3( 0.6, 0.2, 0.2 );",
  202. "vec3 diffuse = mix( vec3 ( diffuseFull ), vec3( diffuseHalf ), wrapRGB );",
  203. // simple lighting
  204. //"float diffuseFull = max( dot( normal, lightDir ), 0.0 );",
  205. //"vec3 diffuse = vec3 ( diffuseFull );",
  206. // specular
  207. "const float shininess = SHININESS;",
  208. "const float specularIntensity = SPECULAR_INTENSITY;",
  209. "vec3 halfVector = normalize( lightDir - normalize( viewPos.xyz ) );",
  210. "float dotNormalHalf = max( dot( normal, halfVector ), 0.0 );",
  211. // simple specular
  212. //"vec3 specular = specularIntensity * max( pow( dotNormalHalf, shininess ), 0.0 ) * diffuse;",
  213. // physically based specular
  214. "vec3 specularColor = specularIntensity * vec3( 1.0 );",
  215. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  216. "vec3 schlick = specularColor + vec3( 1.0 - specularColor ) * pow( 1.0 - dot( lightDir, halfVector ), 5.0 );",
  217. "vec3 specular = schlick * max( pow( dotNormalHalf, shininess ), 0.0 ) * diffuse * specularNormalization;",
  218. // color
  219. "vec4 albedo = texture2D( samplerColor, texCoord );",
  220. // combine
  221. "vec4 color = vec4( 0.0 );",
  222. "color.xyz = albedo.xyz * lightColor * lightIntensity;",
  223. "color.w = attenuation;",
  224. "#ifdef ADDITIVE_SPECULAR",
  225. "gl_FragColor = color * vec4( diffuse, 1.0 ) + vec4( lightColor * lightIntensity * specular, attenuation );",
  226. "#else",
  227. "gl_FragColor = color * vec4( diffuse + specular, 1.0 );",
  228. "#endif",
  229. "}"
  230. ].join("\n"),
  231. vertexShader : [
  232. "varying vec3 lightView;",
  233. "varying vec4 clipPos;",
  234. "uniform vec3 lightPos;",
  235. "uniform mat4 matView;",
  236. "void main() { ",
  237. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  238. "gl_Position = projectionMatrix * mvPosition;",
  239. "lightView = vec3( matView * vec4( lightPos, 1.0 ) );",
  240. "clipPos = gl_Position;",
  241. "}"
  242. ].join("\n")
  243. }
  244. };