From cfa64e8091d014dec9b9611808b8bc9e51ca7da0 Mon Sep 17 00:00:00 2001 From: Marek Legris Date: Thu, 27 Aug 2020 18:16:45 +0200 Subject: [PATCH 01/23] initial draft --- .gitignore | 3 +- crates/bevy_pbr/src/entity.rs | 10 +- crates/bevy_pbr/src/light.rs | 9 +- .../bevy_pbr/src/render_graph/lights_node.rs | 8 +- .../src/camera/visible_entities.rs | 4 +- crates/bevy_render/src/entity.rs | 9 - .../src/render_graph/nodes/camera_node.rs | 2 +- .../render_resource/render_resource.rs | 2 +- .../src/components/local_transform.rs | 41 --- crates/bevy_transform/src/components/mod.rs | 2 - .../src/components/transform.rs | 154 +++++++- .../src/hierarchy/child_builder.rs | 20 +- .../hierarchy/hierarchy_maintenance_system.rs | 6 +- .../src/hierarchy/world_child_builder.rs | 4 +- crates/bevy_transform/src/lib.rs | 9 +- .../src/local_transform_systems.rs | 273 -------------- .../src/transform_propagate_system.rs | 73 ++-- .../bevy_transform/src/transform_systems.rs | 339 ------------------ crates/bevy_ui/src/entity.rs | 13 +- crates/bevy_ui/src/flex/mod.rs | 10 +- crates/bevy_ui/src/focus.rs | 2 +- crates/bevy_ui/src/update.rs | 12 +- crates/bevy_ui/src/widget/text.rs | 4 +- examples/3d/3d_scene.rs | 16 +- examples/3d/load_model.rs | 4 +- examples/3d/msaa.rs | 4 +- examples/3d/parenting.rs | 37 +- examples/3d/spawner.rs | 15 +- examples/3d/texture.rs | 20 +- examples/3d/z_sort_debug.rs | 8 +- examples/asset/asset_loading.rs | 10 +- examples/asset/hot_asset_reloading.rs | 4 +- examples/shader/shader_custom_material.rs | 4 +- examples/shader/shader_defs.rs | 6 +- examples/window/multiple_windows.rs | 6 +- 35 files changed, 296 insertions(+), 847 deletions(-) delete mode 100644 crates/bevy_transform/src/components/local_transform.rs delete mode 100644 crates/bevy_transform/src/local_transform_systems.rs delete mode 100644 crates/bevy_transform/src/transform_systems.rs diff --git a/.gitignore b/.gitignore index b3c22eedfbd83..ca2d44df6205e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ **/*.rs.bk Cargo.lock .cargo/config -/.idea \ No newline at end of file +/.idea +/.vscode \ No newline at end of file diff --git a/crates/bevy_pbr/src/entity.rs b/crates/bevy_pbr/src/entity.rs index d8afaa1506034..981c5e4ff680a 100644 --- a/crates/bevy_pbr/src/entity.rs +++ b/crates/bevy_pbr/src/entity.rs @@ -7,7 +7,7 @@ use bevy_render::{ pipeline::{DynamicBinding, PipelineSpecialization, RenderPipeline, RenderPipelines}, render_graph::base::MainPass, }; -use bevy_transform::prelude::{Rotation, Scale, Transform, Translation}; +use bevy_transform::prelude::Transform; /// A component bundle for "pbr mesh" entities #[derive(Bundle)] @@ -18,9 +18,6 @@ pub struct PbrComponents { pub draw: Draw, pub render_pipelines: RenderPipelines, pub transform: Transform, - pub translation: Translation, - pub rotation: Rotation, - pub scale: Scale, } impl Default for PbrComponents { @@ -49,9 +46,6 @@ impl Default for PbrComponents { main_pass: Default::default(), draw: Default::default(), transform: Default::default(), - translation: Default::default(), - rotation: Default::default(), - scale: Default::default(), } } } @@ -61,6 +55,4 @@ impl Default for PbrComponents { pub struct LightComponents { pub light: Light, pub transform: Transform, - pub translation: Translation, - pub rotation: Rotation, } diff --git a/crates/bevy_pbr/src/light.rs b/crates/bevy_pbr/src/light.rs index 47f238df167fc..5db2007bc1a9a 100644 --- a/crates/bevy_pbr/src/light.rs +++ b/crates/bevy_pbr/src/light.rs @@ -1,11 +1,10 @@ use bevy_core::Byteable; -use bevy_math::Mat4; use bevy_property::Properties; use bevy_render::{ camera::{CameraProjection, PerspectiveProjection}, color::Color, }; -use bevy_transform::components::Translation; +use bevy_transform::components::Transform; use std::ops::Range; /// A point light @@ -37,7 +36,7 @@ pub(crate) struct LightRaw { unsafe impl Byteable for LightRaw {} impl LightRaw { - pub fn from(light: &Light, transform: &Mat4, translation: &Translation) -> LightRaw { + pub fn from(light: &Light, transform: &Transform) -> LightRaw { let perspective = PerspectiveProjection { fov: light.fov, aspect_ratio: 1.0, @@ -45,8 +44,8 @@ impl LightRaw { far: light.depth.end, }; - let proj = perspective.get_projection_matrix() * *transform; - let (x, y, z) = translation.0.into(); + let proj = perspective.get_projection_matrix() * *transform.global_matrix(); + let (x, y, z) = transform.global_translation().0.into(); LightRaw { proj: proj.to_cols_array_2d(), pos: [x, y, z, 1.0], diff --git a/crates/bevy_pbr/src/render_graph/lights_node.rs b/crates/bevy_pbr/src/render_graph/lights_node.rs index dbbc48b1ffae4..2eda4eff1d385 100644 --- a/crates/bevy_pbr/src/render_graph/lights_node.rs +++ b/crates/bevy_pbr/src/render_graph/lights_node.rs @@ -80,7 +80,7 @@ pub fn lights_node_system( render_resource_context: Res>, // TODO: this write on RenderResourceBindings will prevent this system from running in parallel with other systems that do the same mut render_resource_bindings: ResMut, - mut query: Query<(&Light, &Transform, &Translation)>, + mut query: Query<(&Light, &Transform)>, ) { let state = &mut state; let render_resource_context = &**render_resource_context; @@ -132,14 +132,12 @@ pub fn lights_node_system( data[0..light_count_size].copy_from_slice([light_count as u32, 0, 0, 0].as_bytes()); // light array - for ((light, transform, translation), slot) in query + for ((light, transform), slot) in query .iter() .iter() .zip(data[light_count_size..current_light_uniform_size].chunks_exact_mut(size)) { - slot.copy_from_slice( - LightRaw::from(&light, &transform.value, &translation).as_bytes(), - ); + slot.copy_from_slice(LightRaw::from(&light, &transform).as_bytes()); } }, ); diff --git a/crates/bevy_render/src/camera/visible_entities.rs b/crates/bevy_render/src/camera/visible_entities.rs index a7c2b3da59fd2..1107e186492ff 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_transform, mut visible_entities) in &mut camera_query.iter() { visible_entities.value.clear(); - let camera_position = camera_transform.value.w_axis().truncate(); + let camera_position = camera_transform.global_matrix().w_axis().truncate(); let mut no_transform_order = 0.0; let mut transparent_entities = Vec::new(); @@ -40,7 +40,7 @@ pub fn visible_entities_system( } let order = if let Ok(transform) = draw_transform_query.get::(entity) { - let position = transform.value.w_axis().truncate(); + let position = transform.global_matrix().w_axis().truncate(); // 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/entity.rs b/crates/bevy_render/src/entity.rs index 417e6a3709b72..6be53934aca31 100644 --- a/crates/bevy_render/src/entity.rs +++ b/crates/bevy_render/src/entity.rs @@ -17,9 +17,6 @@ pub struct MeshComponents { pub render_pipelines: RenderPipelines, pub main_pass: MainPass, pub transform: Transform, - pub translation: Translation, - pub rotation: Rotation, - pub scale: Scale, } /// A component bundle for "3d camera" entities @@ -29,9 +26,6 @@ pub struct Camera3dComponents { pub perspective_projection: PerspectiveProjection, pub visible_entities: VisibleEntities, pub transform: Transform, - pub translation: Translation, - pub rotation: Rotation, - pub scale: Scale, } impl Default for Camera3dComponents { @@ -44,9 +38,6 @@ impl Default for Camera3dComponents { perspective_projection: Default::default(), visible_entities: Default::default(), transform: Default::default(), - translation: Default::default(), - rotation: Default::default(), - scale: Default::default(), } } } 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 247d9abbc7ba8..0518624e18257 100644 --- a/crates/bevy_render/src/render_graph/nodes/camera_node.rs +++ b/crates/bevy_render/src/render_graph/nodes/camera_node.rs @@ -118,7 +118,7 @@ pub fn camera_node_system( let matrix_size = std::mem::size_of::<[[f32; 4]; 4]>(); let camera_matrix: [f32; 16] = - (camera.projection_matrix * transform.value.inverse()).to_cols_array(); + (camera.projection_matrix * transform.global_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 11d17d19f1f4d..2643737576c61 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::Transform { fn get_render_resource(&self, index: usize) -> Option<&dyn RenderResource> { if index == 0 { - Some(&self.value) + Some(self.global_matrix()) } else { None } diff --git a/crates/bevy_transform/src/components/local_transform.rs b/crates/bevy_transform/src/components/local_transform.rs deleted file mode 100644 index 887e5186dfcc0..0000000000000 --- a/crates/bevy_transform/src/components/local_transform.rs +++ /dev/null @@ -1,41 +0,0 @@ -use bevy_math::Mat4; -use bevy_property::Properties; -use std::{ - fmt, - ops::{Deref, DerefMut}, -}; - -#[derive(Debug, PartialEq, Clone, Copy, Properties)] -pub struct LocalTransform(pub Mat4); - -impl LocalTransform { - pub fn identity() -> Self { - Self(Mat4::identity()) - } -} - -impl Default for LocalTransform { - fn default() -> Self { - Self::identity() - } -} - -impl fmt::Display for LocalTransform { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.0) - } -} - -impl Deref for LocalTransform { - type Target = Mat4; - - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl DerefMut for LocalTransform { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 - } -} diff --git a/crates/bevy_transform/src/components/mod.rs b/crates/bevy_transform/src/components/mod.rs index 979b255607e01..608cd64e40652 100644 --- a/crates/bevy_transform/src/components/mod.rs +++ b/crates/bevy_transform/src/components/mod.rs @@ -1,5 +1,4 @@ mod children; -mod local_transform; mod non_uniform_scale; mod parent; mod rotation; @@ -8,7 +7,6 @@ mod transform; mod translation; pub use children::Children; -pub use local_transform::*; pub use non_uniform_scale::*; pub use parent::{Parent, PreviousParent}; pub use rotation::*; diff --git a/crates/bevy_transform/src/components/transform.rs b/crates/bevy_transform/src/components/transform.rs index 8b2904d28f9b5..0addbfd76eaaf 100644 --- a/crates/bevy_transform/src/components/transform.rs +++ b/crates/bevy_transform/src/components/transform.rs @@ -1,35 +1,100 @@ -use bevy_math::Mat4; +use super::{NonUniformScale, Rotation, Scale, Translation}; +use bevy_math::{Mat4, Vec3}; use bevy_property::Properties; use std::fmt; #[derive(Debug, PartialEq, Clone, Copy, Properties)] pub struct Transform { - pub value: Mat4, - pub sync: bool, // NOTE: this is hopefully a temporary measure to allow setting the transform directly. - // ideally setting the transform automatically propagates back to position / translation / rotation, - // but right now they are always considered the source of truth + local: Mat4, + global: Mat4, } impl Transform { #[inline(always)] pub fn identity() -> Self { Transform { - value: Mat4::identity(), - sync: true, + local: Mat4::identity(), + global: Mat4::identity(), } } #[inline(always)] - pub fn new(value: Mat4) -> Self { - Transform { value, sync: true } + pub fn new(local: Mat4) -> Self { + Transform { + local, + global: local, + } } - /// This creates a new `LocalToWorld` transform with the `sync` field set to `false`. - /// While `sync` is false, position, rotation, and scale components will not be synced to the transform. - /// This is helpful if you want to manually set the transform to a value (ex: Mat4::face_toward) - #[inline(always)] - pub fn new_sync_disabled(value: Mat4) -> Self { - Transform { value, sync: false } + pub fn new_with_parent(local: Mat4, parent: &Mat4) -> Self { + Transform { + local, + global: *parent * local, + } + } + + pub fn from_parent(parent: &Mat4) -> Self { + Transform { + local: Mat4::default(), + global: *parent, + } + } + + pub fn local_matrix(&self) -> &Mat4 { + &self.local + } + + pub fn local_matrix_mut(&mut self) -> &mut Mat4 { + &mut self.local + } + + pub fn global_matrix(&self) -> &Mat4 { + &self.global + } + + pub fn apply_parent_matrix(&mut self, parent: Option) { + match parent { + Some(parent) => self.global = parent * self.local, + None => self.global = self.local, + }; + } + + pub fn local_translation(&self) -> Translation { + Vec3::from(self.local.w_axis().truncate()).into() + } + + // FIXME: only gets updated post update + pub fn global_translation(&self) -> Translation { + Vec3::from(self.global.w_axis().truncate()).into() + } + + pub fn set_local_translation(&mut self, translation: &Translation) { + *self.local.w_axis_mut() = translation.extend(1.0); + } + + pub fn translate(&mut self, translation: &Translation) { + *self.local.w_axis_mut() += translation.extend(0.0); + } + + pub fn rotate(&mut self, rotation: &Rotation) { + self.local = self.local.mul_mat4(&Mat4::from_quat(rotation.0)); + } + + pub fn local_non_uniform_scale(&self) -> NonUniformScale { + NonUniformScale::new( + self.local.x_axis().truncate().length(), + self.local.y_axis().truncate().length(), + self.local.z_axis().truncate().length(), + ) + } + + // FIXME: only gets updated post update + pub fn global_non_uniform_scale(&self) -> NonUniformScale { + NonUniformScale::new( + self.global.x_axis().truncate().length(), + self.global.y_axis().truncate().length(), + self.global.z_axis().truncate().length(), + ) } } @@ -41,6 +106,63 @@ 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.local) + } +} + +impl From for Transform { + fn from(translation: Translation) -> Self { + Transform::new(Mat4::from_translation(translation.0)) + } +} + +impl From for Transform { + fn from(rotation: Rotation) -> Self { + Transform::new(Mat4::from_quat(rotation.0)) + } +} + +// NOTE: extra simple for testing purposes +pub struct TransformBuilder { + translation: Translation, + rotation: Rotation, + nu_scale: NonUniformScale, +} + +impl TransformBuilder { + pub fn new() -> Self { + TransformBuilder { + translation: Translation::default(), + rotation: Rotation::default(), + nu_scale: NonUniformScale::default(), + } + } + + pub fn set_translation(mut self, translation: Translation) -> Self { + self.translation = translation; + self + } + + pub fn set_rotation(mut self, rotation: Rotation) -> Self { + self.rotation = rotation; + self + } + + pub fn set_scale(mut self, scale: Scale) -> Self { + self.nu_scale = NonUniformScale::new(scale.0, scale.0, scale.0); + self + } + + pub fn set_non_uniform_scale(mut self, nu_scale: NonUniformScale) -> Self { + self.nu_scale = nu_scale; + self + } + + pub fn build(self) -> Transform { + Transform::new(Mat4::from_scale_rotation_translation( + self.nu_scale.0, + self.rotation.0, + self.translation.0, + )) } } diff --git a/crates/bevy_transform/src/hierarchy/child_builder.rs b/crates/bevy_transform/src/hierarchy/child_builder.rs index f9c4cc0ac6643..5a084e8bb73cf 100644 --- a/crates/bevy_transform/src/hierarchy/child_builder.rs +++ b/crates/bevy_transform/src/hierarchy/child_builder.rs @@ -1,4 +1,4 @@ -use crate::prelude::{Children, LocalTransform, Parent, PreviousParent}; +use crate::prelude::{Children, Parent, PreviousParent, Transform}; use bevy_ecs::{Commands, CommandsInternal, Component, DynamicBundle, Entity, WorldWriter}; use smallvec::SmallVec; @@ -17,7 +17,7 @@ impl WorldWriter for InsertChildren { ( Parent(self.parent), PreviousParent(Some(self.parent)), - LocalTransform::default(), + Transform::default(), ), ) .unwrap(); @@ -58,7 +58,7 @@ impl WorldWriter for PushChildren { ( Parent(self.parent), PreviousParent(Some(self.parent)), - LocalTransform::default(), + Transform::default(), ), ) .unwrap(); @@ -215,7 +215,7 @@ impl<'a> BuildChildren for ChildBuilder<'a> { #[cfg(test)] mod tests { use super::BuildChildren; - use crate::prelude::{Children, LocalTransform, Parent, PreviousParent}; + use crate::prelude::{Children, Parent, PreviousParent, Transform}; use bevy_ecs::{Commands, Entity, Resources, World}; use smallvec::{smallvec, SmallVec}; @@ -262,8 +262,8 @@ mod tests { PreviousParent(Some(parent)) ); - assert!(world.get::(child1).is_ok()); - assert!(world.get::(child2).is_ok()); + assert!(world.get::(child1).is_ok()); + assert!(world.get::(child2).is_ok()); } #[test] @@ -301,8 +301,8 @@ mod tests { PreviousParent(Some(parent)) ); - assert!(world.get::(child1).is_ok()); - assert!(world.get::(child2).is_ok()); + assert!(world.get::(child1).is_ok()); + assert!(world.get::(child2).is_ok()); commands.insert_children(parent, 1, &entities[3..]); commands.apply(&mut world, &mut resources); @@ -323,7 +323,7 @@ mod tests { PreviousParent(Some(parent)) ); - assert!(world.get::(child3).is_ok()); - assert!(world.get::(child4).is_ok()); + assert!(world.get::(child3).is_ok()); + assert!(world.get::(child4).is_ok()); } } diff --git a/crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs b/crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs index 7fc29ac9ed9c0..7245cfac01e0d 100644 --- a/crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs +++ b/crates/bevy_transform/src/hierarchy/hierarchy_maintenance_system.rs @@ -127,13 +127,13 @@ mod test { let mut parent = None; let mut children = Vec::new(); commands - .spawn((Translation::new(1.0, 0.0, 0.0), Transform::identity())) + .spawn((Transform::from(Translation::new(1.0, 0.0, 0.0)), ())) .for_current_entity(|entity| parent = Some(entity)) .with_children(|parent| { parent - .spawn((Translation::new(0.0, 2.0, 0.0), Transform::identity())) + .spawn((Transform::from(Translation::new(0.0, 2.0, 0.0)), ())) .for_current_entity(|entity| children.push(entity)) - .spawn((Translation::new(0.0, 0.0, 3.0), Transform::identity())) + .spawn((Transform::from(Translation::new(0.0, 0.0, 3.0)), ())) .for_current_entity(|entity| children.push(entity)); }); let parent = parent.unwrap(); diff --git a/crates/bevy_transform/src/hierarchy/world_child_builder.rs b/crates/bevy_transform/src/hierarchy/world_child_builder.rs index 54230ab712536..a628c52316c6c 100644 --- a/crates/bevy_transform/src/hierarchy/world_child_builder.rs +++ b/crates/bevy_transform/src/hierarchy/world_child_builder.rs @@ -1,4 +1,4 @@ -use crate::prelude::{Children, LocalTransform, Parent, PreviousParent}; +use crate::prelude::{Children, Parent, PreviousParent, Transform}; use bevy_ecs::{Component, DynamicBundle, Entity, WorldBuilder}; pub struct WorldChildBuilder<'a, 'b> { @@ -26,7 +26,7 @@ impl<'a, 'b> WorldChildBuilder<'a, 'b> { .with_bundle(( Parent(parent_entity), PreviousParent(Some(parent_entity)), - LocalTransform::default(), + Transform::default(), )); { let world = &mut self.world_builder.world; diff --git a/crates/bevy_transform/src/lib.rs b/crates/bevy_transform/src/lib.rs index 66c59e0c5d263..5f16afd50a03a 100644 --- a/crates/bevy_transform/src/lib.rs +++ b/crates/bevy_transform/src/lib.rs @@ -1,8 +1,6 @@ pub mod components; pub mod hierarchy; -pub mod local_transform_systems; pub mod transform_propagate_system; -pub mod transform_systems; pub mod prelude { pub use crate::{components::*, hierarchy::*, TransformPlugin}; @@ -11,16 +9,12 @@ pub mod prelude { use bevy_app::prelude::*; use bevy_ecs::prelude::*; use bevy_type_registry::RegisterType; -use prelude::{ - Children, LocalTransform, NonUniformScale, Parent, Rotation, Scale, Transform, Translation, -}; +use prelude::{Children, NonUniformScale, Parent, Rotation, Scale, Transform, Translation}; pub(crate) fn transform_systems() -> Vec> { let mut systems = Vec::with_capacity(5); systems.append(&mut hierarchy::hierarchy_maintenance_systems()); - systems.append(&mut local_transform_systems::local_transform_systems()); - systems.append(&mut transform_systems::transform_systems()); systems.push(transform_propagate_system::transform_propagate_system.system()); systems @@ -33,7 +27,6 @@ impl Plugin for TransformPlugin { fn build(&self, app: &mut AppBuilder) { app.register_component::() .register_component::() - .register_component::() .register_component::() .register_component::() .register_component::() diff --git a/crates/bevy_transform/src/local_transform_systems.rs b/crates/bevy_transform/src/local_transform_systems.rs deleted file mode 100644 index 938aa87b3ccc8..0000000000000 --- a/crates/bevy_transform/src/local_transform_systems.rs +++ /dev/null @@ -1,273 +0,0 @@ -use crate::components::*; -use bevy_ecs::prelude::*; -use bevy_math::{Mat4, Quat, Vec3}; - -// TODO: "on changed" for all of these systems -pub fn local_transform_translation_system( - mut query: Query< - Without< - Rotation, - Without>, - >, - >, -) { - for (mut local, translation) in &mut query.iter() { - *local = LocalTransform(Mat4::from_translation(translation.0)); - } -} - -pub fn local_transform_rotation_system( - mut query: Query< - Without< - Translation, - Without>, - >, - >, -) { - for (mut local, rotation) in &mut query.iter() { - *local = LocalTransform(Mat4::from_quat(rotation.0)); - } -} - -pub fn local_transform_scale_system( - mut query: Query< - Without< - Translation, - Without>, - >, - >, -) { - for (mut local, scale) in &mut query.iter() { - *local = LocalTransform(Mat4::from_scale(Vec3::new(scale.0, scale.0, scale.0))); - } -} - -pub fn local_transform_non_uniform_scale_system( - mut query: Query< - Without< - Translation, - Without>, - >, - >, -) { - for (mut local, non_uniform_scale) in &mut query.iter() { - *local = LocalTransform(Mat4::from_scale(non_uniform_scale.0)); - } -} - -pub fn local_transform_translation_rotation_system( - mut query: Query< - Without>, - >, -) { - for (mut local, translation, rotation) in &mut query.iter() { - *local = LocalTransform(Mat4::from_rotation_translation(rotation.0, translation.0)); - } -} - -pub fn local_transform_translation_scale_system( - mut query: Query< - Without>, - >, -) { - for (mut local, translation, scale) in &mut query.iter() { - *local = LocalTransform(Mat4::from_scale_rotation_translation( - Vec3::new(scale.0, scale.0, scale.0), - Quat::default(), - translation.0, - )); - } -} - -pub fn local_transform_translation_non_uniform_scale_system( - mut query: Query< - Without>, - >, -) { - for (mut local, translation, non_uniform_scale) in &mut query.iter() { - *local = LocalTransform(Mat4::from_scale_rotation_translation( - non_uniform_scale.0, - Quat::default(), - translation.0, - )); - } -} - -pub fn local_transform_rotation_scale_system( - mut query: Query< - Without>, - >, -) { - for (mut local, rotation, scale) in &mut query.iter() { - *local = LocalTransform(Mat4::from_scale_rotation_translation( - Vec3::new(scale.0, scale.0, scale.0), - rotation.0, - Vec3::default(), - )); - } -} - -pub fn local_transform_rotation_non_uniform_scale_system( - mut query: Query< - Without>, - >, -) { - for (mut local, rotation, non_uniform_scale) in &mut query.iter() { - *local = LocalTransform(Mat4::from_scale_rotation_translation( - non_uniform_scale.0, - rotation.0, - Vec3::default(), - )); - } -} - -pub fn local_transform_translation_rotation_scale_system( - mut query: Query< - Without, - >, -) { - for (mut local, translation, rotation, scale) in &mut query.iter() { - *local = LocalTransform(Mat4::from_scale_rotation_translation( - Vec3::new(scale.0, scale.0, scale.0), - rotation.0, - translation.0, - )); - } -} - -pub fn local_transform_translation_rotation_non_uniform_scale_system( - mut query: Query< - Without< - Scale, - ( - &mut LocalTransform, - &Translation, - &Rotation, - &NonUniformScale, - ), - >, - >, -) { - for (mut local, translation, rotation, non_uniform_scale) in &mut query.iter() { - *local = LocalTransform(Mat4::from_scale_rotation_translation( - non_uniform_scale.0, - rotation.0, - translation.0, - )); - } -} - -pub fn local_transform_systems() -> Vec> { - vec![ - local_transform_translation_system.system(), - local_transform_rotation_system.system(), - local_transform_scale_system.system(), - local_transform_non_uniform_scale_system.system(), - local_transform_translation_rotation_system.system(), - local_transform_translation_scale_system.system(), - local_transform_translation_non_uniform_scale_system.system(), - local_transform_rotation_scale_system.system(), - local_transform_rotation_non_uniform_scale_system.system(), - local_transform_translation_rotation_scale_system.system(), - local_transform_translation_rotation_non_uniform_scale_system.system(), - ] -} - -#[cfg(test)] -mod test { - use super::*; - use bevy_ecs::{Resources, Schedule, World}; - use bevy_math::{Mat4, Quat, Vec3}; - - #[test] - fn correct_local_transformation() { - let mut world = World::default(); - let mut resources = Resources::default(); - - let mut schedule = Schedule::default(); - schedule.add_stage("update"); - for system in local_transform_systems() { - schedule.add_system_to_stage("update", system); - } - - let local_transform = LocalTransform::identity(); - let t = Translation::new(1.0, 2.0, 3.0); - let r = Rotation(Quat::from_rotation_ypr(1.0, 2.0, 3.0)); - let s = Scale(2.0); - let nus = NonUniformScale::new(1.0, 2.0, 3.0); - - // Add every combination of transform types. - let translation = world.spawn((local_transform, t)); - let rotation = world.spawn((local_transform, r)); - let scale = world.spawn((local_transform, s)); - let non_uniform_scale = world.spawn((local_transform, nus)); - let translation_and_rotation = world.spawn((local_transform, t, r)); - let translation_and_scale = world.spawn((local_transform, t, s)); - let translation_and_nus = world.spawn((local_transform, t, nus)); - let rotation_scale = world.spawn((local_transform, r, s)); - let rotation_nus = world.spawn((local_transform, r, nus)); - let translation_rotation_scale = world.spawn((local_transform, t, r, s)); - let translation_rotation_nus = world.spawn((local_transform, t, r, nus)); - - // Run the system - schedule.run(&mut world, &mut resources); - - // Verify that each was transformed correctly. - assert_eq!( - world.get::(translation).unwrap().0, - Mat4::from_translation(t.0) - ); - assert_eq!( - world.get::(rotation).unwrap().0, - Mat4::from_quat(r.0) - ); - assert_eq!( - world.get::(scale).unwrap().0, - Mat4::from_scale(Vec3::new(s.0, s.0, s.0)) - ); - assert_eq!( - world.get::(non_uniform_scale).unwrap().0, - Mat4::from_scale(nus.0) - ); - assert_eq!( - world - .get::(translation_and_rotation) - .unwrap() - .0, - Mat4::from_rotation_translation(r.0, t.0) - ); - assert_eq!( - world - .get::(translation_and_scale) - .unwrap() - .0, - Mat4::from_scale_rotation_translation(Vec3::new(s.0, s.0, s.0), Quat::default(), t.0) - ); - assert_eq!( - world.get::(translation_and_nus).unwrap().0, - Mat4::from_scale_rotation_translation(nus.0, Quat::default(), t.0) - ); - assert_eq!( - world.get::(rotation_scale).unwrap().0, - Mat4::from_scale_rotation_translation(Vec3::new(s.0, s.0, s.0), r.0, Vec3::default()) - ); - assert_eq!( - world.get::(rotation_nus).unwrap().0, - Mat4::from_scale_rotation_translation(nus.0, r.0, Vec3::default()) - ); - assert_eq!( - world - .get::(translation_rotation_scale) - .unwrap() - .0, - Mat4::from_scale_rotation_translation(Vec3::new(s.0, s.0, s.0), r.0, t.0) - ); - assert_eq!( - world - .get::(translation_rotation_nus) - .unwrap() - .0, - Mat4::from_scale_rotation_translation(nus.0, r.0, t.0) - ); - } -} diff --git a/crates/bevy_transform/src/transform_propagate_system.rs b/crates/bevy_transform/src/transform_propagate_system.rs index 6068cfcc6f3be..bc0bb2a0f21b8 100644 --- a/crates/bevy_transform/src/transform_propagate_system.rs +++ b/crates/bevy_transform/src/transform_propagate_system.rs @@ -1,61 +1,46 @@ use crate::components::*; use bevy_ecs::prelude::*; +use bevy_math::Mat4; +// ANSWERME: make this take all transforms and perform in recursion (looking at compute shader) pub fn transform_propagate_system( - mut root_query: Query< - Without, &mut Transform, Option<&LocalTransform>)>, - >, - mut local_transform_query: Query<(&mut Transform, &LocalTransform, Option<&Children>)>, + mut root_query: Query, &mut Transform)>>, + mut transform_query: Query<(&mut Transform, Option<&Children>)>, ) { - for (children, mut transform, local_transform) in &mut root_query.iter() { - if let Some(local_transform) = local_transform { - transform.value = local_transform.0; - } + for (children, mut transform) in &mut root_query.iter() { + transform.apply_parent_matrix(None); if let Some(children) = children { for child in children.0.iter() { - propagate_recursive(*transform, &mut local_transform_query, *child); + propagate_recursive(*transform.global_matrix(), &mut transform_query, *child); } } } } +// ANSWERME: maybe speed this up with compute fn propagate_recursive( - parent_local_to_world: Transform, - local_transform_query: &mut Query<(&mut Transform, &LocalTransform, Option<&Children>)>, + parent: Mat4, + transform_query: &mut Query<(&mut Transform, Option<&Children>)>, entity: Entity, ) { log::trace!("Updating Transform for {:?}", entity); - let local_transform = { - if let Ok(local_transform) = local_transform_query.get::(entity) { - *local_transform - } else { - log::warn!( - "Entity {:?} is a child in the hierarchy but does not have a LocalTransform", - entity - ); - return; - } - }; - let new_transform = Transform { - value: parent_local_to_world.value * local_transform.0, - sync: true, - }; + let global_matrix = { + let mut transform = transform_query.get_mut::(entity).unwrap(); - { - let mut transform = local_transform_query.get_mut::(entity).unwrap(); - transform.value = new_transform.value; - } + transform.apply_parent_matrix(Some(parent)); + *transform.global_matrix() + }; // Collect children - let children = local_transform_query + let children = transform_query .get::(entity) .map(|e| e.0.iter().cloned().collect::>()) .unwrap_or_default(); for child in children { - propagate_recursive(new_transform, local_transform_query, child); + propagate_recursive(global_matrix, transform_query, child); } } @@ -78,19 +63,15 @@ mod test { } // Root entity - let parent = world.spawn((Translation::new(1.0, 0.0, 0.0), Transform::identity())); + let parent = world.spawn((Transform::from(Translation::new(1.0, 0.0, 0.0)), ())); //FIXME: shouldn't need () to be added let children = world .spawn_batch(vec![ ( - Translation::new(0.0, 2.0, 0.0), - LocalTransform::identity(), - Transform::identity(), + Transform::from(Translation::new(0.0, 2.0, 0.0)), Parent(parent), ), ( - Translation::new(0.0, 0.0, 3.0), - LocalTransform::identity(), - Transform::identity(), + Transform::from(Translation::new(0.0, 0.0, 3.0)), Parent(parent), ), ]) @@ -103,13 +84,13 @@ mod test { schedule.run(&mut world, &mut resources); assert_eq!( - world.get::(children[0]).unwrap().value, + *world.get::(children[0]).unwrap().global_matrix(), 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().global_matrix(), Mat4::from_translation(Vec3::new(1.0, 0.0, 0.0)) * Mat4::from_translation(Vec3::new(0.0, 0.0, 3.0)) ); @@ -130,25 +111,25 @@ mod test { let mut commands = Commands::default(); let mut children = Vec::new(); commands - .spawn((Translation::new(1.0, 0.0, 0.0), Transform::identity())) + .spawn((Transform::from(Translation::new(1.0, 0.0, 0.0)), ())) //FIXME: shouldn't need () to be added .with_children(|parent| { parent - .spawn((Translation::new(0.0, 2.0, 0.0), Transform::identity())) + .spawn((Transform::from(Translation::new(0.0, 2.0, 0.0)), ())) .for_current_entity(|entity| children.push(entity)) - .spawn((Translation::new(0.0, 0.0, 3.0), Transform::identity())) + .spawn((Transform::from(Translation::new(0.0, 0.0, 3.0)), ())) .for_current_entity(|entity| children.push(entity)); }); commands.apply(&mut world, &mut resources); schedule.run(&mut world, &mut resources); assert_eq!( - world.get::(children[0]).unwrap().value, + *world.get::(children[0]).unwrap().global_matrix(), 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().global_matrix(), Mat4::from_translation(Vec3::new(1.0, 0.0, 0.0)) * Mat4::from_translation(Vec3::new(0.0, 0.0, 3.0)) ); diff --git a/crates/bevy_transform/src/transform_systems.rs b/crates/bevy_transform/src/transform_systems.rs deleted file mode 100644 index ec2d57c63ece1..0000000000000 --- a/crates/bevy_transform/src/transform_systems.rs +++ /dev/null @@ -1,339 +0,0 @@ -use crate::components::*; -use bevy_ecs::prelude::*; -use bevy_math::{Mat4, Quat, Vec3}; - -// TODO: on changed for all of these systems -pub fn transform_translation_system( - mut query: Query< - Without< - LocalTransform, - Without< - Rotation, - Without>, - >, - >, - >, -) { - for (mut transform, translation) in &mut query.iter() { - if !transform.sync { - continue; - } - - *transform = Transform::new(Mat4::from_translation(translation.0)); - } -} - -pub fn transform_rotation_system( - mut query: Query< - Without< - LocalTransform, - Without< - Translation, - Without>, - >, - >, - >, -) { - for (mut transform, rotation) in &mut query.iter() { - if !transform.sync { - continue; - } - - *transform = Transform::new(Mat4::from_quat(rotation.0)); - } -} - -pub fn transform_scale_system( - mut query: Query< - Without< - LocalTransform, - Without< - Translation, - Without>, - >, - >, - >, -) { - for (mut transform, scale) in &mut query.iter() { - if !transform.sync { - continue; - } - - *transform = Transform::new(Mat4::from_scale(Vec3::new(scale.0, scale.0, scale.0))); - } -} - -pub fn transform_non_uniform_scale_system( - mut query: Query< - Without< - LocalTransform, - Without< - Translation, - Without>, - >, - >, - >, -) { - for (mut transform, non_uniform_scale) in &mut query.iter() { - if !transform.sync { - continue; - } - - *transform = Transform::new(Mat4::from_scale(non_uniform_scale.0)); - } -} - -pub fn transform_translation_rotation_system( - mut query: Query< - Without< - LocalTransform, - Without>, - >, - >, -) { - for (mut transform, translation, rotation) in &mut query.iter() { - if !transform.sync { - continue; - } - - *transform = Transform::new(Mat4::from_rotation_translation(rotation.0, translation.0)); - } -} - -pub fn transform_translation_scale_system( - mut query: Query< - Without< - LocalTransform, - Without>, - >, - >, -) { - for (mut transform, translation, scale) in &mut query.iter() { - if !transform.sync { - continue; - } - - *transform = Transform::new(Mat4::from_scale_rotation_translation( - Vec3::new(scale.0, scale.0, scale.0), - Quat::default(), - translation.0, - )); - } -} - -pub fn transform_translation_non_uniform_scale_system( - mut query: Query< - Without< - LocalTransform, - Without>, - >, - >, -) { - for (mut transform, translation, non_uniform_scale) in &mut query.iter() { - if !transform.sync { - continue; - } - - *transform = Transform::new(Mat4::from_scale_rotation_translation( - non_uniform_scale.0, - Quat::default(), - translation.0, - )); - } -} - -pub fn transform_rotation_scale_system( - mut query: Query< - Without< - LocalTransform, - Without>, - >, - >, -) { - for (mut transform, rotation, scale) in &mut query.iter() { - if !transform.sync { - continue; - } - - *transform = Transform::new(Mat4::from_scale_rotation_translation( - Vec3::new(scale.0, scale.0, scale.0), - rotation.0, - Vec3::default(), - )); - } -} - -pub fn transform_rotation_non_uniform_scale_system( - mut query: Query< - Without< - LocalTransform, - Without>, - >, - >, -) { - for (mut transform, rotation, non_uniform_scale) in &mut query.iter() { - if !transform.sync { - continue; - } - - *transform = Transform::new(Mat4::from_scale_rotation_translation( - non_uniform_scale.0, - rotation.0, - Vec3::default(), - )); - } -} - -pub fn transform_translation_rotation_scale_system( - mut query: Query< - Without< - LocalTransform, - Without, - >, - >, -) { - for (mut transform, translation, rotation, scale) in &mut query.iter() { - if !transform.sync { - continue; - } - - *transform = Transform::new(Mat4::from_scale_rotation_translation( - Vec3::new(scale.0, scale.0, scale.0), - rotation.0, - translation.0, - )); - } -} - -pub fn transform_translation_rotation_non_uniform_scale_system( - mut query: Query< - Without< - LocalTransform, - Without, - >, - >, -) { - for (mut transform, translation, rotation, non_uniform_scale) in &mut query.iter() { - if !transform.sync { - continue; - } - - *transform = Transform::new(Mat4::from_scale_rotation_translation( - non_uniform_scale.0, - rotation.0, - translation.0, - )); - } -} - -pub fn transform_systems() -> Vec> { - vec![ - transform_translation_system.system(), - transform_rotation_system.system(), - transform_scale_system.system(), - transform_non_uniform_scale_system.system(), - transform_translation_rotation_system.system(), - transform_translation_scale_system.system(), - transform_translation_non_uniform_scale_system.system(), - transform_rotation_scale_system.system(), - transform_rotation_non_uniform_scale_system.system(), - transform_translation_rotation_scale_system.system(), - transform_translation_rotation_non_uniform_scale_system.system(), - ] -} - -#[cfg(test)] -mod test { - use super::*; - use bevy_ecs::{Resources, Schedule, World}; - use bevy_math::{Mat4, Quat, Vec3}; - - #[test] - fn correct_world_transformation() { - let mut world = World::default(); - let mut resources = Resources::default(); - - let mut schedule = Schedule::default(); - schedule.add_stage("update"); - for system in transform_systems() { - schedule.add_system_to_stage("update", system); - } - - let transform = Transform::identity(); - let t = Translation::new(1.0, 2.0, 3.0); - let r = Rotation(Quat::from_rotation_ypr(1.0, 2.0, 3.0)); - let s = Scale(2.0); - let nus = NonUniformScale::new(1.0, 2.0, 3.0); - - // Add every combination of transform types. - let translation = world.spawn((transform, t)); - let rotation = world.spawn((transform, r)); - let scale = world.spawn((transform, s)); - let non_uniform_scale = world.spawn((transform, nus)); - let translation_and_rotation = world.spawn((transform, t, r)); - let translation_and_scale = world.spawn((transform, t, s)); - let translation_and_nus = world.spawn((transform, t, nus)); - let rotation_scale = world.spawn((transform, r, s)); - let rotation_nus = world.spawn((transform, r, nus)); - let translation_rotation_scale = world.spawn((transform, t, r, s)); - let translation_rotation_nus = world.spawn((transform, t, r, nus)); - - // Run the system - schedule.run(&mut world, &mut resources); - - // Verify that each was transformed correctly. - assert_eq!( - world.get::(translation).unwrap().value, - Mat4::from_translation(t.0) - ); - assert_eq!( - world.get::(rotation).unwrap().value, - Mat4::from_quat(r.0) - ); - assert_eq!( - world.get::(scale).unwrap().value, - Mat4::from_scale(Vec3::new(s.0, s.0, s.0)) - ); - assert_eq!( - world.get::(non_uniform_scale).unwrap().value, - Mat4::from_scale(nus.0) - ); - assert_eq!( - world - .get::(translation_and_rotation) - .unwrap() - .value, - Mat4::from_rotation_translation(r.0, t.0) - ); - assert_eq!( - world.get::(translation_and_scale).unwrap().value, - Mat4::from_scale_rotation_translation(Vec3::new(s.0, s.0, s.0), Quat::default(), t.0) - ); - assert_eq!( - world.get::(translation_and_nus).unwrap().value, - Mat4::from_scale_rotation_translation(nus.0, Quat::default(), t.0) - ); - assert_eq!( - world.get::(rotation_scale).unwrap().value, - Mat4::from_scale_rotation_translation(Vec3::new(s.0, s.0, s.0), r.0, Vec3::default()) - ); - assert_eq!( - world.get::(rotation_nus).unwrap().value, - Mat4::from_scale_rotation_translation(nus.0, r.0, Vec3::default()) - ); - assert_eq!( - world - .get::(translation_rotation_scale) - .unwrap() - .value, - Mat4::from_scale_rotation_translation(Vec3::new(s.0, s.0, s.0), r.0, t.0) - ); - assert_eq!( - world - .get::(translation_rotation_nus) - .unwrap() - .value, - Mat4::from_scale_rotation_translation(nus.0, r.0, t.0) - ); - } -} diff --git a/crates/bevy_ui/src/entity.rs b/crates/bevy_ui/src/entity.rs index 6c857f339d241..6a18a6d9d827a 100644 --- a/crates/bevy_ui/src/entity.rs +++ b/crates/bevy_ui/src/entity.rs @@ -13,10 +13,7 @@ use bevy_render::{ pipeline::{DynamicBinding, PipelineSpecialization, RenderPipeline, RenderPipelines}, }; use bevy_sprite::{ColorMaterial, QUAD_HANDLE}; -use bevy_transform::{ - components::LocalTransform, - prelude::{Rotation, Scale, Transform, Translation}, -}; +use bevy_transform::prelude::{Rotation, Scale, Transform, Translation}; #[derive(Bundle)] pub struct NodeComponents { @@ -27,7 +24,6 @@ pub struct NodeComponents { pub draw: Draw, pub render_pipelines: RenderPipelines, pub transform: Transform, - pub local_transform: LocalTransform, } impl Default for NodeComponents { @@ -57,7 +53,6 @@ impl Default for NodeComponents { material: Default::default(), draw: Default::default(), transform: Default::default(), - local_transform: Default::default(), } } } @@ -73,7 +68,6 @@ pub struct ImageComponents { pub draw: Draw, pub render_pipelines: RenderPipelines, pub transform: Transform, - pub local_transform: LocalTransform, } impl Default for ImageComponents { @@ -105,7 +99,6 @@ impl Default for ImageComponents { material: Default::default(), draw: Default::default(), transform: Default::default(), - local_transform: Default::default(), } } } @@ -119,7 +112,6 @@ pub struct TextComponents { pub calculated_size: CalculatedSize, pub focus_policy: FocusPolicy, pub transform: Transform, - pub local_transform: LocalTransform, } impl Default for TextComponents { @@ -135,7 +127,6 @@ impl Default for TextComponents { calculated_size: Default::default(), style: Default::default(), transform: Default::default(), - local_transform: Default::default(), } } } @@ -152,7 +143,6 @@ pub struct ButtonComponents { pub draw: Draw, pub render_pipelines: RenderPipelines, pub transform: Transform, - pub local_transform: LocalTransform, } impl Default for ButtonComponents { @@ -185,7 +175,6 @@ impl Default for ButtonComponents { material: Default::default(), draw: Default::default(), transform: Default::default(), - local_transform: Default::default(), } } } diff --git a/crates/bevy_ui/src/flex/mod.rs b/crates/bevy_ui/src/flex/mod.rs index c9a597ddff853..af764ca44d345 100644 --- a/crates/bevy_ui/src/flex/mod.rs +++ b/crates/bevy_ui/src/flex/mod.rs @@ -3,7 +3,7 @@ mod convert; use crate::{CalculatedSize, Node, Style}; use bevy_ecs::{Changed, Entity, Query, Res, ResMut, With, Without}; use bevy_math::Vec2; -use bevy_transform::prelude::{Children, LocalTransform, Parent}; +use bevy_transform::prelude::{Children, Parent, Transform}; use bevy_window::{Window, WindowId, Windows}; use std::collections::HashMap; use stretch::{number::Number, Stretch}; @@ -154,7 +154,7 @@ pub fn flex_node_system( mut node_query: Query, Option<&CalculatedSize>)>>, mut changed_size_query: Query)>>, mut children_query: Query)>>, - mut node_transform_query: Query<(Entity, &mut Node, &mut LocalTransform, Option<&Parent>)>, + mut node_transform_query: Query<(Entity, &mut Node, &mut Transform, Option<&Parent>)>, ) { // update window root nodes for window in windows.iter() { @@ -190,10 +190,10 @@ pub fn flex_node_system( // compute layouts flex_surface.compute_window_layouts(); - for (entity, mut node, mut local, parent) in &mut node_transform_query.iter() { + 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 mut position = local.w_axis(); + let mut position = transform.local_matrix().w_axis(); 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 { @@ -203,6 +203,6 @@ pub fn flex_node_system( } } - local.set_w_axis(position); + transform.local_matrix_mut().set_w_axis(position); } } diff --git a/crates/bevy_ui/src/focus.rs b/crates/bevy_ui/src/focus.rs index 9006ef20d5c9b..de9ed09ca6e8b 100644 --- a/crates/bevy_ui/src/focus.rs +++ b/crates/bevy_ui/src/focus.rs @@ -73,7 +73,7 @@ pub fn ui_focus_system( let mut moused_over_z_sorted_nodes = query_iter .iter() .filter_map(|(entity, node, transform, interaction, focus_policy)| { - let position = transform.value.w_axis(); + let position = transform.global_matrix().w_axis(); let ui_position = position.truncate().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 93e54b6358874..2a8e22154bce3 100644 --- a/crates/bevy_ui/src/update.rs +++ b/crates/bevy_ui/src/update.rs @@ -2,14 +2,14 @@ use super::Node; use bevy_ecs::{Entity, Query, With, Without}; use bevy_transform::{ hierarchy, - prelude::{Children, LocalTransform, Parent}, + prelude::{Children, Parent, Transform}, }; pub const UI_Z_STEP: f32 = 0.001; pub fn ui_z_system( mut root_node_query: Query>>, - mut node_query: Query<(Entity, &Node, &mut LocalTransform)>, + mut node_query: Query<(Entity, &Node, &mut Transform)>, children_query: Query<&Children>, ) { let mut current_global_z = 0.0; @@ -34,12 +34,12 @@ pub fn ui_z_system( } fn update_node_entity( - node_query: &mut Query<(Entity, &Node, &mut LocalTransform)>, + node_query: &mut Query<(Entity, &Node, &mut Transform)>, entity: Entity, parent_result: Option, previous_result: Option, ) -> Option { - let mut transform = node_query.get_mut::(entity).ok()?; + let mut transform = node_query.get_mut::(entity).ok()?; let mut z = UI_Z_STEP; let parent_global_z = parent_result.unwrap(); if let Some(previous_global_z) = previous_result { @@ -47,9 +47,9 @@ fn update_node_entity( }; let global_z = z + parent_global_z; - let mut position = transform.w_axis(); + let mut position = transform.local_matrix().w_axis(); position.set_z(z); - transform.set_w_axis(position); + transform.local_matrix_mut().set_w_axis(position); Some(global_z) } diff --git a/crates/bevy_ui/src/widget/text.rs b/crates/bevy_ui/src/widget/text.rs index f631e82d1ec39..5ae5a0d08b6a3 100644 --- a/crates/bevy_ui/src/widget/text.rs +++ b/crates/bevy_ui/src/widget/text.rs @@ -61,8 +61,8 @@ pub fn draw_text_system( mut query: Query<(&mut Draw, &Text, &Node, &Transform)>, ) { for (mut draw, text, node, transform) in &mut query.iter() { - let position = - Vec3::from(transform.value.w_axis().truncate()) - (node.size / 2.0).extend(0.0); + let position = Vec3::from(transform.global_matrix().w_axis().truncate()) + - (node.size / 2.0).extend(0.0); let mut drawable_text = DrawableText { font: fonts.get(&text.font).unwrap(), diff --git a/examples/3d/3d_scene.rs b/examples/3d/3d_scene.rs index 3f219d087d686..92a255d9ec0b0 100644 --- a/examples/3d/3d_scene.rs +++ b/examples/3d/3d_scene.rs @@ -26,9 +26,17 @@ fn setup( .spawn(PbrComponents { mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), material: materials.add(Color::rgb(0.5, 0.4, 0.3).into()), - translation: Translation::new(0.0, 1.0, 0.0), + transform: Translation::new(0.0, 1.0, 0.0).into(), ..Default::default() }) + .with_children(|parent| { + parent.spawn(PbrComponents { + mesh: meshes.add(Mesh::from(shape::Cube { size: 0.5 })), + material: materials.add(Color::rgb(0.5, 0.4, 0.3).into()), + transform: Translation::new(0.0, 0.75, 0.0).into(), + ..Default::default() + }); + }) // sphere .spawn(PbrComponents { mesh: meshes.add(Mesh::from(shape::Icosphere { @@ -36,17 +44,17 @@ fn setup( radius: 0.5, })), material: materials.add(Color::rgb(0.1, 0.4, 0.8).into()), - translation: Translation::new(1.5, 1.5, 1.5), + transform: Translation::new(1.5, 1.5, 1.5).into(), ..Default::default() }) // light .spawn(LightComponents { - translation: Translation::new(4.0, 8.0, 4.0), + transform: Translation::new(4.0, 8.0, 4.0).into(), ..Default::default() }) // camera .spawn(Camera3dComponents { - transform: Transform::new_sync_disabled(Mat4::face_toward( + 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), diff --git a/examples/3d/load_model.rs b/examples/3d/load_model.rs index 6781b44a402d4..cc8e487df92e2 100644 --- a/examples/3d/load_model.rs +++ b/examples/3d/load_model.rs @@ -27,12 +27,12 @@ fn setup( }) // light .spawn(LightComponents { - translation: Translation::new(4.0, 5.0, 4.0), + transform: Translation::new(4.0, 5.0, 4.0).into(), ..Default::default() }) // camera .spawn(Camera3dComponents { - transform: Transform::new_sync_disabled(Mat4::face_toward( + 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), diff --git a/examples/3d/msaa.rs b/examples/3d/msaa.rs index fa70a2deaa7f4..b4b885555aca5 100644 --- a/examples/3d/msaa.rs +++ b/examples/3d/msaa.rs @@ -27,12 +27,12 @@ fn setup( }) // light .spawn(LightComponents { - translation: Translation::new(4.0, 8.0, 4.0), + transform: Translation::new(4.0, 8.0, 4.0).into(), ..Default::default() }) // camera .spawn(Camera3dComponents { - transform: Transform::new_sync_disabled(Mat4::face_toward( + 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), diff --git a/examples/3d/parenting.rs b/examples/3d/parenting.rs index 16ebd93c5f6b9..f9017e727d33f 100644 --- a/examples/3d/parenting.rs +++ b/examples/3d/parenting.rs @@ -8,6 +8,7 @@ fn main() { .add_default_plugins() .add_startup_system(setup.system()) .add_system(rotator_system.system()) + .add_system(print_children.system()) .run(); } @@ -15,9 +16,22 @@ fn main() { struct Rotator; /// rotates the parent, which will result in the child also rotating -fn rotator_system(time: Res