Skip to content

Commit 47db728

Browse files
authored
compute world_from_cascade analytically (#20364)
# Objective - doc comment says numeric inverse is avoided because of precision issues, but its still used in two places anyways (the other being fixed by #20359) - dont do that lmao ## Solution - calculate it manually instead of using a numeric inverse - also rename some variables to match new x_from_y style while im at it ## Testing - shadow biases example
1 parent 7300f56 commit 47db728

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

crates/bevy_light/src/cascade.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,16 +297,22 @@ fn calculate_cascade(
297297
max.z,
298298
);
299299

300-
// It is critical for `world_to_cascade` to be stable. So rather than forming `cascade_to_world`
300+
// It is critical for `cascade_from_world` to be stable. So rather than forming `world_from_cascade`
301301
// and inverting it, which risks instability due to numerical precision, we directly form
302-
// `world_to_cascade` as the reference material suggests.
303-
let light_to_world_transpose = world_from_light.transpose();
302+
// `cascade_from_world` as the reference material suggests.
303+
let world_from_light_transpose = world_from_light.transpose();
304304
let cascade_from_world = Mat4::from_cols(
305-
light_to_world_transpose.x_axis,
306-
light_to_world_transpose.y_axis,
307-
light_to_world_transpose.z_axis,
305+
world_from_light_transpose.x_axis,
306+
world_from_light_transpose.y_axis,
307+
world_from_light_transpose.z_axis,
308308
(-near_plane_center).extend(1.0),
309309
);
310+
let world_from_cascade = Mat4::from_cols(
311+
world_from_light.x_axis,
312+
world_from_light.y_axis,
313+
world_from_light.z_axis,
314+
world_from_light * near_plane_center.extend(1.0),
315+
);
310316

311317
// Right-handed orthographic projection, centered at `near_plane_center`.
312318
// NOTE: This is different from the reference material, as we use reverse Z.
@@ -320,7 +326,7 @@ fn calculate_cascade(
320326

321327
let clip_from_world = clip_from_cascade * cascade_from_world;
322328
Cascade {
323-
world_from_cascade: cascade_from_world.inverse(),
329+
world_from_cascade,
324330
clip_from_cascade,
325331
clip_from_world,
326332
texel_size: cascade_texel_size,

0 commit comments

Comments
 (0)