diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index e124b71d73c07b..8beffe4cb9d535 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -23,7 +23,7 @@ use bevy_render::{ view::{ExtractedView, ViewUniforms, Visibility}, Extract, RenderApp, RenderStage, }; -use bevy_sprite::{Rect, SpriteAssetEvents, TextureAtlas}; +use bevy_sprite::{Rect, SpriteAssetEvents, TextureAtlas, TextureSheet}; use bevy_text::{DefaultTextPipeline, Text}; use bevy_transform::components::GlobalTransform; use bevy_utils::FloatOrd; @@ -176,6 +176,7 @@ pub struct ExtractedUiNodes { pub fn extract_uinodes( mut extracted_uinodes: ResMut, images: Extract>>, + texture_atlases: Extract>>, uinode_query: Extract< Query<( &Node, @@ -183,13 +184,15 @@ pub fn extract_uinodes( &UiColor, &UiImage, &Visibility, + Option<&TextureSheet>, Option<&CalculatedClip>, )>, >, ) { extracted_uinodes.uinodes.clear(); - for (uinode, transform, color, image, visibility, clip) in uinode_query.iter() { - if !visibility.is_visible { + for (uinode, transform, color, image, visibility, sheet, clip) in uinode_query.iter() { + // Skip if the node is invisible or if its size is set to zero (e.g. when a parent is set to `Display::None`) + if !visibility.is_visible || uinode.size == Vec2::ZERO { continue; } let image = image.0.clone_weak(); @@ -197,15 +200,29 @@ pub fn extract_uinodes( if !images.contains(&image) { continue; } + let (atlas_size, rect_min) = sheet + .and_then(|s| { + texture_atlases + .get(&s.texture_atlas) + .map(|a| (a, s.texture_index)) + }) + .and_then(|(atlas, index)| { + atlas + .textures + .get(index) + .map(|rect| (Some(atlas.size), rect.min)) + }) + .unwrap_or((None, Vec2::ZERO)); + extracted_uinodes.uinodes.push(ExtractedUiNode { transform: transform.compute_matrix(), color: color.0, rect: bevy_sprite::Rect { - min: Vec2::ZERO, - max: uinode.size, + min: rect_min, + max: rect_min + uinode.size, }, image, - atlas_size: None, + atlas_size, clip: clip.map(|clip| clip.clip), }); } diff --git a/examples/ui/button.rs b/examples/ui/button.rs index 456ed9f1b400f2..b9158b6de0edc3 100644 --- a/examples/ui/button.rs +++ b/examples/ui/button.rs @@ -10,6 +10,7 @@ fn main() { .insert_resource(WinitSettings::desktop_app()) .add_startup_system(setup) .add_system(button_system) + .add_system(sheet_button_system) .run(); } @@ -43,9 +44,35 @@ fn button_system( } } -fn setup(mut commands: Commands, asset_server: Res) { +fn sheet_button_system( + mut interaction_query: Query< + (&Interaction, &mut TextureSheet), + (Changed, With