Skip to content

Commit

Permalink
Fix directional light shadow frustum culling near clip plane to infin…
Browse files Browse the repository at this point in the history
…ity (#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
  • Loading branch information
atlv24 authored Mar 6, 2024
1 parent f9e70ab commit 13d37c5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 8 additions & 3 deletions crates/bevy_pbr/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*,
Expand Down Expand Up @@ -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()
Expand All @@ -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 {
Expand All @@ -1191,7 +1196,7 @@ pub fn prepare_lights(
hdr: false,
color_grading: Default::default(),
},
*frusta,
frustum,
RenderPhase::<Shadow>::default(),
LightEntity::Directional {
light_entity,
Expand Down

0 comments on commit 13d37c5

Please sign in to comment.