Skip to content
Closed
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
30 changes: 20 additions & 10 deletions crates/bevy_sprite/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,27 +459,37 @@ pub fn queue_sprites(
}

// Calculate vertex data for this item

let mut uvs = QUAD_UVS;
if extracted_sprite.flip_x {
uvs = [uvs[1], uvs[0], uvs[3], uvs[2]];
}
if extracted_sprite.flip_y {
uvs = [uvs[3], uvs[2], uvs[1], uvs[0]];
}

// By default, the size of the quad is the size of the texture
let mut quad_size = current_image_size;

// If a rect is specified, adjust UVs and the size of the quad
// If a rect is specified, adjust UVs and the size of the quad.
// The UVs are shrunk by half the size of a texel to prevent
// floating point inaccuracies from causing incorrect sampling.
if let Some(rect) = extracted_sprite.rect {
let rect_size = rect.size();
for uv in &mut uvs {
*uv = (rect.min + *uv * rect_size) / current_image_size;
let texel_half_extent = 1.0 / current_image_size / 2.0;
let shift = [
texel_half_extent * Vec2::new(1., -1.),
texel_half_extent * Vec2::new(-1., -1.),
texel_half_extent * Vec2::new(-1., 1.),
texel_half_extent * Vec2::new(1., 1.),
];

for i in 0..4usize {
uvs[i] = (rect.min + uvs[i] * rect_size) / current_image_size + shift[i];
}
quad_size = rect_size;
}

if extracted_sprite.flip_x {
uvs = [uvs[1], uvs[0], uvs[3], uvs[2]];
}
if extracted_sprite.flip_y {
uvs = [uvs[3], uvs[2], uvs[1], uvs[0]];
}

Comment on lines +486 to +492

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving these lines so the UV order is guaranteed when computing the shift values above.

// Override the size if a custom one is specified
if let Some(custom_size) = extracted_sprite.custom_size {
quad_size = custom_size;
Expand Down