HorizontalBlurShader.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. ( function () {
  2. /**
  3. * Two pass Gaussian blur filter (horizontal and vertical blur shaders)
  4. * - described in http://www.gamerendering.com/2008/10/11/gaussian-blur-filter-shader/
  5. * and used in http://www.cake23.de/traveling-wavefronts-lit-up.html
  6. *
  7. * - 9 samples per pass
  8. * - standard deviation 2.7
  9. * - "h" and "v" parameters should be set to "1 / width" and "1 / height"
  10. */
  11. var HorizontalBlurShader = {
  12. uniforms: {
  13. 'tDiffuse': {
  14. value: null
  15. },
  16. 'h': {
  17. value: 1.0 / 512.0
  18. }
  19. },
  20. vertexShader:
  21. /* glsl */
  22. `
  23. varying vec2 vUv;
  24. void main() {
  25. vUv = uv;
  26. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  27. }`,
  28. fragmentShader:
  29. /* glsl */
  30. `
  31. uniform sampler2D tDiffuse;
  32. uniform float h;
  33. varying vec2 vUv;
  34. void main() {
  35. vec4 sum = vec4( 0.0 );
  36. sum += texture2D( tDiffuse, vec2( vUv.x - 4.0 * h, vUv.y ) ) * 0.051;
  37. sum += texture2D( tDiffuse, vec2( vUv.x - 3.0 * h, vUv.y ) ) * 0.0918;
  38. sum += texture2D( tDiffuse, vec2( vUv.x - 2.0 * h, vUv.y ) ) * 0.12245;
  39. sum += texture2D( tDiffuse, vec2( vUv.x - 1.0 * h, vUv.y ) ) * 0.1531;
  40. sum += texture2D( tDiffuse, vec2( vUv.x, vUv.y ) ) * 0.1633;
  41. sum += texture2D( tDiffuse, vec2( vUv.x + 1.0 * h, vUv.y ) ) * 0.1531;
  42. sum += texture2D( tDiffuse, vec2( vUv.x + 2.0 * h, vUv.y ) ) * 0.12245;
  43. sum += texture2D( tDiffuse, vec2( vUv.x + 3.0 * h, vUv.y ) ) * 0.0918;
  44. sum += texture2D( tDiffuse, vec2( vUv.x + 4.0 * h, vUv.y ) ) * 0.051;
  45. gl_FragColor = sum;
  46. }`
  47. };
  48. THREE.HorizontalBlurShader = HorizontalBlurShader;
  49. } )();