Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | rubendw03 |
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.