Skip to content

Commit

Permalink
Allow user defined directional light cascades.
Browse files Browse the repository at this point in the history
  • Loading branch information
exjam authored and HackerFoo committed Apr 25, 2023
1 parent 8bc9b80 commit 8ff18b9
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions crates/bevy_pbr/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ pub struct CascadeShadowConfig {
pub overlap_proportion: f32,
/// The (positive) distance to the near boundary of the first cascade.
pub minimum_distance: f32,
/// Whether the cascades are user controlled
pub manual_cascades: bool,
}

impl Default for CascadeShadowConfig {
Expand Down Expand Up @@ -347,6 +349,7 @@ impl CascadeShadowConfigBuilder {
),
overlap_proportion: self.overlap_proportion,
minimum_distance: self.minimum_distance,
manual_cascades: false,
}
}
}
Expand Down Expand Up @@ -384,21 +387,21 @@ impl From<CascadeShadowConfigBuilder> for CascadeShadowConfig {
#[reflect(Component)]
pub struct Cascades {
/// Map from a view to the configuration of each of its [`Cascade`]s.
pub(crate) cascades: HashMap<Entity, Vec<Cascade>>,
pub cascades: HashMap<Entity, Vec<Cascade>>,
}

#[derive(Clone, Debug, Default, Reflect, FromReflect)]
pub struct Cascade {
/// The transform of the light, i.e. the view to world matrix.
pub(crate) view_transform: Mat4,
pub view_transform: Mat4,
/// The orthographic projection for this cascade.
pub(crate) projection: Mat4,
pub projection: Mat4,
/// The view-projection matrix for this cascade, converting world space into light clip space.
/// Importantly, this is derived and stored separately from `view_transform` and `projection` to
/// ensure shadow stability.
pub(crate) view_projection: Mat4,
pub view_projection: Mat4,
/// Size of each shadow map texel in world units.
pub(crate) texel_size: f32,
pub texel_size: f32,
}

pub fn update_directional_light_cascades(
Expand All @@ -423,7 +426,7 @@ pub fn update_directional_light_cascades(
.collect::<Vec<_>>();

for (transform, directional_light, cascades_config, mut cascades) in lights.iter_mut() {
if !directional_light.shadows_enabled {
if !directional_light.shadows_enabled || cascades_config.manual_cascades {
continue;
}

Expand Down

0 comments on commit 8ff18b9

Please sign in to comment.