Rendering issue in Godot: wave mesh parts incorrectly appearing in front

Godot Version

4.3.stable.arch_linux

Question

Issue Description:

I’m working with a wave mesh in Godot and encountering a rendering issue where parts of the mesh that should be behind are appearing in front. It seems like the depth buffer might not be functioning correctly, or there could be an issue with transparency or depth draw settings.

Steps to Reproduce:

  1. I created a subdivided mesh plane with 50 segments in both width and depth.

  2. I’m using a custom shader that modifies the Y coordinate with a sine function and calculates the normals for the plane. Here’s the shader code:

shader_type spatial;

uniform float height_scale = 0.1;
uniform vec4 water_color;

float sinWave(vec2 pos, vec2 direction, float waveLength, float speed) {
    float w = 2.0 * PI / waveLength;
    return height_scale * sin(dot(direction, pos) * w + (TIME * speed));
}

vec3 normalWave(vec2 pos, vec2 direction, float waveLength, float speed) {
    float w = 2.0 * PI / waveLength;

    float binormal = w * dot(direction, vec2(1,0)) * height_scale*cos(dot(direction,pos) * w + (TIME * speed));
	float tangente = w * dot(direction, vec2(0,1)) * height_scale*cos(dot(direction,pos) * w + (TIME * speed));

    return cross(vec3(1,0,binormal), vec3(0,1,tangente));
}

void vertex() {
    VERTEX.y = sinWave(vec2(VERTEX.x, VERTEX.z), vec2(1, 0.5), 1, 1);
    NORMAL = normalWave(vec2(VERTEX.x, VERTEX.z), vec2(1, 0.5), 1, 1);
}

void fragment() {
	// Definir a cor do material com base na intensidade da luz
    ALBEDO = water_color.rgb;
    ALPHA = 1.0;
}

  1. The problem is that parts of the wave mesh that should be hidden behind others are visible in front, causing a visual glitch.

I’m following concepts from the book GPU Gems: Programming Techniques, Tips, and Tricks for Real-Time Graphics. Could this issue be related to the way the normals are being calculated? Any advice on how to fix this or where I might be going wrong? Thanks in advance for the help!

Remove ALPHA = 1.0;

Transparent objects that overlap themselves have this issue. Might want to use a screen texture to do psuedo-transparency and refraction later on anyways.