Skip to content

Commit

Permalink
Implement rotation for Text2d
Browse files Browse the repository at this point in the history
  • Loading branch information
nside committed May 4, 2021
1 parent afaf4ad commit c4580c4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 36 deletions.
29 changes: 11 additions & 18 deletions crates/bevy_text/src/draw.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{PositionedGlyph, TextSection};
use bevy_math::{Mat4, Vec3};
use bevy_render::pipeline::IndexFormat;
use bevy_render::{
draw::{Draw, DrawContext, DrawError, Drawable},
mesh,
Expand All @@ -8,19 +10,18 @@ use bevy_render::{
renderer::{BindGroup, RenderResourceBindings, RenderResourceId},
};
use bevy_sprite::TextureAtlasSprite;
use bevy_transform::prelude::GlobalTransform;
use bevy_utils::tracing::error;

use crate::{PositionedGlyph, TextSection};
use bevy_render::pipeline::IndexFormat;

pub struct DrawableText<'a> {
pub render_resource_bindings: &'a mut RenderResourceBindings,
pub position: Vec3,
pub global_transform: GlobalTransform,
pub scale_factor: f32,
pub sections: &'a [TextSection],
pub text_glyphs: &'a Vec<PositionedGlyph>,
pub msaa: &'a Msaa,
pub font_quad_vertex_layout: &'a VertexBufferLayout,
pub alignment_offset: Vec3,
}

impl<'a> Drawable for DrawableText<'a> {
Expand Down Expand Up @@ -76,20 +77,12 @@ impl<'a> Drawable for DrawableText<'a> {
flip_y: false,
};

// To get the rendering right for non-one scaling factors, we need
// the sprite to be drawn in "physical" coordinates. This is because
// the shader uses the size of the sprite to control the size on
// screen. To accomplish this we make the sprite transform
// convert from physical coordinates to logical coordinates in
// addition to altering the origin. Since individual glyphs will
// already be in physical coordinates, we just need to convert the
// overall position to physical coordinates to get the sprites
// physical position.

let transform = Mat4::from_scale(Vec3::splat(1. / self.scale_factor))
* Mat4::from_translation(
self.position * self.scale_factor + tv.position.extend(0.),
);
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.));

let transform_buffer = context.get_uniform_buffer(&transform).unwrap();
let sprite_buffer = context.get_uniform_buffer(&sprite).unwrap();
Expand Down
25 changes: 12 additions & 13 deletions crates/bevy_text/src/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,25 @@ pub fn draw_text2d_system(
let (width, height) = (calculated_size.size.width, calculated_size.size.height);

if let Some(text_glyphs) = text_pipeline.get_glyphs(&entity) {
let position = global_transform.translation
+ match text.alignment.vertical {
VerticalAlign::Top => Vec3::ZERO,
VerticalAlign::Center => Vec3::new(0.0, -height * 0.5, 0.0),
VerticalAlign::Bottom => Vec3::new(0.0, -height, 0.0),
}
+ match text.alignment.horizontal {
HorizontalAlign::Left => Vec3::new(-width, 0.0, 0.0),
HorizontalAlign::Center => Vec3::new(-width * 0.5, 0.0, 0.0),
HorizontalAlign::Right => Vec3::ZERO,
};
let alignment_offset = match text.alignment.vertical {
VerticalAlign::Top => Vec3::ZERO,
VerticalAlign::Center => Vec3::new(0.0, -height * 0.5, 0.0),
VerticalAlign::Bottom => Vec3::new(0.0, -height, 0.0),
} + match text.alignment.horizontal {
HorizontalAlign::Left => Vec3::new(-width, 0.0, 0.0),
HorizontalAlign::Center => Vec3::new(-width * 0.5, 0.0, 0.0),
HorizontalAlign::Right => Vec3::ZERO,
};

let mut drawable_text = DrawableText {
render_resource_bindings: &mut render_resource_bindings,
position,
global_transform: *global_transform,
scale_factor,
msaa: &msaa,
text_glyphs: &text_glyphs.glyphs,
font_quad_vertex_layout: &font_quad_vertex_layout,
scale_factor,
sections: &text.sections,
alignment_offset: alignment_offset * scale_factor,
};

drawable_text.draw(&mut draw, &mut context).unwrap();
Expand Down
5 changes: 2 additions & 3 deletions crates/bevy_ui/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,15 @@ pub fn draw_text_system(
}

if let Some(text_glyphs) = text_pipeline.get_glyphs(&entity) {
let position = global_transform.translation - (node.size / 2.0).extend(0.0);

let mut drawable_text = DrawableText {
render_resource_bindings: &mut render_resource_bindings,
position,
global_transform: *global_transform,
scale_factor: scale_factor as f32,
msaa: &msaa,
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),
};

drawable_text.draw(&mut draw, &mut context).unwrap();
Expand Down
5 changes: 3 additions & 2 deletions examples/2d/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {

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

0 comments on commit c4580c4

Please sign in to comment.