TriangleBlurShader.js 1.4 KB

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