From d2dad4eed2d04f95f304c6c7ef0f7de5df10fba7 Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Sun, 8 Oct 2023 23:53:09 +0100 Subject: [PATCH] fix orthographic cluster aabb for spotlight culling (#9614) # 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). --- crates/bevy_pbr/src/light.rs | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/crates/bevy_pbr/src/light.rs b/crates/bevy_pbr/src/light.rs index 502b22047de4f..7316604ad3f5c 100644 --- a/crates/bevy_pbr/src/light.rs +++ b/crates/bevy_pbr/src/light.rs @@ -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);