TriangleBlurShader.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import {
  2. Vector2
  3. } from 'three';
  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. const TriangleBlurShader = {
  14. name: 'TriangleBlurShader',
  15. uniforms: {
  16. 'texture': { value: null },
  17. 'delta': { value: new Vector2( 1, 1 ) }
  18. },
  19. vertexShader: /* glsl */`
  20. varying vec2 vUv;
  21. void main() {
  22. vUv = uv;
  23. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  24. }`,
  25. fragmentShader: /* glsl */`
  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. };
  45. export { TriangleBlurShader };