Configurable Box Blur
I could not find a good blur shader and just ended with learning the shader language. I used the post from Schorsch as a template https://godotforums.org/d/20506-a-good-blur-shader. He linked to a created shadertoy by him; so it might be that the license has to be changed?
I’m not very familiar with the shading language but I thought I read that GLES2 does not support for loops. So I gues this will work in Vulkan and GLES3.
Do note that this shader also blurs the alpha channel; something that is easily changed.
Bugfix, edge blur was incorrect. Looked at Exuin’s Blur.
Shader code
shader_type canvas_item;
uniform int strength: hint_range(1, 512) = 2;
vec4 blur_size(sampler2D tex,vec2 fragCoord, vec2 pixelSize) {
vec4 color = vec4(0.,0.,0.,0.);
float strengthFloat = float(strength);
vec2 pixel = fragCoord/pixelSize;
int x_min = max(int(pixel.x-strengthFloat), 0);
int x_max = min(int(pixel.x+strengthFloat), int(1./pixelSize.x));
int y_min = max(int(pixel.y-strengthFloat), 0);
int y_max = min(int(pixel.y+strengthFloat), int(1./pixelSize.y));
int count =0;
// Sum the pixels colors
for(int x=x_min; x <= x_max; x++) {
for(int y = y_min; y <= y_max; y++) {
color += texture(tex, vec2(float(x), float(y)) * pixelSize);
count++;
}
}
// Divide the color by the number of colors you summed up
color /= float(count);
return color;
}
void fragment() {
COLOR = blur_size(TEXTURE,UV,TEXTURE_PIXEL_SIZE);
}
Thanks for that nice Shader. Works really well! š
Hi,
thanks for the amazing shader. I used it to create a dynamic depth of field effect: https://godotshaders.com/shader/dynamic-depth-of-field/
for
loops with a constant iteration count are guaranteed to work in GLES2/WebGL 1.0, but not when they have a variable iteration count.while
loops won’t work either.