TriangleBlurShader.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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:
  21. /* glsl */
  22. `
  23. varying vec2 vUv;
  24. void main() {
  25. vUv = uv;
  26. gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
  27. }`,
  28. fragmentShader:
  29. /* glsl */
  30. `
  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. THREE.TriangleBlurShader = TriangleBlurShader;
  51. } )();