Blur

Ported from GLSL shaders by kiwipxl

To use

  • Add ColorRect to your Node2d or Control
  • Set Anchor preset to full rect and add
  • Set Material in Inspector to ShaderMaterial and load the Shader
Shader code
shader_type canvas_item;

uniform sampler2D t0 : hint_screen_texture;
uniform vec2 blur_size = vec2(.5, .5);

void fragment() {
    vec2 size = vec2(textureSize(t0, 0));

    float uv_x = float(SCREEN_UV.x) * size.x;
    float uv_y = float(SCREEN_UV.y) * size.y;

    vec4 sum = vec4(0.0);
    for (int n = 0; n < 9; ++n) {
        uv_y = float(SCREEN_UV.y) * size.y + (blur_size.y * (float(n) - 4.5));
        vec4 h_sum = vec4(0.0);
        h_sum += texelFetch(t0, ivec2(int(uv_x) - int(4.0 * blur_size.x), int(uv_y)), 0);
        h_sum += texelFetch(t0, ivec2(int(uv_x) - int(3.0 * blur_size.x), int(uv_y)), 0);
        h_sum += texelFetch(t0, ivec2(int(uv_x) - int(2.0 * blur_size.x), int(uv_y)), 0);
        h_sum += texelFetch(t0, ivec2(int(uv_x) - int(blur_size.x), int(uv_y)), 0);
        h_sum += texelFetch(t0, ivec2(int(uv_x), int(uv_y)), 0);
        h_sum += texelFetch(t0, ivec2(int(uv_x) + int(blur_size.x), int(uv_y)), 0);
        h_sum += texelFetch(t0, ivec2(int(uv_x) + int(2.0 * blur_size.x), int(uv_y)), 0);
        h_sum += texelFetch(t0, ivec2(int(uv_x) + int(3.0 * blur_size.x), int(uv_y)), 0);
        h_sum += texelFetch(t0, ivec2(int(uv_x) + int(4.0 * blur_size.x), int(uv_y)), 0);
        sum += h_sum / 9.0;
    }

    COLOR = sum / 9.0;
}
Tags
blur, GLSL
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 VEGAMETA

Glow

Bloom

Simple cube axis

Related shaders

Gaussian Blur

Customizable Gausian Blur

Simple blur with CanvasGroup support

guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments