Shader help (convolution matrix for a simple blur)
Posted: Thu Dec 12, 2019 2:54 pm
I'm relatively new to shaders. Been reading a bit through https://thebookofshaders.com/, and using Moonshine as a reference. However, I struggle quite a bit just to get a boxblur shader working. I know how to do it in 'normal' code, but I can't seem to figure out how to get an arbitrary pixel position relative to the currrent pixel in the shader pipeline. So suppose given the default shader function:
I would like to get the pixels surrounding the 'current' pixel, e.g. the pixel topleft, top, topright, left, right, bottom left, bottom, and bottom right, and then averaging it and set that color as the current pixel. I tried something like this:
but it's not really working (nothing is blurred). Can somebody point me into the right direction?
Code: Select all
vec4 effect(vec4 color, Image tex, vec2 tc, vec2 sc) {
vec4 current = Texel(tex, tc);
return current * color;
}
Code: Select all
// I know it's verbose and maybe there's better way to do this... but bear with me.
vec4 effect(vec4 color, Image tex, vec2 tc, vec2 sc) {
vec2 scale = love_ScreenSize.xy;
vec4 topleft = Texel(tex, (tc + vec2(-1.0, -1.0) / scale));
vec4 top = Texel(tex, (tc + vec2(0, -1.0) / scale));
vec4 topright = Texel(tex, (tc + vec2(1.0, -1.0) / scale));
vec4 left = Texel(tex, (tc + vec2(-1.0, 0) / scale));
vec4 curr = Texel(tex, tc);
vec4 right = Texel(tex, (tc + vec2(1.0, 0) / scale));
vec4 botleft = Texel(tex, (tc + vec2(-1.0, 1.0) / scale));
vec4 bottom = Texel(tex, (tc + vec2(0, 1.0) / scale));
vec4 botright = Texel(tex, (tc + vec2(1.0, 1.0) / scale));
vec4 avg = (topleft + top + topright + left + curr + right + botleft + bottom + botright) / 9.0;
return avg * color;
}