Fast way to blur image
Posted: Thu Apr 06, 2023 9:17 am
Hi all!
I recently implemented a bloom effect into my game, which for the most part works pretty well. The way it works is by making a canvas, drawing the glow layer of sprites onto it and then subtracting away parts that should be blocked by things in front of it, before finally gaussian blurring the whole canvas and drawing it on top. It looks like so:
It has a few issues though:
1) Induvidual entities can't have different levels of bloom. It's not really a huge issue, but I would prefer greater control of how the bloom looks
2) I suspect it will look different on different machines. I'm still pretty new to shaders, but I'm pretty sure that the bloom will look different depending on the resoultion you display it as (I tried to implement this shader https://learnopengl.com/Advanced-Lighting/Bloom)
3) The blur hates the edge of the screen. I'm pretty sure I can fix this, but if there's a better solution that doesn't have that issue that would be nice.
4) It's slow. In order to get a larger radius, I have to apply the shader to the canvas multiple times, and that really slows it down. The amount I have at the moment is alright, but it's too subtle for my liking.
This is my current shader code:
I've read that box blurs are supposed to be faster, but I can't really work out why that would be since it's basically the same thing but with equal weights.
I recently implemented a bloom effect into my game, which for the most part works pretty well. The way it works is by making a canvas, drawing the glow layer of sprites onto it and then subtracting away parts that should be blocked by things in front of it, before finally gaussian blurring the whole canvas and drawing it on top. It looks like so:
It has a few issues though:
1) Induvidual entities can't have different levels of bloom. It's not really a huge issue, but I would prefer greater control of how the bloom looks
2) I suspect it will look different on different machines. I'm still pretty new to shaders, but I'm pretty sure that the bloom will look different depending on the resoultion you display it as (I tried to implement this shader https://learnopengl.com/Advanced-Lighting/Bloom)
3) The blur hates the edge of the screen. I'm pretty sure I can fix this, but if there's a better solution that doesn't have that issue that would be nice.
4) It's slow. In order to get a larger radius, I have to apply the shader to the canvas multiple times, and that really slows it down. The amount I have at the moment is alright, but it's too subtle for my liking.
This is my current shader code:
Code: Select all
extern vec2 texture_size;
extern bool horizontal;
uniform float weight[5] = float[] (0.227027, 0.1945946, 0.1216216, 0.054054, 0.016216);
vec4 effect( vec4 colour, Image texture, vec2 texture_coords, vec2 screen_coords )
{
vec4 pixel = Texel(texture, texture_coords) * colour ;
vec2 tex_offset = 1.0 / texture_size;
vec4 result = pixel*weight[0];
if (horizontal)
{
for(int i = 1; i < 5; ++i)
{
result += Texel(texture, texture_coords + vec2(tex_offset.x*i, 0), 0) * weight[i];
result += Texel(texture, texture_coords - vec2(tex_offset.x*i, 0), 0) * weight[i];
}
}
else
{
for(int i = 1; i < 5; ++i)
{
result += Texel(texture, texture_coords + vec2(0, tex_offset.y*i), 0) * weight[i];
result += Texel(texture, texture_coords - vec2(0, tex_offset.y*i), 0) * weight[i];
}
}
return result * colour;
}