GodRaysShader.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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.Vector2( 0.5, 0.5 )
  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 vec2 vSunPositionScreenSpace;",
  79. "uniform float fStepSize;", // filter step size
  80. "void main() {",
  81. // delta from current pixel to "sun" position
  82. " vec2 delta = vSunPositionScreenSpace - 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. " if ( 0.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r;",
  108. " uv += stepv;",
  109. " if ( 1.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r;",
  110. " uv += stepv;",
  111. " if ( 2.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r;",
  112. " uv += stepv;",
  113. " if ( 3.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r;",
  114. " uv += stepv;",
  115. " if ( 4.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r;",
  116. " uv += stepv;",
  117. " if ( 5.0 <= iters && uv.y < 1.0 ) col += texture2D( tInput, uv ).r;",
  118. " uv += stepv;",
  119. // Should technically be dividing by 'iters', but 'TAPS_PER_PASS' smooths out
  120. // objectionable artifacts, in particular near the sun position. The side
  121. // effect is that the result is darker than it should be around the sun, as
  122. // TAPS_PER_PASS is greater than the number of samples actually accumulated.
  123. // When the result is inverted (in the shader 'godrays_combine', this produces
  124. // a slight bright spot at the position of the sun, even when it is occluded.
  125. " gl_FragColor = vec4( col/TAPS_PER_PASS );",
  126. " gl_FragColor.a = 1.0;",
  127. "}"
  128. ].join( "\n" )
  129. };
  130. /**
  131. * Additively applies god rays from texture tGodRays to a background (tColors).
  132. * fGodRayIntensity attenuates the god rays.
  133. */
  134. THREE.GodRaysCombineShader = {
  135. uniforms: {
  136. tColors: {
  137. value: null
  138. },
  139. tGodRays: {
  140. value: null
  141. },
  142. fGodRayIntensity: {
  143. value: 0.69
  144. },
  145. vSunPositionScreenSpace: {
  146. value: new THREE.Vector2( 0.5, 0.5 )
  147. }
  148. },
  149. vertexShader: [
  150. "varying vec2 vUv;",
  151. "void main() {",
  152. " vUv = uv;",
  153. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  154. "}"
  155. ].join( "\n" ),
  156. fragmentShader: [
  157. "varying vec2 vUv;",
  158. "uniform sampler2D tColors;",
  159. "uniform sampler2D tGodRays;",
  160. "uniform vec2 vSunPositionScreenSpace;",
  161. "uniform float fGodRayIntensity;",
  162. "void main() {",
  163. // Since THREE.MeshDepthMaterial renders foreground objects white and background
  164. // objects black, the god-rays will be white streaks. Therefore value is inverted
  165. // before being combined with tColors
  166. " gl_FragColor = texture2D( tColors, vUv ) + fGodRayIntensity * vec4( 1.0 - texture2D( tGodRays, vUv ).r );",
  167. " gl_FragColor.a = 1.0;",
  168. "}"
  169. ].join( "\n" )
  170. };
  171. /**
  172. * A dodgy sun/sky shader. Makes a bright spot at the sun location. Would be
  173. * cheaper/faster/simpler to implement this as a simple sun sprite.
  174. */
  175. THREE.GodRaysFakeSunShader = {
  176. uniforms: {
  177. vSunPositionScreenSpace: {
  178. value: new THREE.Vector2( 0.5, 0.5 )
  179. },
  180. fAspect: {
  181. value: 1.0
  182. },
  183. sunColor: {
  184. value: new THREE.Color( 0xffee00 )
  185. },
  186. bgColor: {
  187. value: new THREE.Color( 0x000000 )
  188. }
  189. },
  190. vertexShader: [
  191. "varying vec2 vUv;",
  192. "void main() {",
  193. " vUv = uv;",
  194. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  195. "}"
  196. ].join( "\n" ),
  197. fragmentShader: [
  198. "varying vec2 vUv;",
  199. "uniform vec2 vSunPositionScreenSpace;",
  200. "uniform float fAspect;",
  201. "uniform vec3 sunColor;",
  202. "uniform vec3 bgColor;",
  203. "void main() {",
  204. " vec2 diff = vUv - vSunPositionScreenSpace;",
  205. // Correct for aspect ratio
  206. " diff.x *= fAspect;",
  207. " float prop = clamp( length( diff ) / 0.5, 0.0, 1.0 );",
  208. " prop = 0.35 * pow( 1.0 - prop, 3.0 );",
  209. " gl_FragColor.xyz = mix( sunColor, bgColor, 1.0 - prop );",
  210. " gl_FragColor.w = 1.0;",
  211. "}"
  212. ].join( "\n" )
  213. };