From 5ea5ec751810c7b82bc03d03d8d6c7eb041d1dca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kurt=20K=C3=BChnert?= Date: Mon, 20 Feb 2023 00:16:46 +0000 Subject: [PATCH] Remove dependency on the mesh struct in the pbr function (#7597) # Objective Currently, it is quite awkward to use the `pbr` function in a custom shader without binding a mesh bind group. This is because the `pbr` function depends on the `MESH_FLAGS_SHADOW_RECEIVER_BIT` flag. ## Solution I have removed this dependency by adding the flag as a parameter to the `PbrInput` struct. I am not sure if this is the ideal solution since the mesh flag indicates both `MESH_FLAGS_SIGN_DETERMINANT_MODEL_3X3_BIT` and `MESH_FLAGS_SHADOW_RECEIVER_BIT`. The former seems to be unrelated to PBR. Maybe the flag should be split. --- crates/bevy_pbr/src/render/pbr.wgsl | 2 ++ crates/bevy_pbr/src/render/pbr_functions.wgsl | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/bevy_pbr/src/render/pbr.wgsl b/crates/bevy_pbr/src/render/pbr.wgsl index b455d7bb6a520..b87208edca6b3 100644 --- a/crates/bevy_pbr/src/render/pbr.wgsl +++ b/crates/bevy_pbr/src/render/pbr.wgsl @@ -92,6 +92,8 @@ fn fragment(in: FragmentInput) -> @location(0) vec4 { pbr_input.V = calculate_view(in.world_position, pbr_input.is_orthographic); pbr_input.occlusion = occlusion; + pbr_input.flags = mesh.flags; + output_color = pbr(pbr_input); } else { output_color = alpha_discard(material, output_color); diff --git a/crates/bevy_pbr/src/render/pbr_functions.wgsl b/crates/bevy_pbr/src/render/pbr_functions.wgsl index 852b84230d825..5cb140b6f412f 100644 --- a/crates/bevy_pbr/src/render/pbr_functions.wgsl +++ b/crates/bevy_pbr/src/render/pbr_functions.wgsl @@ -134,6 +134,7 @@ struct PbrInput { // view world position V: vec3, is_orthographic: bool, + flags: u32, }; // Creates a PbrInput with default values @@ -152,6 +153,8 @@ fn pbr_input_new() -> PbrInput { pbr_input.N = vec3(0.0, 0.0, 1.0); pbr_input.V = vec3(1.0, 0.0, 0.0); + pbr_input.flags = 0u; + return pbr_input; } @@ -203,7 +206,7 @@ fn pbr( for (var i: u32 = offset_and_counts[0]; i < offset_and_counts[0] + offset_and_counts[1]; i = i + 1u) { let light_id = get_light_id(i); var shadow: f32 = 1.0; - if ((mesh.flags & MESH_FLAGS_SHADOW_RECEIVER_BIT) != 0u + if ((in.flags & MESH_FLAGS_SHADOW_RECEIVER_BIT) != 0u && (point_lights.data[light_id].flags & POINT_LIGHT_FLAGS_SHADOWS_ENABLED_BIT) != 0u) { shadow = fetch_point_shadow(light_id, in.world_position, in.world_normal); } @@ -215,7 +218,7 @@ fn pbr( for (var i: u32 = offset_and_counts[0] + offset_and_counts[1]; i < offset_and_counts[0] + offset_and_counts[1] + offset_and_counts[2]; i = i + 1u) { let light_id = get_light_id(i); var shadow: f32 = 1.0; - if ((mesh.flags & MESH_FLAGS_SHADOW_RECEIVER_BIT) != 0u + if ((in.flags & MESH_FLAGS_SHADOW_RECEIVER_BIT) != 0u && (point_lights.data[light_id].flags & POINT_LIGHT_FLAGS_SHADOWS_ENABLED_BIT) != 0u) { shadow = fetch_spot_shadow(light_id, in.world_position, in.world_normal); } @@ -227,7 +230,7 @@ fn pbr( let n_directional_lights = lights.n_directional_lights; for (var i: u32 = 0u; i < n_directional_lights; i = i + 1u) { var shadow: f32 = 1.0; - if ((mesh.flags & MESH_FLAGS_SHADOW_RECEIVER_BIT) != 0u + if ((in.flags & MESH_FLAGS_SHADOW_RECEIVER_BIT) != 0u && (lights.directional_lights[i].flags & DIRECTIONAL_LIGHT_FLAGS_SHADOWS_ENABLED_BIT) != 0u) { shadow = fetch_directional_shadow(i, in.world_position, in.world_normal, view_z); }