From 691ad98f95b6db5bc69954aeb77f9ad3135962ac Mon Sep 17 00:00:00 2001 From: Felix de Maneville Date: Tue, 28 Jun 2022 15:33:58 +0200 Subject: [PATCH 1/6] GlobalTransform Rework --- crates/bevy_pbr/src/light.rs | 6 +- crates/bevy_pbr/src/render/light.rs | 8 +- crates/bevy_render/src/camera/camera.rs | 2 +- crates/bevy_render/src/view/mod.rs | 4 +- crates/bevy_text/src/text2d.rs | 2 +- .../src/components/global_transform.rs | 246 ++---------------- .../src/components/transform.rs | 15 +- crates/bevy_transform/src/lib.rs | 2 +- crates/bevy_transform/src/systems.rs | 18 +- crates/bevy_ui/src/render/mod.rs | 4 +- crates/bevy_utils/src/float_ord.rs | 16 +- examples/animation/custom_skinned_mesh.rs | 4 +- 12 files changed, 63 insertions(+), 264 deletions(-) diff --git a/crates/bevy_pbr/src/light.rs b/crates/bevy_pbr/src/light.rs index 575c926b4ce40..2b903bc35b2ee 100644 --- a/crates/bevy_pbr/src/light.rs +++ b/crates/bevy_pbr/src/light.rs @@ -14,7 +14,7 @@ use bevy_render::{ renderer::RenderDevice, view::{ComputedVisibility, RenderLayers, Visibility, VisibleEntities}, }; -use bevy_transform::components::GlobalTransform; +use bevy_transform::components::{GlobalTransform, Transform}; use bevy_utils::tracing::warn; use crate::{ @@ -1201,7 +1201,7 @@ pub fn update_point_light_frusta( Mat4::perspective_infinite_reverse_rh(std::f32::consts::FRAC_PI_2, 1.0, POINT_LIGHT_NEAR_Z); let view_rotations = CUBE_MAP_FACES .iter() - .map(|CubeMapFace { target, up }| GlobalTransform::identity().looking_at(*target, *up)) + .map(|CubeMapFace { target, up }| Transform::identity().looking_at(*target, *up)) .collect::>(); for (entity, transform, point_light, mut cubemap_frusta) in views.iter_mut() { @@ -1217,7 +1217,7 @@ pub fn update_point_light_frusta( // ignore scale because we don't want to effectively scale light radius and range // by applying those as a view transform to shadow map rendering of objects // and ignore rotation because we want the shadow map projections to align with the axes - let view_translation = GlobalTransform::from_translation(transform.translation); + let view_translation = Transform::from_translation(transform.translation); let view_backward = transform.back(); for (view_rotation, frustum) in view_rotations.iter().zip(cubemap_frusta.iter_mut()) { diff --git a/crates/bevy_pbr/src/render/light.rs b/crates/bevy_pbr/src/render/light.rs index bf50d1a8e5fc3..9c0f19747c5e2 100644 --- a/crates/bevy_pbr/src/render/light.rs +++ b/crates/bevy_pbr/src/render/light.rs @@ -28,7 +28,7 @@ use bevy_render::{ ExtractedView, ViewUniform, ViewUniformOffset, ViewUniforms, Visibility, VisibleEntities, }, }; -use bevy_transform::components::GlobalTransform; +use bevy_transform::components::{GlobalTransform, Transform}; use bevy_utils::FloatOrd; use bevy_utils::{ tracing::{error, warn}, @@ -668,7 +668,7 @@ pub fn prepare_lights( Mat4::perspective_infinite_reverse_rh(std::f32::consts::FRAC_PI_2, 1.0, POINT_LIGHT_NEAR_Z); let cube_face_rotations = CUBE_MAP_FACES .iter() - .map(|CubeMapFace { target, up }| GlobalTransform::identity().looking_at(*target, *up)) + .map(|CubeMapFace { target, up }| Transform::identity().looking_at(*target, *up)) .collect::>(); global_light_meta.entity_to_index.clear(); @@ -807,7 +807,7 @@ pub fn prepare_lights( // ignore scale because we don't want to effectively scale light radius and range // by applying those as a view transform to shadow map rendering of objects // and ignore rotation because we want the shadow map projections to align with the axes - let view_translation = GlobalTransform::from_translation(light.transform.translation); + let view_translation = Transform::from_translation(light.transform.translation); for (face_index, view_rotation) in cube_face_rotations.iter().enumerate() { let depth_texture_view = @@ -921,7 +921,7 @@ pub fn prepare_lights( ExtractedView { width: directional_light_shadow_map.size as u32, height: directional_light_shadow_map.size as u32, - transform: GlobalTransform::from_matrix(view.inverse()), + transform: Transform::from_matrix(view.inverse()), projection, }, RenderPhase::::default(), diff --git a/crates/bevy_render/src/camera/camera.rs b/crates/bevy_render/src/camera/camera.rs index 06f8ebf818f7e..15950348c6911 100644 --- a/crates/bevy_render/src/camera/camera.rs +++ b/crates/bevy_render/src/camera/camera.rs @@ -428,7 +428,7 @@ pub fn extract_cameras( }, ExtractedView { projection: camera.projection_matrix(), - transform: *transform, + transform: transform.into_inner(), width: viewport_size.x, height: viewport_size.y, }, diff --git a/crates/bevy_render/src/view/mod.rs b/crates/bevy_render/src/view/mod.rs index 2638e94d4581a..e6df521bb561e 100644 --- a/crates/bevy_render/src/view/mod.rs +++ b/crates/bevy_render/src/view/mod.rs @@ -21,7 +21,7 @@ use crate::{ use bevy_app::{App, Plugin}; use bevy_ecs::prelude::*; use bevy_math::{Mat4, Vec3}; -use bevy_transform::components::GlobalTransform; +use bevy_transform::components::Transform; use bevy_utils::HashMap; pub struct ViewPlugin; @@ -76,7 +76,7 @@ impl Default for Msaa { #[derive(Component)] pub struct ExtractedView { pub projection: Mat4, - pub transform: GlobalTransform, + pub transform: Transform, pub width: u32, pub height: u32, } diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index d0bebbad3609a..6335f4a80c974 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -108,7 +108,7 @@ pub fn extract_text2d_sprite( alignment_offset * scale_factor + text_glyph.position.extend(0.), ); - let transform = text_transform.mul_transform(glyph_transform); + let transform = text_transform * glyph_transform; extracted_sprites.sprites.push(ExtractedSprite { transform, diff --git a/crates/bevy_transform/src/components/global_transform.rs b/crates/bevy_transform/src/components/global_transform.rs index be1d75cfcb46b..98a1c83cae5bf 100644 --- a/crates/bevy_transform/src/components/global_transform.rs +++ b/crates/bevy_transform/src/components/global_transform.rs @@ -1,8 +1,8 @@ use super::Transform; use bevy_ecs::{component::Component, reflect::ReflectComponent}; -use bevy_math::{const_vec3, Affine3A, Mat3, Mat4, Quat, Vec3}; +use bevy_math::Vec3; use bevy_reflect::prelude::*; -use std::ops::Mul; +use std::ops::{Deref, DerefMut, Mul}; /// Describe the position of an entity relative to the reference frame. /// @@ -24,249 +24,43 @@ use std::ops::Mul; /// This system runs in stage [`CoreStage::PostUpdate`](crate::CoreStage::PostUpdate). If you /// update the[`Transform`] of an entity in this stage or after, you will notice a 1 frame lag /// before the [`GlobalTransform`] is updated. -#[derive(Component, Debug, PartialEq, Clone, Copy, Reflect)] +#[derive(Component, Default, Debug, PartialEq, Clone, Copy, Reflect)] #[reflect(Component, Default, PartialEq)] -pub struct GlobalTransform { - /// The position of the global transform - pub translation: Vec3, - /// The rotation of the global transform - pub rotation: Quat, - /// The scale of the global transform - pub scale: Vec3, -} +pub struct GlobalTransform(pub(crate) Transform); impl GlobalTransform { - #[doc(hidden)] - #[inline] - pub const fn from_xyz(x: f32, y: f32, z: f32) -> Self { - Self::from_translation(const_vec3!([x, y, z])) - } - - /// Creates a new identity [`GlobalTransform`], with no translation, rotation, and a scale of 1 - /// on all axes. - #[inline] - pub const fn identity() -> Self { - GlobalTransform { - translation: Vec3::ZERO, - rotation: Quat::IDENTITY, - scale: Vec3::ONE, - } - } - - #[doc(hidden)] - #[inline] - pub fn from_matrix(matrix: Mat4) -> Self { - let (scale, rotation, translation) = matrix.to_scale_rotation_translation(); - - GlobalTransform { - translation, - rotation, - scale, - } - } - - #[doc(hidden)] - #[inline] - pub const fn from_translation(translation: Vec3) -> Self { - GlobalTransform { - translation, - ..Self::identity() - } - } - - #[doc(hidden)] - #[inline] - pub const fn from_rotation(rotation: Quat) -> Self { - GlobalTransform { - rotation, - ..Self::identity() - } - } - - #[doc(hidden)] - #[inline] - pub const fn from_scale(scale: Vec3) -> Self { - GlobalTransform { - scale, - ..Self::identity() - } - } - - #[doc(hidden)] - #[inline] - #[must_use] - pub fn looking_at(mut self, target: Vec3, up: Vec3) -> Self { - self.look_at(target, up); - self - } - - #[doc(hidden)] - #[inline] - #[must_use] - pub const fn with_translation(mut self, translation: Vec3) -> Self { - self.translation = translation; - self - } - - #[doc(hidden)] - #[inline] - #[must_use] - pub const fn with_rotation(mut self, rotation: Quat) -> Self { - self.rotation = rotation; - self - } - - #[doc(hidden)] - #[inline] - #[must_use] - pub const fn with_scale(mut self, scale: Vec3) -> Self { - self.scale = scale; - self - } - - /// Returns the 3d affine transformation matrix from this transforms translation, - /// rotation, and scale. - #[inline] - pub fn compute_matrix(&self) -> Mat4 { - Mat4::from_scale_rotation_translation(self.scale, self.rotation, self.translation) - } - - /// Returns the 3d affine transformation from this transforms translation, - /// rotation, and scale. - #[inline] - pub fn compute_affine(&self) -> Affine3A { - Affine3A::from_scale_rotation_translation(self.scale, self.rotation, self.translation) - } - - /// Get the unit vector in the local x direction - #[inline] - pub fn local_x(&self) -> Vec3 { - self.rotation * Vec3::X - } - - /// Equivalent to [`-local_x()`][GlobalTransform::local_x] - #[inline] - pub fn left(&self) -> Vec3 { - -self.local_x() - } - - /// Equivalent to [`local_x()`][GlobalTransform::local_x] - #[inline] - pub fn right(&self) -> Vec3 { - self.local_x() - } - - /// Get the unit vector in the local y direction - #[inline] - pub fn local_y(&self) -> Vec3 { - self.rotation * Vec3::Y - } - - /// Equivalent to [`local_y()`][GlobalTransform::local_y] - #[inline] - pub fn up(&self) -> Vec3 { - self.local_y() - } - - /// Equivalent to [`-local_y()`][GlobalTransform::local_y] - #[inline] - pub fn down(&self) -> Vec3 { - -self.local_y() - } - - /// Get the unit vector in the local z direction - #[inline] - pub fn local_z(&self) -> Vec3 { - self.rotation * Vec3::Z - } - - /// Equivalent to [`-local_z()`][GlobalTransform::local_z] - #[inline] - pub fn forward(&self) -> Vec3 { - -self.local_z() - } - - /// Equivalent to [`local_z()`][GlobalTransform::local_z] - #[inline] - pub fn back(&self) -> Vec3 { - self.local_z() - } - - #[doc(hidden)] - #[inline] - pub fn rotate(&mut self, rotation: Quat) { - self.rotation = rotation * self.rotation; - } - - #[doc(hidden)] - #[inline] - pub fn rotate_around(&mut self, point: Vec3, rotation: Quat) { - self.translation = point + rotation * (self.translation - point); - self.rotation *= rotation; - } - - /// Multiplies `self` with `transform` component by component, returning the - /// resulting [`GlobalTransform`] - #[inline] - #[must_use] - 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; - Self { - translation, - rotation, - scale, - } - } - - /// Returns a [`Vec3`] of this [`Transform`] applied to `value`. - #[inline] - pub fn mul_vec3(&self, mut value: Vec3) -> Vec3 { - value = self.scale * value; - value = self.rotation * value; - value += self.translation; - value + /// Retrieves the [`Transform`] inner type of `Self` + pub fn into_inner(self) -> Transform { + self.0 } +} - #[doc(hidden)] - #[inline] - pub fn apply_non_uniform_scale(&mut self, scale: Vec3) { - self.scale *= scale; - } +impl Deref for GlobalTransform { + type Target = Transform; - #[doc(hidden)] - #[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_mat3(&Mat3::from_cols(right, up, forward)); + fn deref(&self) -> &Self::Target { + &self.0 } } -impl Default for GlobalTransform { - fn default() -> Self { - Self::identity() +impl DerefMut for GlobalTransform { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 } } impl From for GlobalTransform { - fn from(transform: Transform) -> Self { - Self { - translation: transform.translation, - rotation: transform.rotation, - scale: transform.scale, - } + fn from(t: Transform) -> Self { + Self(t) } } impl Mul for GlobalTransform { - type Output = GlobalTransform; + type Output = Self; #[inline] fn mul(self, global_transform: GlobalTransform) -> Self::Output { - self.mul_transform(global_transform.into()) + Self(self.mul_transform(global_transform.0)) } } @@ -275,7 +69,7 @@ impl Mul for GlobalTransform { #[inline] fn mul(self, transform: Transform) -> Self::Output { - self.mul_transform(transform) + Self(self.mul_transform(transform)) } } diff --git a/crates/bevy_transform/src/components/transform.rs b/crates/bevy_transform/src/components/transform.rs index bbca98e76961d..88bcbef1d0944 100644 --- a/crates/bevy_transform/src/components/transform.rs +++ b/crates/bevy_transform/src/components/transform.rs @@ -1,6 +1,6 @@ use super::GlobalTransform; use bevy_ecs::{component::Component, reflect::ReflectComponent}; -use bevy_math::{const_vec3, Mat3, Mat4, Quat, Vec3}; +use bevy_math::{const_vec3, Affine3A, Mat3, Mat4, Quat, Vec3}; use bevy_reflect::prelude::*; use bevy_reflect::Reflect; use std::ops::Mul; @@ -141,6 +141,13 @@ impl Transform { Mat4::from_scale_rotation_translation(self.scale, self.rotation, self.translation) } + /// Returns the 3d affine transformation from this transforms translation, + /// rotation, and scale. + #[inline] + pub fn compute_affine(&self) -> Affine3A { + Affine3A::from_scale_rotation_translation(self.scale, self.rotation, self.translation) + } + /// Get the unit vector in the local x direction. #[inline] pub fn local_x(&self) -> Vec3 { @@ -259,11 +266,7 @@ impl Default for Transform { impl From for Transform { fn from(transform: GlobalTransform) -> Self { - Self { - translation: transform.translation, - rotation: transform.rotation, - scale: transform.scale, - } + transform.into_inner() } } diff --git a/crates/bevy_transform/src/lib.rs b/crates/bevy_transform/src/lib.rs index a88017c7d7d60..db67ee4956a70 100644 --- a/crates/bevy_transform/src/lib.rs +++ b/crates/bevy_transform/src/lib.rs @@ -66,7 +66,7 @@ impl TransformBundle { pub const fn identity() -> Self { TransformBundle { local: Transform::identity(), - global: GlobalTransform::identity(), + global: GlobalTransform(Transform::identity()), } } } diff --git a/crates/bevy_transform/src/systems.rs b/crates/bevy_transform/src/systems.rs index 672e67b513523..2d7fd53090995 100644 --- a/crates/bevy_transform/src/systems.rs +++ b/crates/bevy_transform/src/systems.rs @@ -72,7 +72,7 @@ fn propagate_recursive( ); changed |= transform_changed; if changed { - *global_transform = parent.mul_transform(*transform); + global_transform.0 = parent.mul_transform(*transform); } *global_transform }; @@ -143,12 +143,14 @@ mod test { assert_eq!( *world.get::(children[0]).unwrap(), - GlobalTransform::from_xyz(1.0, 0.0, 0.0) * Transform::from_xyz(0.0, 2.0, 0.0) + GlobalTransform::from(Transform::from_xyz(1.0, 0.0, 0.0)) + * Transform::from_xyz(0.0, 2.0, 0.0) ); assert_eq!( *world.get::(children[1]).unwrap(), - GlobalTransform::from_xyz(1.0, 0.0, 0.0) * Transform::from_xyz(0.0, 0.0, 3.0) + GlobalTransform::from(Transform::from_xyz(1.0, 0.0, 0.0)) + * Transform::from_xyz(0.0, 0.0, 3.0) ); } @@ -186,12 +188,14 @@ mod test { assert_eq!( *world.get::(children[0]).unwrap(), - GlobalTransform::from_xyz(1.0, 0.0, 0.0) * Transform::from_xyz(0.0, 2.0, 0.0) + GlobalTransform(Transform::from_xyz(1.0, 0.0, 0.0)) + * Transform::from_xyz(0.0, 2.0, 0.0) ); assert_eq!( *world.get::(children[1]).unwrap(), - GlobalTransform::from_xyz(1.0, 0.0, 0.0) * Transform::from_xyz(0.0, 0.0, 3.0) + GlobalTransform(Transform::from_xyz(1.0, 0.0, 0.0)) + * Transform::from_xyz(0.0, 0.0, 3.0) ); } @@ -328,8 +332,8 @@ mod test { let mut state = app.world.query::<&GlobalTransform>(); for global in state.iter(&app.world) { assert_eq!( - global, - &GlobalTransform { + global.into_inner(), + Transform { translation, ..Default::default() }, diff --git a/crates/bevy_ui/src/render/mod.rs b/crates/bevy_ui/src/render/mod.rs index 598dd10eaa866..f84fc67ac0024 100644 --- a/crates/bevy_ui/src/render/mod.rs +++ b/crates/bevy_ui/src/render/mod.rs @@ -25,7 +25,7 @@ use bevy_render::{ }; use bevy_sprite::{Rect, SpriteAssetEvents, TextureAtlas}; use bevy_text::{DefaultTextPipeline, Text}; -use bevy_transform::components::GlobalTransform; +use bevy_transform::components::{GlobalTransform, Transform}; use bevy_utils::FloatOrd; use bevy_utils::HashMap; use bevy_window::{WindowId, Windows}; @@ -254,7 +254,7 @@ pub fn extract_default_ui_camera_view( .get_or_spawn(default_camera_view) .insert(ExtractedView { projection: projection.get_projection_matrix(), - transform: GlobalTransform::from_xyz( + transform: Transform::from_xyz( 0.0, 0.0, UI_CAMERA_FAR + UI_CAMERA_TRANSFORM_OFFSET, diff --git a/crates/bevy_utils/src/float_ord.rs b/crates/bevy_utils/src/float_ord.rs index 45832198ad4f1..c029bf3caf3b2 100644 --- a/crates/bevy_utils/src/float_ord.rs +++ b/crates/bevy_utils/src/float_ord.rs @@ -13,15 +13,13 @@ pub struct FloatOrd(pub f32); #[allow(clippy::derive_ord_xor_partial_ord)] impl Ord for FloatOrd { fn cmp(&self, other: &Self) -> Ordering { - self.0.partial_cmp(&other.0).unwrap_or_else(|| { - if self.0.is_nan() && !other.0.is_nan() { - Ordering::Less - } else if !self.0.is_nan() && other.0.is_nan() { - Ordering::Greater - } else { - Ordering::Equal - } - }) + self.0 + .partial_cmp(&other.0) + .unwrap_or_else(|| match (self.0.is_nan(), other.0.is_nan()) { + (true, false) => Ordering::Less, + (false, true) => Ordering::Greater, + (_, _) => Ordering::Equal, + }) } } diff --git a/examples/animation/custom_skinned_mesh.rs b/examples/animation/custom_skinned_mesh.rs index cfdf6ebd3f5e8..2580a63331e7e 100644 --- a/examples/animation/custom_skinned_mesh.rs +++ b/examples/animation/custom_skinned_mesh.rs @@ -123,14 +123,14 @@ fn setup( let joint_0 = commands .spawn_bundle(( Transform::from_xyz(i as f32 * 1.5, 0.0, 0.0), - GlobalTransform::identity(), + GlobalTransform::default(), )) .id(); let joint_1 = commands .spawn_bundle(( AnimatedJoint, Transform::identity(), - GlobalTransform::identity(), + GlobalTransform::default(), )) .id(); From 3da2d7e0a4c4aa9f1012184a81411761f18b693b Mon Sep 17 00:00:00 2001 From: Felix de Maneville Date: Tue, 28 Jun 2022 15:43:39 +0200 Subject: [PATCH 2/6] sprite extraction --- crates/bevy_sprite/src/render/mod.rs | 8 ++++---- crates/bevy_text/src/text2d.rs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/bevy_sprite/src/render/mod.rs b/crates/bevy_sprite/src/render/mod.rs index c0066482014f7..3a84e6fc82ffb 100644 --- a/crates/bevy_sprite/src/render/mod.rs +++ b/crates/bevy_sprite/src/render/mod.rs @@ -25,7 +25,7 @@ use bevy_render::{ view::{Msaa, ViewUniform, ViewUniformOffset, ViewUniforms, Visibility}, RenderWorld, }; -use bevy_transform::components::GlobalTransform; +use bevy_transform::components::{GlobalTransform, Transform}; use bevy_utils::FloatOrd; use bevy_utils::HashMap; use bytemuck::{Pod, Zeroable}; @@ -173,7 +173,7 @@ impl SpecializedRenderPipeline for SpritePipeline { #[derive(Component, Clone, Copy)] pub struct ExtractedSprite { - pub transform: GlobalTransform, + pub transform: Transform, pub color: Color, /// Select an area of the texture pub rect: Option, @@ -241,7 +241,7 @@ pub fn extract_sprites( // PERF: we don't check in this function that the `Image` asset is ready, since it should be in most cases and hashing the handle is expensive extracted_sprites.sprites.alloc().init(ExtractedSprite { color: sprite.color, - transform: *transform, + transform: transform.into_inner(), // Use the full texture rect: None, // Pass the custom size @@ -260,7 +260,7 @@ pub fn extract_sprites( let rect = Some(texture_atlas.textures[atlas_sprite.index as usize]); extracted_sprites.sprites.alloc().init(ExtractedSprite { color: atlas_sprite.color, - transform: *transform, + transform: transform.into_inner(), // Select the area in the texture atlas rect, // Pass the custom size diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index 6335f4a80c974..d0bebbad3609a 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -108,7 +108,7 @@ pub fn extract_text2d_sprite( alignment_offset * scale_factor + text_glyph.position.extend(0.), ); - let transform = text_transform * glyph_transform; + let transform = text_transform.mul_transform(glyph_transform); extracted_sprites.sprites.push(ExtractedSprite { transform, From bb9e4d38dd61261cd7dd9aea1516f38665fa9f15 Mon Sep 17 00:00:00 2001 From: Felix de Maneville Date: Tue, 28 Jun 2022 15:45:05 +0200 Subject: [PATCH 3/6] light extraction --- crates/bevy_pbr/src/render/light.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_pbr/src/render/light.rs b/crates/bevy_pbr/src/render/light.rs index 9c0f19747c5e2..ab8f64fa86f83 100644 --- a/crates/bevy_pbr/src/render/light.rs +++ b/crates/bevy_pbr/src/render/light.rs @@ -52,7 +52,7 @@ pub struct ExtractedPointLight { intensity: f32, range: f32, radius: f32, - transform: GlobalTransform, + transform: Transform, shadows_enabled: bool, shadow_depth_bias: f32, shadow_normal_bias: f32, @@ -446,7 +446,7 @@ pub fn extract_lights( intensity: point_light.intensity / (4.0 * std::f32::consts::PI), range: point_light.range, radius: point_light.radius, - transform: *transform, + transform: transform.into_inner(), shadows_enabled: point_light.shadows_enabled, shadow_depth_bias: point_light.shadow_depth_bias, // The factor of SQRT_2 is for the worst-case diagonal offset From 422ec85b218879ab0c79de3c4bf59c205c454907 Mon Sep 17 00:00:00 2001 From: Felix de Maneville Date: Tue, 28 Jun 2022 15:57:38 +0200 Subject: [PATCH 4/6] Dropped derefmut --- crates/bevy_text/src/text2d.rs | 2 +- crates/bevy_transform/src/components/global_transform.rs | 8 +------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/crates/bevy_text/src/text2d.rs b/crates/bevy_text/src/text2d.rs index d0bebbad3609a..938012ca776cd 100644 --- a/crates/bevy_text/src/text2d.rs +++ b/crates/bevy_text/src/text2d.rs @@ -89,7 +89,7 @@ pub fn extract_text2d_sprite( HorizontalAlign::Right => Vec3::new(-width, 0.0, 0.0), }; - let mut text_transform = *transform; + let mut text_transform = transform.into_inner(); text_transform.scale /= scale_factor; for text_glyph in text_glyphs { diff --git a/crates/bevy_transform/src/components/global_transform.rs b/crates/bevy_transform/src/components/global_transform.rs index 98a1c83cae5bf..5e2de38a225d2 100644 --- a/crates/bevy_transform/src/components/global_transform.rs +++ b/crates/bevy_transform/src/components/global_transform.rs @@ -2,7 +2,7 @@ use super::Transform; use bevy_ecs::{component::Component, reflect::ReflectComponent}; use bevy_math::Vec3; use bevy_reflect::prelude::*; -use std::ops::{Deref, DerefMut, Mul}; +use std::ops::{Deref, Mul}; /// Describe the position of an entity relative to the reference frame. /// @@ -43,12 +43,6 @@ impl Deref for GlobalTransform { } } -impl DerefMut for GlobalTransform { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 - } -} - impl From for GlobalTransform { fn from(t: Transform) -> Self { Self(t) From c1c7b47ad131e3928ce85885565c2cdae76914eb Mon Sep 17 00:00:00 2001 From: Felix de Maneville Date: Tue, 28 Jun 2022 16:00:30 +0200 Subject: [PATCH 5/6] Dropped deprecated example --- Cargo.toml | 11 - .../transforms/global_vs_local_translation.rs | 191 ------------------ 2 files changed, 202 deletions(-) delete mode 100644 examples/transforms/global_vs_local_translation.rs diff --git a/Cargo.toml b/Cargo.toml index 69ec5086a5349..6393005df083b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1277,17 +1277,6 @@ description = "A simple way to view glTF models with Bevy. Just run `cargo run - category = "Tools" wasm = true -# Transforms -[[example]] -name = "global_vs_local_translation" -path = "examples/transforms/global_vs_local_translation.rs" - -[package.metadata.example.global_vs_local_translation] -name = "Global / Local Translation" -description = "Illustrates the difference between direction of a translation in respect to local object or global object Transform" -category = "Transforms" -wasm = true - [[example]] name = "3d_rotation" path = "examples/transforms/3d_rotation.rs" diff --git a/examples/transforms/global_vs_local_translation.rs b/examples/transforms/global_vs_local_translation.rs deleted file mode 100644 index 1ca662c679f04..0000000000000 --- a/examples/transforms/global_vs_local_translation.rs +++ /dev/null @@ -1,191 +0,0 @@ -//! Illustrates the difference between direction of a translation in respect to local object or -//! global object Transform. - -use bevy::prelude::*; - -// Define a marker for entities that should be changed via their global transform. -#[derive(Component)] -struct ChangeGlobal; - -// Define a marker for entities that should be changed via their local transform. -#[derive(Component)] -struct ChangeLocal; - -// Define a marker for entities that should move. -#[derive(Component)] -struct Move; - -// Define a resource for the current movement direction; -#[derive(Default)] -struct Direction(Vec3); - -// Define component to decide when an entity should be ignored by the movement systems. -#[derive(Component)] -struct ToggledBy(KeyCode); - -fn main() { - App::new() - .add_plugins(DefaultPlugins) - .add_startup_system(setup) - .init_resource::() - .add_system(move_cubes_according_to_global_transform) - .add_system(move_cubes_according_to_local_transform) - .add_system(update_directional_input) - .add_system(toggle_movement) - .run(); -} - -// Startup system to setup the scene and spawn all relevant entities. -fn setup( - mut commands: Commands, - mut meshes: ResMut>, - mut materials: ResMut>, - asset_server: Res, -) { - // To show the difference between a local transform (rotation, scale and position in respect to a given entity) - // and global transform (rotation, scale and position in respect to the base coordinate system of the visible scene) - // it's helpful to add multiple entities that are attached to each other. - // This way we'll see that the transform in respect to an entity's parent is different to the - // global transform within the visible scene. - // This example focuses on translation only to clearly demonstrate the differences. - - // Spawn a basic cube to have an entity as reference. - let mut main_entity = commands.spawn(); - main_entity - .insert_bundle(PbrBundle { - mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), - material: materials.add(Color::YELLOW.into()), - ..default() - }) - .insert(ChangeGlobal) - .insert(Move) - .insert(ToggledBy(KeyCode::Key1)); - - // Spawn two entities as children above the original main entity. - // The red entity spawned here will be changed via its global transform - // where the green one will be changed via its local transform. - main_entity.with_children(|child_builder| { - // also see parenting example - child_builder - .spawn_bundle(PbrBundle { - mesh: meshes.add(Mesh::from(shape::Cube { size: 0.5 })), - material: materials.add(Color::RED.into()), - transform: Transform::from_translation(Vec3::Y - Vec3::Z), - ..default() - }) - .insert(ChangeGlobal) - .insert(Move) - .insert(ToggledBy(KeyCode::Key2)); - child_builder - .spawn_bundle(PbrBundle { - mesh: meshes.add(Mesh::from(shape::Cube { size: 0.5 })), - material: materials.add(Color::GREEN.into()), - transform: Transform::from_translation(Vec3::Y + Vec3::Z), - ..default() - }) - .insert(ChangeLocal) - .insert(Move) - .insert(ToggledBy(KeyCode::Key3)); - }); - - // Spawn a camera looking at the entities to show what's happening in this example. - commands.spawn_bundle(Camera3dBundle { - transform: Transform::from_xyz(0.0, 10.0, 20.0).looking_at(Vec3::ZERO, Vec3::Y), - ..default() - }); - - // Add a light source for better 3d visibility. - commands.spawn_bundle(PointLightBundle { - transform: Transform::from_translation(Vec3::splat(3.0)), - ..default() - }); - - // Add text to explain inputs and what is happening. - commands.spawn_bundle(TextBundle { - text: Text::with_section( - "Press the arrow keys to move the cubes. Toggle movement for yellow (1), red (2) and green (3) cubes via number keys. - -Notice how the green cube will translate further in respect to the yellow in contrast to the red cube. -This is due to the use of its LocalTransform that is relative to the yellow cubes transform instead of the GlobalTransform as in the case of the red cube. -The red cube is moved through its GlobalTransform and thus is unaffected by the yellows transform.", - TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), - font_size: 22.0, - color: Color::WHITE, - }, - TextAlignment { - horizontal: HorizontalAlign::Left, - ..default() - }, - ), - ..default() - }); -} - -// This system will move all cubes that are marked as ChangeGlobal according to their global transform. -fn move_cubes_according_to_global_transform( - mut cubes: Query<&mut GlobalTransform, (With, With)>, - direction: Res, - timer: Res