ShaderGodRays.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * @author huwb / http://huwbowles.com/
  3. *
  4. */
  5. THREE.ShaderGodRays = {
  6. /* -------------------------------------------------------------------------
  7. // God-rays
  8. // Possibly the same as the implementation in CryEngine 2 (Sousa2008).
  9. // http://artmartinsh.blogspot.com/2010/02/glsl-lens-blur-filter-with-bokeh.html
  10. ------------------------------------------------------------------------- */
  11. 'godrays_fake_sun' : {
  12. uniforms: { vSunPositionScreenSpace: { type: "v2", value: new THREE.Vector2( 0.5, 0.5 ) },
  13. fAspect: { type: "f", value: 1.0 },
  14. },
  15. vertexShader: [
  16. "varying vec2 vUv;",
  17. "void main() {",
  18. "vUv = vec2( uv.x, 1.0 - uv.y );",
  19. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  20. "}"
  21. ].join("\n"),
  22. fragmentShader: [
  23. "varying vec2 vUv;",
  24. "uniform vec2 vSunPositionScreenSpace;",
  25. "uniform float fAspect;",
  26. "void main() {",
  27. "vec2 diff = vUv-vSunPositionScreenSpace;",
  28. "diff.x *= fAspect;",
  29. "float prop = clamp(length(diff)/.5,0.,1.);",
  30. "prop = .35*pow( 1.0 - prop, 3. ) ;",
  31. "gl_FragColor = vec4(prop,prop,0.2,1.);",
  32. "}"
  33. ].join("\n")
  34. },
  35. 'godrays_generate' : {
  36. uniforms: { tInput: { type: "t", value: 0, texture: null },
  37. fStepSize: { type: "f", value: 1.0 },
  38. vSunPositionScreenSpace: { type: "v2", value: new THREE.Vector2( 0.5, 0.5 ) },
  39. },
  40. vertexShader: [
  41. "varying vec2 vUv;",
  42. "void main() {",
  43. "vUv = vec2( uv.x, 1.0 - uv.y );",
  44. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  45. "}"
  46. ].join("\n"),
  47. fragmentShader: [
  48. "varying vec2 vUv;",
  49. "uniform sampler2D tInput;",
  50. "uniform vec2 vSunPositionScreenSpace;",
  51. "uniform float fStepSize;", // filter step size
  52. "#define TAPS_PER_PASS 6.0",
  53. "void main() {",
  54. "vec2 delta = (vSunPositionScreenSpace - vUv);",
  55. "vec2 uv = vUv.xy;",
  56. "float dist = length(delta);",
  57. "vec2 stepv = fStepSize*delta/dist;",
  58. "float iters = dist/fStepSize;", // floor unnecessary
  59. "float col = 0.0;",
  60. //unrolling didnt do much on my hardware so i've just left the loop
  61. "for (float i = 0.0; i < TAPS_PER_PASS; i+=1.0 ) {",
  62. // accumulate samples, making sure we dont walk past the light source
  63. "col += (i <= iters && uv.y<1. ? texture2D( tInput, uv ).r : .0) ;",
  64. "uv += stepv;",
  65. "}",
  66. // should technically be dividing by iters but TAPS_PER_PASS smooths out
  67. // objectionable artifacts in particular near the sun position.
  68. "gl_FragColor = vec4( col/TAPS_PER_PASS );",
  69. "gl_FragColor.a = 1.;",
  70. /*
  71. */
  72. "}"
  73. ].join("\n")
  74. },
  75. 'godrays_combine' : {
  76. uniforms: { tColors: { type: "t", value: 0, texture: null },
  77. tGodRays: { type: "t", value: 1, texture: null },
  78. fGodRayIntensity: { type: "f", value: 0.69 },
  79. vSunPositionScreenSpace: { type: "v2", value: new THREE.Vector2( 0.5, 0.5 ) },
  80. },
  81. vertexShader: [
  82. "varying vec2 vUv;",
  83. "void main() {",
  84. "vUv = vec2( uv.x, 1.0 - uv.y );",
  85. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  86. "}"
  87. ].join("\n"),
  88. fragmentShader: [
  89. "varying vec2 vUv;",
  90. "uniform sampler2D tColors;",
  91. "uniform sampler2D tGodRays;",
  92. "uniform vec2 vSunPositionScreenSpace;",
  93. "uniform float fGodRayIntensity;", // filter step size
  94. "void main() {",
  95. "gl_FragColor = texture2D( tColors, vUv ) + fGodRayIntensity*vec4( 1.-texture2D( tGodRays, vUv ).r );",
  96. "gl_FragColor.a = 1.;",
  97. "}"
  98. ].join("\n")
  99. }
  100. };