Godot Version
4.2.2
Question
Hi, I’ve been trying to make a forcefield-type effect for a gizmo, of all things. I absolutely need this kind of effect where a mesh highlights whenever it is near another mesh to build levels (don’t ask, I won’t be able to explain), and I know this is possible in Unity, so I’ve been trying to implement this in Godot in a shader.
A starting point I tried was getting the distance from a fragment to a point in the DEPTH_TEXTURE point in worldspace, and mix that with a fresnel and other code that isn’t relevant to the problem in question. To do that, I’ve been following the Advanced Post-processing page in the Godot documentation. However, when I tried its screenspace to worldspace conversion, the output seemed strange.
I was expecting that whatever I saw through the quad would be the coordinates represented in RGB, but instead I see something that keeps changing whenever I move the camera.
It seems to output something between the camera position, mesh position and depth_texture position. Positive xz is wither black or white instead of magenta, depending if the camera is in orthogonal or perspective mode.
I was expecting something like the following (achieved through a different method that outputs the coordinates of the mesh the shader is attached to).
The shader code is pretty much exactly as is in the page mentioned, but here’s the pseudocode anyways (I don’t know how to do code blocks in this site):
shader_type spatial;
render_mode unshaded, cull_disabled;
uniform sampler2D depth_tex : hint_depth_texture;
void vertex() {
POSITION = vec4(VERTEX, 1.0);
}
void fragment() {
float depth = texture(depth_tex, SCREEN_UV).r;
vec3 ndc = vec3(SCREEN_UV, depth) * 2.0 - 1.0;
vec4 world = INV_VIEW_MATRIX * INV_PROJECTION_MATRIX * vec4(ndc, 1.0);
vec3 behind_pos = world.xyz / world.w;
ALBEDO = vec3(clamp(worldpos, 0.0, 1.0));
ALPHA = 1.0;
}
I remember using the 3.x version of this code and it worked as expected. Did something change in 4.0 that affected the behavior here?
Either way, I tried posting this on reddit, but the new account doesn’t have enough karma, but I am truly desperate, even if I realize this method won’t really work for what I want. I could still really use a replacement, and this approach is a good start towards that.

