TriangleBlurShader.js 1.2 KB

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