TriangleBlurShader.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import {
  2. Vector2
  3. } from 'three';
  4. /** @module TriangleBlurShader */
  5. /**
  6. * Triangle blur shader based on [glfx.js triangle blur shader]{@link 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. * @constant
  13. * @type {Object}
  14. */
  15. const TriangleBlurShader = {
  16. name: 'TriangleBlurShader',
  17. uniforms: {
  18. 'texture': { value: null },
  19. 'delta': { value: new Vector2( 1, 1 ) }
  20. },
  21. vertexShader: /* glsl */`
  22. varying vec2 vUv;
  23. void main() {
  24. vUv = uv;
  25. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  26. }`,
  27. fragmentShader: /* glsl */`
  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. };
  47. export { TriangleBlurShader };
粤ICP备19079148号