TriangleBlurShader.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import {
  2. Vector2
  3. } from "../../../build/three.module.js";
  4. /**
  5. * Triangle blur shader
  6. * based on glfx.js triangle blur shader
  7. * https://github.com/evanw/glfx.js
  8. *
  9. * A basic blur filter, which convolves the image with a
  10. * pyramid filter. The pyramid filter is separable and is applied as two
  11. * perpendicular triangle filters.
  12. */
  13. var TriangleBlurShader = {
  14. uniforms: {
  15. "texture": { value: null },
  16. "delta": { value: new Vector2( 1, 1 ) }
  17. },
  18. vertexShader: [
  19. "varying vec2 vUv;",
  20. "void main() {",
  21. " vUv = uv;",
  22. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  23. "}"
  24. ].join( "\n" ),
  25. fragmentShader: [
  26. "#include <common>",
  27. "#define ITERATIONS 10.0",
  28. "uniform sampler2D texture;",
  29. "uniform vec2 delta;",
  30. "varying vec2 vUv;",
  31. "void main() {",
  32. " vec4 color = vec4( 0.0 );",
  33. " float total = 0.0;",
  34. // randomize the lookup values to hide the fixed number of samples
  35. " float offset = rand( vUv );",
  36. " for ( float t = -ITERATIONS; t <= ITERATIONS; t ++ ) {",
  37. " float percent = ( t + offset - 0.5 ) / ITERATIONS;",
  38. " float weight = 1.0 - abs( percent );",
  39. " color += texture2D( texture, vUv + delta * percent ) * weight;",
  40. " total += weight;",
  41. " }",
  42. " gl_FragColor = color / total;",
  43. "}"
  44. ].join( "\n" )
  45. };
  46. export { TriangleBlurShader };