ShaderGodRays.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. * Sousa2008 - Crysis Next Gen Effects, GDC2008, http://www.crytek.com/sites/default/files/GDC08_SousaT_CrysisEffects.ppt
  18. */
  19. THREE.ShaderGodRays = {
  20. /* -------------------------------------------------------------------------
  21. // The god-ray generation shader.
  22. //
  23. // First pass:
  24. // The input is the depth map. I found that the output from the
  25. // THREE.MeshDepthMaterial material was directly suitable without
  26. // requiring any treatment whatsoever.
  27. //
  28. // The depth map is blurred along radial lines towards the "sun". The
  29. // output is written to a temporary render target (I used a 1/4 sized
  30. // target).
  31. //
  32. // Pass two & three:
  33. // The results of the previous pass are re-blurred, each time with a
  34. // decreased distance between samples.
  35. //
  36. ------------------------------------------------------------------------- */
  37. 'godrays_generate' : {
  38. uniforms: { tInput: { type: "t", value: 0, texture: null },
  39. fStepSize: { type: "f", value: 1.0 },
  40. vSunPositionScreenSpace: { type: "v2", value: new THREE.Vector2( 0.5, 0.5 ) },
  41. },
  42. vertexShader: [
  43. "varying vec2 vUv;",
  44. "void main() {",
  45. "vUv = vec2( uv.x, 1.0 - uv.y );",
  46. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  47. "}"
  48. ].join("\n"),
  49. fragmentShader: [
  50. "varying vec2 vUv;",
  51. "uniform sampler2D tInput;",
  52. "uniform vec2 vSunPositionScreenSpace;",
  53. "uniform float fStepSize;", // filter step size
  54. "#define TAPS_PER_PASS 6.0",
  55. "void main() {",
  56. // delta from current pixel to "sun" position
  57. "vec2 delta = (vSunPositionScreenSpace - vUv);",
  58. "float dist = length(delta);",
  59. // Step vector (uv space)
  60. "vec2 stepv = fStepSize*delta/dist;",
  61. // Number of iterations between pixel and sun
  62. "float iters = dist/fStepSize;",
  63. "vec2 uv = vUv.xy;",
  64. "float col = 0.0;",
  65. // Unrolling didnt do much on my hardware (ATI Mobility Radeon 3450),
  66. // so i've just left the loop
  67. "for (float i = 0.0; i < TAPS_PER_PASS; i+=1.0 ) {",
  68. // Accumulate samples, making sure we dont walk past the light source.
  69. // The check for uv.y<1 would not be necessary with "border" UV wrap
  70. // mode, with a black border colour. I don't think this is currently
  71. // exposed by three.js. As a result there might be artifacts when the
  72. // sun is to the left, right or bottom of screen as these cases are
  73. // not specifically handled.
  74. "col += (i <= iters && uv.y<1. ? texture2D( tInput, uv ).r : .0) ;",
  75. "uv += stepv;",
  76. "}",
  77. // Should technically be dividing by 'iters', but 'TAPS_PER_PASS' smooths out
  78. // objectionable artifacts, in particular near the sun position. The side
  79. // effect is that the result is darker than it should be around the sun, as
  80. // TAPS_PER_PASS is greater than the number of samples actually accumulated.
  81. // When the result is inverted (in the shader 'godrays_combine', this produces
  82. // a slight bright spot at the position of the sun, even when it is occluded.
  83. "gl_FragColor = vec4( col/TAPS_PER_PASS );",
  84. "gl_FragColor.a = 1.;",
  85. "}"
  86. ].join("\n")
  87. },
  88. /* -------------------------------------------------------------------------
  89. // Additively applies god rays from texture tGodRays to a background (tColors).
  90. //
  91. // fGodRayIntensity attenuates the god rays.
  92. ------------------------------------------------------------------------- */
  93. 'godrays_combine' : {
  94. uniforms: { tColors: { type: "t", value: 0, texture: null },
  95. tGodRays: { type: "t", value: 1, texture: null },
  96. fGodRayIntensity: { type: "f", value: 0.69 },
  97. vSunPositionScreenSpace: { type: "v2", value: new THREE.Vector2( 0.5, 0.5 ) },
  98. },
  99. vertexShader: [
  100. "varying vec2 vUv;",
  101. "void main() {",
  102. "vUv = vec2( uv.x, 1.0 - uv.y );",
  103. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  104. "}"
  105. ].join("\n"),
  106. fragmentShader: [
  107. "varying vec2 vUv;",
  108. "uniform sampler2D tColors;",
  109. "uniform sampler2D tGodRays;",
  110. "uniform vec2 vSunPositionScreenSpace;",
  111. "uniform float fGodRayIntensity;",
  112. "void main() {",
  113. // Since THREE.MeshDepthMaterial renders foreground objects white and background
  114. // objects black, the god-rays will be white streaks. Therefore value is inverted
  115. // before being combined with tColors
  116. "gl_FragColor = texture2D( tColors, vUv ) + fGodRayIntensity*vec4( 1.-texture2D( tGodRays, vUv ).r );",
  117. "gl_FragColor.a = 1.;",
  118. "}"
  119. ].join("\n")
  120. },
  121. /* -------------------------------------------------------------------------
  122. // A dodgy sun/sky shader. Makes a bright spot at the sun location. Would be
  123. // cheaper/faster/simpler to implement this as a simple sun sprite.
  124. ------------------------------------------------------------------------- */
  125. 'godrays_fake_sun' : {
  126. uniforms: { vSunPositionScreenSpace: { type: "v2", value: new THREE.Vector2( 0.5, 0.5 ) },
  127. fAspect: { type: "f", value: 1.0 },
  128. },
  129. vertexShader: [
  130. "varying vec2 vUv;",
  131. "void main() {",
  132. "vUv = vec2( uv.x, 1.0 - uv.y );",
  133. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  134. "}"
  135. ].join("\n"),
  136. fragmentShader: [
  137. "varying vec2 vUv;",
  138. "uniform vec2 vSunPositionScreenSpace;",
  139. "uniform float fAspect;",
  140. "void main() {",
  141. "vec2 diff = vUv-vSunPositionScreenSpace;",
  142. // Correct for aspect ratio
  143. "diff.x *= fAspect;",
  144. "float prop = clamp(length(diff)/.5,0.,1.);",
  145. "prop = .35*pow( 1.0 - prop, 3. ) ;",
  146. "gl_FragColor = vec4(prop,prop,0.2,1.);",
  147. "}"
  148. ].join("\n")
  149. }
  150. };