Mesh culling issue with wgpu

I'm rendering a mesh using wgpu, but I can see the character's tail and arm when they should be rendered behind the rest of the mesh.

example:
https://imgur.com/dxQKh5H

I've made sure the alpha channel is fully opaque.

I can't think of any reason this could be occurring, other than some rendering rule I don't know know about or some wgpu setting I need to change.

Here's my shader code, let me know if it would be helpful to see the mesh or my rust rendering code, which is mostly copied from the wgpu tutorial.

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

struct VertexOutput {
    @builtin(position) clip_position: vec4<f32>,
    @location(0) vert_pos: vec3<f32>,
};

struct PushConstants {
    cursor_x: f32,
}

var<push_constant> constants: PushConstants;

@vertex
fn vs_main(
    model: VertexInput
) -> VertexOutput {
    var out: VertexOutput;
    
    let PI = 3.141592654;

    let c = cos(2.0*PI*constants.cursor_x);
    let s = sin(2.0*PI*constants.cursor_x);

    let x = model.position.x * c - model.position.z * s;
    let z = model.position.x * s + model.position.z * c;
    let y = model.position.y;

    out.clip_position = vec4<f32>(x / 1.5, y - 10.0, z / 100.0 + 0.5, 30.0);
    out.vert_pos = out.clip_position.xyz;
    return out;
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
    let color = vec3<f32>(1.0, 1.0, 1.0) * in.vert_pos.z;
    return vec4<f32>(color, 1.0);
}

Thanks!

This means you don't have a depth buffer working. If the tail is always in front then the tail must be at the end of your vertex/index list — the GPU always draws primitives in the order given, not from back to front. To hide things that are behind other things, you have to configure it to skip drawing things which are behind already-drawn things which is done with a depth buffer. You must:

  • create a depth texture
  • specify it as a depth_stencil_attachment to the render pass
  • configure depth_stencil in your render pipeline, with both writing and comparison enabled

If you are following learn-wgpu, then consult the "Depth Buffer" chapter to see the specific code changes.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.