GodRaysShader.js 6.7 KB

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