Skip to content

Commit

Permalink
fix normal prepass (#8890)
Browse files Browse the repository at this point in the history
# Objective

- Fix broken normals when the NormalPrepass is enabled

## Solution

- Don't use the normal prepass for the world_normal
- Only loadthe normal prepass 
    - when msaa is disabled
- for opaque or alpha mask meshes and only for use it for N not
world_normal
  • Loading branch information
IceSentry authored Jun 21, 2023
1 parent 0294bb1 commit 72b4aac
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
10 changes: 6 additions & 4 deletions crates/bevy_pbr/src/render/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,10 +682,6 @@ impl SpecializedMeshPipeline for MeshPipeline {
let mut shader_defs = Vec::new();
let mut vertex_attributes = Vec::new();

if key.contains(MeshPipelineKey::NORMAL_PREPASS) {
shader_defs.push("LOAD_PREPASS_NORMALS".into());
}

if layout.contains(Mesh::ATTRIBUTE_POSITION) {
shader_defs.push("VERTEX_POSITIONS".into());
vertex_attributes.push(Mesh::ATTRIBUTE_POSITION.at_shader_location(0));
Expand Down Expand Up @@ -747,6 +743,7 @@ impl SpecializedMeshPipeline for MeshPipeline {

let (label, blend, depth_write_enabled);
let pass = key.intersection(MeshPipelineKey::BLEND_RESERVED_BITS);
let mut is_opaque = false;
if pass == MeshPipelineKey::BLEND_ALPHA {
label = "alpha_blend_mesh_pipeline".into();
blend = Some(BlendState::ALPHA_BLENDING);
Expand Down Expand Up @@ -783,6 +780,11 @@ impl SpecializedMeshPipeline for MeshPipeline {
// the current fragment value in the output and the depth is written to the
// depth buffer
depth_write_enabled = true;
is_opaque = true;
}

if key.contains(MeshPipelineKey::NORMAL_PREPASS) && key.msaa_samples() == 1 && is_opaque {
shader_defs.push("LOAD_PREPASS_NORMALS".into());
}

if key.contains(MeshPipelineKey::TONEMAP_IN_SHADER) {
Expand Down
9 changes: 5 additions & 4 deletions crates/bevy_pbr/src/render/pbr.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,17 @@ fn fragment(in: FragmentInput) -> @location(0) vec4<f32> {
pbr_input.frag_coord = in.frag_coord;
pbr_input.world_position = in.world_position;

#ifdef LOAD_PREPASS_NORMALS
pbr_input.world_normal = prepass_normal(in.frag_coord, 0u);
#else // LOAD_PREPASS_NORMALS
pbr_input.world_normal = prepare_world_normal(
in.world_normal,
(material.flags & STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u,
in.is_front,
);
#endif // LOAD_PREPASS_NORMALS

pbr_input.is_orthographic = is_orthographic;

#ifdef LOAD_PREPASS_NORMALS
pbr_input.N = prepass_normal(in.frag_coord, 0u);
#else
pbr_input.N = apply_normal_mapping(
material.flags,
pbr_input.world_normal,
Expand All @@ -133,6 +132,8 @@ fn fragment(in: FragmentInput) -> @location(0) vec4<f32> {
uv,
#endif
);
#endif

pbr_input.V = V;
pbr_input.occlusion = occlusion;

Expand Down

0 comments on commit 72b4aac

Please sign in to comment.