Fix directional light shadow frustum culling near clip plane to infinity (#12342)

# Objective

- Fix slightly wrong logic from #11442
- Directional lights should not have a near clip plane

## Solution

- Push near clip out to infinity, so that the frustum normal is still
available if its needed for whatever reason in shader
- also opportunistically nabs a typo
This commit is contained in:
vero 2024-03-06 11:47:12 -08:00 committed by GitHub
parent f9e70abcac
commit 13d37c534f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 4 deletions

View file

@ -589,7 +589,7 @@ fn calculate_cascade(
// It is critical for `world_to_cascade` to be stable. So rather than forming `cascade_to_world`
// and inverting it, which risks instability due to numerical precision, we directly form
// `world_to_cascde` as the reference material suggests.
// `world_to_cascade` as the reference material suggests.
let light_to_world_transpose = light_to_world.transpose();
let world_to_cascade = Mat4::from_cols(
light_to_world_transpose.x_axis,

View file

@ -5,7 +5,7 @@ use bevy_math::{Mat4, UVec3, UVec4, Vec2, Vec3, Vec3Swizzles, Vec4, Vec4Swizzles
use bevy_render::{
camera::Camera,
mesh::Mesh,
primitives::{CascadesFrusta, CubemapFrusta, Frustum},
primitives::{CascadesFrusta, CubemapFrusta, Frustum, HalfSpace},
render_asset::RenderAssets,
render_graph::{Node, NodeRunError, RenderGraphContext},
render_phase::*,
@ -1144,7 +1144,7 @@ pub fn prepare_lights(
.unwrap()
.iter()
.take(MAX_CASCADES_PER_LIGHT);
for (cascade_index, ((cascade, frusta), bound)) in cascades
for (cascade_index, ((cascade, frustum), bound)) in cascades
.zip(frusta)
.zip(&light.cascade_shadow_config.bounds)
.enumerate()
@ -1171,6 +1171,11 @@ pub fn prepare_lights(
});
directional_depth_texture_array_index += 1;
let mut frustum = *frustum;
// Push the near clip plane out to infinity for directional lights
frustum.half_spaces[4] =
HalfSpace::new(frustum.half_spaces[4].normal().extend(f32::INFINITY));
let view_light_entity = commands
.spawn((
ShadowView {
@ -1191,7 +1196,7 @@ pub fn prepare_lights(
hdr: false,
color_grading: Default::default(),
},
*frusta,
frustum,
RenderPhase::<Shadow>::default(),
LightEntity::Directional {
light_entity,