2
0

TriangleBlurShader.js 1.2 KB

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