Skip to content

Commit

Permalink
Fix text2d view-visibility (bevyengine#10100)
Browse files Browse the repository at this point in the history
# Objective

Fixes bevyengine#9676
Possible alternative to bevyengine#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<Entity>` 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%|
  • Loading branch information
rparrett authored and ameknite committed Nov 6, 2023
1 parent 8b5fb7e commit ec14490
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 8 additions & 1 deletion crates/bevy_sprite/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entity>,
}

#[derive(Resource, Default)]
Expand Down Expand Up @@ -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,
},
);
}
Expand Down Expand Up @@ -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,
},
);
}
Expand Down Expand Up @@ -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;
}

Expand Down
9 changes: 7 additions & 2 deletions crates/bevy_text/src/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub fn extract_text2d_sprite(
windows: Extract<Query<&Window, With<PrimaryWindow>>>,
text2d_query: Extract<
Query<(
Entity,
&ViewVisibility,
&Text,
&TextLayoutInfo,
Expand All @@ -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;
}
Expand All @@ -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,
Expand All @@ -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),
},
);
}
Expand Down

0 comments on commit ec14490

Please sign in to comment.