GodRaysShader.js 6.7 KB

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