From d220eccbb14f8d65b54a1dfbc5b09205d43fcc21 Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Mon, 6 Jan 2025 19:11:04 +0000 Subject: [PATCH] More DefaultUiCamera fixes (#17120) # Objective Found more excessive `DefaultUiCamera` queries outside of extraction. The default UI camera lookup only needs to be done once. Do it first, not per node. --------- Co-authored-by: MichiRecRoom <1008889+LikeLakers2@users.noreply.github.com> --- crates/bevy_ui/src/focus.rs | 4 +++- crates/bevy_ui/src/picking_backend.rs | 6 ++++-- crates/bevy_ui/src/widget/text.rs | 4 +++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/crates/bevy_ui/src/focus.rs b/crates/bevy_ui/src/focus.rs index f5175c61a8594..577ac82c6bac2 100644 --- a/crates/bevy_ui/src/focus.rs +++ b/crates/bevy_ui/src/focus.rs @@ -213,6 +213,8 @@ pub fn ui_focus_system( }) .collect(); + let default_camera_entity = default_ui_camera.get(); + // prepare an iterator that contains all the nodes that have the cursor in their rect, // from the top node to the bottom one. this will also reset the interaction to `None` // for all nodes encountered that are no longer hovered. @@ -239,7 +241,7 @@ pub fn ui_focus_system( let camera_entity = node .target_camera .map(TargetCamera::entity) - .or(default_ui_camera.get())?; + .or(default_camera_entity)?; let node_rect = Rect::from_center_size( node.global_transform.translation().truncate(), diff --git a/crates/bevy_ui/src/picking_backend.rs b/crates/bevy_ui/src/picking_backend.rs index 279acdb880241..f1a7cdd4c84be 100644 --- a/crates/bevy_ui/src/picking_backend.rs +++ b/crates/bevy_ui/src/picking_backend.rs @@ -72,6 +72,8 @@ pub fn ui_picking( // For each camera, the pointer and its position let mut pointer_pos_by_camera = HashMap::>::default(); + let default_camera_entity = default_ui_camera.get(); + for (pointer_id, pointer_location) in pointers.iter().filter_map(|(pointer, pointer_location)| { Some(*pointer).zip(pointer_location.location().cloned()) @@ -133,7 +135,7 @@ pub fn ui_picking( let Some(camera_entity) = node .target_camera .map(TargetCamera::entity) - .or(default_ui_camera.get()) + .or(default_camera_entity) else { continue; }; @@ -189,7 +191,7 @@ pub fn ui_picking( let Some(camera_entity) = node .target_camera .map(TargetCamera::entity) - .or(default_ui_camera.get()) + .or(default_camera_entity) else { continue; }; diff --git a/crates/bevy_ui/src/widget/text.rs b/crates/bevy_ui/src/widget/text.rs index 6b0ee8c3cb75e..62cc3f649db82 100644 --- a/crates/bevy_ui/src/widget/text.rs +++ b/crates/bevy_ui/src/widget/text.rs @@ -268,10 +268,12 @@ pub fn measure_text_system( ) { scale_factors_buffer.clear(); + let default_camera_entity = default_ui_camera.get(); + for (entity, block, content_size, text_flags, computed, maybe_camera) in &mut text_query { let Some(camera_entity) = maybe_camera .map(TargetCamera::entity) - .or(default_ui_camera.get()) + .or(default_camera_entity) else { continue; };