Skip to content

Commit

Permalink
Eliminate GlobalTransform -> TRS conversions and add warning
Browse files Browse the repository at this point in the history
  • Loading branch information
HackerFoo committed Jul 15, 2022
1 parent 5ce99d1 commit e861074
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
13 changes: 6 additions & 7 deletions crates/bevy_text/src/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub fn extract_text2d_sprite(
text2d_query: Extract<Query<(Entity, &Visibility, &Text, &GlobalTransform, &Text2dSize)>>,
) {
let scale_factor = windows.scale_factor(WindowId::primary()) as f32;
for (entity, visibility, text, transform, calculated_size) in text2d_query.iter() {
for (entity, visibility, text, text_transform, calculated_size) in text2d_query.iter() {
if !visibility.is_visible {
continue;
}
Expand All @@ -86,9 +86,6 @@ pub fn extract_text2d_sprite(
HorizontalAlign::Right => Vec3::new(-width, 0.0, 0.0),
};

let mut text_transform = Transform::from(*transform);
text_transform.scale /= scale_factor;

for text_glyph in text_glyphs {
let color = text.sections[text_glyph.section_index]
.style
Expand All @@ -104,11 +101,13 @@ pub fn extract_text2d_sprite(
let glyph_transform = Transform::from_translation(
alignment_offset * scale_factor + text_glyph.position.extend(0.),
);

let transform = text_transform.mul_transform(glyph_transform);
// NOTE: Should match `bevy_ui::render::extract_text_uinodes`
let transform = *text_transform
* GlobalTransform::from_scale(Vec3::splat(scale_factor.recip()))
* glyph_transform;

extracted_sprites.sprites.push(ExtractedSprite {
transform: transform.into(),
transform,
color,
rect,
custom_size: None,
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_transform/src/components/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ impl Default for Transform {
}
}

/// The transform is expected to be non-degenerate and without shearing, or the output
/// will be invalid.
impl From<GlobalTransform> for Transform {
fn from(transform: GlobalTransform) -> Self {
transform.compute_transform()
Expand Down
7 changes: 3 additions & 4 deletions crates/bevy_ui/src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,9 @@ pub fn extract_text_uinodes(
let rect = atlas.textures[index];
let atlas_size = Some(atlas.size);

let (scale, rotation, translation) =
global_transform.to_scale_rotation_translation();
let extracted_transform = Mat4::from_rotation_translation(rotation, translation)
* Mat4::from_scale(scale / scale_factor)
// NOTE: Should match `bevy_text::text2d::extract_text2d_sprite`
let extracted_transform = global_transform.compute_matrix()
* Mat4::from_scale(Vec3::splat(scale_factor.recip()))
* Mat4::from_translation(
alignment_offset * scale_factor + text_glyph.position.extend(0.),
);
Expand Down

0 comments on commit e861074

Please sign in to comment.