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

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
uniform vec2 resolution;

// from https://github.com/Jam3/glsl-fast-gaussian-blur
14
vec4 blur9(sampler2D image, vec2 uv, vec2 resolution, vec2 direction) {
15
  vec4 color = vec4(0.0);
16 17 18 19 20 21 22
  vec2 off1 = vec2(1.3846153846) * direction;
  vec2 off2 = vec2(3.2307692308) * direction;
  color += texture2D(image, uv) * 0.2270270270;
  color += texture2D(image, uv + (off1 / resolution)) * 0.3162162162;
  color += texture2D(image, uv - (off1 / resolution)) * 0.3162162162;
  color += texture2D(image, uv + (off2 / resolution)) * 0.0702702703;
  color += texture2D(image, uv - (off2 / resolution)) * 0.0702702703;
23 24 25 26
  return color;
}

void main () {
27
  gl_FragColor = blur9(t, uv, resolution, direction);
28 29 30 31 32
}
    `
  }
});

33 34
module.exports = GL.createComponent(
  ({ width, height, direction, children }) =>
35
    <GL.Node
36
      shader={shaders.blur1D}
37 38 39
      width={width}
      height={height}
      uniforms={{
40
        direction,
41 42 43
        resolution: [ width, height ]
      }}>
      <GL.Uniform name="t">{children}</GL.Uniform>
44
    </GL.Node>
45 46 47
, {
  displayName: "Blur1D"
});