bevy_light/cascade.rs
1//! Provides shadow cascade configuration and construction helpers.
2
3use bevy_camera::{Camera, Projection};
4use bevy_ecs::{entity::EntityHashMap, prelude::*};
5use bevy_math::{ops, Mat4, Vec3A, Vec4};
6use bevy_reflect::prelude::*;
7use bevy_transform::components::GlobalTransform;
8
9use crate::{DirectionalLight, DirectionalLightShadowMap};
10
11/// Controls how cascaded shadow mapping works.
12/// Prefer using [`CascadeShadowConfigBuilder`] to construct an instance.
13///
14/// ```
15/// # use bevy_light::CascadeShadowConfig;
16/// # use bevy_light::CascadeShadowConfigBuilder;
17/// # use bevy_utils::default;
18/// #
19/// let config: CascadeShadowConfig = CascadeShadowConfigBuilder {
20/// maximum_distance: 100.0,
21/// ..default()
22/// }.into();
23/// ```
24#[derive(Component, Clone, Debug, Reflect)]
25#[reflect(Component, Default, Debug, Clone)]
26pub struct CascadeShadowConfig {
27 /// The (positive) distance to the far boundary of each cascade.
28 pub bounds: Vec<f32>,
29 /// The proportion of overlap each cascade has with the previous cascade.
30 pub overlap_proportion: f32,
31 /// The (positive) distance to the near boundary of the first cascade.
32 pub minimum_distance: f32,
33}
34
35impl Default for CascadeShadowConfig {
36 fn default() -> Self {
37 CascadeShadowConfigBuilder::default().into()
38 }
39}
40
41fn calculate_cascade_bounds(
42 num_cascades: usize,
43 nearest_bound: f32,
44 shadow_maximum_distance: f32,
45) -> Vec<f32> {
46 if num_cascades == 1 {
47 return vec![shadow_maximum_distance];
48 }
49 let base = ops::powf(
50 shadow_maximum_distance / nearest_bound,
51 1.0 / (num_cascades - 1) as f32,
52 );
53 (0..num_cascades)
54 .map(|i| nearest_bound * ops::powf(base, i as f32))
55 .collect()
56}
57
58/// Builder for [`CascadeShadowConfig`].
59pub struct CascadeShadowConfigBuilder {
60 /// The number of shadow cascades.
61 /// More cascades increases shadow quality by mitigating perspective aliasing - a phenomenon where areas
62 /// nearer the camera are covered by fewer shadow map texels than areas further from the camera, causing
63 /// blocky looking shadows.
64 ///
65 /// This does come at the cost increased rendering overhead, however this overhead is still less
66 /// than if you were to use fewer cascades and much larger shadow map textures to achieve the
67 /// same quality level.
68 ///
69 /// In case rendered geometry covers a relatively narrow and static depth relative to camera, it may
70 /// make more sense to use fewer cascades and a higher resolution shadow map texture as perspective aliasing
71 /// is not as much an issue. Be sure to adjust `minimum_distance` and `maximum_distance` appropriately.
72 pub num_cascades: usize,
73 /// The minimum shadow distance, which can help improve the texel resolution of the first cascade.
74 /// Areas nearer to the camera than this will likely receive no shadows.
75 ///
76 /// NOTE: Due to implementation details, this usually does not impact shadow quality as much as
77 /// `first_cascade_far_bound` and `maximum_distance`. At many view frustum field-of-views, the
78 /// texel resolution of the first cascade is dominated by the width / height of the view frustum plane
79 /// at `first_cascade_far_bound` rather than the depth of the frustum from `minimum_distance` to
80 /// `first_cascade_far_bound`.
81 pub minimum_distance: f32,
82 /// The maximum shadow distance.
83 /// Areas further from the camera than this will likely receive no shadows.
84 pub maximum_distance: f32,
85 /// Sets the far bound of the first cascade, relative to the view origin.
86 /// In-between cascades will be exponentially spaced relative to the maximum shadow distance.
87 /// NOTE: This is ignored if there is only one cascade, the maximum distance takes precedence.
88 pub first_cascade_far_bound: f32,
89 /// Sets the overlap proportion between cascades.
90 /// The overlap is used to make the transition from one cascade's shadow map to the next
91 /// less abrupt by blending between both shadow maps.
92 pub overlap_proportion: f32,
93}
94
95impl CascadeShadowConfigBuilder {
96 /// Returns the cascade config as specified by this builder.
97 pub fn build(&self) -> CascadeShadowConfig {
98 assert!(
99 self.num_cascades > 0,
100 "num_cascades must be positive, but was {}",
101 self.num_cascades
102 );
103 assert!(
104 self.minimum_distance >= 0.0,
105 "maximum_distance must be non-negative, but was {}",
106 self.minimum_distance
107 );
108 assert!(
109 self.num_cascades == 1 || self.minimum_distance < self.first_cascade_far_bound,
110 "minimum_distance must be less than first_cascade_far_bound, but was {}",
111 self.minimum_distance
112 );
113 assert!(
114 self.maximum_distance > self.minimum_distance,
115 "maximum_distance must be greater than minimum_distance, but was {}",
116 self.maximum_distance
117 );
118 assert!(
119 (0.0..1.0).contains(&self.overlap_proportion),
120 "overlap_proportion must be in [0.0, 1.0) but was {}",
121 self.overlap_proportion
122 );
123 CascadeShadowConfig {
124 bounds: calculate_cascade_bounds(
125 self.num_cascades,
126 self.first_cascade_far_bound,
127 self.maximum_distance,
128 ),
129 overlap_proportion: self.overlap_proportion,
130 minimum_distance: self.minimum_distance,
131 }
132 }
133}
134
135impl Default for CascadeShadowConfigBuilder {
136 fn default() -> Self {
137 // The defaults are chosen to be similar to be Unity, Unreal, and Godot.
138 // Unity: first cascade far bound = 10.05, maximum distance = 150.0
139 // Unreal Engine 5: maximum distance = 200.0
140 // Godot: first cascade far bound = 10.0, maximum distance = 100.0
141 Self {
142 // Currently only support one cascade in WebGL 2.
143 num_cascades: if cfg!(all(
144 feature = "webgl",
145 target_arch = "wasm32",
146 not(feature = "webgpu")
147 )) {
148 1
149 } else {
150 4
151 },
152 minimum_distance: 0.1,
153 maximum_distance: 150.0,
154 first_cascade_far_bound: 10.0,
155 overlap_proportion: 0.2,
156 }
157 }
158}
159
160impl From<CascadeShadowConfigBuilder> for CascadeShadowConfig {
161 fn from(builder: CascadeShadowConfigBuilder) -> Self {
162 builder.build()
163 }
164}
165
166/// A [`DirectionalLight`]'s per-view list of [`Cascade`]s.
167#[derive(Component, Clone, Debug, Default, Reflect)]
168#[reflect(Component, Debug, Default, Clone)]
169pub struct Cascades {
170 /// Map from a view to the configuration of each of its [`Cascade`]s.
171 pub cascades: EntityHashMap<Vec<Cascade>>,
172}
173
174/// A single cascade of a view's shadow map cascade. Several of these are
175/// used to cover most of the view to ensure most geometry gets shadows, with
176/// some overlap for blending at cascade transitions. Farther away cascades
177/// are larger and have a lower effective shadowmap texel per world unit
178/// resolution. All cascades have the same pixel dimensions however.
179#[derive(Clone, Debug, Default, Reflect)]
180#[reflect(Clone, Default)]
181pub struct Cascade {
182 /// The transform of the light, i.e. the view to world matrix.
183 pub world_from_cascade: Mat4,
184 /// The orthographic projection for this cascade.
185 pub clip_from_cascade: Mat4,
186 /// The view-projection matrix for this cascade, converting world space into light clip space.
187 /// Importantly, this is derived and stored separately from `view_transform` and `projection` to
188 /// ensure shadow stability.
189 pub clip_from_world: Mat4,
190 /// Size of each shadow map texel in world units.
191 pub texel_size: f32,
192}
193
194/// Sets up [`Cascades`] for all shadow mapped [`DirectionalLight`]s.
195pub fn build_directional_light_cascades(
196 directional_light_shadow_map: Res<DirectionalLightShadowMap>,
197 views: Query<(Entity, &GlobalTransform, &Projection, &Camera)>,
198 mut lights: Query<(
199 &GlobalTransform,
200 &DirectionalLight,
201 &CascadeShadowConfig,
202 &mut Cascades,
203 )>,
204) {
205 let views = views
206 .iter()
207 .filter_map(|(entity, transform, projection, camera)| {
208 if camera.is_active {
209 Some((entity, projection, transform.to_matrix()))
210 } else {
211 None
212 }
213 })
214 .collect::<Vec<_>>();
215
216 for (transform, directional_light, cascades_config, mut cascades) in &mut lights {
217 if !directional_light.shadow_maps_enabled {
218 continue;
219 }
220 cascades.cascades.clear();
221
222 // It is very important to the numerical and thus visual stability of shadows that
223 // `world_from_light` has orthogonal upper-left 3x3 and zero translation.
224 // Even though only the direction (i.e. rotation) of the light matters, we don't constrain
225 // users to not change any other aspects of the transform - there's no guarantee
226 // `transform.to_matrix()` will give us a matrix with our desired properties.
227 // Instead, we directly create a good matrix from just the rotation.
228 let world_from_light = Mat4::from_quat(transform.rotation());
229 // The transpose is the inverse for orthogonal matrices.
230 let light_from_world = world_from_light.transpose();
231
232 for (view_entity, projection, world_from_view) in views.iter().copied() {
233 let light_view_from_camera = light_from_world * world_from_view;
234 let overlap_factor = 1.0 - cascades_config.overlap_proportion;
235 let far_bounds = cascades_config.bounds.iter();
236 let near_bounds = [cascades_config.minimum_distance]
237 .into_iter()
238 .chain(far_bounds.clone().map(|bound| overlap_factor * bound));
239 let view_cascades = near_bounds
240 .zip(far_bounds)
241 .map(|(near_bound, far_bound)| {
242 // Negate bounds as -z is camera forward direction.
243 let corners = projection.get_frustum_corners(-near_bound, -far_bound);
244 calculate_cascade(
245 corners,
246 directional_light_shadow_map.size as f32,
247 world_from_light,
248 light_view_from_camera,
249 )
250 })
251 .collect();
252 cascades.cascades.insert(view_entity, view_cascades);
253 }
254 }
255}
256
257/// Returns a [`Cascade`] for the frustum defined by `frustum_corners`.
258///
259/// The corner vertices should be specified in the following order:
260/// first the bottom right, top right, top left, bottom left for the near plane, then similar for the far plane.
261///
262/// See this [reference](https://developer.download.nvidia.com/SDK/10.5/opengl/src/cascaded_shadow_maps/doc/cascaded_shadow_maps.pdf) for more details.
263fn calculate_cascade(
264 frustum_corners: [Vec3A; 8],
265 cascade_texture_size: f32,
266 world_from_light: Mat4,
267 light_from_camera: Mat4,
268) -> Cascade {
269 let mut min = Vec3A::splat(f32::MAX);
270 let mut max = Vec3A::splat(f32::MIN);
271 for corner_camera_view in frustum_corners {
272 let corner_light_view = light_from_camera.transform_point3a(corner_camera_view);
273 min = min.min(corner_light_view);
274 max = max.max(corner_light_view);
275 }
276
277 // NOTE: Use the larger of the frustum slice far plane diagonal and body diagonal lengths as this
278 // will be the maximum possible projection size. Use the ceiling to get an integer which is
279 // very important for floating point stability later. It is also important that these are
280 // calculated using the original camera space corner positions for floating point precision
281 // as even though the lengths using corner_light_view above should be the same, precision can
282 // introduce small but significant differences.
283 // NOTE: The size remains the same unless the view frustum or cascade configuration is modified.
284 let body_diagonal = (frustum_corners[0] - frustum_corners[6]).length_squared();
285 let far_plane_diagonal = (frustum_corners[4] - frustum_corners[6]).length_squared();
286 let cascade_diameter = body_diagonal.max(far_plane_diagonal).sqrt().ceil();
287
288 // NOTE: If we ensure that cascade_texture_size is a power of 2, then as we made cascade_diameter an
289 // integer, cascade_texel_size is then an integer multiple of a power of 2 and can be
290 // exactly represented in a floating point value.
291 let cascade_texel_size = cascade_diameter / cascade_texture_size;
292 // NOTE: For shadow stability it is very important that the near_plane_center is at integer
293 // multiples of the texel size to be exactly representable in a floating point value.
294 let near_plane_center = Vec3A::new(
295 (0.5 * (min.x + max.x) / cascade_texel_size).floor() * cascade_texel_size,
296 (0.5 * (min.y + max.y) / cascade_texel_size).floor() * cascade_texel_size,
297 // NOTE: max.z is the near plane for right-handed y-up
298 max.z,
299 );
300
301 // It is critical for `cascade_from_world` to be stable. So rather than forming `world_from_cascade`
302 // and inverting it, which risks instability due to numerical precision, we directly form
303 // `cascade_from_world` as the reference material suggests.
304 let world_from_light_transpose = world_from_light.transpose();
305 let cascade_from_world = Mat4::from_cols(
306 world_from_light_transpose.x_axis,
307 world_from_light_transpose.y_axis,
308 world_from_light_transpose.z_axis,
309 (-near_plane_center).extend(1.0),
310 );
311 let world_from_cascade = Mat4::from_cols(
312 world_from_light.x_axis,
313 world_from_light.y_axis,
314 world_from_light.z_axis,
315 world_from_light * near_plane_center.extend(1.0),
316 );
317
318 // Right-handed orthographic projection, centered at `near_plane_center`.
319 // NOTE: This is different from the reference material, as we use reverse Z.
320 let r = (max.z - min.z).recip();
321 let clip_from_cascade = Mat4::from_cols(
322 Vec4::new(2.0 / cascade_diameter, 0.0, 0.0, 0.0),
323 Vec4::new(0.0, 2.0 / cascade_diameter, 0.0, 0.0),
324 Vec4::new(0.0, 0.0, r, 0.0),
325 Vec4::new(0.0, 0.0, 1.0, 1.0),
326 );
327
328 let clip_from_world = clip_from_cascade * cascade_from_world;
329 Cascade {
330 world_from_cascade,
331 clip_from_cascade,
332 clip_from_world,
333 texel_size: cascade_texel_size,
334 }
335}