Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix normal prepass #8890

Merged
merged 5 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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