Box Blur

Have you ever wondered what a box blur implementation would look like in Godot? No? Too bad!

Shader code
shader_type canvas_item;

void fragment(){
	vec2 pixel = UV / TEXTURE_PIXEL_SIZE;
	int x_min = max(int(pixel.x-1.), 0);
	int x_max = min(int(pixel.x+2.), int(1./TEXTURE_PIXEL_SIZE.x));
	int y_min = max(int(pixel.y-1.), 0);
	int y_max = min(int(pixel.y+2.), int(1./TEXTURE_PIXEL_SIZE.y));
	vec4 total = vec4(0., 0., 0., 0.);
	for(int i = x_min; i < x_max; i++){
		for(int j = y_min; j < y_max; j++){
			total += texture(TEXTURE, vec2(float(i), float(j)) * TEXTURE_PIXEL_SIZE);
		}
	}
	total /= 9.;
	COLOR = total;
}
Tags
blur
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from Exuin

Scrolling background

Gaussian Blur

Square Pixelation

Related shaders

Gaussian Blur

Simple blur, mixed with a color

Radial Blur Shader

guest
0 Comments
Inline Feedbacks
View all comments