Skip to content

Commit e62a404

Browse files
committed
Cascaded shadow maps.
Co-authored-by: Robert Swain <robert.swain@gmail.com> Implements cascaded shadow maps for directional lights, which produces better quality shadows without needing excessively large shadow maps.
1 parent b027d40 commit e62a404

File tree

20 files changed

+616
-232
lines changed

20 files changed

+616
-232
lines changed

crates/bevy_core_pipeline/src/core_2d/camera_2d.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl Camera2dBundle {
6161
let transform = Transform::from_xyz(0.0, 0.0, far - 0.1);
6262
let view_projection =
6363
projection.get_projection_matrix() * transform.compute_matrix().inverse();
64-
let frustum = Frustum::from_view_projection(
64+
let frustum = Frustum::from_view_projection_custom_far(
6565
&view_projection,
6666
&transform.translation,
6767
&transform.back(),

crates/bevy_pbr/src/bundle.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
use crate::{DirectionalLight, Material, PointLight, SpotLight, StandardMaterial};
1+
use crate::{
2+
CascadeShadowConfig, Cascades, DirectionalLight, Material, PointLight, SpotLight,
3+
StandardMaterial,
4+
};
25
use bevy_asset::Handle;
3-
use bevy_ecs::{bundle::Bundle, component::Component, reflect::ReflectComponent};
6+
use bevy_ecs::{bundle::Bundle, component::Component, prelude::Entity, reflect::ReflectComponent};
47
use bevy_reflect::Reflect;
58
use bevy_render::{
69
mesh::Mesh,
7-
primitives::{CubemapFrusta, Frustum},
10+
primitives::{CascadesFrusta, CubemapFrusta, Frustum},
811
view::{ComputedVisibility, Visibility, VisibleEntities},
912
};
1013
use bevy_transform::components::{GlobalTransform, Transform};
14+
use bevy_utils::HashMap;
1115

1216
/// A component bundle for PBR entities with a [`Mesh`] and a [`StandardMaterial`].
1317
pub type PbrBundle = MaterialMeshBundle<StandardMaterial>;
@@ -63,6 +67,14 @@ impl CubemapVisibleEntities {
6367
}
6468
}
6569

70+
#[derive(Component, Clone, Debug, Default, Reflect)]
71+
#[reflect(Component)]
72+
pub struct CascadesVisibleEntities {
73+
/// The visible entities for each cascade frustrum, for each view.
74+
#[reflect(ignore)]
75+
pub entities: HashMap<Entity, Vec<VisibleEntities>>,
76+
}
77+
6678
/// A component bundle for [`PointLight`] entities.
6779
#[derive(Debug, Bundle, Default)]
6880
pub struct PointLightBundle {
@@ -95,8 +107,10 @@ pub struct SpotLightBundle {
95107
#[derive(Debug, Bundle, Default)]
96108
pub struct DirectionalLightBundle {
97109
pub directional_light: DirectionalLight,
98-
pub frustum: Frustum,
99-
pub visible_entities: VisibleEntities,
110+
pub frusta: CascadesFrusta,
111+
pub cascades: Cascades,
112+
pub cascade_shadow_config: CascadeShadowConfig,
113+
pub visible_entities: CascadesVisibleEntities,
100114
pub transform: Transform,
101115
pub global_transform: GlobalTransform,
102116
/// Enables or disables the light

crates/bevy_pbr/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,20 @@ impl Plugin for PbrPlugin {
158158
.after(CameraUpdateSystem)
159159
.after(ModifiesWindows),
160160
)
161+
.add_system_to_stage(
162+
CoreStage::PostUpdate,
163+
update_directional_light_cascades
164+
.label(SimulationLightSystems::UpdateDirectionalLightCascades)
165+
.after(TransformSystem::TransformPropagate),
166+
)
161167
.add_system_to_stage(
162168
CoreStage::PostUpdate,
163169
update_directional_light_frusta
164170
.label(SimulationLightSystems::UpdateLightFrusta)
165171
// This must run after CheckVisibility because it relies on ComputedVisibility::is_visible()
166172
.after(VisibilitySystems::CheckVisibility)
167173
.after(TransformSystem::TransformPropagate)
174+
.after(SimulationLightSystems::UpdateDirectionalLightCascades)
168175
// We assume that no entity will be both a directional light and a spot light,
169176
// so these systems will run indepdently of one another.
170177
// FIXME: Add an archetype invariant for this https://github.com/bevyengine/bevy/issues/1481.

0 commit comments

Comments
 (0)