diff --git a/crates/bevy_text/src/draw.rs b/crates/bevy_text/src/draw.rs index c2828e5252bbe..e4dd639d37598 100644 --- a/crates/bevy_text/src/draw.rs +++ b/crates/bevy_text/src/draw.rs @@ -77,12 +77,13 @@ impl<'a> Drawable for DrawableText<'a> { flip_y: false, }; - let scale_transform = Mat4::from_scale(Vec3::splat(1. / self.scale_factor)); let transform = Mat4::from_rotation_translation( self.global_transform.rotation, self.global_transform.translation, - ) * scale_transform - * Mat4::from_translation(self.alignment_offset + tv.position.extend(0.)); + ) * Mat4::from_scale(self.global_transform.scale / self.scale_factor) + * Mat4::from_translation( + self.alignment_offset * self.scale_factor + tv.position.extend(0.), + ); let transform_buffer = context.get_uniform_buffer(&transform).unwrap(); let sprite_buffer = context.get_uniform_buffer(&sprite).unwrap(); diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index a48fd4193bda7..b2471bee128e2 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -93,13 +93,13 @@ pub fn draw_text2d_system( if let Some(text_glyphs) = text_pipeline.get_glyphs(&entity) { let alignment_offset = match text.alignment.vertical { - VerticalAlign::Top => Vec3::ZERO, + VerticalAlign::Top => Vec3::new(0.0, -height, 0.0), VerticalAlign::Center => Vec3::new(0.0, -height * 0.5, 0.0), - VerticalAlign::Bottom => Vec3::new(0.0, -height, 0.0), + VerticalAlign::Bottom => Vec3::ZERO, } + match text.alignment.horizontal { - HorizontalAlign::Left => Vec3::new(-width, 0.0, 0.0), + HorizontalAlign::Left => Vec3::ZERO, HorizontalAlign::Center => Vec3::new(-width * 0.5, 0.0, 0.0), - HorizontalAlign::Right => Vec3::ZERO, + HorizontalAlign::Right => Vec3::new(-width, 0.0, 0.0), }; let mut drawable_text = DrawableText { @@ -110,7 +110,7 @@ pub fn draw_text2d_system( text_glyphs: &text_glyphs.glyphs, font_quad_vertex_layout: &font_quad_vertex_layout, sections: &text.sections, - alignment_offset: alignment_offset * scale_factor, + alignment_offset, }; drawable_text.draw(&mut draw, &mut context).unwrap(); diff --git a/crates/bevy_ui/src/widget/text.rs b/crates/bevy_ui/src/widget/text.rs index 469ac3573e368..e5657e71da5d0 100644 --- a/crates/bevy_ui/src/widget/text.rs +++ b/crates/bevy_ui/src/widget/text.rs @@ -175,7 +175,7 @@ pub fn draw_text_system( text_glyphs: &text_glyphs.glyphs, font_quad_vertex_layout: &vertex_buffer_layout, sections: &text.sections, - alignment_offset: (node.size / -2.0).extend(0.0) * (scale_factor as f32), + alignment_offset: (node.size / -2.0).extend(0.0), }; drawable_text.draw(&mut draw, &mut context).unwrap(); diff --git a/examples/2d/text2d.rs b/examples/2d/text2d.rs index db71262ea1e1e..40512d498b4be 100644 --- a/examples/2d/text2d.rs +++ b/examples/2d/text2d.rs @@ -8,33 +8,65 @@ fn main() { .run(); } +struct AnimateTranslation; +struct AnimateRotation; +struct AnimateScale; + fn setup(mut commands: Commands, asset_server: Res) { + let font = asset_server.load("fonts/FiraSans-Bold.ttf"); + let text_style = TextStyle { + font, + font_size: 60.0, + color: Color::WHITE, + }; + let text_alignment = TextAlignment { + vertical: VerticalAlign::Center, + horizontal: HorizontalAlign::Center, + }; // 2d camera commands.spawn_bundle(OrthographicCameraBundle::new_2d()); - commands.spawn_bundle(Text2dBundle { - text: Text::with_section( - "This text is in the 2D scene.", - TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), - font_size: 60.0, - color: Color::WHITE, - }, - TextAlignment { - vertical: VerticalAlign::Center, - horizontal: HorizontalAlign::Center, - }, - ), - ..Default::default() - }); + // Demonstrate changing translation + commands + .spawn_bundle(Text2dBundle { + text: Text::with_section("translation", text_style.clone(), text_alignment), + ..Default::default() + }) + .insert(AnimateTranslation); + // Demonstrate changing rotation + commands + .spawn_bundle(Text2dBundle { + text: Text::with_section("rotation", text_style.clone(), text_alignment), + ..Default::default() + }) + .insert(AnimateRotation); + // Demonstrate changing scale + commands + .spawn_bundle(Text2dBundle { + text: Text::with_section("scale", text_style, text_alignment), + ..Default::default() + }) + .insert(AnimateScale); } -fn animate(time: Res