ShaderGodRays.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. type: "t",
  43. value: 0,
  44. texture: null
  45. },
  46. fStepSize: {
  47. type: "f",
  48. value: 1.0
  49. },
  50. vSunPositionScreenSpace: {
  51. type: "v2",
  52. value: new THREE.Vector2( 0.5, 0.5 )
  53. }
  54. },
  55. vertexShader: [
  56. "varying vec2 vUv;",
  57. "void main() {",
  58. "vUv = vec2( uv.x, 1.0 - uv.y );",
  59. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  60. "}"
  61. ].join("\n"),
  62. fragmentShader: [
  63. "varying vec2 vUv;",
  64. "uniform sampler2D tInput;",
  65. "uniform vec2 vSunPositionScreenSpace;",
  66. "uniform float fStepSize;", // filter step size
  67. "#define TAPS_PER_PASS 6.0",
  68. "void main() {",
  69. // delta from current pixel to "sun" position
  70. "vec2 delta = (vSunPositionScreenSpace - vUv);",
  71. "float dist = length(delta);",
  72. // Step vector (uv space)
  73. "vec2 stepv = fStepSize*delta/dist;",
  74. // Number of iterations between pixel and sun
  75. "float iters = dist/fStepSize;",
  76. "vec2 uv = vUv.xy;",
  77. "float col = 0.0;",
  78. // Unrolling didnt do much on my hardware (ATI Mobility Radeon 3450),
  79. // so i've just left the loop
  80. "for (float i = 0.0; i < TAPS_PER_PASS; i+=1.0 ) {",
  81. // Accumulate samples, making sure we dont walk past the light source.
  82. // The check for uv.y<1 would not be necessary with "border" UV wrap
  83. // mode, with a black border colour. I don't think this is currently
  84. // exposed by three.js. As a result there might be artifacts when the
  85. // sun is to the left, right or bottom of screen as these cases are
  86. // not specifically handled.
  87. "col += (i <= iters && uv.y<1. ? texture2D( tInput, uv ).r : .0) ;",
  88. "uv += stepv;",
  89. "}",
  90. // Should technically be dividing by 'iters', but 'TAPS_PER_PASS' smooths out
  91. // objectionable artifacts, in particular near the sun position. The side
  92. // effect is that the result is darker than it should be around the sun, as
  93. // TAPS_PER_PASS is greater than the number of samples actually accumulated.
  94. // When the result is inverted (in the shader 'godrays_combine', this produces
  95. // a slight bright spot at the position of the sun, even when it is occluded.
  96. "gl_FragColor = vec4( col/TAPS_PER_PASS );",
  97. "gl_FragColor.a = 1.;",
  98. "}"
  99. ].join("\n")
  100. },
  101. /**
  102. * Additively applies god rays from texture tGodRays to a background (tColors).
  103. * fGodRayIntensity attenuates the god rays.
  104. */
  105. 'godrays_combine': {
  106. uniforms: {
  107. tColors: {
  108. type: "t",
  109. value: 0,
  110. texture: null
  111. },
  112. tGodRays: {
  113. type: "t",
  114. value: 1,
  115. texture: null
  116. },
  117. fGodRayIntensity: {
  118. type: "f",
  119. value: 0.69
  120. },
  121. vSunPositionScreenSpace: {
  122. type: "v2",
  123. value: new THREE.Vector2( 0.5, 0.5 )
  124. },
  125. },
  126. vertexShader: [
  127. "varying vec2 vUv;",
  128. "void main() {",
  129. "vUv = vec2( uv.x, 1.0 - uv.y );",
  130. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  131. "}"
  132. ].join("\n"),
  133. fragmentShader: [
  134. "varying vec2 vUv;",
  135. "uniform sampler2D tColors;",
  136. "uniform sampler2D tGodRays;",
  137. "uniform vec2 vSunPositionScreenSpace;",
  138. "uniform float fGodRayIntensity;",
  139. "void main() {",
  140. // Since THREE.MeshDepthMaterial renders foreground objects white and background
  141. // objects black, the god-rays will be white streaks. Therefore value is inverted
  142. // before being combined with tColors
  143. "gl_FragColor = texture2D( tColors, vUv ) + fGodRayIntensity*vec4( 1.-texture2D( tGodRays, vUv ).r );",
  144. "gl_FragColor.a = 1.;",
  145. "}"
  146. ].join("\n")
  147. },
  148. /**
  149. * A dodgy sun/sky shader. Makes a bright spot at the sun location. Would be
  150. * cheaper/faster/simpler to implement this as a simple sun sprite.
  151. */
  152. 'godrays_fake_sun': {
  153. uniforms: {
  154. vSunPositionScreenSpace: {
  155. type: "v2",
  156. value: new THREE.Vector2( 0.5, 0.5 )
  157. },
  158. fAspect: {
  159. type: "f",
  160. value: 1.0
  161. },
  162. },
  163. vertexShader: [
  164. "varying vec2 vUv;",
  165. "void main() {",
  166. "vUv = vec2( uv.x, 1.0 - uv.y );",
  167. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  168. "}"
  169. ].join("\n"),
  170. fragmentShader: [
  171. "varying vec2 vUv;",
  172. "uniform vec2 vSunPositionScreenSpace;",
  173. "uniform float fAspect;",
  174. "void main() {",
  175. "vec2 diff = vUv-vSunPositionScreenSpace;",
  176. // Correct for aspect ratio
  177. "diff.x *= fAspect;",
  178. "float prop = clamp(length(diff)/.5,0.,1.);",
  179. "prop = .35*pow( 1.0 - prop, 3. ) ;",
  180. "gl_FragColor = vec4(prop,prop,0.2,1.);",
  181. "}"
  182. ].join("\n")
  183. }
  184. };