Skip to content

Commit

Permalink
fix prepass normal_mapping (#8978)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
robtfm authored Jun 29, 2023
1 parent ab3b429 commit 15445c9
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 1 deletion.
1 change: 1 addition & 0 deletions assets/shaders/array_texture.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions crates/bevy_pbr/src/render/pbr.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ fn fragment(
#ifdef VERTEX_UVS
uv,
#endif
view.mip_bias,
);
#endif

Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_pbr/src/render/pbr_functions.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ fn apply_normal_mapping(
#ifdef VERTEX_UVS
uv: vec2<f32>,
#endif
mip_bias: f32,
) -> vec3<f32> {
// 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
Expand All @@ -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<f32>(Nt.rg * 2.0 - 1.0, 0.0);
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_pbr/src/render/pbr_prepass.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 15445c9

Please sign in to comment.