GodRaysShader.js 6.6 KB

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