@@ -389,6 +389,13 @@ impl Clusters {
389
389
// NOTE: Maximum 4096 clusters due to uniform buffer size constraints
390
390
debug_assert ! ( self . dimensions. x * self . dimensions. y * self . dimensions. z <= 4096 ) ;
391
391
}
392
+ fn clear ( & mut self ) {
393
+ self . tile_size = UVec2 :: ONE ;
394
+ self . dimensions = UVec3 :: ONE ;
395
+ self . near = 0.0 ;
396
+ self . far = 0.0 ;
397
+ self . lights . clear ( ) ;
398
+ }
392
399
}
393
400
394
401
fn clip_to_view ( inverse_projection : Mat4 , clip : Vec4 ) -> Vec4 {
@@ -722,17 +729,22 @@ pub(crate) fn assign_lights_to_clusters(
722
729
for ( view_entity, camera_transform, camera, frustum, config, clusters, mut visible_lights) in
723
730
views. iter_mut ( )
724
731
{
732
+ let clusters = clusters. into_inner ( ) ;
733
+
725
734
if matches ! ( config, ClusterConfig :: None ) && visible_lights. is_some ( ) {
726
735
commands. entity ( view_entity) . remove :: < VisiblePointLights > ( ) ;
736
+ clusters. clear ( ) ;
727
737
continue ;
728
738
}
729
739
730
- let clusters = clusters. into_inner ( ) ;
731
- let screen_size = camera. target . get_physical_size ( & windows, & images) ;
732
-
733
- clusters. lights . clear ( ) ;
740
+ let screen_size =
741
+ if let Some ( screen_size) = camera. target . get_physical_size ( & windows, & images) {
742
+ screen_size
743
+ } else {
744
+ clusters. clear ( ) ;
745
+ continue ;
746
+ } ;
734
747
735
- let screen_size = screen_size. unwrap_or_default ( ) ;
736
748
let mut requested_cluster_dimensions = config. dimensions_for_screen_size ( screen_size) ;
737
749
738
750
let view_transform = camera_transform. compute_matrix ( ) ;
@@ -862,10 +874,6 @@ pub(crate) fn assign_lights_to_clusters(
862
874
VisiblePointLights :: default,
863
875
) ;
864
876
865
- if screen_size. x == 0 || screen_size. y == 0 {
866
- continue ;
867
- }
868
-
869
877
// Calculate the x/y/z cluster frustum planes in view space
870
878
let mut x_planes = Vec :: with_capacity ( clusters. dimensions . x as usize + 1 ) ;
871
879
let mut y_planes = Vec :: with_capacity ( clusters. dimensions . y as usize + 1 ) ;
@@ -923,16 +931,7 @@ pub(crate) fn assign_lights_to_clusters(
923
931
z_planes. push ( Plane :: new ( normal. extend ( d) ) ) ;
924
932
}
925
933
926
- let mut visible_lights_scratch = Vec :: new ( ) ;
927
-
928
- {
929
- // reuse existing visible lights Vec, if it exists
930
- let visible_lights = if let Some ( visible_lights) = visible_lights. as_mut ( ) {
931
- visible_lights. entities . clear ( ) ;
932
- & mut visible_lights. entities
933
- } else {
934
- & mut visible_lights_scratch
935
- } ;
934
+ let mut update_from_light_intersections = |visible_lights : & mut Vec < Entity > | {
936
935
for light in lights. iter ( ) {
937
936
let light_sphere = Sphere {
938
937
center : Vec3A :: from ( light. translation ) ,
@@ -1085,12 +1084,18 @@ pub(crate) fn assign_lights_to_clusters(
1085
1084
}
1086
1085
}
1087
1086
}
1088
- }
1087
+ } ;
1089
1088
1090
- if visible_lights. is_none ( ) {
1091
- commands. entity ( view_entity) . insert ( VisiblePointLights {
1092
- entities : visible_lights_scratch,
1093
- } ) ;
1089
+ // reuse existing visible lights Vec, if it exists
1090
+ if let Some ( visible_lights) = visible_lights. as_mut ( ) {
1091
+ visible_lights. entities . clear ( ) ;
1092
+ update_from_light_intersections ( & mut visible_lights. entities ) ;
1093
+ } else {
1094
+ let mut entities = Vec :: new ( ) ;
1095
+ update_from_light_intersections ( & mut entities) ;
1096
+ commands
1097
+ . entity ( view_entity)
1098
+ . insert ( VisiblePointLights { entities } ) ;
1094
1099
}
1095
1100
}
1096
1101
}
0 commit comments