GodRaysShader.js 7.0 KB

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