From 922a25d29575b47a619d969bbef686d0e76623e7 Mon Sep 17 00:00:00 2001 From: akimakinai <105044389+akimakinai@users.noreply.github.com> Date: Fri, 11 Oct 2024 03:49:23 +0900 Subject: [PATCH] Picking: Filter out invisible sprites early (#15819) # Objective - We don't have to `collect` and `sort` invisible sprites in `sprite_picking` system. ## Solution - Filter by `ViewVisibility::get()` earlier ## Testing - `sprite_picking` example still works. --- crates/bevy_sprite/src/picking_backend.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/crates/bevy_sprite/src/picking_backend.rs b/crates/bevy_sprite/src/picking_backend.rs index c2f3344b52..f62c3c4e04 100644 --- a/crates/bevy_sprite/src/picking_backend.rs +++ b/crates/bevy_sprite/src/picking_backend.rs @@ -40,7 +40,13 @@ pub fn sprite_picking( ) { let mut sorted_sprites: Vec<_> = sprite_query .iter() - .filter(|x| !x.2.affine().is_nan()) + .filter_map(|(entity, sprite, transform, picking_behavior, vis)| { + if !transform.affine().is_nan() && vis.get() { + Some((entity, sprite, transform, picking_behavior)) + } else { + None + } + }) .collect(); sorted_sprites.sort_by_key(|x| Reverse(FloatOrd(x.2.translation().z))); @@ -74,8 +80,7 @@ pub fn sprite_picking( let picks: Vec<(Entity, HitData)> = sorted_sprites .iter() .copied() - .filter(|(.., visibility)| visibility.get()) - .filter_map(|(entity, sprite, sprite_transform, picking_behavior, ..)| { + .filter_map(|(entity, sprite, sprite_transform, picking_behavior)| { if blocked { return None; } @@ -125,7 +130,9 @@ pub fn sprite_picking( let is_cursor_in_sprite = rect.contains(cursor_pos_sprite); blocked = is_cursor_in_sprite - && picking_behavior.map(|p| p.should_block_lower) != Some(false); + && picking_behavior + .map(|p| p.should_block_lower) + .unwrap_or(true); is_cursor_in_sprite.then(|| { let hit_pos_world =