Path: blob/main/crates/bevy_transform/src/components/global_transform.rs
9353 views
use core::ops::Mul;12use super::Transform;3use bevy_math::{ops, Affine3A, Dir3, Isometry3d, Mat4, Quat, Vec3, Vec3A};4use derive_more::derive::From;56#[cfg(all(feature = "bevy_reflect", feature = "serialize"))]7use bevy_reflect::{ReflectDeserialize, ReflectSerialize};89#[cfg(feature = "bevy-support")]10use bevy_ecs::component::Component;1112#[cfg(feature = "bevy_reflect")]13use {14bevy_ecs::reflect::ReflectComponent,15bevy_reflect::{std_traits::ReflectDefault, Reflect},16};1718/// [`GlobalTransform`] is an affine transformation from entity-local coordinates to worldspace coordinates.19///20/// You cannot directly mutate [`GlobalTransform`]; instead, you change an entity's transform by manipulating21/// its [`Transform`], which indirectly causes Bevy to update its [`GlobalTransform`].22///23/// * To get the global transform of an entity, you should get its [`GlobalTransform`].24/// * For transform hierarchies to work correctly, you must have both a [`Transform`] and a [`GlobalTransform`].25/// [`GlobalTransform`] is automatically inserted whenever [`Transform`] is inserted.26///27/// ## [`Transform`] and [`GlobalTransform`]28///29/// [`Transform`] transforms an entity relative to its parent's reference frame, or relative to world space coordinates,30/// if it doesn't have a [`ChildOf`](bevy_ecs::hierarchy::ChildOf) component.31///32/// [`GlobalTransform`] is managed by Bevy; it is computed by successively applying the [`Transform`] of each ancestor33/// entity which has a Transform. This is done automatically by Bevy-internal systems in the [`TransformSystems::Propagate`]34/// system set.35///36/// This system runs during [`PostUpdate`](bevy_app::PostUpdate). If you37/// update the [`Transform`] of an entity in this schedule or after, you will notice a 1 frame lag38/// before the [`GlobalTransform`] is updated.39///40/// [`TransformSystems::Propagate`]: crate::TransformSystems::Propagate41///42/// # Examples43///44/// - [`transform`][transform_example]45///46/// [transform_example]: https://github.com/bevyengine/bevy/blob/latest/examples/transforms/transform.rs47#[derive(Debug, PartialEq, Clone, Copy, From)]48#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]49#[cfg_attr(feature = "bevy-support", derive(Component))]50#[cfg_attr(51feature = "bevy_reflect",52derive(Reflect),53reflect(Component, Default, PartialEq, Debug, Clone)54)]55#[cfg_attr(56all(feature = "bevy_reflect", feature = "serialize"),57reflect(Serialize, Deserialize)58)]59pub struct GlobalTransform(Affine3A);6061macro_rules! impl_local_axis {62($pos_name: ident, $neg_name: ident, $axis: ident) => {63#[doc=core::concat!("Return the local ", core::stringify!($pos_name), " vector (", core::stringify!($axis) ,").")]64#[inline]65pub fn $pos_name(&self) -> Dir3 {66Dir3::new_unchecked((self.0.matrix3 * Vec3::$axis).normalize())67}6869#[doc=core::concat!("Return the local ", core::stringify!($neg_name), " vector (-", core::stringify!($axis) ,").")]70#[inline]71pub fn $neg_name(&self) -> Dir3 {72-self.$pos_name()73}74};75}7677impl GlobalTransform {78/// An identity [`GlobalTransform`] that maps all points in space to themselves.79pub const IDENTITY: Self = Self(Affine3A::IDENTITY);8081#[doc(hidden)]82#[inline]83pub fn from_xyz(x: f32, y: f32, z: f32) -> Self {84Self::from_translation(Vec3::new(x, y, z))85}8687#[doc(hidden)]88#[inline]89pub fn from_translation(translation: Vec3) -> Self {90GlobalTransform(Affine3A::from_translation(translation))91}9293#[doc(hidden)]94#[inline]95pub fn from_rotation(rotation: Quat) -> Self {96GlobalTransform(Affine3A::from_rotation_translation(rotation, Vec3::ZERO))97}9899#[doc(hidden)]100#[inline]101pub fn from_scale(scale: Vec3) -> Self {102GlobalTransform(Affine3A::from_scale(scale))103}104105#[doc(hidden)]106#[inline]107pub fn from_isometry(iso: Isometry3d) -> Self {108Self(iso.into())109}110111/// Returns the 3d affine transformation matrix as a [`Mat4`].112#[inline]113pub fn to_matrix(&self) -> Mat4 {114Mat4::from(self.0)115}116117/// Returns the 3d affine transformation matrix as an [`Affine3A`].118#[inline]119pub fn affine(&self) -> Affine3A {120self.0121}122123/// Returns the transformation as a [`Transform`].124///125/// The transform is expected to be non-degenerate and without shearing, or the output126/// will be invalid.127#[inline]128pub fn compute_transform(&self) -> Transform {129let (scale, rotation, translation) = self.0.to_scale_rotation_translation();130Transform {131translation,132rotation,133scale,134}135}136137/// Computes a Scale-Rotation-Translation decomposition of the transformation and returns138/// the isometric part as an [isometry]. Any scaling done by the transformation will be ignored.139/// Note: this is a somewhat costly and lossy conversion.140///141/// The transform is expected to be non-degenerate and without shearing, or the output142/// will be invalid.143///144/// [isometry]: Isometry3d145#[inline]146pub fn to_isometry(&self) -> Isometry3d {147let (_, rotation, translation) = self.0.to_scale_rotation_translation();148Isometry3d::new(translation, rotation)149}150151/// Returns the [`Transform`] `self` would have if it was a child of an entity152/// with the `parent` [`GlobalTransform`].153///154/// This is useful if you want to "reparent" an [`Entity`](bevy_ecs::entity::Entity).155/// Say you have an entity `e1` that you want to turn into a child of `e2`,156/// but you want `e1` to keep the same global transform, even after re-parenting. You would use:157///158/// ```159/// # use bevy_transform::prelude::{GlobalTransform, Transform};160/// # use bevy_ecs::prelude::{Entity, Query, Component, Commands, ChildOf};161/// #[derive(Component)]162/// struct ToReparent {163/// new_parent: Entity,164/// }165/// fn reparent_system(166/// mut commands: Commands,167/// mut targets: Query<(&mut Transform, Entity, &GlobalTransform, &ToReparent)>,168/// transforms: Query<&GlobalTransform>,169/// ) {170/// for (mut transform, entity, initial, to_reparent) in targets.iter_mut() {171/// if let Ok(parent_transform) = transforms.get(to_reparent.new_parent) {172/// *transform = initial.reparented_to(parent_transform);173/// commands.entity(entity)174/// .remove::<ToReparent>()175/// .insert(ChildOf(to_reparent.new_parent));176/// }177/// }178/// }179/// ```180///181/// The transform is expected to be non-degenerate and without shearing, or the output182/// will be invalid.183#[inline]184pub fn reparented_to(&self, parent: &GlobalTransform) -> Transform {185let relative_affine = parent.affine().inverse() * self.affine();186let (scale, rotation, translation) = relative_affine.to_scale_rotation_translation();187Transform {188translation,189rotation,190scale,191}192}193194/// Extracts `scale`, `rotation` and `translation` from `self`.195///196/// The transform is expected to be non-degenerate and without shearing, or the output197/// will be invalid.198#[inline]199pub fn to_scale_rotation_translation(&self) -> (Vec3, Quat, Vec3) {200self.0.to_scale_rotation_translation()201}202203impl_local_axis!(right, left, X);204impl_local_axis!(up, down, Y);205impl_local_axis!(back, forward, Z);206207/// Get the translation as a [`Vec3`].208#[inline]209pub fn translation(&self) -> Vec3 {210self.0.translation.into()211}212213/// Get the translation as a [`Vec3A`].214#[inline]215pub fn translation_vec3a(&self) -> Vec3A {216self.0.translation217}218219/// Get the rotation as a [`Quat`].220///221/// The transform is expected to be non-degenerate and without shearing, or the output will be invalid.222///223/// # Warning224///225/// This is calculated using `to_scale_rotation_translation`, meaning that you226/// should probably use it directly if you also need translation or scale.227#[inline]228pub fn rotation(&self) -> Quat {229self.to_scale_rotation_translation().1230}231232/// Get the scale as a [`Vec3`].233///234/// The transform is expected to be non-degenerate and without shearing, or the output will be invalid.235///236/// Some of the computations overlap with `to_scale_rotation_translation`, which means you should use237/// it instead if you also need rotation.238#[inline]239pub fn scale(&self) -> Vec3 {240//Formula based on glam's implementation https://github.com/bitshifter/glam-rs/blob/2e4443e70c709710dfb25958d866d29b11ed3e2b/src/f32/affine3a.rs#L290241let det = self.0.matrix3.determinant();242Vec3::new(243self.0.matrix3.x_axis.length() * ops::copysign(1., det),244self.0.matrix3.y_axis.length(),245self.0.matrix3.z_axis.length(),246)247}248249/// Get an upper bound of the radius from the given `extents`.250#[inline]251pub fn radius_vec3a(&self, extents: Vec3A) -> f32 {252(self.0.matrix3 * extents).length()253}254255/// Transforms the given point from local space to global space, applying shear, scale, rotation and translation.256///257/// It can be used like this:258///259/// ```260/// # use bevy_transform::prelude::{GlobalTransform};261/// # use bevy_math::prelude::Vec3;262/// let global_transform = GlobalTransform::from_xyz(1., 2., 3.);263/// let local_point = Vec3::new(1., 2., 3.);264/// let global_point = global_transform.transform_point(local_point);265/// assert_eq!(global_point, Vec3::new(2., 4., 6.));266/// ```267///268/// ```269/// # use bevy_transform::prelude::{GlobalTransform};270/// # use bevy_math::Vec3;271/// let global_point = Vec3::new(2., 4., 6.);272/// let global_transform = GlobalTransform::from_xyz(1., 2., 3.);273/// let local_point = global_transform.affine().inverse().transform_point3(global_point);274/// assert_eq!(local_point, Vec3::new(1., 2., 3.))275/// ```276///277/// To apply shear, scale, and rotation *without* applying translation, different functions are available:278/// ```279/// # use bevy_transform::prelude::{GlobalTransform};280/// # use bevy_math::prelude::Vec3;281/// let global_transform = GlobalTransform::from_xyz(1., 2., 3.);282/// let local_direction = Vec3::new(1., 2., 3.);283/// let global_direction = global_transform.affine().transform_vector3(local_direction);284/// assert_eq!(global_direction, Vec3::new(1., 2., 3.));285/// let roundtripped_local_direction = global_transform.affine().inverse().transform_vector3(global_direction);286/// assert_eq!(roundtripped_local_direction, local_direction);287/// ```288#[inline]289pub fn transform_point(&self, point: Vec3) -> Vec3 {290self.0.transform_point3(point)291}292293/// Multiplies `self` with `transform` component by component, returning the294/// resulting [`GlobalTransform`]295#[inline]296pub fn mul_transform(&self, transform: Transform) -> Self {297Self(self.0 * transform.compute_affine())298}299}300301impl Default for GlobalTransform {302fn default() -> Self {303Self::IDENTITY304}305}306307impl From<Transform> for GlobalTransform {308fn from(transform: Transform) -> Self {309Self(transform.compute_affine())310}311}312313impl From<Mat4> for GlobalTransform {314fn from(world_from_local: Mat4) -> Self {315Self(Affine3A::from_mat4(world_from_local))316}317}318319impl Mul<GlobalTransform> for GlobalTransform {320type Output = GlobalTransform;321322#[inline]323fn mul(self, global_transform: GlobalTransform) -> Self::Output {324GlobalTransform(self.0 * global_transform.0)325}326}327328impl Mul<Transform> for GlobalTransform {329type Output = GlobalTransform;330331#[inline]332fn mul(self, transform: Transform) -> Self::Output {333self.mul_transform(transform)334}335}336337impl Mul<Vec3> for GlobalTransform {338type Output = Vec3;339340#[inline]341fn mul(self, value: Vec3) -> Self::Output {342self.transform_point(value)343}344}345346#[cfg(test)]347mod test {348use super::*;349350use bevy_math::EulerRot::XYZ;351352fn transform_equal(left: GlobalTransform, right: Transform) -> bool {353left.0.abs_diff_eq(right.compute_affine(), 0.01)354}355356#[test]357fn reparented_to_transform_identity() {358fn reparent_to_same(t1: GlobalTransform, t2: GlobalTransform) -> Transform {359t2.mul_transform(t1.into()).reparented_to(&t2)360}361let t1 = GlobalTransform::from(Transform {362translation: Vec3::new(1034.0, 34.0, -1324.34),363rotation: Quat::from_euler(XYZ, 1.0, 0.9, 2.1),364scale: Vec3::new(1.0, 1.0, 1.0),365});366let t2 = GlobalTransform::from(Transform {367translation: Vec3::new(0.0, -54.493, 324.34),368rotation: Quat::from_euler(XYZ, 1.9, 0.3, 3.0),369scale: Vec3::new(1.345, 1.345, 1.345),370});371let retransformed = reparent_to_same(t1, t2);372assert!(373transform_equal(t1, retransformed),374"t1:{:#?} retransformed:{:#?}",375t1.compute_transform(),376retransformed,377);378}379#[test]380fn reparented_usecase() {381let t1 = GlobalTransform::from(Transform {382translation: Vec3::new(1034.0, 34.0, -1324.34),383rotation: Quat::from_euler(XYZ, 0.8, 1.9, 2.1),384scale: Vec3::new(10.9, 10.9, 10.9),385});386let t2 = GlobalTransform::from(Transform {387translation: Vec3::new(28.0, -54.493, 324.34),388rotation: Quat::from_euler(XYZ, 0.0, 3.1, 0.1),389scale: Vec3::new(0.9, 0.9, 0.9),390});391// goal: find `X` such as `t2 * X = t1`392let reparented = t1.reparented_to(&t2);393let t1_prime = t2 * reparented;394assert!(395transform_equal(t1, t1_prime.into()),396"t1:{:#?} t1_prime:{:#?}",397t1.compute_transform(),398t1_prime.compute_transform(),399);400}401402#[test]403fn scale() {404let test_values = [-42.42, 0., 42.42];405for x in test_values {406for y in test_values {407for z in test_values {408let scale = Vec3::new(x, y, z);409let gt = GlobalTransform::from_scale(scale);410assert_eq!(gt.scale(), gt.to_scale_rotation_translation().0);411}412}413}414}415}416417418