rustvertex-shaderwgpu-rswgsl

WGpu z-coordinate ignored


I am using wgpu crate in rust to render triangles in layers. The problem is that the z coordinate of the vertices seems to be ignored. The shader returns the z coordinate in the output position, and there is no further transformation applied to it. The result however is that triangles that are rendered last (last positions in the vertex buffer) are always drawn on top of the previous ones, regardless of their z coordinate.

struct VertexInput {
    @location(0) position: vec3<f32>,
}

struct VertexOutput {
    @builtin(position) clip_position: vec4<f32>,
}

@vertex
fn vs_main(model: VertexInput) -> VertexOutput {
    var out: VertexOutput;
    out.clip_position = vec4<f32>(model.position[0], model.position[1], model.position[2], 1.0);
    return out;
}

Simplest example is having 8 vertices: the first 4 represent a plane with z coordinate 0.1, the other 4 represent a plane with z coordinate 0 (with half the area of the first). In this case the second plane is rendered on top of the first one despite the z coordinate. Setting the z to negative doesn't render the vertex at all.

What did I get wrong?


Solution

  • There are many ways to handle layering of objects but the simplest and default for GPU-accelerated graphics is to attach a depth buffer and configure depth testing.

    Does that respect transparent textures?

    Displaying transparent objects in general is hard in the triangle-rasterization world. (It's trivial in a raytracer.) You cannot just make it work by doing things properly; the best practical solution depends on the details of your problem.