GodRaysShader.js 7.0 KB

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