From 6f8848a6c222f3c2f12f0b0ea5daed5c1e1ce819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Wed, 1 Nov 2023 17:40:25 +0100 Subject: [PATCH] double sided normals: fix apply_normal_mapping calls (#10330) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Objective - After #10326, examples `array_texture`, `ssao` and `shader_prepass` don't render correctly ``` error: failed to build a valid final module: Entry point fragment at Fragment is invalid ┌─ crates/bevy_pbr/src/render/pbr_prepass.wgsl:26:22 │ 26 │ let normal = evy_pbr::pbr_functions::31mapply_normal_mapping( │ ╭──────────────────────^ 27 │ │ bevy_pbr::pbr_bindings::material.flags, 28 │ │ world_normal, 29 │ │ · │ 36 │ │ 37 │ │ bevy_pbr::mesh_view_bindings::view.mip_bias, │ ╰───────────────────────────────────────────────────────────────────────────────────────^ invalid function call │ = Call to [9] is invalid = Requires 6 arguments, but 4 are provided ``` ## Solution - fix `apply_normal_mapping` calls --- assets/shaders/array_texture.wgsl | 6 +++++- crates/bevy_pbr/src/render/pbr_prepass.wgsl | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/assets/shaders/array_texture.wgsl b/assets/shaders/array_texture.wgsl index 24eaa1b549..161efadb18 100644 --- a/assets/shaders/array_texture.wgsl +++ b/assets/shaders/array_texture.wgsl @@ -25,11 +25,13 @@ fn fragment( pbr_input.material.base_color = pbr_input.material.base_color * mesh.color; #endif + let double_sided = (pbr_input.material.flags & STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u; + pbr_input.frag_coord = mesh.position; pbr_input.world_position = mesh.world_position; pbr_input.world_normal = fns::prepare_world_normal( mesh.world_normal, - (pbr_input.material.flags & STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u, + double_sided, is_front, ); @@ -38,6 +40,8 @@ fn fragment( pbr_input.N = fns::apply_normal_mapping( pbr_input.material.flags, mesh.world_normal, + double_sided, + is_front, #ifdef VERTEX_TANGENTS #ifdef STANDARDMATERIAL_NORMAL_MAP mesh.world_tangent, diff --git a/crates/bevy_pbr/src/render/pbr_prepass.wgsl b/crates/bevy_pbr/src/render/pbr_prepass.wgsl index 5af33bfc3e..e17826a3f4 100644 --- a/crates/bevy_pbr/src/render/pbr_prepass.wgsl +++ b/crates/bevy_pbr/src/render/pbr_prepass.wgsl @@ -24,15 +24,19 @@ fn fragment( #ifdef NORMAL_PREPASS // NOTE: Unlit bit not set means == 0 is true, so the true case is if lit if (material.flags & pbr_types::STANDARD_MATERIAL_FLAGS_UNLIT_BIT) == 0u { + let double_sided = (material.flags & pbr_types::STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u; + let world_normal = pbr_functions::prepare_world_normal( in.world_normal, - (material.flags & pbr_types::STANDARD_MATERIAL_FLAGS_DOUBLE_SIDED_BIT) != 0u, + double_sided, is_front, ); let normal = pbr_functions::apply_normal_mapping( material.flags, world_normal, + double_sided, + is_front, #ifdef VERTEX_TANGENTS #ifdef STANDARDMATERIAL_NORMAL_MAP in.world_tangent,