From 05c87f3c01b59272790ca6c8b76ac55fdd669fb2 Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Fri, 13 Oct 2023 12:14:31 -0700 Subject: [PATCH] Fix text2d view-visibility (#10100) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Objective Fixes #9676 Possible alternative to #9708 `Text2dBundles` are not currently drawn because the render-world-only entities for glyphs that are created in `extract_text2d_sprite` are not tracked by the per-view `VisibleEntities`. ## Solution Add an `Option` to `ExtractedSprite` that keeps track of the original entity that caused a "glyph entity" to be created. Use that in `queue_sprites` if it exists when checking view visibility. ## Benchmarks Quick benchmarks. Average FPS over 1500 frames. | bench | before fps | after fps | diff | |-|-|-|-| |many_sprites|884.93|879.00|🟡 -0.7%| |bevymark -- --benchmark --waves 100 --per-wave 1000 --mode sprite|75.99|75.93|🟡 -0.1%| |bevymark -- --benchmark --waves 50 --per-wave 1000 --mode mesh2d|32.85|32.58|🟡 -0.8%| --- crates/bevy_sprite/src/render/mod.rs | 9 ++++++++- crates/bevy_text/src/text2d.rs | 9 +++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/crates/bevy_sprite/src/render/mod.rs b/crates/bevy_sprite/src/render/mod.rs index c7d79dcc088e8..c70d33286ccf8 100644 --- a/crates/bevy_sprite/src/render/mod.rs +++ b/crates/bevy_sprite/src/render/mod.rs @@ -325,6 +325,9 @@ pub struct ExtractedSprite { pub flip_x: bool, pub flip_y: bool, pub anchor: Vec2, + /// For cases where additional ExtractedSprites are created during extraction, this stores the + /// entity that caused that creation for use in determining visibility. + pub original_entity: Option, } #[derive(Resource, Default)] @@ -390,6 +393,7 @@ pub fn extract_sprites( flip_y: sprite.flip_y, image_handle_id: handle.id(), anchor: sprite.anchor.as_vec(), + original_entity: None, }, ); } @@ -425,6 +429,7 @@ pub fn extract_sprites( flip_y: atlas_sprite.flip_y, image_handle_id: texture_atlas.texture.id(), anchor: atlas_sprite.anchor.as_vec(), + original_entity: None, }, ); } @@ -550,7 +555,9 @@ pub fn queue_sprites( .reserve(extracted_sprites.sprites.len()); for (entity, extracted_sprite) in extracted_sprites.sprites.iter() { - if !view_entities.contains(entity.index() as usize) { + let index = extracted_sprite.original_entity.unwrap_or(*entity).index(); + + if !view_entities.contains(index as usize) { continue; } diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index dc066b26ae6a5..ffb285deba577 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -85,6 +85,7 @@ pub fn extract_text2d_sprite( windows: Extract>>, text2d_query: Extract< Query<( + Entity, &ViewVisibility, &Text, &TextLayoutInfo, @@ -100,7 +101,9 @@ pub fn extract_text2d_sprite( .unwrap_or(1.0); let scaling = GlobalTransform::from_scale(Vec2::splat(scale_factor.recip()).extend(1.)); - for (view_visibility, text, text_layout_info, anchor, global_transform) in text2d_query.iter() { + for (original_entity, view_visibility, text, text_layout_info, anchor, global_transform) in + text2d_query.iter() + { if !view_visibility.get() { continue; } @@ -125,8 +128,9 @@ pub fn extract_text2d_sprite( } let atlas = texture_atlases.get(&atlas_info.texture_atlas).unwrap(); + let entity = commands.spawn_empty().id(); extracted_sprites.sprites.insert( - commands.spawn_empty().id(), + entity, ExtractedSprite { transform: transform * GlobalTransform::from_translation(position.extend(0.)), color, @@ -136,6 +140,7 @@ pub fn extract_text2d_sprite( flip_x: false, flip_y: false, anchor: Anchor::Center.as_vec(), + original_entity: Some(original_entity), }, ); }