GodRaysShader.js 6.7 KB

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