TriangleBlurShader.js 1.4 KB

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