Skip to content

Commit 7f82aa1

Browse files
authored
use iterators and zip instead of enumeration and indexing in shadow map cascades (#20360)
# Objective - refactor code to be easier to understand ## Solution - use iterator methods like zip chain map instead of if statements and indexing ## Testing - shadow_biases example
1 parent b378e0a commit 7f82aa1

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

crates/bevy_light/src/cascade.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -230,22 +230,16 @@ pub fn build_directional_light_cascades(
230230

231231
for (view_entity, projection, view_to_world) in views.iter().copied() {
232232
let camera_to_light_view = light_to_world_inverse * view_to_world;
233-
let view_cascades = cascades_config
234-
.bounds
235-
.iter()
236-
.enumerate()
237-
.map(|(idx, far_bound)| {
233+
let overlap_factor = 1.0 - cascades_config.overlap_proportion;
234+
let far_bounds = cascades_config.bounds.iter();
235+
let near_bounds = [cascades_config.minimum_distance]
236+
.into_iter()
237+
.chain(far_bounds.clone().map(|bound| overlap_factor * bound));
238+
let view_cascades = near_bounds
239+
.zip(far_bounds)
240+
.map(|(near_bound, far_bound)| {
238241
// Negate bounds as -z is camera forward direction.
239-
let z_near = if idx > 0 {
240-
(1.0 - cascades_config.overlap_proportion)
241-
* -cascades_config.bounds[idx - 1]
242-
} else {
243-
-cascades_config.minimum_distance
244-
};
245-
let z_far = -far_bound;
246-
247-
let corners = projection.get_frustum_corners(z_near, z_far);
248-
242+
let corners = projection.get_frustum_corners(-near_bound, -far_bound);
249243
calculate_cascade(
250244
corners,
251245
directional_light_shadow_map.size as f32,

0 commit comments

Comments
 (0)