GodRaysShader.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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: /* glsl */`
  26. varying vec2 vUv;
  27. void main() {
  28. vUv = uv;
  29. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  30. }`,
  31. fragmentShader: /* glsl */`
  32. varying vec2 vUv;
  33. uniform sampler2D tInput;
  34. void main() {
  35. gl_FragColor = vec4( 1.0 ) - texture2D( tInput, vUv );
  36. }`
  37. };
  38. /**
  39. * The god-ray generation shader.
  40. *
  41. * First pass:
  42. *
  43. * The depth map is blurred along radial lines towards the "sun". The
  44. * output is written to a temporary render target (I used a 1/4 sized
  45. * target).
  46. *
  47. * Pass two & three:
  48. *
  49. * The results of the previous pass are re-blurred, each time with a
  50. * decreased distance between samples.
  51. */
  52. const GodRaysGenerateShader = {
  53. uniforms: {
  54. tInput: {
  55. value: null
  56. },
  57. fStepSize: {
  58. value: 1.0
  59. },
  60. vSunPositionScreenSpace: {
  61. value: new THREE.Vector3()
  62. }
  63. },
  64. vertexShader: /* glsl */`
  65. varying vec2 vUv;
  66. void main() {
  67. vUv = uv;
  68. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  69. }`,
  70. fragmentShader: /* glsl */`
  71. #define TAPS_PER_PASS 6.0
  72. varying vec2 vUv;
  73. uniform sampler2D tInput;
  74. uniform vec3 vSunPositionScreenSpace;
  75. uniform float fStepSize; // filter step size
  76. void main() {
  77. // delta from current pixel to "sun" position
  78. vec2 delta = vSunPositionScreenSpace.xy - vUv;
  79. float dist = length( delta );
  80. // Step vector (uv space)
  81. vec2 stepv = fStepSize * delta / dist;
  82. // Number of iterations between pixel and sun
  83. float iters = dist/fStepSize;
  84. vec2 uv = vUv.xy;
  85. float col = 0.0;
  86. // This breaks ANGLE in Chrome 22
  87. // - see http://code.google.com/p/chromium/issues/detail?id=153105
  88. /*
  89. // Unrolling didnt do much on my hardware (ATI Mobility Radeon 3450),
  90. // so i've just left the loop
  91. "for ( float i = 0.0; i < TAPS_PER_PASS; i += 1.0 ) {",
  92. // Accumulate samples, making sure we dont walk past the light source.
  93. // The check for uv.y < 1 would not be necessary with "border" UV wrap
  94. // mode, with a black border color. I don't think this is currently
  95. // exposed by three.js. As a result there might be artifacts when the
  96. // sun is to the left, right or bottom of screen as these cases are
  97. // not specifically handled.
  98. " col += ( i <= iters && uv.y < 1.0 ? texture2D( tInput, uv ).r : 0.0 );",
  99. " uv += stepv;",
  100. "}",
  101. */
  102. // Unrolling loop manually makes it work in ANGLE
  103. float f = min( 1.0, max( vSunPositionScreenSpace.z / 1000.0, 0.0 ) ); // used to fade out godrays
  104. if ( 0.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r * f;
  105. uv += stepv;
  106. if ( 1.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r * f;
  107. uv += stepv;
  108. if ( 2.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r * f;
  109. uv += stepv;
  110. if ( 3.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r * f;
  111. uv += stepv;
  112. if ( 4.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r * f;
  113. uv += stepv;
  114. if ( 5.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r * f;
  115. uv += stepv;
  116. // Should technically be dividing by 'iters but 'TAPS_PER_PASS' smooths out
  117. // objectionable artifacts, in particular near the sun position. The side
  118. // effect is that the result is darker than it should be around the sun, as
  119. // TAPS_PER_PASS is greater than the number of samples actually accumulated.
  120. // When the result is inverted (in the shader 'godrays_combine this produces
  121. // a slight bright spot at the position of the sun, even when it is occluded.
  122. gl_FragColor = vec4( col/TAPS_PER_PASS );
  123. gl_FragColor.a = 1.0;
  124. }`
  125. };
  126. /**
  127. * Additively applies god rays from texture tGodRays to a background (tColors).
  128. * fGodRayIntensity attenuates the god rays.
  129. */
  130. const GodRaysCombineShader = {
  131. uniforms: {
  132. tColors: {
  133. value: null
  134. },
  135. tGodRays: {
  136. value: null
  137. },
  138. fGodRayIntensity: {
  139. value: 0.69
  140. }
  141. },
  142. vertexShader: /* glsl */`
  143. varying vec2 vUv;
  144. void main() {
  145. vUv = uv;
  146. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  147. }`,
  148. fragmentShader: /* glsl */`
  149. varying vec2 vUv;
  150. uniform sampler2D tColors;
  151. uniform sampler2D tGodRays;
  152. uniform float fGodRayIntensity;
  153. void main() {
  154. // Since THREE.MeshDepthMaterial renders foreground objects white and background
  155. // objects black, the god-rays will be white streaks. Therefore value is inverted
  156. // before being combined with tColors
  157. gl_FragColor = texture2D( tColors, vUv ) + fGodRayIntensity * vec4( 1.0 - texture2D( tGodRays, vUv ).r );
  158. gl_FragColor.a = 1.0;
  159. }`
  160. };
  161. /**
  162. * A dodgy sun/sky shader. Makes a bright spot at the sun location. Would be
  163. * cheaper/faster/simpler to implement this as a simple sun sprite.
  164. */
  165. const GodRaysFakeSunShader = {
  166. uniforms: {
  167. vSunPositionScreenSpace: {
  168. value: new THREE.Vector3()
  169. },
  170. fAspect: {
  171. value: 1.0
  172. },
  173. sunColor: {
  174. value: new THREE.Color( 0xffee00 )
  175. },
  176. bgColor: {
  177. value: new THREE.Color( 0x000000 )
  178. }
  179. },
  180. vertexShader: /* glsl */`
  181. varying vec2 vUv;
  182. void main() {
  183. vUv = uv;
  184. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  185. }`,
  186. fragmentShader: /* glsl */`
  187. varying vec2 vUv;
  188. uniform vec3 vSunPositionScreenSpace;
  189. uniform float fAspect;
  190. uniform vec3 sunColor;
  191. uniform vec3 bgColor;
  192. void main() {
  193. vec2 diff = vUv - vSunPositionScreenSpace.xy;
  194. // Correct for aspect ratio
  195. diff.x *= fAspect;
  196. float prop = clamp( length( diff ) / 0.5, 0.0, 1.0 );
  197. prop = 0.35 * pow( 1.0 - prop, 3.0 );
  198. gl_FragColor.xyz = ( vSunPositionScreenSpace.z > 0.0 ) ? mix( sunColor, bgColor, 1.0 - prop ) : bgColor;
  199. gl_FragColor.w = 1.0;
  200. }`
  201. };
  202. THREE.GodRaysCombineShader = GodRaysCombineShader;
  203. THREE.GodRaysDepthMaskShader = GodRaysDepthMaskShader;
  204. THREE.GodRaysFakeSunShader = GodRaysFakeSunShader;
  205. THREE.GodRaysGenerateShader = GodRaysGenerateShader;
  206. } )();