From 95b96c1ffd089300c753b0c67550f0a3cf1a1933 Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Tue, 29 Aug 2023 01:23:16 +0100 Subject: [PATCH 1/5] fix ortho cluster aabb --- crates/bevy_pbr/src/light.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/bevy_pbr/src/light.rs b/crates/bevy_pbr/src/light.rs index 08b21db7a3cd6..930f026bcdf97 100644 --- a/crates/bevy_pbr/src/light.rs +++ b/crates/bevy_pbr/src/light.rs @@ -1050,14 +1050,14 @@ 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( + let mut 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( + let mut p_max = screen_to_view( screen_size, inverse_projection, p_max, @@ -1065,6 +1065,12 @@ fn compute_aabb_for_cluster( ) .xyz(); + // translate the view depth into cluster depth + // - z_far is the extent of the clusters + // - inverse_projection.z_axis.z is the far plane of the camera + p_min.z *= z_far / inverse_projection.z_axis.z; + p_max.z *= z_far / inverse_projection.z_axis.z; + cluster_min = p_min.min(p_max); cluster_max = p_min.max(p_max); } else { From 541aec0d77211dfbd7c166106c008fc1436d8970 Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Tue, 29 Aug 2023 01:40:43 +0100 Subject: [PATCH 2/5] account for near plane --- crates/bevy_pbr/src/light.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/crates/bevy_pbr/src/light.rs b/crates/bevy_pbr/src/light.rs index 930f026bcdf97..dbf912a751db2 100644 --- a/crates/bevy_pbr/src/light.rs +++ b/crates/bevy_pbr/src/light.rs @@ -1066,10 +1066,10 @@ fn compute_aabb_for_cluster( .xyz(); // translate the view depth into cluster depth - // - z_far is the extent of the clusters - // - inverse_projection.z_axis.z is the far plane of the camera - p_min.z *= z_far / inverse_projection.z_axis.z; - p_max.z *= z_far / inverse_projection.z_axis.z; + let cam_far = inverse_projection.w_axis.z; + let cam_near = inverse_projection.z_axis.z + inverse_projection.w_axis.z; + p_min.z = -(((p_min.z - cam_near) / cam_far) * (z_far - z_near) + z_near); + p_max.z = -(((p_max.z - cam_near) / cam_far) * (z_far - z_near) + z_near); cluster_min = p_min.min(p_max); cluster_max = p_min.max(p_max); @@ -1435,6 +1435,9 @@ pub(crate) fn assign_lights_to_clusters( let inverse_projection = camera.projection_matrix().inverse(); + println!("proj: {:?}", camera.projection_matrix()); + println!("inv {:?}", camera.projection_matrix().inverse()); + for lights in &mut clusters.lights { lights.entities.clear(); lights.point_light_count = 0; From b0b74368e4eec0046dd3f794520435fa7cf6e066 Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Tue, 29 Aug 2023 01:47:50 +0100 Subject: [PATCH 3/5] simplify --- crates/bevy_pbr/src/light.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/crates/bevy_pbr/src/light.rs b/crates/bevy_pbr/src/light.rs index dbf912a751db2..45e0ce0686cf9 100644 --- a/crates/bevy_pbr/src/light.rs +++ b/crates/bevy_pbr/src/light.rs @@ -1054,22 +1054,20 @@ fn compute_aabb_for_cluster( screen_size, inverse_projection, p_min, - 1.0 - (ijk.z / cluster_dimensions.z as f32), + 0.0, ) .xyz(); let mut p_max = screen_to_view( screen_size, inverse_projection, p_max, - 1.0 - ((ijk.z + 1.0) / cluster_dimensions.z as f32), + 0.0, ) .xyz(); - // translate the view depth into cluster depth - let cam_far = inverse_projection.w_axis.z; - let cam_near = inverse_projection.z_axis.z + inverse_projection.w_axis.z; - p_min.z = -(((p_min.z - cam_near) / cam_far) * (z_far - z_near) + z_near); - p_max.z = -(((p_max.z - cam_near) / cam_far) * (z_far - z_near) + z_near); + // 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); From 280f7e3e69bbe35715634e17c365b5882c42af35 Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Tue, 29 Aug 2023 01:49:48 +0100 Subject: [PATCH 4/5] remove debug --- crates/bevy_pbr/src/light.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/crates/bevy_pbr/src/light.rs b/crates/bevy_pbr/src/light.rs index 45e0ce0686cf9..2cce12a37b395 100644 --- a/crates/bevy_pbr/src/light.rs +++ b/crates/bevy_pbr/src/light.rs @@ -1433,9 +1433,6 @@ pub(crate) fn assign_lights_to_clusters( let inverse_projection = camera.projection_matrix().inverse(); - println!("proj: {:?}", camera.projection_matrix()); - println!("inv {:?}", camera.projection_matrix().inverse()); - for lights in &mut clusters.lights { lights.entities.clear(); lights.point_light_count = 0; From b70eb21e0d696ff8982ab8294e7b1898face5592 Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Tue, 29 Aug 2023 01:56:59 +0100 Subject: [PATCH 5/5] fmt --- crates/bevy_pbr/src/light.rs | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/crates/bevy_pbr/src/light.rs b/crates/bevy_pbr/src/light.rs index 2cce12a37b395..466091d77591c 100644 --- a/crates/bevy_pbr/src/light.rs +++ b/crates/bevy_pbr/src/light.rs @@ -1050,20 +1050,8 @@ 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 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(); + 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;