Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions crates/bevy_pbr/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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],
}
Expand Down Expand Up @@ -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())
Expand All @@ -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],
};
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_pbr/src/render/mesh_view_types.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ struct Lights {
cluster_factors: vec4<f32>,
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<RectLight, #{MAX_RECT_LIGHTS}u>,
};

const AMBIENT_LIGHT_FLAGS_AFFECTS_LIGHTMAPPED_MESHES_BIT: u32 = 1u << 0u;

struct Fog {
base_color: vec4<f32>,
directional_light_color: vec4<f32>,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/render/pbr_functions.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading