From 6a1ba9c456663808aecce2bcdc0a181eed6204d5 Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Mon, 25 Jul 2022 16:24:54 +0000 Subject: [PATCH] Spotlight shadow bugfix (#5451) # Objective fix an error in shadow map indexing that occurs when point lights without shadows are used in conjunction with spotlights with shadows ## Solution calculate point_light_count correctly --- crates/bevy_pbr/src/render/light.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/bevy_pbr/src/render/light.rs b/crates/bevy_pbr/src/render/light.rs index 29c3d0b5b439d..5a9e75b0ea0e0 100644 --- a/crates/bevy_pbr/src/render/light.rs +++ b/crates/bevy_pbr/src/render/light.rs @@ -797,10 +797,14 @@ pub fn prepare_lights( let point_light_count = point_lights .iter() - .filter(|light| light.1.shadows_enabled && light.1.spot_light_angles.is_none()) + .filter(|light| light.1.spot_light_angles.is_none()) .count(); - let point_light_shadow_maps_count = point_light_count.min(max_texture_cubes); + let point_light_shadow_maps_count = point_lights + .iter() + .filter(|light| light.1.shadows_enabled && light.1.spot_light_angles.is_none()) + .count() + .min(max_texture_cubes); let directional_shadow_maps_count = directional_lights .iter() @@ -970,7 +974,7 @@ pub fn prepare_lights( n_directional_lights: directional_lights.iter().len() as u32, // spotlight shadow maps are stored in the directional light array, starting at directional_shadow_maps_count. // the spot lights themselves start in the light array at point_light_count. so to go from light - // index to shadow map index, we need to subtract point light shadowmap count and add directional shadowmap count. + // index to shadow map index, we need to subtract point light count and add directional shadowmap count. spot_light_shadowmap_offset: directional_shadow_maps_count as i32 - point_light_count as i32, }; @@ -1045,7 +1049,7 @@ pub fn prepare_lights( let spot_view_transform = spot_view_matrix.into(); let angle = light.spot_light_angles.expect("lights should be sorted so that \ - [point_light_shadow_maps_count..point_light_shadow_maps_count + spot_light_shadow_maps_count] are spot lights").1; + [point_light_count..point_light_count + spot_light_shadow_maps_count] are spot lights").1; let spot_projection = spot_light_projection_matrix(angle); let depth_texture_view =