ShaderGodRays.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**
  2. * @author huwb / http://huwbowles.com/
  3. *
  4. * God-rays (crepuscular rays)
  5. *
  6. * Similar implementation to the one used by Crytek for CryEngine 2 [Sousa2008].
  7. * Blurs a mask generated from the depth map along radial lines emanating from the light
  8. * source. The blur repeatedly applies a blur filter of increasing support but constant
  9. * sample count to produce a blur filter with large support.
  10. *
  11. * My implementation performs 3 passes, similar to the implementation from Sousa. I found
  12. * just 6 samples per pass produced acceptible results. The blur is applied three times,
  13. * with decreasing filter support. The result is equivalent to a single pass with
  14. * 6*6*6 = 216 samples.
  15. *
  16. * References:
  17. *
  18. * Sousa2008 - Crysis Next Gen Effects, GDC2008, http://www.crytek.com/sites/default/files/GDC08_SousaT_CrysisEffects.ppt
  19. */
  20. THREE.ShaderGodRays = {
  21. /**
  22. * The god-ray generation shader.
  23. *
  24. * First pass:
  25. *
  26. * The input is the depth map. I found that the output from the
  27. * THREE.MeshDepthMaterial material was directly suitable without
  28. * requiring any treatment whatsoever.
  29. *
  30. * The depth map is blurred along radial lines towards the "sun". The
  31. * output is written to a temporary render target (I used a 1/4 sized
  32. * target).
  33. *
  34. * Pass two & three:
  35. *
  36. * The results of the previous pass are re-blurred, each time with a
  37. * decreased distance between samples.
  38. */
  39. 'godrays_generate': {
  40. uniforms: {
  41. tInput: {
  42. value: null
  43. },
  44. fStepSize: {
  45. value: 1.0
  46. },
  47. vSunPositionScreenSpace: {
  48. value: new THREE.Vector2( 0.5, 0.5 )
  49. }
  50. },
  51. vertexShader: [
  52. "varying vec2 vUv;",
  53. "void main() {",
  54. "vUv = uv;",
  55. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  56. "}"
  57. ].join( "\n" ),
  58. fragmentShader: [
  59. "#define TAPS_PER_PASS 6.0",
  60. "varying vec2 vUv;",
  61. "uniform sampler2D tInput;",
  62. "uniform vec2 vSunPositionScreenSpace;",
  63. "uniform float fStepSize;", // filter step size
  64. "void main() {",
  65. // delta from current pixel to "sun" position
  66. "vec2 delta = vSunPositionScreenSpace - vUv;",
  67. "float dist = length( delta );",
  68. // Step vector (uv space)
  69. "vec2 stepv = fStepSize * delta / dist;",
  70. // Number of iterations between pixel and sun
  71. "float iters = dist/fStepSize;",
  72. "vec2 uv = vUv.xy;",
  73. "float col = 0.0;",
  74. // This breaks ANGLE in Chrome 22
  75. // - see http://code.google.com/p/chromium/issues/detail?id=153105
  76. /*
  77. // Unrolling didnt do much on my hardware (ATI Mobility Radeon 3450),
  78. // so i've just left the loop
  79. "for ( float i = 0.0; i < TAPS_PER_PASS; i += 1.0 ) {",
  80. // Accumulate samples, making sure we dont walk past the light source.
  81. // The check for uv.y < 1 would not be necessary with "border" UV wrap
  82. // mode, with a black border colour. I don't think this is currently
  83. // exposed by three.js. As a result there might be artifacts when the
  84. // sun is to the left, right or bottom of screen as these cases are
  85. // not specifically handled.
  86. "col += ( i <= iters && uv.y < 1.0 ? texture2D( tInput, uv ).r : 0.0 );",
  87. "uv += stepv;",
  88. "}",
  89. */
  90. // Unrolling loop manually makes it work in ANGLE
  91. "if ( 0.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r;",
  92. "uv += stepv;",
  93. "if ( 1.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r;",
  94. "uv += stepv;",
  95. "if ( 2.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r;",
  96. "uv += stepv;",
  97. "if ( 3.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r;",
  98. "uv += stepv;",
  99. "if ( 4.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r;",
  100. "uv += stepv;",
  101. "if ( 5.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r;",
  102. "uv += stepv;",
  103. // Should technically be dividing by 'iters', but 'TAPS_PER_PASS' smooths out
  104. // objectionable artifacts, in particular near the sun position. The side
  105. // effect is that the result is darker than it should be around the sun, as
  106. // TAPS_PER_PASS is greater than the number of samples actually accumulated.
  107. // When the result is inverted (in the shader 'godrays_combine', this produces
  108. // a slight bright spot at the position of the sun, even when it is occluded.
  109. "gl_FragColor = vec4( col/TAPS_PER_PASS );",
  110. "gl_FragColor.a = 1.0;",
  111. "}"
  112. ].join( "\n" )
  113. },
  114. /**
  115. * Additively applies god rays from texture tGodRays to a background (tColors).
  116. * fGodRayIntensity attenuates the god rays.
  117. */
  118. 'godrays_combine': {
  119. uniforms: {
  120. tColors: {
  121. value: null
  122. },
  123. tGodRays: {
  124. value: null
  125. },
  126. fGodRayIntensity: {
  127. value: 0.69
  128. },
  129. vSunPositionScreenSpace: {
  130. value: new THREE.Vector2( 0.5, 0.5 )
  131. }
  132. },
  133. vertexShader: [
  134. "varying vec2 vUv;",
  135. "void main() {",
  136. "vUv = uv;",
  137. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  138. "}"
  139. ].join( "\n" ),
  140. fragmentShader: [
  141. "varying vec2 vUv;",
  142. "uniform sampler2D tColors;",
  143. "uniform sampler2D tGodRays;",
  144. "uniform vec2 vSunPositionScreenSpace;",
  145. "uniform float fGodRayIntensity;",
  146. "void main() {",
  147. // Since THREE.MeshDepthMaterial renders foreground objects white and background
  148. // objects black, the god-rays will be white streaks. Therefore value is inverted
  149. // before being combined with tColors
  150. "gl_FragColor = texture2D( tColors, vUv ) + fGodRayIntensity * vec4( 1.0 - texture2D( tGodRays, vUv ).r );",
  151. "gl_FragColor.a = 1.0;",
  152. "}"
  153. ].join( "\n" )
  154. },
  155. /**
  156. * A dodgy sun/sky shader. Makes a bright spot at the sun location. Would be
  157. * cheaper/faster/simpler to implement this as a simple sun sprite.
  158. */
  159. 'godrays_fake_sun': {
  160. uniforms: {
  161. vSunPositionScreenSpace: {
  162. value: new THREE.Vector2( 0.5, 0.5 )
  163. },
  164. fAspect: {
  165. value: 1.0
  166. },
  167. sunColor: {
  168. value: new THREE.Color( 0xffee00 )
  169. },
  170. bgColor: {
  171. value: new THREE.Color( 0x000000 )
  172. }
  173. },
  174. vertexShader: [
  175. "varying vec2 vUv;",
  176. "void main() {",
  177. "vUv = uv;",
  178. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  179. "}"
  180. ].join( "\n" ),
  181. fragmentShader: [
  182. "varying vec2 vUv;",
  183. "uniform vec2 vSunPositionScreenSpace;",
  184. "uniform float fAspect;",
  185. "uniform vec3 sunColor;",
  186. "uniform vec3 bgColor;",
  187. "void main() {",
  188. "vec2 diff = vUv - vSunPositionScreenSpace;",
  189. // Correct for aspect ratio
  190. "diff.x *= fAspect;",
  191. "float prop = clamp( length( diff ) / 0.5, 0.0, 1.0 );",
  192. "prop = 0.35 * pow( 1.0 - prop, 3.0 );",
  193. "gl_FragColor.xyz = mix( sunColor, bgColor, 1.0 - prop );",
  194. "gl_FragColor.w = 1.0;",
  195. "}"
  196. ].join( "\n" )
  197. }
  198. };