From b4483dbfc89926d47b56f23754794e6770c76630 Mon Sep 17 00:00:00 2001 From: dataphract Date: Tue, 8 Mar 2022 01:00:22 +0000 Subject: [PATCH] perf: only recalculate frusta of changed lights (#4086) ## Objective Currently, all directional and point lights have their viewing frusta recalculated every frame, even if they have not moved or been disabled/enabled. ## Solution The relevant systems now make use of change detection to only update those lights whose viewing frusta may have changed. --- crates/bevy_pbr/src/light.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/crates/bevy_pbr/src/light.rs b/crates/bevy_pbr/src/light.rs index 4bda70f0ac..f444e3fe0f 100644 --- a/crates/bevy_pbr/src/light.rs +++ b/crates/bevy_pbr/src/light.rs @@ -702,12 +702,15 @@ pub(crate) fn assign_lights_to_clusters( } pub fn update_directional_light_frusta( - mut views: Query<( - &GlobalTransform, - &DirectionalLight, - &mut Frustum, - &Visibility, - )>, + mut views: Query< + ( + &GlobalTransform, + &DirectionalLight, + &mut Frustum, + &Visibility, + ), + Or<(Changed, Changed)>, + >, ) { for (transform, directional_light, mut frustum, visibility) in views.iter_mut() { // The frustum is used for culling meshes to the light for shadow mapping @@ -731,7 +734,10 @@ pub fn update_directional_light_frusta( // NOTE: Run this after assign_lights_to_clusters! pub fn update_point_light_frusta( global_lights: Res, - mut views: Query<(Entity, &GlobalTransform, &PointLight, &mut CubemapFrusta)>, + mut views: Query< + (Entity, &GlobalTransform, &PointLight, &mut CubemapFrusta), + Or<(Changed, Changed)>, + >, ) { let projection = Mat4::perspective_infinite_reverse_rh(std::f32::consts::FRAC_PI_2, 1.0, POINT_LIGHT_NEAR_Z);