GodRaysShader.js 6.9 KB

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