From ae23b4d6057043ae2ea846b0cc18279c2a35de6a Mon Sep 17 00:00:00 2001 From: Marek Legris Date: Mon, 28 Sep 2020 12:40:13 +0200 Subject: [PATCH 1/8] removed unnessary GlobalTransform methods --- crates/bevy_pbr/src/light.rs | 2 +- .../src/render_graph/nodes/camera_node.rs | 2 +- .../render_resource/render_resource.rs | 2 +- .../src/components/global_transform.rs | 125 +----------------- .../src/transform_propagate_system.rs | 20 +-- 5 files changed, 14 insertions(+), 137 deletions(-) diff --git a/crates/bevy_pbr/src/light.rs b/crates/bevy_pbr/src/light.rs index 153a2b19307a2..b527fff3b93c5 100644 --- a/crates/bevy_pbr/src/light.rs +++ b/crates/bevy_pbr/src/light.rs @@ -44,7 +44,7 @@ impl LightRaw { far: light.depth.end, }; - let proj = perspective.get_projection_matrix() * *global_transform.value(); + let proj = perspective.get_projection_matrix() * global_transform.value; let (x, y, z) = global_transform.translation().into(); LightRaw { proj: proj.to_cols_array_2d(), 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 f1a37e58c5356..d5dd2ff3e20b4 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.value.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 3778647ae7acf..83ea2cf3138ec 100644 --- a/crates/bevy_render/src/renderer/render_resource/render_resource.rs +++ b/crates/bevy_render/src/renderer/render_resource/render_resource.rs @@ -186,7 +186,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.value) } else { None } diff --git a/crates/bevy_transform/src/components/global_transform.rs b/crates/bevy_transform/src/components/global_transform.rs index b6c795f0df82e..e2ee5a917a12e 100644 --- a/crates/bevy_transform/src/components/global_transform.rs +++ b/crates/bevy_transform/src/components/global_transform.rs @@ -4,15 +4,10 @@ use std::fmt; #[derive(Debug, PartialEq, Clone, Copy, Properties)] pub struct GlobalTransform { - value: Mat4, + pub value: Mat4, } impl GlobalTransform { - #[inline(always)] - pub fn new(value: Mat4) -> Self { - GlobalTransform { value } - } - #[inline(always)] pub fn identity() -> Self { GlobalTransform { @@ -20,86 +15,6 @@ impl GlobalTransform { } } - pub fn from_translation(translation: Vec3) -> Self { - GlobalTransform::new(Mat4::from_translation(translation)) - } - - 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), - rotation, - translation, - )) - } - - 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, - )) - } - - pub fn from_non_uniform_scale(scale: Vec3) -> Self { - GlobalTransform::new(Mat4::from_scale(scale)) - } - - pub fn with_translation(mut self, translation: Vec3) -> Self { - self.set_translation(translation); - self - } - - pub fn with_rotation(mut self, rotation: Quat) -> Self { - self.set_rotation(rotation); - self - } - - 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()) } @@ -121,44 +36,6 @@ impl GlobalTransform { 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); - } - - pub fn rotate(&mut self, rotation: Quat) { - self.value = Mat4::from_quat(rotation) * self.value; - } - - pub fn apply_scale(&mut self, scale: f32) { - self.value = Mat4::from_scale(Vec3::splat(scale)) * self.value; - } - - pub fn apply_non_uniform_scale(&mut self, scale: Vec3) { - self.value = Mat4::from_scale(scale) * self.value; - } } impl Default for GlobalTransform { diff --git a/crates/bevy_transform/src/transform_propagate_system.rs b/crates/bevy_transform/src/transform_propagate_system.rs index bfc03ae581cb6..3c383d6a19d9a 100644 --- a/crates/bevy_transform/src/transform_propagate_system.rs +++ b/crates/bevy_transform/src/transform_propagate_system.rs @@ -7,18 +7,18 @@ pub fn transform_propagate_system( 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.value = *transform.value(); if let Some(children) = children { for child in children.0.iter() { - propagate_recursive(*global_transform.value(), &mut transform_query, *child); + propagate_recursive(&global_transform.value, &mut transform_query, *child); } } } } fn propagate_recursive( - parent: Mat4, + parent: &Mat4, transform_query: &mut Query<(&Transform, &mut GlobalTransform, Option<&Children>)>, entity: Entity, ) { @@ -29,8 +29,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.value = *parent * *transform.value(); + global_transform.value } else { return; } @@ -43,7 +43,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); } } @@ -92,13 +92,13 @@ mod test { schedule.run(&mut world, &mut resources); assert_eq!( - *world.get::(children[0]).unwrap().value(), + 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)) ); assert_eq!( - *world.get::(children[1]).unwrap().value(), + 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)) ); @@ -141,13 +141,13 @@ mod test { schedule.run(&mut world, &mut resources); assert_eq!( - *world.get::(children[0]).unwrap().value(), + 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)) ); assert_eq!( - *world.get::(children[1]).unwrap().value(), + 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)) ); From 6e9aee15369b27779a44e41ed7bf27752e736b99 Mon Sep 17 00:00:00 2001 From: Marek Legris Date: Mon, 28 Sep 2020 14:40:42 +0200 Subject: [PATCH 2/8] Moved transform data out of Mat4 --- .../src/components/transform.rs | 178 ++++++++---------- .../src/transform_propagate_system.rs | 4 +- crates/bevy_ui/src/flex/mod.rs | 2 +- crates/bevy_ui/src/update.rs | 2 +- examples/3d/3d_scene.rs | 6 +- examples/3d/load_model.rs | 6 +- examples/3d/msaa.rs | 6 +- examples/3d/parenting.rs | 6 +- examples/3d/spawner.rs | 6 +- examples/3d/texture.rs | 6 +- examples/3d/z_sort_debug.rs | 9 +- examples/asset/asset_loading.rs | 6 +- examples/asset/hot_asset_reloading.rs | 6 +- examples/ecs/parallel_query.rs | 8 +- examples/game/breakout.rs | 6 +- examples/shader/shader_custom_material.rs | 6 +- examples/shader/shader_defs.rs | 6 +- examples/window/multiple_windows.rs | 12 +- 18 files changed, 102 insertions(+), 179 deletions(-) diff --git a/crates/bevy_transform/src/components/transform.rs b/crates/bevy_transform/src/components/transform.rs index f1e28e6c17967..fffcb0d4419b9 100644 --- a/crates/bevy_transform/src/components/transform.rs +++ b/crates/bevy_transform/src/components/transform.rs @@ -1,167 +1,143 @@ -use bevy_math::{Mat3, Mat4, Quat, Vec3, Vec4}; +use bevy_math::{Mat3, Mat4, Quat, Vec3}; use bevy_property::Properties; use std::fmt; #[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 } + pub fn identity() -> Self { + Transform { + translation: Vec3::zero(), + rotation: Quat::identity(), + scale: Vec3::one(), + } } - #[inline(always)] - pub fn identity() -> Self { + pub fn from_matrix(matrix: Mat4) -> Self { + let (scale, rotation, translation) = matrix.to_scale_rotation_translation(); + Transform { - value: Mat4::identity(), + translation, + rotation, + scale, } } pub fn from_translation(translation: Vec3) -> Self { - Transform::new(Mat4::from_translation(translation)) + Transform { + translation, + ..Default::default() + } } pub fn from_rotation(rotation: Quat) -> Self { - Transform::new(Mat4::from_quat(rotation)) + Transform { + rotation, + ..Default::default() + } } pub fn from_scale(scale: f32) -> Self { - Transform::new(Mat4::from_scale(Vec3::splat(scale))) + Transform { + scale: Vec3::one() * scale, + ..Default::default() + } } - pub fn from_translation_rotation(translation: Vec3, rotation: Quat) -> Self { - Transform::new(Mat4::from_scale_rotation_translation( - Vec3::splat(1.0), - rotation, - translation, - )) + pub fn from_non_uniform_scale(scale: Vec3) -> Self { + Transform { + scale, + ..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, + pub fn from_translation_rotation(translation: Vec3, rotation: Quat) -> Self { + Transform { translation, - )) - } - - pub fn from_non_uniform_scale(scale: Vec3) -> Self { - Transform::new(Mat4::from_scale(scale)) + rotation, + ..Default::default() + } } pub fn with_translation(mut self, translation: Vec3) -> Self { - self.set_translation(translation); + self.translation = translation; self } pub fn with_rotation(mut self, rotation: Quat) -> Self { - self.set_rotation(rotation); + self.rotation = rotation; self } pub fn with_scale(mut self, scale: f32) -> Self { - self.set_scale(scale); + self.scale = Vec3::one() * 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.scale = scale; self } - pub fn with_apply_scale(mut self, scale: f32) -> Self { - self.apply_scale(scale); - self + /// Returns transform with the same translation and scale, but rotation so that transform.forward() points at the origin + pub fn looking_at_origin(self) -> Self { + self.looking_at(Vec3::zero(), Vec3::unit_y()) } - pub fn with_apply_non_uniform_scale(mut self, scale: Vec3) -> Self { - self.apply_non_uniform_scale(scale); + /// Returns transform with the same translation and scale, but rotation so that transform.forward() points at target + pub fn looking_at(mut self, target: Vec3, up: Vec3) -> Self { + self.look_at(target, up); 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 compute_matrix(&self) -> Mat4 { + Mat4::from_scale_rotation_translation(self.scale, self.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 forward(&self) -> Vec3 { + self.rotation * Vec3::unit_z() } + /// Translates the transform by the given translation relative to its orientation + /// + /// # Examples + /// ``` + /// // Moves the transform by two units in the direction it's looking + /// use bevy::prelude::*; + /// let transform = Transform { + /// rotation: Quat::from_axis_angle(Vec3::unit_y(), 2.0), + /// ..Default::default() + /// }; + /// transform.translate(Vec3::unit_z() * 2.0); + /// ``` pub fn translate(&mut self, translation: Vec3) { - *self.value.w_axis_mut() += translation.extend(0.0); + self.translation += self.rotation * translation; } + /// Rotate the transform by the given rotation pub fn rotate(&mut self, rotation: Quat) { - self.value = Mat4::from_quat(rotation) * self.value; + self.rotation = self.rotation * rotation; } pub fn apply_scale(&mut self, scale: f32) { - self.value = Mat4::from_scale(Vec3::splat(scale)) * self.value; + self.scale *= scale; } pub fn apply_non_uniform_scale(&mut self, scale: Vec3) { - self.value = Mat4::from_scale(scale) * self.value; + self.scale *= scale; + } + + 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)); } } @@ -173,6 +149,6 @@ impl Default for Transform { impl fmt::Display for Transform { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.value) + write!(f, "{}", self.compute_matrix()) } } diff --git a/crates/bevy_transform/src/transform_propagate_system.rs b/crates/bevy_transform/src/transform_propagate_system.rs index 3c383d6a19d9a..4eff1a1b6ac56 100644 --- a/crates/bevy_transform/src/transform_propagate_system.rs +++ b/crates/bevy_transform/src/transform_propagate_system.rs @@ -7,7 +7,7 @@ pub fn transform_propagate_system( mut transform_query: Query<(&Transform, &mut GlobalTransform, Option<&Children>)>, ) { for (children, transform, mut global_transform) in &mut root_query.iter() { - global_transform.value = *transform.value(); + global_transform.value = transform.compute_matrix(); if let Some(children) = children { for child in children.0.iter() { @@ -29,7 +29,7 @@ fn propagate_recursive( transform_query.get::(entity), transform_query.get_mut::(entity), ) { - global_transform.value = *parent * *transform.value(); + global_transform.value = *parent * transform.compute_matrix(); global_transform.value } else { return; diff --git a/crates/bevy_ui/src/flex/mod.rs b/crates/bevy_ui/src/flex/mod.rs index 760fad6c690ae..e885bb9abfda2 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/update.rs b/crates/bevy_ui/src/update.rs index 4723091be4993..bafa9fa9c01e1 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/examples/3d/3d_scene.rs b/examples/3d/3d_scene.rs index 7dad997ef02f1..933afc28026b0 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 14be4f1135d0c..d21962360699e 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 e25a1400ad0d6..629ef104c34b5 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 9eb56cf7379a7..3531604c96539 100644 --- a/examples/3d/parenting.rs +++ b/examples/3d/parenting.rs @@ -58,11 +58,7 @@ fn setup( }) // camera .spawn(Camera3dComponents { - transform: Transform::new(Mat4::face_toward( - Vec3::new(5.0, 10.0, 10.0), - Vec3::new(0.0, 0.0, 0.0), - Vec3::new(0.0, 1.0, 0.0), - )), + transform: Transform::from_translation(Vec3::new(5.0, 10.0, 10.0)).looking_at_origin(), ..Default::default() }); } diff --git a/examples/3d/spawner.rs b/examples/3d/spawner.rs index b729324077c9b..a5f7520218cb7 100644 --- a/examples/3d/spawner.rs +++ b/examples/3d/spawner.rs @@ -44,11 +44,7 @@ fn setup( }) // camera .spawn(Camera3dComponents { - transform: Transform::new(Mat4::face_toward( - Vec3::new(0.0, 15.0, 150.0), - Vec3::new(0.0, 0.0, 0.0), - Vec3::new(0.0, 0.0, 1.0), - )), + transform: Transform::from_translation(Vec3::new(0.0, 15.0, 150.0)).looking_at_origin(), ..Default::default() }); diff --git a/examples/3d/texture.rs b/examples/3d/texture.rs index 4852070100eb1..e8f0f0c56e3f7 100644 --- a/examples/3d/texture.rs +++ b/examples/3d/texture.rs @@ -99,11 +99,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/z_sort_debug.rs b/examples/3d/z_sort_debug.rs index 97f7ddbe49026..13d917151369c 100644 --- a/examples/3d/z_sort_debug.rs +++ b/examples/3d/z_sort_debug.rs @@ -21,8 +21,7 @@ struct Rotator; /// rotates the parent, which will result in the child also rotating fn rotator_system(time: Res