How to get node 2D screenposition

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rubendw03


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_type canvas_item;

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

void fragment() {
	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.

:bust_in_silhouette: Reply From: exuin

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

Thanks! I’ll try it out

rubendw03 | 2021-03-31 06:56