Skip to content

Commit c4580c4

Browse files
committed
Implement rotation for Text2d
1 parent afaf4ad commit c4580c4

File tree

4 files changed

+28
-36
lines changed

4 files changed

+28
-36
lines changed

crates/bevy_text/src/draw.rs

+11-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
use crate::{PositionedGlyph, TextSection};
12
use bevy_math::{Mat4, Vec3};
3+
use bevy_render::pipeline::IndexFormat;
24
use bevy_render::{
35
draw::{Draw, DrawContext, DrawError, Drawable},
46
mesh,
@@ -8,19 +10,18 @@ use bevy_render::{
810
renderer::{BindGroup, RenderResourceBindings, RenderResourceId},
911
};
1012
use bevy_sprite::TextureAtlasSprite;
13+
use bevy_transform::prelude::GlobalTransform;
1114
use bevy_utils::tracing::error;
1215

13-
use crate::{PositionedGlyph, TextSection};
14-
use bevy_render::pipeline::IndexFormat;
15-
1616
pub struct DrawableText<'a> {
1717
pub render_resource_bindings: &'a mut RenderResourceBindings,
18-
pub position: Vec3,
18+
pub global_transform: GlobalTransform,
1919
pub scale_factor: f32,
2020
pub sections: &'a [TextSection],
2121
pub text_glyphs: &'a Vec<PositionedGlyph>,
2222
pub msaa: &'a Msaa,
2323
pub font_quad_vertex_layout: &'a VertexBufferLayout,
24+
pub alignment_offset: Vec3,
2425
}
2526

2627
impl<'a> Drawable for DrawableText<'a> {
@@ -76,20 +77,12 @@ impl<'a> Drawable for DrawableText<'a> {
7677
flip_y: false,
7778
};
7879

79-
// To get the rendering right for non-one scaling factors, we need
80-
// the sprite to be drawn in "physical" coordinates. This is because
81-
// the shader uses the size of the sprite to control the size on
82-
// screen. To accomplish this we make the sprite transform
83-
// convert from physical coordinates to logical coordinates in
84-
// addition to altering the origin. Since individual glyphs will
85-
// already be in physical coordinates, we just need to convert the
86-
// overall position to physical coordinates to get the sprites
87-
// physical position.
88-
89-
let transform = Mat4::from_scale(Vec3::splat(1. / self.scale_factor))
90-
* Mat4::from_translation(
91-
self.position * self.scale_factor + tv.position.extend(0.),
92-
);
80+
let scale_transform = Mat4::from_scale(Vec3::splat(1. / self.scale_factor));
81+
let transform = Mat4::from_rotation_translation(
82+
self.global_transform.rotation,
83+
self.global_transform.translation,
84+
) * scale_transform
85+
* Mat4::from_translation(self.alignment_offset + tv.position.extend(0.));
9386

9487
let transform_buffer = context.get_uniform_buffer(&transform).unwrap();
9588
let sprite_buffer = context.get_uniform_buffer(&sprite).unwrap();

crates/bevy_text/src/text2d.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -92,26 +92,25 @@ pub fn draw_text2d_system(
9292
let (width, height) = (calculated_size.size.width, calculated_size.size.height);
9393

9494
if let Some(text_glyphs) = text_pipeline.get_glyphs(&entity) {
95-
let position = global_transform.translation
96-
+ match text.alignment.vertical {
97-
VerticalAlign::Top => Vec3::ZERO,
98-
VerticalAlign::Center => Vec3::new(0.0, -height * 0.5, 0.0),
99-
VerticalAlign::Bottom => Vec3::new(0.0, -height, 0.0),
100-
}
101-
+ match text.alignment.horizontal {
102-
HorizontalAlign::Left => Vec3::new(-width, 0.0, 0.0),
103-
HorizontalAlign::Center => Vec3::new(-width * 0.5, 0.0, 0.0),
104-
HorizontalAlign::Right => Vec3::ZERO,
105-
};
95+
let alignment_offset = match text.alignment.vertical {
96+
VerticalAlign::Top => Vec3::ZERO,
97+
VerticalAlign::Center => Vec3::new(0.0, -height * 0.5, 0.0),
98+
VerticalAlign::Bottom => Vec3::new(0.0, -height, 0.0),
99+
} + match text.alignment.horizontal {
100+
HorizontalAlign::Left => Vec3::new(-width, 0.0, 0.0),
101+
HorizontalAlign::Center => Vec3::new(-width * 0.5, 0.0, 0.0),
102+
HorizontalAlign::Right => Vec3::ZERO,
103+
};
106104

107105
let mut drawable_text = DrawableText {
108106
render_resource_bindings: &mut render_resource_bindings,
109-
position,
107+
global_transform: *global_transform,
108+
scale_factor,
110109
msaa: &msaa,
111110
text_glyphs: &text_glyphs.glyphs,
112111
font_quad_vertex_layout: &font_quad_vertex_layout,
113-
scale_factor,
114112
sections: &text.sections,
113+
alignment_offset: alignment_offset * scale_factor,
115114
};
116115

117116
drawable_text.draw(&mut draw, &mut context).unwrap();

crates/bevy_ui/src/widget/text.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,15 @@ pub fn draw_text_system(
167167
}
168168

169169
if let Some(text_glyphs) = text_pipeline.get_glyphs(&entity) {
170-
let position = global_transform.translation - (node.size / 2.0).extend(0.0);
171-
172170
let mut drawable_text = DrawableText {
173171
render_resource_bindings: &mut render_resource_bindings,
174-
position,
172+
global_transform: *global_transform,
175173
scale_factor: scale_factor as f32,
176174
msaa: &msaa,
177175
text_glyphs: &text_glyphs.glyphs,
178176
font_quad_vertex_layout: &vertex_buffer_layout,
179177
sections: &text.sections,
178+
alignment_offset: (node.size / -2.0).extend(0.0) * (scale_factor as f32),
180179
};
181180

182181
drawable_text.draw(&mut draw, &mut context).unwrap();

examples/2d/text2d.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
3030

3131
fn animate(time: Res<Time>, mut query: Query<&mut Transform, With<Text>>) {
3232
// `Transform.translation` will determine the location of the text.
33-
// `Transform.scale` and `Transform.rotation` do not yet affect text (though you can set the
34-
// size of the text via `Text.style.font_size`)
33+
// `Transform.scale` (though you can set the size of the text via
34+
// `Text.style.font_size`)
3535
for mut transform in query.iter_mut() {
3636
transform.translation.x = 100.0 * time.seconds_since_startup().sin() as f32;
3737
transform.translation.y = 100.0 * time.seconds_since_startup().cos() as f32;
38+
transform.rotation = Quat::from_rotation_z(time.seconds_since_startup().cos() as f32);
3839
}
3940
}

0 commit comments

Comments
 (0)