Blur a shader based on a GradientTexture2D

Godot Version

v4.2.2.stable.mono.official [15073afe3]


Hey hey! I’m coming here after several hours of trying to figure something out but failing unfortunately. I’m looking for a way to add blur to a shader I modified, based on a Gradient texture that I give it. What I’m looking for specifically, is if I set the gradient texture to be 50% black and 50% alpha (or black), to have the shader gradually fade out at the half point, no matter how large the Item’s surface is where the shader is applied to.
This is the shader that it needs to work in:

shader_type canvas_item;

uniform sampler2D screen : hint_screen_texture, filter_nearest;
uniform vec2 scale = vec2(1.0, 1.0);
uniform float y_zoom;
uniform vec4 reflection_color: source_color;
uniform sampler2D fadeGradient;

void fragment() {
    float uv_height = SCREEN_PIXEL_SIZE.y / TEXTURE_PIXEL_SIZE.y;
    vec2 reflected_screenuv = vec2(SCREEN_UV.x, SCREEN_UV.y - uv_height * UV.y * scale.y * y_zoom * 2.0);
    vec4 reflection = texture(screen, reflected_screenuv) / 2.0;
	COLOR.rgb = mix(reflection.rgb, reflection_color.rgb, reflection.a);

I’ve already addded the sampler2D for the Gradient texture, but even after reading up some docs and watching some videos, I can’t really figure out how to apply the texture to the “blur amount”.

what i do to blur the water is just:

uniform vec4 water_color:source_color;


vec4 reflection=textureLod(SCREEN_TEXTURE,reflected_screen_uv,1.0);

change the 1.0 value to something else to see more blurry or less blurry

the main thing for making it blurry is that textureLod

1 Like

This doesn’t really answer my question about how to get the alpha data from a gradient texture and use it to control the blur in parts of the shader surface, not the entire thing overall.

you use the intensity of the gradient texture and applied it to the strength of the mix like shown in above code, COLOR.rgb’s water_color.a, change it to the gradient intensity

answering this alone,

vec4 gradient= texture(fadeGradient, UV);

then you use the gradient.a

You’ll also need to set the screen texture filter_nearest_mipmap or filter_linear_mipmap to be able to access the different blurred mipmaps in textureLod()