Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the UV calculations for clipped and flipped ImageNodes #8195

Merged
merged 3 commits into from
Apr 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,8 @@ pub fn prepare_uinodes(
current_batch_handle = extracted_uinode.image.clone_weak();
}

let uinode_rect = extracted_uinode.rect;
let mut uinode_rect = extracted_uinode.rect;

let rect_size = uinode_rect.size().extend(1.0);

// Specify the corners of the node
Expand All @@ -421,7 +422,7 @@ pub fn prepare_uinodes(

// Calculate the effect of clipping
// Note: this won't work with rotation/scaling, but that's much more complex (may need more that 2 quads)
let positions_diff = if let Some(clip) = extracted_uinode.clip {
let mut positions_diff = if let Some(clip) = extracted_uinode.clip {
[
Vec2::new(
f32::max(clip.min.x - positions[0].x, 0.),
Expand Down Expand Up @@ -471,7 +472,21 @@ pub fn prepare_uinodes(
[Vec2::ZERO, Vec2::X, Vec2::ONE, Vec2::Y]
} else {
let atlas_extent = extracted_uinode.atlas_size.unwrap_or(uinode_rect.max);
let mut uvs = [
if extracted_uinode.flip_x {
std::mem::swap(&mut uinode_rect.max.x, &mut uinode_rect.min.x);
positions_diff[0].x *= -1.;
positions_diff[1].x *= -1.;
positions_diff[2].x *= -1.;
positions_diff[3].x *= -1.;
}
if extracted_uinode.flip_y {
std::mem::swap(&mut uinode_rect.max.y, &mut uinode_rect.min.y);
positions_diff[0].y *= -1.;
positions_diff[1].y *= -1.;
positions_diff[2].y *= -1.;
positions_diff[3].y *= -1.;
}
[
Vec2::new(
uinode_rect.min.x + positions_diff[0].x,
uinode_rect.min.y + positions_diff[0].y,
Expand All @@ -489,15 +504,7 @@ pub fn prepare_uinodes(
uinode_rect.max.y + positions_diff[3].y,
),
]
.map(|pos| pos / atlas_extent);

if extracted_uinode.flip_x {
uvs = [uvs[1], uvs[0], uvs[3], uvs[2]];
}
if extracted_uinode.flip_y {
uvs = [uvs[3], uvs[2], uvs[1], uvs[0]];
}
uvs
.map(|pos| pos / atlas_extent)
};

let color = extracted_uinode.color.as_linear_rgba_f32();
Expand Down