diff --git a/crates/bevy_pbr/src/render/light.rs b/crates/bevy_pbr/src/render/light.rs index fa35677f0f558..3d2c68dfc8f87 100644 --- a/crates/bevy_pbr/src/render/light.rs +++ b/crates/bevy_pbr/src/render/light.rs @@ -192,6 +192,16 @@ pub struct GpuRectLight { range: f32, } +// NOTE: These must match the bit flags in bevy_pbr/src/render/mesh_view_types.wgsl! +bitflags::bitflags! { + #[repr(transparent)] + struct AmbientLightFlags: u32 { + const AFFECTS_LIGHTMAPPED_MESHES = 1 << 0; + const NONE = 0; + const UNINITIALIZED = 0xFFFF; + } +} + #[derive(Copy, Clone, Debug, ShaderType)] pub struct GpuLights { directional_lights: [GpuDirectionalLight; MAX_DIRECTIONAL_LIGHTS], @@ -205,7 +215,7 @@ pub struct GpuLights { n_directional_lights: u32, // offset from spot light's light index to spot light's shadow map index spot_light_shadowmap_offset: i32, - ambient_light_affects_lightmapped_meshes: u32, + ambient_light_flags: u32, n_rect_lights: u32, rect_lights: [GpuRectLight; MAX_RECT_LIGHTS], } @@ -1879,6 +1889,12 @@ pub fn prepare_lights( num_directional_cascades_enabled_for_this_view += num_cascades; } + let mut ambient_light_flags = AmbientLightFlags::NONE; + + if ambient_light.affects_lightmapped_meshes { + ambient_light_flags |= AmbientLightFlags::AFFECTS_LIGHTMAPPED_MESHES; + } + let mut gpu_lights = GpuLights { directional_lights: gpu_directional_lights, ambient_color: Vec4::from_slice(&LinearRgba::from(ambient_light.color).to_f32_array()) @@ -1896,8 +1912,7 @@ pub fn prepare_lights( // index to shadow map index, we need to subtract point light count and add directional shadowmap count. spot_light_shadowmap_offset: num_directional_cascades_enabled as i32 - point_light_count as i32, - ambient_light_affects_lightmapped_meshes: ambient_light.affects_lightmapped_meshes - as u32, + ambient_light_flags: ambient_light_flags.bits(), n_rect_lights: 0, rect_lights: [GpuRectLight::default(); MAX_RECT_LIGHTS], }; diff --git a/crates/bevy_pbr/src/render/mesh_view_types.wgsl b/crates/bevy_pbr/src/render/mesh_view_types.wgsl index 9e5a7f132f975..a93a9f4da2c61 100644 --- a/crates/bevy_pbr/src/render/mesh_view_types.wgsl +++ b/crates/bevy_pbr/src/render/mesh_view_types.wgsl @@ -81,11 +81,13 @@ struct Lights { cluster_factors: vec4, n_directional_lights: u32, spot_light_shadowmap_offset: i32, - ambient_light_affects_lightmapped_meshes: u32, + ambient_light_flags: u32, n_rect_lights: u32, rect_lights: array, }; +const AMBIENT_LIGHT_FLAGS_AFFECTS_LIGHTMAPPED_MESHES_BIT: u32 = 1u << 0u; + struct Fog { base_color: vec4, directional_light_color: vec4, diff --git a/crates/bevy_pbr/src/render/pbr_functions.wgsl b/crates/bevy_pbr/src/render/pbr_functions.wgsl index ed367181dc33a..9a8bdaba75aee 100644 --- a/crates/bevy_pbr/src/render/pbr_functions.wgsl +++ b/crates/bevy_pbr/src/render/pbr_functions.wgsl @@ -754,7 +754,7 @@ fn apply_pbr_lighting( // If we are lightmapped, disable the ambient contribution if requested. // This is to avoid double-counting ambient light. (It might be part of the lightmap) #ifdef LIGHTMAP - let enable_ambient = view_bindings::lights.ambient_light_affects_lightmapped_meshes != 0u; + let enable_ambient = (view_bindings::lights.ambient_light_flags & mesh_view_types::AMBIENT_LIGHT_FLAGS_AFFECTS_LIGHTMAPPED_MESHES_BIT) != 0u; #else // LIGHTMAP let enable_ambient = true; #endif // LIGHTMAP