Skip to content

Commit 5ea5ec7

Browse files
committed
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.
1 parent 03575ae commit 5ea5ec7

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

crates/bevy_pbr/src/render/pbr.wgsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ fn fragment(in: FragmentInput) -> @location(0) vec4<f32> {
9292
pbr_input.V = calculate_view(in.world_position, pbr_input.is_orthographic);
9393
pbr_input.occlusion = occlusion;
9494

95+
pbr_input.flags = mesh.flags;
96+
9597
output_color = pbr(pbr_input);
9698
} else {
9799
output_color = alpha_discard(material, output_color);

crates/bevy_pbr/src/render/pbr_functions.wgsl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ struct PbrInput {
134134
// view world position
135135
V: vec3<f32>,
136136
is_orthographic: bool,
137+
flags: u32,
137138
};
138139

139140
// Creates a PbrInput with default values
@@ -152,6 +153,8 @@ fn pbr_input_new() -> PbrInput {
152153
pbr_input.N = vec3<f32>(0.0, 0.0, 1.0);
153154
pbr_input.V = vec3<f32>(1.0, 0.0, 0.0);
154155

156+
pbr_input.flags = 0u;
157+
155158
return pbr_input;
156159
}
157160

@@ -203,7 +206,7 @@ fn pbr(
203206
for (var i: u32 = offset_and_counts[0]; i < offset_and_counts[0] + offset_and_counts[1]; i = i + 1u) {
204207
let light_id = get_light_id(i);
205208
var shadow: f32 = 1.0;
206-
if ((mesh.flags & MESH_FLAGS_SHADOW_RECEIVER_BIT) != 0u
209+
if ((in.flags & MESH_FLAGS_SHADOW_RECEIVER_BIT) != 0u
207210
&& (point_lights.data[light_id].flags & POINT_LIGHT_FLAGS_SHADOWS_ENABLED_BIT) != 0u) {
208211
shadow = fetch_point_shadow(light_id, in.world_position, in.world_normal);
209212
}
@@ -215,7 +218,7 @@ fn pbr(
215218
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) {
216219
let light_id = get_light_id(i);
217220
var shadow: f32 = 1.0;
218-
if ((mesh.flags & MESH_FLAGS_SHADOW_RECEIVER_BIT) != 0u
221+
if ((in.flags & MESH_FLAGS_SHADOW_RECEIVER_BIT) != 0u
219222
&& (point_lights.data[light_id].flags & POINT_LIGHT_FLAGS_SHADOWS_ENABLED_BIT) != 0u) {
220223
shadow = fetch_spot_shadow(light_id, in.world_position, in.world_normal);
221224
}
@@ -227,7 +230,7 @@ fn pbr(
227230
let n_directional_lights = lights.n_directional_lights;
228231
for (var i: u32 = 0u; i < n_directional_lights; i = i + 1u) {
229232
var shadow: f32 = 1.0;
230-
if ((mesh.flags & MESH_FLAGS_SHADOW_RECEIVER_BIT) != 0u
233+
if ((in.flags & MESH_FLAGS_SHADOW_RECEIVER_BIT) != 0u
231234
&& (lights.directional_lights[i].flags & DIRECTIONAL_LIGHT_FLAGS_SHADOWS_ENABLED_BIT) != 0u) {
232235
shadow = fetch_directional_shadow(i, in.world_position, in.world_normal, view_z);
233236
}

0 commit comments

Comments
 (0)