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.
This commit is contained in:
akimakinai 2024-10-11 03:49:23 +09:00 committed by GitHub
parent dd812b3e49
commit 922a25d295
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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 =