Skip to content

Instantly share code, notes, and snippets.

@onedayitwillmake
Created December 1, 2017 11:33
Show Gist options
  • Save onedayitwillmake/fc8d6617bc559253acc433cfc37985b3 to your computer and use it in GitHub Desktop.
Save onedayitwillmake/fc8d6617bc559253acc433cfc37985b3 to your computer and use it in GitHub Desktop.
gaussian blur
#define pow2(x) (x * x)
const float pi = atan(1.0) * 4.0;
const int samples = 25;
float gaussian(vec2 i, float sigma) {
return 1.0 / (2.0 * pi * pow2(sigma)) * exp(-((pow2(i.x) + pow2(i.y)) / (2.0 * pow2(sigma))));
}
vec3 blur(sampler2D sp, vec2 uv, vec2 scale) {
vec3 col = vec3(0.0);
float accum = 0.0;
float weight;
vec2 offset;
float sigma = float(samples) * ((iMouse.x/iResolution.x) * 0.28);
for (int x = -samples / 2; x < samples / 2; ++x) {
for (int y = -samples / 2; y < samples / 2; ++y) {
offset = vec2(x, y);
weight = gaussian(offset, sigma);
col += texture(sp, uv + scale * offset).rgb * weight;
accum += weight;
}
}
return col / accum;
}
void mainImage(out vec4 color, vec2 coord) {
vec2 ps = vec2(1.0) / iResolution.xy;
vec2 uv = coord * ps;
color.rgb = blur(iChannel0, uv, ps);
color.a = 1.0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment