TriangleBlurShader.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * @author zz85 / http://www.lab4games.net/zz85/blog
  3. *
  4. * Triangle blur shader
  5. * based on glfx.js triangle blur shader
  6. * 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. THREE.TriangleBlurShader = {
  13. uniforms : {
  14. "texture": { value: null },
  15. "delta": { value: new THREE.Vector2( 1, 1 ) }
  16. },
  17. vertexShader: [
  18. "varying vec2 vUv;",
  19. "void main() {",
  20. "vUv = uv;",
  21. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  22. "}"
  23. ].join( "\n" ),
  24. fragmentShader: [
  25. "#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. ].join( "\n" )
  44. };