Godot Version
v4.3.stable.steam [77dcf97d8]
Question
In trying to create a water shader, I accessed the depth texture to try and create an edge detection system. While the system works, it also causes faces to render in the wrong order, faces behind rendering in front. It does not matter if I use the depth texture, simply accessing it on a displaced mesh seems to cause this issue.
Here is a simplified version of the shader:
shader_type spatial;
varying vec3 world_position;
uniform sampler2D DEPTH_TEXTURE : hint_depth_texture;
uniform sampler2D noise;
void vertex() {
world_position = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;
VERTEX.y = texture(noise, world_position.xz).x;
}
void fragment() {
float depth = texture(DEPTH_TEXTURE, SCREEN_UV).x;
ALBEDO = world_position;
}
//void light() {
// Called for every pixel for every light affecting the material.
// Uncomment to replace the default light processing function with this one.
//}
The results look like this
It seems depending on the viewing angle, especially when viewing towards the object’s local -Z direction, the faces get rendered incorrectly. The weirdest part is that I don’t use the depth texture anywhere but in defining a variable, and that variable does not get used in the fragment shader, yet the bug still occurs.