0 votes

Hai

I'm making a 2D topdown shooter and I just implemented a shader to initiate a shockwave when the player shoots.
Now, I need the shockwave's middle point to be the position of the player on the screen.
Since the shader's center position should be between (0.0, 0.0) and (1.0, 1.0) I tried this:

var blastwave = $Wave/Blastwave.material
var playercanvasposition = get_global_transform_with_canvas().origin / get_viewport_rect().size.x
blastwave.set_shader_param("center", playercanvasposition)

(get the players origin on the screen and then dividing it with the viewport size)

Shader:

shader_type canvas_item;

uniform vec2 center;
uniform float force;
uniform float size;
uniform float thickness;

void fragment() {
    float ratio = SCREEN_PIXEL_SIZE.x / SCREEN_PIXEL_SIZE.y;
    vec2 scaledUV = (SCREEN_UV - vec2(0.5, 0.0)) / vec2(ratio, 1.0) + vec2(0.5, 0.0);
    float mask = (1.0 - smoothstep(size - 0.1, size, length(scaledUV - center))) * 
            smoothstep(size - thickness - 0.1, size - thickness, length(scaledUV - center));
    vec2 disp = normalize(scaledUV - center) * force * mask;
    COLOR = texture(SCREEN_TEXTURE, SCREEN_UV - disp) * mask * force * 50.0;
}

Sadly, this isn't working. Does anyone have any idea of what I'm doing wrong?
Thanks in advance.

in Engine by (44 points)

1 Answer

+1 vote

I tried implementing the solution here a while ago and it worked

by (7,190 points)

Thanks! I'll try it out

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.