Skip to content

Commit

Permalink
fix orthographic cluster aabb for spotlight culling (#9614)
Browse files Browse the repository at this point in the history
# Objective

fix #9605

spotlight culling uses an incorrect cluster aabb for orthographic
projections: it does not take into account the near and far cluster
bounds at all.

## Solution

use z_near and z_far to determine cluster aabb in orthographic mode.

i'm not 100% sure this is the only change that's needed, but i am sure
this change is needed, and the example seems to work well now
(CLUSTERED_FORWARD_DEBUG_CLUSTER_LIGHT_COMPLEXITY shows good bounds
around the cone for a variety of orthographic setups).
  • Loading branch information
robtfm authored Oct 8, 2023
1 parent 78e4bb2 commit d2dad4e
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions crates/bevy_pbr/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,20 +1081,12 @@ fn compute_aabb_for_cluster(

// Convert to view space at the cluster near and far planes
// NOTE: 1.0 is the near plane due to using reverse z projections
let p_min = screen_to_view(
screen_size,
inverse_projection,
p_min,
1.0 - (ijk.z / cluster_dimensions.z as f32),
)
.xyz();
let p_max = screen_to_view(
screen_size,
inverse_projection,
p_max,
1.0 - ((ijk.z + 1.0) / cluster_dimensions.z as f32),
)
.xyz();
let mut p_min = screen_to_view(screen_size, inverse_projection, p_min, 0.0).xyz();
let mut p_max = screen_to_view(screen_size, inverse_projection, p_max, 0.0).xyz();

// calculate cluster depth using z_near and z_far
p_min.z = -z_near + (z_near - z_far) * ijk.z / cluster_dimensions.z as f32;
p_max.z = -z_near + (z_near - z_far) * (ijk.z + 1.0) / cluster_dimensions.z as f32;

cluster_min = p_min.min(p_max);
cluster_max = p_min.max(p_max);
Expand Down

0 comments on commit d2dad4e

Please sign in to comment.