#DepthPrepass replaces backfacing sides with clear color

16 messages · Page 1 of 1 (latest)

frigid jacinth
#

I've added DepthPrepass to my camera and suddenly all the backfacing sides of my voxels have the clear color instead of just being transparent. This isn't great because if the camera every clips through an object it looks much worse now. Is there a way to fix this? I don't know what's causing it. The land shader material for the voxels hasn't changed, and I'm using using the built in PBR prepass shader.

Any advice appreciated! I'm guessing the default PBR shader is using the clear color somehow, but I'm not sure if there is a way I can override that and still have access to the depth buffer.

kind grove
#

Is the voxel material using alpha_mode blend by any chance?

frigid jacinth
#
    fn vertex_shader() -> ShaderRef {
        "shaders/land.wgsl".into()
    }

    fn fragment_shader() -> ShaderRef {
        "shaders/land.wgsl".into()
    }

    fn prepass_fragment_shader() -> ShaderRef {
        PBR_PREPASS_SHADER_HANDLE.typed().into()
    }

    fn alpha_mode(&self) -> AlphaMode {
        self.alpha_mode
    }

    fn specialize(
        _pipeline: &MaterialPipeline<Self>,
        descriptor: &mut RenderPipelineDescriptor,
        layout: &MeshVertexBufferLayout,
        _key: MaterialPipelineKey<Self>,
    ) -> Result<(), SpecializedMeshPipelineError> {
        let vertex_layout = layout.get_layout(&[
            Mesh::ATTRIBUTE_POSITION.at_shader_location(0),
            Mesh::ATTRIBUTE_NORMAL.at_shader_location(1),
            Mesh::ATTRIBUTE_UV_0.at_shader_location(2),
            Mesh::ATTRIBUTE_TANGENT.at_shader_location(3),
            Mesh::ATTRIBUTE_COLOR.at_shader_location(4),
            ATTRIBUTE_TEXTURE_INDEX.at_shader_location(7),
            ATTRIBUTE_NEIGHBOR_INDICES.at_shader_location(8),
        ])?;
        descriptor.vertex.buffers = vec![vertex_layout];
        Ok(())
    }
}

kind grove
#

what happens if you use the default prepass fragment instead of the pbr prepass?

frigid jacinth
#

Same exact thing, I only changed it over in an attempt to fix this issue.

#

If I do the following:

let material = MaterialPlugin::<LandMaterial>::default();
material.prepass_enabled = false;

The clear color goes away again so it's definitely caused by enabling the prepass on the land material. But I can't disable since I definitely want to include it in the depth buffer since things like water rely on the distance from the land.

#

But I only want the depth buffer, I don't want the fragment shader in the prepass to change the color of my voxels.

frigid jacinth
kind grove
#

can you show me land.wgsl? I'm really not seeing what could be causing this

#

I guess maybe the custom vertex layout? But I wouldn't expect it to break like that

frigid jacinth
# kind grove can you show me land.wgsl? I'm really not seeing what could be causing this

Sure, it's basically the following. There's a bunch of logic for texture blending, etc. but that doesn't really change the core pieces:

@vertex
fn vertex(vertex: Vertex) -> VertexOutput {
    var out: VertexOutput;
    var model = mesh.model;
    out.world_normal = mesh_functions::mesh_normal_local_to_world(vertex.normal);
    out.world_position = mesh_functions::mesh_position_local_to_world(model, vec4<f32>(vertex.position, 1.0));
    out.uv = vertex.uv;
    out.color = vertex.color;
    out.clip_position = mesh_functions::mesh_position_world_to_clip(out.world_position);
    out.texture_idx = vertex.texture_idx;

    return out;
}

@fragment
fn fragment(in: FragmentInput) -> @location(0) vec4<f32> {
    var pbr_input: pbr_functions::PbrInput = pbr_functions::pbr_input_new();
    pbr_input.material.perceptual_roughness = 0.9;
    pbr_input.material.reflectance = 0.1;
    pbr_input.material.base_color = textureSample(land_array_texture, land_texture_sampler, in.uv, i32(in.texture_idx));

    pbr_input.frag_coord = in.frag_coord;
    pbr_input.world_position = in.world_position;
    pbr_input.world_normal = pbr_functions::prepare_world_normal(
        in.world_normal,
        false,
        in.is_front,
    );

    pbr_input.is_orthographic = view.projection[3].w == 1.0;
    pbr_input.N = pbr_functions::apply_normal_mapping(
        pbr_input.material.flags,
        pbr_input.world_normal,
        in.uv,
        view.mip_bias,
    );

    pbr_input.V = pbr_functions::calculate_view(in.world_position, pbr_input.is_orthographic);

    return pbr_functions::pbr(pbr_input);
}
#
struct Vertex {
    @builtin(instance_index) instance_index: u32,
#ifdef VERTEX_POSITIONS
    @location(0) position: vec3<f32>,
#endif
#ifdef VERTEX_NORMALS
    @location(1) normal: vec3<f32>,
#endif
#ifdef VERTEX_UVS
    @location(2) uv: vec2<f32>,
#endif
#ifdef VERTEX_TANGENTS
    @location(3) tangent: vec4<f32>,
#endif
#ifdef VERTEX_COLORS
    @location(4) color: vec4<f32>,
#endif
    @location(7) texture_idx: u32,
    @location(8) neighbor_indices: vec4<u32>,
};

struct VertexOutput {
    @builtin(position) clip_position: vec4<f32>,
    @location(0) world_position: vec4<f32>,
    @location(1) world_normal: vec3<f32>,
#ifdef VERTEX_UVS
    @location(2) uv: vec2<f32>,
#endif
#ifdef VERTEX_TANGENTS
    @location(3) world_tangent: vec4<f32>,
#endif
#ifdef VERTEX_COLORS
    @location(4) color: vec4<f32>,
#endif
    @location(5) texture_idx: u32,
    @location(6) neighbor_indices: vec4<u32>,
};

struct FragmentInput {
    @builtin(front_facing) is_front: bool,
    @builtin(position) frag_coord: vec4<f32>,
    @location(0) world_position: vec4<f32>,
    @location(1) world_normal: vec3<f32>,
#ifdef VERTEX_UVS
    @location(2) uv: vec2<f32>,
#endif
#ifdef VERTEX_TANGENTS
    @location(3) world_tangent: vec4<f32>,
#endif
#ifdef VERTEX_COLORS
    @location(4) color: vec4<f32>,
#endif
    @location(5) texture_idx: u32,
    @location(6) neighbor_indices: vec4<u32>,
};
kind grove
#

sorry, I don't have more time right now to look into it. This is very weird but I'm not seeing anything wrong on your side at least

#

could you try maybe making a minimal reproduction that I could run?

#

or just share your repo if it's open source