GodRaysShader.js 6.8 KB

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