From 5acebed73184175bf089aec5516782b094d08199 Mon Sep 17 00:00:00 2001 From: Marek Legris <34162957+MarekLg@users.noreply.github.com> Date: Sun, 18 Oct 2020 22:03:16 +0200 Subject: [PATCH] Transform and GlobalTransform are now Similarities (#596) Transform and GlobalTransform are now Similarities. This resolves precision errors and simplifies the api Co-authored-by: Carter Anderson --- crates/bevy_pbr/src/light.rs | 4 +- .../src/camera/visible_entities.rs | 4 +- .../src/render_graph/nodes/camera_node.rs | 2 +- .../render_resource/render_resource.rs | 22 +- .../src/components/global_transform.rs | 241 +++++++++--------- .../src/components/transform.rs | 236 ++++++++--------- .../src/transform_propagate_system.rs | 39 ++- crates/bevy_ui/src/flex/mod.rs | 2 +- crates/bevy_ui/src/focus.rs | 2 +- crates/bevy_ui/src/update.rs | 2 +- crates/bevy_ui/src/widget/text.rs | 2 +- examples/2d/sprite_sheet.rs | 2 +- examples/2d/texture_atlas.rs | 6 +- examples/3d/3d_scene.rs | 6 +- examples/3d/load_model.rs | 6 +- examples/3d/msaa.rs | 6 +- examples/3d/parenting.rs | 8 +- examples/3d/spawner.rs | 8 +- examples/3d/texture.rs | 33 ++- examples/3d/z_sort_debug.rs | 9 +- examples/asset/asset_loading.rs | 6 +- examples/asset/hot_asset_reloading.rs | 6 +- examples/ecs/hierarchy.rs | 20 +- examples/ecs/parallel_query.rs | 12 +- examples/game/breakout.rs | 8 +- examples/shader/shader_custom_material.rs | 6 +- examples/shader/shader_defs.rs | 6 +- examples/window/multiple_windows.rs | 12 +- 28 files changed, 332 insertions(+), 384 deletions(-) diff --git a/crates/bevy_pbr/src/light.rs b/crates/bevy_pbr/src/light.rs index 153a2b1930..e9d7c4427a 100644 --- a/crates/bevy_pbr/src/light.rs +++ b/crates/bevy_pbr/src/light.rs @@ -44,8 +44,8 @@ impl LightRaw { far: light.depth.end, }; - let proj = perspective.get_projection_matrix() * *global_transform.value(); - let (x, y, z) = global_transform.translation().into(); + let proj = perspective.get_projection_matrix() * global_transform.compute_matrix(); + let (x, y, z) = global_transform.translation.into(); LightRaw { proj: proj.to_cols_array_2d(), pos: [x, y, z, 1.0], diff --git a/crates/bevy_render/src/camera/visible_entities.rs b/crates/bevy_render/src/camera/visible_entities.rs index fa29e950a3..e518da2a7b 100644 --- a/crates/bevy_render/src/camera/visible_entities.rs +++ b/crates/bevy_render/src/camera/visible_entities.rs @@ -30,7 +30,7 @@ pub fn visible_entities_system( ) { for (camera, camera_global_transform, mut visible_entities) in &mut camera_query.iter() { visible_entities.value.clear(); - let camera_position = camera_global_transform.translation(); + let camera_position = camera_global_transform.translation; let mut no_transform_order = 0.0; let mut transparent_entities = Vec::new(); @@ -41,7 +41,7 @@ pub fn visible_entities_system( let order = if let Ok(global_transform) = draw_transform_query.get::(entity) { - let position = global_transform.translation(); + let position = global_transform.translation; // smaller distances are sorted to lower indices by using the distance from the camera FloatOrd(match camera.depth_calculation { DepthCalculation::ZDifference => camera_position.z() - position.z(), diff --git a/crates/bevy_render/src/render_graph/nodes/camera_node.rs b/crates/bevy_render/src/render_graph/nodes/camera_node.rs index f1a37e58c5..3cda1c54d3 100644 --- a/crates/bevy_render/src/render_graph/nodes/camera_node.rs +++ b/crates/bevy_render/src/render_graph/nodes/camera_node.rs @@ -120,7 +120,7 @@ pub fn camera_node_system( let matrix_size = std::mem::size_of::<[[f32; 4]; 4]>(); let camera_matrix: [f32; 16] = - (camera.projection_matrix * global_transform.value().inverse()).to_cols_array(); + (camera.projection_matrix * global_transform.compute_matrix().inverse()).to_cols_array(); render_resource_context.write_mapped_buffer( staging_buffer, diff --git a/crates/bevy_render/src/renderer/render_resource/render_resource.rs b/crates/bevy_render/src/renderer/render_resource/render_resource.rs index 3778647ae7..121722a7dd 100644 --- a/crates/bevy_render/src/renderer/render_resource/render_resource.rs +++ b/crates/bevy_render/src/renderer/render_resource/render_resource.rs @@ -5,6 +5,7 @@ use bevy_asset::Handle; use bevy_core::{Byteable, Bytes}; pub use bevy_derive::{RenderResource, RenderResources}; use bevy_math::{Mat4, Vec2, Vec3, Vec4}; +use bevy_transform::components::GlobalTransform; #[derive(Debug, Clone, Eq, PartialEq)] pub enum RenderResourceType { @@ -179,6 +180,25 @@ where } } +impl RenderResource for GlobalTransform { + fn resource_type(&self) -> Option { + Some(RenderResourceType::Buffer) + } + + fn write_buffer_bytes(&self, buffer: &mut [u8]) { + let mat4 = self.compute_matrix(); + mat4.write_bytes(buffer); + } + + fn buffer_byte_len(&self) -> Option { + Some(std::mem::size_of::<[f32; 16]>()) + } + + fn texture(&self) -> Option> { + None + } +} + impl RenderResources for bevy_transform::prelude::GlobalTransform { fn render_resources_len(&self) -> usize { 1 @@ -186,7 +206,7 @@ impl RenderResources for bevy_transform::prelude::GlobalTransform { fn get_render_resource(&self, index: usize) -> Option<&dyn RenderResource> { if index == 0 { - Some(self.value()) + Some(self) } else { None } diff --git a/crates/bevy_transform/src/components/global_transform.rs b/crates/bevy_transform/src/components/global_transform.rs index b6c795f0df..71eb9af3e0 100644 --- a/crates/bevy_transform/src/components/global_transform.rs +++ b/crates/bevy_transform/src/components/global_transform.rs @@ -1,163 +1,121 @@ use bevy_math::{Mat3, Mat4, Quat, Vec3}; use bevy_property::Properties; -use std::fmt; +use std::ops::Mul; + +use super::Transform; #[derive(Debug, PartialEq, Clone, Copy, Properties)] pub struct GlobalTransform { - value: Mat4, + pub translation: Vec3, + pub rotation: Quat, + pub scale: Vec3, } impl GlobalTransform { - #[inline(always)] - pub fn new(value: Mat4) -> Self { - GlobalTransform { value } - } - - #[inline(always)] + #[inline] pub fn identity() -> Self { GlobalTransform { - value: Mat4::identity(), + translation: Vec3::zero(), + rotation: Quat::identity(), + scale: Vec3::one(), } } + #[inline] + pub fn from_matrix(matrix: Mat4) -> Self { + let (scale, rotation, translation) = matrix.to_scale_rotation_translation(); + + GlobalTransform { + translation, + rotation, + scale, + } + } + + #[inline] pub fn from_translation(translation: Vec3) -> Self { - GlobalTransform::new(Mat4::from_translation(translation)) + GlobalTransform { + translation, + ..Default::default() + } } + #[inline] pub fn from_rotation(rotation: Quat) -> Self { - GlobalTransform::new(Mat4::from_quat(rotation)) - } - - pub fn from_scale(scale: f32) -> Self { - GlobalTransform::new(Mat4::from_scale(Vec3::splat(scale))) - } - - pub fn from_translation_rotation(translation: Vec3, rotation: Quat) -> Self { - GlobalTransform::new(Mat4::from_scale_rotation_translation( - Vec3::splat(1.0), + GlobalTransform { rotation, - translation, - )) + ..Default::default() + } } - pub fn from_translation_rotation_scale(translation: Vec3, rotation: Quat, scale: f32) -> Self { - GlobalTransform::new(Mat4::from_scale_rotation_translation( - Vec3::splat(scale), - rotation, - translation, - )) + #[inline] + pub fn from_scale(scale: Vec3) -> Self { + GlobalTransform { + scale, + ..Default::default() + } } - pub fn from_non_uniform_scale(scale: Vec3) -> Self { - GlobalTransform::new(Mat4::from_scale(scale)) + /// Returns transform with the same translation and scale, but rotation so that transform.forward() points at the origin + #[inline] + pub fn looking_at_origin(self) -> Self { + self.looking_at(Vec3::zero(), Vec3::unit_y()) } - pub fn with_translation(mut self, translation: Vec3) -> Self { - self.set_translation(translation); + /// Returns transform with the same translation and scale, but rotation so that transform.forward() points at target + #[inline] + pub fn looking_at(mut self, target: Vec3, up: Vec3) -> Self { + self.look_at(target, up); self } - pub fn with_rotation(mut self, rotation: Quat) -> Self { - self.set_rotation(rotation); - self + #[inline] + pub fn compute_matrix(&self) -> Mat4 { + Mat4::from_scale_rotation_translation(self.scale, self.rotation, self.translation) } - pub fn with_scale(mut self, scale: f32) -> Self { - self.set_scale(scale); - self - } - - pub fn with_non_uniform_scale(mut self, scale: Vec3) -> Self { - self.set_non_uniform_scale(scale); - self - } - - pub fn with_translate(mut self, translation: Vec3) -> Self { - self.translate(translation); - self - } - - pub fn with_rotate(mut self, rotation: Quat) -> Self { - self.rotate(rotation); - self - } - - pub fn with_apply_scale(mut self, scale: f32) -> Self { - self.apply_scale(scale); - self - } - - pub fn with_apply_non_uniform_scale(mut self, scale: Vec3) -> Self { - self.apply_non_uniform_scale(scale); - self - } - - pub fn value(&self) -> &Mat4 { - &self.value - } - - pub fn value_mut(&mut self) -> &mut Mat4 { - &mut self.value - } - - pub fn translation(&self) -> Vec3 { - Vec3::from(self.value.w_axis().truncate()) - } - - pub fn rotation(&self) -> Quat { - let scale = self.scale(); - - Quat::from_rotation_mat3(&Mat3::from_cols( - Vec3::from(self.value.x_axis().truncate()) / scale.x(), - Vec3::from(self.value.y_axis().truncate()) / scale.y(), - Vec3::from(self.value.z_axis().truncate()) / scale.z(), - )) - } - - pub fn scale(&self) -> Vec3 { - Vec3::new( - self.value.x_axis().truncate().length(), - self.value.y_axis().truncate().length(), - self.value.z_axis().truncate().length(), - ) - } - - pub fn set_translation(&mut self, translation: Vec3) { - *self.value.w_axis_mut() = translation.extend(1.0); - } - - pub fn set_rotation(&mut self, rotation: Quat) { - self.value = - Mat4::from_scale_rotation_translation(self.scale(), rotation, self.translation()); - } - - pub fn set_scale(&mut self, scale: f32) { - self.value = Mat4::from_scale_rotation_translation( - Vec3::splat(scale), - self.rotation(), - self.translation(), - ); - } - - pub fn set_non_uniform_scale(&mut self, scale: Vec3) { - self.value = - Mat4::from_scale_rotation_translation(scale, self.rotation(), self.translation()); - } - - pub fn translate(&mut self, translation: Vec3) { - *self.value.w_axis_mut() += translation.extend(0.0); + #[inline] + pub fn forward(&self) -> Vec3 { + self.rotation * Vec3::unit_z() } + #[inline] + /// Rotate the transform by the given rotation pub fn rotate(&mut self, rotation: Quat) { - self.value = Mat4::from_quat(rotation) * self.value; + self.rotation *= rotation; } - pub fn apply_scale(&mut self, scale: f32) { - self.value = Mat4::from_scale(Vec3::splat(scale)) * self.value; + #[inline] + pub fn mul_transform(&self, transform: Transform) -> GlobalTransform { + let translation = self.mul_vec3(transform.translation); + let rotation = self.rotation * transform.rotation; + let scale = self.scale * transform.scale; + GlobalTransform { + scale, + rotation, + translation, + } } + #[inline] + pub fn mul_vec3(&self, mut value: Vec3) -> Vec3 { + value = self.rotation * value; + value = self.scale * value; + value += self.translation; + value + } + + #[inline] pub fn apply_non_uniform_scale(&mut self, scale: Vec3) { - self.value = Mat4::from_scale(scale) * self.value; + self.scale *= scale; + } + + #[inline] + pub fn look_at(&mut self, target: Vec3, up: Vec3) { + let forward = Vec3::normalize(self.translation - target); + let right = up.cross(forward).normalize(); + let up = forward.cross(right); + self.rotation = Quat::from_rotation_mat3(&Mat3::from_cols(right, up, forward)); } } @@ -167,8 +125,39 @@ impl Default for GlobalTransform { } } -impl fmt::Display for GlobalTransform { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.value) +impl From for GlobalTransform { + fn from(transform: Transform) -> Self { + Self { + translation: transform.translation, + rotation: transform.rotation, + scale: transform.scale, + } + } +} + +impl Mul for GlobalTransform { + type Output = GlobalTransform; + + #[inline] + fn mul(self, global_transform: GlobalTransform) -> Self::Output { + self.mul_transform(global_transform.into()) + } +} + +impl Mul for GlobalTransform { + type Output = GlobalTransform; + + #[inline] + fn mul(self, transform: Transform) -> Self::Output { + self.mul_transform(transform) + } +} + +impl Mul for GlobalTransform { + type Output = Vec3; + + #[inline] + fn mul(self, value: Vec3) -> Self::Output { + self.mul_vec3(value) } } diff --git a/crates/bevy_transform/src/components/transform.rs b/crates/bevy_transform/src/components/transform.rs index f1e28e6c17..70002bbe26 100644 --- a/crates/bevy_transform/src/components/transform.rs +++ b/crates/bevy_transform/src/components/transform.rs @@ -1,167 +1,121 @@ -use bevy_math::{Mat3, Mat4, Quat, Vec3, Vec4}; +use bevy_math::{Mat3, Mat4, Quat, Vec3}; use bevy_property::Properties; -use std::fmt; +use std::ops::Mul; + +use super::GlobalTransform; #[derive(Debug, PartialEq, Clone, Copy, Properties)] pub struct Transform { - value: Mat4, + pub translation: Vec3, + pub rotation: Quat, + pub scale: Vec3, } impl Transform { - #[inline(always)] - pub fn new(value: Mat4) -> Self { - Transform { value } - } - - #[inline(always)] + #[inline] pub fn identity() -> Self { Transform { - value: Mat4::identity(), + translation: Vec3::zero(), + rotation: Quat::identity(), + scale: Vec3::one(), } } + #[inline] + pub fn from_matrix(matrix: Mat4) -> Self { + let (scale, rotation, translation) = matrix.to_scale_rotation_translation(); + + Transform { + translation, + rotation, + scale, + } + } + + #[inline] pub fn from_translation(translation: Vec3) -> Self { - Transform::new(Mat4::from_translation(translation)) + Transform { + translation, + ..Default::default() + } } + #[inline] pub fn from_rotation(rotation: Quat) -> Self { - Transform::new(Mat4::from_quat(rotation)) - } - - pub fn from_scale(scale: f32) -> Self { - Transform::new(Mat4::from_scale(Vec3::splat(scale))) - } - - pub fn from_translation_rotation(translation: Vec3, rotation: Quat) -> Self { - Transform::new(Mat4::from_scale_rotation_translation( - Vec3::splat(1.0), + Transform { rotation, - translation, - )) + ..Default::default() + } } - pub fn from_translation_rotation_scale(translation: Vec3, rotation: Quat, scale: f32) -> Self { - Transform::new(Mat4::from_scale_rotation_translation( - Vec3::splat(scale), - rotation, - translation, - )) + #[inline] + pub fn from_scale(scale: Vec3) -> Self { + Transform { + scale, + ..Default::default() + } } - pub fn from_non_uniform_scale(scale: Vec3) -> Self { - Transform::new(Mat4::from_scale(scale)) + /// Returns transform with the same translation and scale, but rotation so that transform.forward() points at the origin + #[inline] + pub fn looking_at_origin(self) -> Self { + self.looking_at(Vec3::zero(), Vec3::unit_y()) } - pub fn with_translation(mut self, translation: Vec3) -> Self { - self.set_translation(translation); + /// Returns transform with the same translation and scale, but rotation so that transform.forward() points at target + #[inline] + pub fn looking_at(mut self, target: Vec3, up: Vec3) -> Self { + self.look_at(target, up); self } - pub fn with_rotation(mut self, rotation: Quat) -> Self { - self.set_rotation(rotation); - self + #[inline] + pub fn compute_matrix(&self) -> Mat4 { + Mat4::from_scale_rotation_translation(self.scale, self.rotation, self.translation) } - pub fn with_scale(mut self, scale: f32) -> Self { - self.set_scale(scale); - self - } - - pub fn with_non_uniform_scale(mut self, scale: Vec3) -> Self { - self.set_non_uniform_scale(scale); - self - } - - pub fn with_translate(mut self, translation: Vec3) -> Self { - self.translate(translation); - self - } - - pub fn with_rotate(mut self, rotation: Quat) -> Self { - self.rotate(rotation); - self - } - - pub fn with_apply_scale(mut self, scale: f32) -> Self { - self.apply_scale(scale); - self - } - - pub fn with_apply_non_uniform_scale(mut self, scale: Vec3) -> Self { - self.apply_non_uniform_scale(scale); - self - } - - pub fn value(&self) -> &Mat4 { - &self.value - } - - pub fn value_mut(&mut self) -> &mut Mat4 { - &mut self.value - } - - pub fn translation(&self) -> Vec3 { - Vec3::from(self.value.w_axis().truncate()) - } - - pub fn translation_mut(&mut self) -> &mut Vec4 { - self.value.w_axis_mut() - } - - pub fn rotation(&self) -> Quat { - let scale = self.scale(); - - Quat::from_rotation_mat3(&Mat3::from_cols( - Vec3::from(self.value.x_axis().truncate()) / scale.x(), - Vec3::from(self.value.y_axis().truncate()) / scale.y(), - Vec3::from(self.value.z_axis().truncate()) / scale.z(), - )) - } - - pub fn scale(&self) -> Vec3 { - Vec3::new( - self.value.x_axis().truncate().length(), - self.value.y_axis().truncate().length(), - self.value.z_axis().truncate().length(), - ) - } - - pub fn set_translation(&mut self, translation: Vec3) { - *self.value.w_axis_mut() = translation.extend(1.0); - } - - pub fn set_rotation(&mut self, rotation: Quat) { - self.value = - Mat4::from_scale_rotation_translation(self.scale(), rotation, self.translation()); - } - - pub fn set_scale(&mut self, scale: f32) { - self.value = Mat4::from_scale_rotation_translation( - Vec3::splat(scale), - self.rotation(), - self.translation(), - ); - } - - pub fn set_non_uniform_scale(&mut self, scale: Vec3) { - self.value = - Mat4::from_scale_rotation_translation(scale, self.rotation(), self.translation()); - } - - pub fn translate(&mut self, translation: Vec3) { - *self.value.w_axis_mut() += translation.extend(0.0); + #[inline] + pub fn forward(&self) -> Vec3 { + self.rotation * Vec3::unit_z() } + #[inline] + /// Rotate the transform by the given rotation pub fn rotate(&mut self, rotation: Quat) { - self.value = Mat4::from_quat(rotation) * self.value; + self.rotation *= rotation; } - pub fn apply_scale(&mut self, scale: f32) { - self.value = Mat4::from_scale(Vec3::splat(scale)) * self.value; + #[inline] + pub fn mul_transform(&self, transform: Transform) -> Self { + let translation = self.mul_vec3(transform.translation); + let rotation = self.rotation * transform.rotation; + let scale = self.scale * transform.scale; + Transform { + scale, + rotation, + translation, + } } + #[inline] + pub fn mul_vec3(&self, mut value: Vec3) -> Vec3 { + value = self.rotation * value; + value = self.scale * value; + value += self.translation; + value + } + + #[inline] pub fn apply_non_uniform_scale(&mut self, scale: Vec3) { - self.value = Mat4::from_scale(scale) * self.value; + self.scale *= scale; + } + + #[inline] + pub fn look_at(&mut self, target: Vec3, up: Vec3) { + let forward = Vec3::normalize(self.translation - target); + let right = up.cross(forward).normalize(); + let up = forward.cross(right); + self.rotation = Quat::from_rotation_mat3(&Mat3::from_cols(right, up, forward)); } } @@ -171,8 +125,28 @@ impl Default for Transform { } } -impl fmt::Display for Transform { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.value) +impl From for Transform { + fn from(transform: GlobalTransform) -> Self { + Self { + translation: transform.translation, + rotation: transform.rotation, + scale: transform.scale, + } + } +} + +impl Mul for Transform { + type Output = Transform; + + fn mul(self, transform: Transform) -> Self::Output { + self.mul_transform(transform) + } +} + +impl Mul for Transform { + type Output = Vec3; + + fn mul(self, value: Vec3) -> Self::Output { + self.mul_vec3(value) } } diff --git a/crates/bevy_transform/src/transform_propagate_system.rs b/crates/bevy_transform/src/transform_propagate_system.rs index bfc03ae581..a76ea3af56 100644 --- a/crates/bevy_transform/src/transform_propagate_system.rs +++ b/crates/bevy_transform/src/transform_propagate_system.rs @@ -1,24 +1,23 @@ use crate::components::*; use bevy_ecs::prelude::*; -use bevy_math::Mat4; pub fn transform_propagate_system( mut root_query: Query, &Transform, &mut GlobalTransform)>>, mut transform_query: Query<(&Transform, &mut GlobalTransform, Option<&Children>)>, ) { for (children, transform, mut global_transform) in &mut root_query.iter() { - *global_transform.value_mut() = *transform.value(); + *global_transform = GlobalTransform::from(*transform); if let Some(children) = children { for child in children.0.iter() { - propagate_recursive(*global_transform.value(), &mut transform_query, *child); + propagate_recursive(&global_transform, &mut transform_query, *child); } } } } fn propagate_recursive( - parent: Mat4, + parent: &GlobalTransform, transform_query: &mut Query<(&Transform, &mut GlobalTransform, Option<&Children>)>, entity: Entity, ) { @@ -29,8 +28,8 @@ fn propagate_recursive( transform_query.get::(entity), transform_query.get_mut::(entity), ) { - *global_transform.value_mut() = parent * *transform.value(); - *global_transform.value() + *global_transform = parent.mul_transform(*transform); + *global_transform } else { return; } @@ -43,7 +42,7 @@ fn propagate_recursive( .unwrap_or_default(); for child in children { - propagate_recursive(global_matrix, transform_query, child); + propagate_recursive(&global_matrix, transform_query, child); } } @@ -52,7 +51,7 @@ mod test { use super::*; use crate::{hierarchy::BuildChildren, transform_systems}; use bevy_ecs::{Resources, Schedule, World}; - use bevy_math::{Mat4, Vec3}; + use bevy_math::Vec3; #[test] fn did_propagate() { @@ -92,15 +91,15 @@ mod test { schedule.run(&mut world, &mut resources); assert_eq!( - *world.get::(children[0]).unwrap().value(), - Mat4::from_translation(Vec3::new(1.0, 0.0, 0.0)) - * Mat4::from_translation(Vec3::new(0.0, 2.0, 0.0)) + *world.get::(children[0]).unwrap(), + GlobalTransform::from_translation(Vec3::new(1.0, 0.0, 0.0)) + * Transform::from_translation(Vec3::new(0.0, 2.0, 0.0)) ); assert_eq!( - *world.get::(children[1]).unwrap().value(), - Mat4::from_translation(Vec3::new(1.0, 0.0, 0.0)) - * Mat4::from_translation(Vec3::new(0.0, 0.0, 3.0)) + *world.get::(children[1]).unwrap(), + GlobalTransform::from_translation(Vec3::new(1.0, 0.0, 0.0)) + * Transform::from_translation(Vec3::new(0.0, 0.0, 3.0)) ); } @@ -141,15 +140,15 @@ mod test { schedule.run(&mut world, &mut resources); assert_eq!( - *world.get::(children[0]).unwrap().value(), - Mat4::from_translation(Vec3::new(1.0, 0.0, 0.0)) - * Mat4::from_translation(Vec3::new(0.0, 2.0, 0.0)) + *world.get::(children[0]).unwrap(), + GlobalTransform::from_translation(Vec3::new(1.0, 0.0, 0.0)) + * Transform::from_translation(Vec3::new(0.0, 2.0, 0.0)) ); assert_eq!( - *world.get::(children[1]).unwrap().value(), - Mat4::from_translation(Vec3::new(1.0, 0.0, 0.0)) - * Mat4::from_translation(Vec3::new(0.0, 0.0, 3.0)) + *world.get::(children[1]).unwrap(), + GlobalTransform::from_translation(Vec3::new(1.0, 0.0, 0.0)) + * Transform::from_translation(Vec3::new(0.0, 0.0, 3.0)) ); } } diff --git a/crates/bevy_ui/src/flex/mod.rs b/crates/bevy_ui/src/flex/mod.rs index 760fad6c69..e885bb9abf 100644 --- a/crates/bevy_ui/src/flex/mod.rs +++ b/crates/bevy_ui/src/flex/mod.rs @@ -203,7 +203,7 @@ pub fn flex_node_system( for (entity, mut node, mut transform, parent) in &mut node_transform_query.iter() { let layout = flex_surface.get_layout(entity).unwrap(); node.size = Vec2::new(layout.size.width, layout.size.height); - let position = transform.translation_mut(); + let position = &mut transform.translation; position.set_x(layout.location.x + layout.size.width / 2.0); position.set_y(layout.location.y + layout.size.height / 2.0); if let Some(parent) = parent { diff --git a/crates/bevy_ui/src/focus.rs b/crates/bevy_ui/src/focus.rs index 8ca9a4ddb0..1f23cdd104 100644 --- a/crates/bevy_ui/src/focus.rs +++ b/crates/bevy_ui/src/focus.rs @@ -76,7 +76,7 @@ pub fn ui_focus_system( .iter() .filter_map( |(entity, node, global_transform, interaction, focus_policy)| { - let position = global_transform.translation(); + let position = global_transform.translation; let ui_position = position.truncate(); let extents = node.size / 2.0; let min = ui_position - extents; diff --git a/crates/bevy_ui/src/update.rs b/crates/bevy_ui/src/update.rs index 4723091be4..bafa9fa9c0 100644 --- a/crates/bevy_ui/src/update.rs +++ b/crates/bevy_ui/src/update.rs @@ -47,7 +47,7 @@ fn update_node_entity( let global_z = z + parent_global_z; let mut transform = node_query.get_mut::(entity).ok()?; - transform.translation_mut().set_z(z); + transform.translation.set_z(z); Some(global_z) } diff --git a/crates/bevy_ui/src/widget/text.rs b/crates/bevy_ui/src/widget/text.rs index 4646ddf702..3ac715e166 100644 --- a/crates/bevy_ui/src/widget/text.rs +++ b/crates/bevy_ui/src/widget/text.rs @@ -104,7 +104,7 @@ pub fn draw_text_system( ) { for (mut draw, text, node, global_transform) in &mut query.iter() { if let Some(font) = fonts.get(&text.font) { - let position = global_transform.translation() - (node.size / 2.0).extend(0.0); + let position = global_transform.translation - (node.size / 2.0).extend(0.0); let mut drawable_text = DrawableText { font, font_atlas_set: font_atlas_sets diff --git a/examples/2d/sprite_sheet.rs b/examples/2d/sprite_sheet.rs index 155dd6afd5..a47c7476bc 100644 --- a/examples/2d/sprite_sheet.rs +++ b/examples/2d/sprite_sheet.rs @@ -39,7 +39,7 @@ fn setup( .spawn(Camera2dComponents::default()) .spawn(SpriteSheetComponents { texture_atlas: texture_atlas_handle, - transform: Transform::from_scale(6.0), + transform: Transform::from_scale(Vec3::splat(6.0)), ..Default::default() }) .with(Timer::from_seconds(0.1, true)); diff --git a/examples/2d/texture_atlas.rs b/examples/2d/texture_atlas.rs index f7681e5f50..fa9a95078f 100644 --- a/examples/2d/texture_atlas.rs +++ b/examples/2d/texture_atlas.rs @@ -61,7 +61,11 @@ fn load_atlas( .spawn(Camera2dComponents::default()) // draw a sprite from the atlas .spawn(SpriteSheetComponents { - transform: Transform::from_scale(4.0).with_translation(Vec3::new(150.0, 0.0, 0.0)), + transform: Transform { + translation: Vec3::new(150.0, 0.0, 0.0), + scale: Vec3::splat(4.0), + ..Default::default() + }, sprite: TextureAtlasSprite::new(vendor_index as u32), texture_atlas: atlas_handle, ..Default::default() diff --git a/examples/3d/3d_scene.rs b/examples/3d/3d_scene.rs index 7dad997ef0..933afc2802 100644 --- a/examples/3d/3d_scene.rs +++ b/examples/3d/3d_scene.rs @@ -36,11 +36,7 @@ fn setup( }) // camera .spawn(Camera3dComponents { - transform: Transform::new(Mat4::face_toward( - Vec3::new(-3.0, 5.0, 8.0), - Vec3::new(0.0, 0.0, 0.0), - Vec3::new(0.0, 1.0, 0.0), - )), + transform: Transform::from_translation(Vec3::new(-3.0, 5.0, 8.0)).looking_at_origin(), ..Default::default() }); } diff --git a/examples/3d/load_model.rs b/examples/3d/load_model.rs index 14be4f1135..d219623606 100644 --- a/examples/3d/load_model.rs +++ b/examples/3d/load_model.rs @@ -44,11 +44,7 @@ fn setup( }) // camera .spawn(Camera3dComponents { - transform: Transform::new(Mat4::face_toward( - Vec3::new(-2.0, 2.0, 6.0), - Vec3::new(0.0, 0.0, 0.0), - Vec3::new(0.0, 1.0, 0.0), - )), + transform: Transform::from_translation(Vec3::new(-2.0, 2.0, 6.0)).looking_at_origin(), ..Default::default() }); } diff --git a/examples/3d/msaa.rs b/examples/3d/msaa.rs index e25a1400ad..629ef104c3 100644 --- a/examples/3d/msaa.rs +++ b/examples/3d/msaa.rs @@ -32,11 +32,7 @@ fn setup( }) // camera .spawn(Camera3dComponents { - transform: Transform::new(Mat4::face_toward( - Vec3::new(-3.0, 3.0, 5.0), - Vec3::new(0.0, 0.0, 0.0), - Vec3::new(0.0, 1.0, 0.0), - )), + transform: Transform::from_translation(Vec3::new(-3.0, 3.0, 5.0)).looking_at_origin(), ..Default::default() }); } diff --git a/examples/3d/parenting.rs b/examples/3d/parenting.rs index 9eb56cf737..114e08599e 100644 --- a/examples/3d/parenting.rs +++ b/examples/3d/parenting.rs @@ -17,7 +17,7 @@ struct Rotator; /// rotates the parent, which will result in the child also rotating fn rotator_system(time: Res