From 25870498f1a8ee71cd561a8efddb5f411531d736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Mon, 24 Oct 2022 14:33:51 +0000 Subject: [PATCH] Fix clipping in UI (#6351) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Objective - Clipping (visible in the UI example with text scrolling) is funky - Fixes #6287 ## Solution - Fix UV calculation: - correct order for values (issue introduced in #6000) - add the `y` values instead of subtracting them now that vertical order is reversed - take scale factor into account (bug already present before reversing the order) - While around clipping, I changed clip to only mutate when changed No more funkiness! 😞 Screenshot 2022-10-23 at 22 44 18 --- crates/bevy_ui/src/render/mod.rs | 21 +++++++++++++-------- crates/bevy_ui/src/update.rs | 4 +++- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index 835d0fbc972027..3e836cc303f7c5 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -166,6 +166,7 @@ pub struct ExtractedUiNode { pub image: Handle, pub atlas_size: Option, pub clip: Option, + pub scale_factor: f32, } #[derive(Resource, Default)] @@ -176,6 +177,7 @@ pub struct ExtractedUiNodes { pub fn extract_uinodes( mut extracted_uinodes: ResMut, images: Extract>>, + windows: Extract>, uinode_query: Extract< Query<( &Node, @@ -187,6 +189,7 @@ pub fn extract_uinodes( )>, >, ) { + let scale_factor = windows.scale_factor(WindowId::primary()) as f32; extracted_uinodes.uinodes.clear(); for (uinode, transform, color, image, visibility, clip) in uinode_query.iter() { if !visibility.is_visible() { @@ -211,6 +214,7 @@ pub fn extract_uinodes( image, atlas_size: None, clip: clip.map(|clip| clip.clip), + scale_factor, }); } } @@ -330,6 +334,7 @@ pub fn extract_text_uinodes( image: texture, atlas_size, clip: clip.map(|clip| clip.clip), + scale_factor, }); } } @@ -464,20 +469,20 @@ pub fn prepare_uinodes( let atlas_extent = extracted_uinode.atlas_size.unwrap_or(uinode_rect.max); let uvs = [ Vec2::new( - uinode_rect.min.x + positions_diff[3].x, - uinode_rect.min.y - positions_diff[3].y, + uinode_rect.min.x + positions_diff[0].x * extracted_uinode.scale_factor, + uinode_rect.min.y + positions_diff[0].y * extracted_uinode.scale_factor, ), Vec2::new( - uinode_rect.max.x + positions_diff[2].x, - uinode_rect.min.y - positions_diff[2].y, + uinode_rect.max.x + positions_diff[1].x * extracted_uinode.scale_factor, + uinode_rect.min.y + positions_diff[1].y * extracted_uinode.scale_factor, ), Vec2::new( - uinode_rect.max.x + positions_diff[1].x, - uinode_rect.max.y - positions_diff[1].y, + uinode_rect.max.x + positions_diff[2].x * extracted_uinode.scale_factor, + uinode_rect.max.y + positions_diff[2].y * extracted_uinode.scale_factor, ), Vec2::new( - uinode_rect.min.x + positions_diff[0].x, - uinode_rect.max.y - positions_diff[0].y, + uinode_rect.min.x + positions_diff[3].x * extracted_uinode.scale_factor, + uinode_rect.max.y + positions_diff[3].y * extracted_uinode.scale_factor, ), ] .map(|pos| pos / atlas_extent); diff --git a/crates/bevy_ui/src/update.rs b/crates/bevy_ui/src/update.rs index 4b4398df1b5d93..aac455bb163350 100644 --- a/crates/bevy_ui/src/update.rs +++ b/crates/bevy_ui/src/update.rs @@ -99,7 +99,9 @@ fn update_clipping( commands.entity(entity).insert(CalculatedClip { clip }); } (Some(clip), Some(mut old_clip)) => { - *old_clip = CalculatedClip { clip }; + if old_clip.clip != clip { + *old_clip = CalculatedClip { clip }; + } } }