Blur1D.js 1.53 KB
Newer Older
1 2 3 4
const React = require("react-native");
const GL = require("gl-react-native");

const shaders = GL.Shaders.create({
5
  blur1D: {
6 7 8
    frag: `
precision highp float;
varying vec2 uv;
9 10
uniform sampler2D t;
uniform vec2 direction;
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
uniform vec2 resolution;

// from https://github.com/Jam3/glsl-fast-gaussian-blur
vec4 blur13(sampler2D image, vec2 uv, vec2 resolution, vec2 direction) {
  vec4 color = vec4(0.0);
  vec2 off1 = vec2(1.411764705882353) * direction;
  vec2 off2 = vec2(3.2941176470588234) * direction;
  vec2 off3 = vec2(5.176470588235294) * direction;
  color += texture2D(image, uv) * 0.1964825501511404;
  color += texture2D(image, uv + (off1 / resolution)) * 0.2969069646728344;
  color += texture2D(image, uv - (off1 / resolution)) * 0.2969069646728344;
  color += texture2D(image, uv + (off2 / resolution)) * 0.09447039785044732;
  color += texture2D(image, uv - (off2 / resolution)) * 0.09447039785044732;
  color += texture2D(image, uv + (off3 / resolution)) * 0.010381362401148057;
  color += texture2D(image, uv - (off3 / resolution)) * 0.010381362401148057;
  return color;
}

void main () {
30
  gl_FragColor = blur13(t, uv, resolution, direction);
31 32 33 34 35
}
    `
  }
});

36
class Blur1D extends GL.Component {
37
  render () {
38
    const { width, height, direction, children } = this.props;
39
    return <GL.View
40
      shader={shaders.blur1D}
41 42 43
      width={width}
      height={height}
      uniforms={{
44
        direction,
45 46
        resolution: [ width, height ]
      }}>
47
      <GL.Target uniform="t">{children}</GL.Target>
48 49 50 51
    </GL.View>;
  }
}

52
module.exports = Blur1D;