From 15445c990e42486ba33faca7f36e2ad04943115f Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Thu, 29 Jun 2023 01:28:34 +0100 Subject: [PATCH] fix prepass normal_mapping (#8978) # Objective #5703 caused the normal prepass to fail as the prepass uses `pbr_functions::apply_normal_mapping`, which uses `mesh_view_bindings::view` to determine mip bias, which conflicts with `prepass_bindings::view`. ## Solution pass the mip bias to the `apply_normal_mapping` function explicitly. --- assets/shaders/array_texture.wgsl | 1 + crates/bevy_pbr/src/render/pbr.wgsl | 1 + crates/bevy_pbr/src/render/pbr_functions.wgsl | 3 ++- crates/bevy_pbr/src/render/pbr_prepass.wgsl | 1 + 4 files changed, 5 insertions(+), 1 deletion(-) diff --git a/assets/shaders/array_texture.wgsl b/assets/shaders/array_texture.wgsl index 17c714f420742..4650491724d23 100644 --- a/assets/shaders/array_texture.wgsl +++ b/assets/shaders/array_texture.wgsl @@ -44,6 +44,7 @@ fn fragment( #endif #endif mesh.uv, + view.mip_bias, ); pbr_input.V = fns::calculate_view(mesh.world_position, pbr_input.is_orthographic); diff --git a/crates/bevy_pbr/src/render/pbr.wgsl b/crates/bevy_pbr/src/render/pbr.wgsl index 245a822c09e89..cdb9586d435d2 100644 --- a/crates/bevy_pbr/src/render/pbr.wgsl +++ b/crates/bevy_pbr/src/render/pbr.wgsl @@ -131,6 +131,7 @@ fn fragment( #ifdef VERTEX_UVS uv, #endif + view.mip_bias, ); #endif diff --git a/crates/bevy_pbr/src/render/pbr_functions.wgsl b/crates/bevy_pbr/src/render/pbr_functions.wgsl index 393ff2728dffa..4e9fce0242e40 100644 --- a/crates/bevy_pbr/src/render/pbr_functions.wgsl +++ b/crates/bevy_pbr/src/render/pbr_functions.wgsl @@ -70,6 +70,7 @@ fn apply_normal_mapping( #ifdef VERTEX_UVS uv: vec2, #endif + mip_bias: f32, ) -> vec3 { // NOTE: The mikktspace method of normal mapping explicitly requires that the world normal NOT // be re-normalized in the fragment shader. This is primarily to match the way mikktspace @@ -94,7 +95,7 @@ fn apply_normal_mapping( #ifdef VERTEX_UVS #ifdef STANDARDMATERIAL_NORMAL_MAP // Nt is the tangent-space normal. - var Nt = textureSampleBias(pbr_bindings::normal_map_texture, pbr_bindings::normal_map_sampler, uv, view_bindings::view.mip_bias).rgb; + var Nt = textureSampleBias(pbr_bindings::normal_map_texture, pbr_bindings::normal_map_sampler, uv, mip_bias).rgb; if (standard_material_flags & pbr_types::STANDARD_MATERIAL_FLAGS_TWO_COMPONENT_NORMAL_MAP) != 0u { // Only use the xy components and derive z for 2-component normal maps. Nt = vec3(Nt.rg * 2.0 - 1.0, 0.0); diff --git a/crates/bevy_pbr/src/render/pbr_prepass.wgsl b/crates/bevy_pbr/src/render/pbr_prepass.wgsl index b5103e73a9ab1..542e540309a5c 100644 --- a/crates/bevy_pbr/src/render/pbr_prepass.wgsl +++ b/crates/bevy_pbr/src/render/pbr_prepass.wgsl @@ -107,6 +107,7 @@ fn fragment(in: FragmentInput) -> FragmentOutput { #ifdef VERTEX_UVS in.uv, #endif // VERTEX_UVS + bevy_pbr::prepass_bindings::view.mip_bias, ); out.normal = vec4(normal * 0.5 + vec3(0.5), 1.0);