From 72b4aacf86008a8851b30e35cb6adc58976a37ff Mon Sep 17 00:00:00 2001 From: IceSentry Date: Wed, 21 Jun 2023 13:25:20 -0400 Subject: [PATCH] fix normal prepass (#8890) # 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 --- crates/bevy_pbr/src/render/mesh.rs | 10 ++++++---- crates/bevy_pbr/src/render/pbr.wgsl | 9 +++++---- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/crates/bevy_pbr/src/render/mesh.rs b/crates/bevy_pbr/src/render/mesh.rs index 89f95932eb..20cb2a1986 100644 --- a/crates/bevy_pbr/src/render/mesh.rs +++ b/crates/bevy_pbr/src/render/mesh.rs @@ -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)); @@ -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); @@ -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) { diff --git a/crates/bevy_pbr/src/render/pbr.wgsl b/crates/bevy_pbr/src/render/pbr.wgsl index e342823b8f..eb84895aa6 100644 --- a/crates/bevy_pbr/src/render/pbr.wgsl +++ b/crates/bevy_pbr/src/render/pbr.wgsl @@ -109,18 +109,17 @@ fn fragment(in: FragmentInput) -> @location(0) vec4 { 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, @@ -133,6 +132,8 @@ fn fragment(in: FragmentInput) -> @location(0) vec4 { uv, #endif ); +#endif + pbr_input.V = V; pbr_input.occlusion = occlusion;