Shader clipping at edges of sprite! Please Help!

Godot Version

v4.6.1

Question

How do I prevent my shader from clipping at the edges of the Sprite 2d like this?

shader_type canvas_item;

uniform float amount: hint_range(0,1);

uniform float wave_speed = 5.0; 
uniform float wave_freq = 1.0; 
uniform float wave_width = 2; 

uniform vec4 color: source_color;
uniform float transparency: hint_range(0.0, 1.0);

void fragment(){
	if (amount > 0.0) {
		vec2 wave_uv_offset;
		wave_uv_offset.x = (cos((TIME*wave_speed)+UV.x+UV.y*wave_freq*2.0)*wave_width*0.01) * amount;
		vec2 new_UV = vec2(UV.x+wave_uv_offset.x, UV.y);
		COLOR = texture(TEXTURE,new_UV);
		if (COLOR.a > 0.0) {
			vec4 mixed_color = mix(COLOR, color, amount);
			COLOR = mixed_color;
			COLOR.a -= 1.0 - clamp(transparency/amount, 0.0, 1.0);
		}
	}
}

Scale the UVs around the center prior to offseting them.

1 Like

Sorry, I am ridiculously new to shaders. Could you explain what that means and how to do it?

vec2 uv_scaled = (UV - 0.5) / scale + 0.5;
// proceed to offset the uvs here
2 Likes

What do I do with that variable? And what is the “scale” variable in that supposed to be?

You use it with your existing code instead of UV. Scale can be an uniform float you can adjust manually. The larger your deformation amplitude the more you’ll have to scale down.

1 Like

So after that I just scale up the sprite itself so its will be the same size?

Yeah, you can scale the sprite up if you need to.

1 Like

Thanks! That works now!