Skip to content

Text2d bounding box fix #20148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
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
22 changes: 9 additions & 13 deletions crates/bevy_text/src/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use bevy_ecs::{
system::{Commands, Local, Query, Res, ResMut},
};
use bevy_image::prelude::*;
use bevy_math::Vec2;
use bevy_math::{Vec2, Vec3};
use bevy_reflect::{prelude::ReflectDefault, Reflect};
use bevy_render::sync_world::TemporaryRenderEntity;
use bevy_render::view::{self, Visibility, VisibilityClass};
Expand Down Expand Up @@ -186,6 +186,7 @@ pub fn extract_text2d_sprite(
let top_left = (Anchor::TOP_LEFT.0 - anchor.as_vec()) * size;
let transform =
*global_transform * GlobalTransform::from_translation(top_left.extend(0.)) * scaling;

let mut color = LinearRgba::WHITE;
let mut current_span = usize::MAX;

Expand Down Expand Up @@ -366,22 +367,17 @@ pub fn calculate_bounds_text2d(
text_bounds.width.unwrap_or(layout_info.size.x),
text_bounds.height.unwrap_or(layout_info.size.y),
);
let center = (-anchor.as_vec() * size + (size.y - layout_info.size.y) * Vec2::Y)
.extend(0.)
.into();

let half_extents = (0.5 * layout_info.size).extend(0.0).into();
let x1 = (Anchor::TOP_LEFT.0.x - anchor.as_vec().x) * size.x;
let x2 = (Anchor::TOP_LEFT.0.x - anchor.as_vec().x + 1.) * size.x;
let y1 = (Anchor::TOP_LEFT.0.y - anchor.as_vec().y - 1.) * size.y;
let y2 = (Anchor::TOP_LEFT.0.y - anchor.as_vec().y) * size.y;
let new_aabb = Aabb::from_min_max(Vec3::new(x1, y1, 0.), Vec3::new(x2, y2, 0.));

if let Some(mut aabb) = aabb {
*aabb = Aabb {
center,
half_extents,
};
*aabb = new_aabb;
} else {
commands.entity(entity).try_insert(Aabb {
center,
half_extents,
});
commands.entity(entity).try_insert(new_aabb);
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion examples/testbed/2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ mod text {
&mut commands,
300. * Vec3::X + y * Vec3::Y,
justify,
Some(TextBounds::new(150., 55.)),
Some(TextBounds::new(150., 60.)),
);
}

Expand Down Expand Up @@ -221,6 +221,9 @@ mod text {
Transform::from_translation(dest + Vec3::Z),
anchor,
DespawnOnExitState(super::Scene::Text),
ShowAabbGizmo {
color: Some(palettes::tailwind::AMBER_400.into()),
},
children![
(
TextSpan::new(format!("{}, {}\n", anchor.x, anchor.y)),
Expand Down
Loading