Web Safe Darkened Gaussian Blur

Shader based on https://godotshaders.com/shader/gaussian-blur-2 that applies a gaussian blur and also https://godotshaders.com/shader/darkened-blur which darkens the shader. This shader is also safe to be used on the web, other shaders don’t work correctly due to a regression bug still in Godot 4.3.

To use it, just make a ColorRect, set the anchor preset to “Full Rect”, and assign this shader to the material.

Params:

  • strength – changes how blurry the result will be
  • mix_percentage – changes how dark the blur will be
Shader code
shader_type canvas_item;

uniform sampler2D SCREEN_TEXTURE: hint_screen_texture,repeat_disable, filter_nearest;
/**
* How blurry the result should be.
* Limited to 20 because of performance, if you want feel free to break it.
*/
uniform float strength : hint_range(0.1, 20.0) = 3.3;
/**
* How dark the blur will be
*/
uniform float mix_percentage: hint_range(0.0, 1.0) = 0.3;

float gaussianDistribution(float x, float STD){ // STD stands for standard deviation
	return exp(-(x*x)/(2.*STD*STD))/(sqrt(2.*PI)*STD);
}

vec3 gaussianblur(sampler2D sampler, vec2 pos, vec2 pixel_size, float sigmaUsed, int radius){
    vec3 blurredPixel = vec3(0.0);
    float total_weight = 0.0;
	// Loop over the radius (tecnically its a square)
    for(int i = -radius ; i <= radius; i++){ 
        for(int j = -radius; j <= radius; j++){
			// Calculate the offset from the current pixel
			vec2 offset = vec2(float(i), float(j))*pixel_size;
			vec2 changedPos = pos + offset;        
			
			// Calculate the weight based on the Gaussian distribution multiplying both dimentions (how far are X and Y form the center (pos))
			float weight = gaussianDistribution(float(i), sigmaUsed)*gaussianDistribution(float(j), sigmaUsed);
			// Add the weighted color value to the blurred pixel
			blurredPixel += texture(SCREEN_TEXTURE, changedPos).rgb * weight;
			total_weight += weight;
        }
    }
	// Normalize the blurred pixel color by the total weight
	blurredPixel/=total_weight;
    return blurredPixel;
}
void fragment() {
    vec3 PixelBlurred = gaussianblur(SCREEN_TEXTURE, SCREEN_UV, SCREEN_PIXEL_SIZE, strength, int(round(3.0 * strength)));
	vec3 color = mix(PixelBlurred, vec3(0.0), mix_percentage);
    // The radius is 3*strength because it is the point where the Gaussian weight is near zero.
    COLOR = vec4(color, 1.);
}
Tags
blur, blurring, dark, darken, gaussian blur, Mix, shader
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.

Related shaders

Darkened Blur

Gaussian blur for Godot 4.3 Canvase layer

Gaussian Blur

Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
LambBrainz
1 month ago

Thanks for linking my shader! This one is really good and I’ll definitely be using it. Great job!