From ad166c85615293a450877391b86a05a999f01956 Mon Sep 17 00:00:00 2001 From: devil-ira Date: Sat, 16 Jul 2022 17:58:32 +0200 Subject: [PATCH 1/4] Add constants. Deprecate `identity()`. --- .../src/components/global_transform.rs | 8 +++++-- .../src/components/transform.rs | 22 +++++++++++-------- crates/bevy_transform/src/lib.rs | 15 ++++++++----- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/crates/bevy_transform/src/components/global_transform.rs b/crates/bevy_transform/src/components/global_transform.rs index f1129f8681116..033333a69bba7 100644 --- a/crates/bevy_transform/src/components/global_transform.rs +++ b/crates/bevy_transform/src/components/global_transform.rs @@ -46,6 +46,9 @@ macro_rules! impl_local_axis { } impl GlobalTransform { + /// An identity [`GlobalTransform`] that maps all points in space to themselves. + pub const IDENTITY: Self = Self(Affine3A::IDENTITY); + #[doc(hidden)] #[inline] pub fn from_xyz(x: f32, y: f32, z: f32) -> Self { @@ -107,8 +110,9 @@ impl GlobalTransform { /// Creates a new identity [`GlobalTransform`], that maps all points in space to themselves. #[inline] + #[deprecated = "Use `GlobalTransform::IDENTITY` instead."] pub const fn identity() -> Self { - Self(Affine3A::IDENTITY) + GlobalTransform::IDENTITY } impl_local_axis!(right, left, X); @@ -154,7 +158,7 @@ impl GlobalTransform { impl Default for GlobalTransform { fn default() -> Self { - Self::identity() + Self::IDENTITY } } diff --git a/crates/bevy_transform/src/components/transform.rs b/crates/bevy_transform/src/components/transform.rs index e22a7df0cd173..d0006af9ed691 100644 --- a/crates/bevy_transform/src/components/transform.rs +++ b/crates/bevy_transform/src/components/transform.rs @@ -38,6 +38,13 @@ pub struct Transform { } impl Transform { + /// An identity [`Transform`] with no translation, rotation, and a scale of 1 on all axes. + pub const IDENTITY: Self = Transform { + translation: Vec3::ZERO, + rotation: Quat::IDENTITY, + scale: Vec3::ONE, + }; + /// Creates a new [`Transform`] at the position `(x, y, z)`. In 2d, the `z` component /// is used for z-ordering elements: higher `z`-value will be in front of lower /// `z`-value. @@ -49,12 +56,9 @@ impl Transform { /// Creates a new identity [`Transform`], with no translation, rotation, and a scale of 1 on /// all axes. #[inline] + #[deprecated = "Use `Transform::IDENTITY` instead."] pub const fn identity() -> Self { - Transform { - translation: Vec3::ZERO, - rotation: Quat::IDENTITY, - scale: Vec3::ONE, - } + Transform::IDENTITY } /// Extracts the translation, rotation, and scale from `matrix`. It must be a 3d affine @@ -76,7 +80,7 @@ impl Transform { pub const fn from_translation(translation: Vec3) -> Self { Transform { translation, - ..Self::identity() + ..Self::IDENTITY } } @@ -86,7 +90,7 @@ impl Transform { pub const fn from_rotation(rotation: Quat) -> Self { Transform { rotation, - ..Self::identity() + ..Self::IDENTITY } } @@ -96,7 +100,7 @@ impl Transform { pub const fn from_scale(scale: Vec3) -> Self { Transform { scale, - ..Self::identity() + ..Self::IDENTITY } } @@ -335,7 +339,7 @@ impl Transform { impl Default for Transform { fn default() -> Self { - Self::identity() + Self::IDENTITY } } diff --git a/crates/bevy_transform/src/lib.rs b/crates/bevy_transform/src/lib.rs index 03484997e34e3..a0f89743eacf9 100644 --- a/crates/bevy_transform/src/lib.rs +++ b/crates/bevy_transform/src/lib.rs @@ -46,6 +46,12 @@ pub struct TransformBundle { } impl TransformBundle { + /// An identity [`TransformBundle`] with no translation, rotation, and a scale of 1 on all axes. + pub const IDENTITY: Self = Self { + local: Transform::IDENTITY, + global: GlobalTransform::IDENTITY, + }; + /// Creates a new [`TransformBundle`] from a [`Transform`]. /// /// This initializes [`GlobalTransform`] as identity, to be updated later by the @@ -54,19 +60,16 @@ impl TransformBundle { pub const fn from_transform(transform: Transform) -> Self { TransformBundle { local: transform, - // Note: `..Default::default()` cannot be used here, because it isn't const - ..Self::identity() + ..Self::IDENTITY } } /// Creates a new identity [`TransformBundle`], with no translation, rotation, and a scale of 1 /// on all axes. #[inline] + #[deprecated = "Use `TransformBundle::IDENTITY` instead."] pub const fn identity() -> Self { - TransformBundle { - local: Transform::identity(), - global: GlobalTransform::identity(), - } + TransformBundle::IDENTITY } } From 2300fa55b23d400cac4b10287684f13f1012a410 Mon Sep 17 00:00:00 2001 From: devil-ira Date: Sat, 16 Jul 2022 18:00:42 +0200 Subject: [PATCH 2/4] Use constant. --- crates/bevy_gltf/src/loader.rs | 4 +-- crates/bevy_pbr/src/light.rs | 2 +- crates/bevy_pbr/src/render/light.rs | 2 +- crates/bevy_render/src/spatial_bundle.rs | 29 ++++++++++--------- crates/bevy_render/src/view/visibility/mod.rs | 23 +++++++-------- .../src/components/global_transform.rs | 7 ----- .../src/components/transform.rs | 8 ----- crates/bevy_transform/src/systems.rs | 14 ++++----- crates/bevy_ui/src/update.rs | 2 +- examples/animation/custom_skinned_mesh.rs | 6 ++-- examples/ecs/component_change_detection.rs | 2 +- examples/scene/scene.rs | 2 +- 12 files changed, 43 insertions(+), 58 deletions(-) diff --git a/crates/bevy_gltf/src/loader.rs b/crates/bevy_gltf/src/loader.rs index 19a626a9f7de0..2db5126b7743f 100644 --- a/crates/bevy_gltf/src/loader.rs +++ b/crates/bevy_gltf/src/loader.rs @@ -466,7 +466,7 @@ async fn load_gltf<'a, 'b>( world .spawn() - .insert_bundle(SpatialBundle::visible_identity()) + .insert_bundle(SpatialBundle::VISIBLE_IDENTITY) .with_children(|parent| { for node in scene.nodes() { let result = load_node( @@ -1169,7 +1169,7 @@ mod test { GltfNode { children: vec![], mesh: None, - transform: bevy_transform::prelude::Transform::identity(), + transform: bevy_transform::prelude::Transform::IDENTITY, } } } diff --git a/crates/bevy_pbr/src/light.rs b/crates/bevy_pbr/src/light.rs index 87d2a2ecb4991..c9d799bb1011b 100644 --- a/crates/bevy_pbr/src/light.rs +++ b/crates/bevy_pbr/src/light.rs @@ -1450,7 +1450,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 }| Transform::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 &mut views { diff --git a/crates/bevy_pbr/src/render/light.rs b/crates/bevy_pbr/src/render/light.rs index c9c13d69bf310..6b79102d3b844 100644 --- a/crates/bevy_pbr/src/render/light.rs +++ b/crates/bevy_pbr/src/render/light.rs @@ -781,7 +781,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 }| Transform::identity().looking_at(*target, *up)) + .map(|CubeMapFace { target, up }| Transform::IDENTITY.looking_at(*target, *up)) .collect::>(); global_light_meta.entity_to_index.clear(); diff --git a/crates/bevy_render/src/spatial_bundle.rs b/crates/bevy_render/src/spatial_bundle.rs index 152a14ebf2ae5..1ac2c69cc0c83 100644 --- a/crates/bevy_render/src/spatial_bundle.rs +++ b/crates/bevy_render/src/spatial_bundle.rs @@ -33,22 +33,25 @@ impl SpatialBundle { pub const fn from_transform(transform: Transform) -> Self { SpatialBundle { transform, - // Note: `..Default::default()` cannot be used here, because it isn't const - ..Self::visible_identity() + ..Self::VISIBLE_IDENTITY } } - /// Creates a new identity [`SpatialBundle`], with no translation, rotation, and a scale of 1 - /// on all axes. - #[inline] - pub const fn visible_identity() -> Self { - SpatialBundle { - transform: Transform::identity(), - global_transform: GlobalTransform::identity(), - visibility: Visibility::visible(), - computed: ComputedVisibility::not_visible(), - } - } + /// A visible [`SpatialBundle`], with no translation, rotation, and a scale of 1 on all axes. + pub const VISIBLE_IDENTITY: Self = SpatialBundle { + visibility: Visibility::VISIBLE, + computed: ComputedVisibility::NOT_VISIBLE, + transform: Transform::IDENTITY, + global_transform: GlobalTransform::IDENTITY, + }; + + /// An invisible [`SpatialBundle`], with no translation, rotation, and a scale of 1 on all axes. + pub const INVISIBLE_IDENTITY: Self = SpatialBundle { + visibility: Visibility::INVISIBLE, + computed: ComputedVisibility::NOT_VISIBLE, + transform: Transform::IDENTITY, + global_transform: GlobalTransform::IDENTITY, + }; } impl From for SpatialBundle { diff --git a/crates/bevy_render/src/view/visibility/mod.rs b/crates/bevy_render/src/view/visibility/mod.rs index bf7b110b1277e..827977197f647 100644 --- a/crates/bevy_render/src/view/visibility/mod.rs +++ b/crates/bevy_render/src/view/visibility/mod.rs @@ -37,15 +37,16 @@ pub struct Visibility { impl Default for Visibility { fn default() -> Self { - Self::visible() + Visibility::VISIBLE } } impl Visibility { - /// Creates a new [`Visibility`], set as visible - pub const fn visible() -> Self { - Self { is_visible: true } - } + /// A [`Visibility`], set as visible. + pub const VISIBLE: Self = Visibility { is_visible: true }; + + /// A [`Visibility`], set as invisible. + pub const INVISIBLE: Self = Visibility { is_visible: false }; } /// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering @@ -58,18 +59,16 @@ pub struct ComputedVisibility { impl Default for ComputedVisibility { fn default() -> Self { - Self::not_visible() + Self::NOT_VISIBLE } } impl ComputedVisibility { /// Creates a new [`ComputedVisibility`], set as not visible - pub const fn not_visible() -> Self { - Self { - is_visible_in_hierarchy: false, - is_visible_in_view: false, - } - } + pub const NOT_VISIBLE: Self = ComputedVisibility { + is_visible_in_hierarchy: false, + is_visible_in_view: false, + }; /// Whether this entity is visible to something this frame. This is true if and only if [`Self::is_visible_in_hierarchy`] and [`Self::is_visible_in_view`] /// are true. This is the canonical method to call to determine if an entity should be drawn. diff --git a/crates/bevy_transform/src/components/global_transform.rs b/crates/bevy_transform/src/components/global_transform.rs index 033333a69bba7..66d2ea03fb7a5 100644 --- a/crates/bevy_transform/src/components/global_transform.rs +++ b/crates/bevy_transform/src/components/global_transform.rs @@ -108,13 +108,6 @@ impl GlobalTransform { self.0.to_scale_rotation_translation() } - /// Creates a new identity [`GlobalTransform`], that maps all points in space to themselves. - #[inline] - #[deprecated = "Use `GlobalTransform::IDENTITY` instead."] - pub const fn identity() -> Self { - GlobalTransform::IDENTITY - } - impl_local_axis!(right, left, X); impl_local_axis!(up, down, Y); impl_local_axis!(back, forward, Z); diff --git a/crates/bevy_transform/src/components/transform.rs b/crates/bevy_transform/src/components/transform.rs index d0006af9ed691..3920499622ae2 100644 --- a/crates/bevy_transform/src/components/transform.rs +++ b/crates/bevy_transform/src/components/transform.rs @@ -53,14 +53,6 @@ impl Transform { Self::from_translation(Vec3::new(x, y, z)) } - /// Creates a new identity [`Transform`], with no translation, rotation, and a scale of 1 on - /// all axes. - #[inline] - #[deprecated = "Use `Transform::IDENTITY` instead."] - pub const fn identity() -> Self { - Transform::IDENTITY - } - /// Extracts the translation, rotation, and scale from `matrix`. It must be a 3d affine /// transformation matrix. #[inline] diff --git a/crates/bevy_transform/src/systems.rs b/crates/bevy_transform/src/systems.rs index 9406149fb0847..390f804b3aca3 100644 --- a/crates/bevy_transform/src/systems.rs +++ b/crates/bevy_transform/src/systems.rs @@ -297,14 +297,12 @@ mod test { .world .spawn() .insert(Transform::from_translation(translation)) - .insert(GlobalTransform::default()) + .insert(GlobalTransform::IDENTITY) .with_children(|builder| { child = builder - .spawn_bundle((Transform::identity(), GlobalTransform::default())) + .spawn_bundle(TransformBundle::IDENTITY) .with_children(|builder| { - grandchild = builder - .spawn_bundle((Transform::identity(), GlobalTransform::default())) - .id(); + grandchild = builder.spawn_bundle(TransformBundle::IDENTITY).id(); }) .id(); }) @@ -338,11 +336,11 @@ mod test { let mut grandchild = Entity::from_raw(0); let child = world .spawn() - .insert_bundle((Transform::identity(), GlobalTransform::default())) + .insert_bundle(TransformBundle::IDENTITY) .with_children(|builder| { grandchild = builder .spawn() - .insert_bundle((Transform::identity(), GlobalTransform::default())) + .insert_bundle(TransformBundle::IDENTITY) .id(); }) .id(); @@ -357,7 +355,7 @@ mod test { app.world .spawn() - .insert_bundle((Transform::default(), GlobalTransform::default())) + .insert_bundle(TransformBundle::IDENTITY) .push_children(&[child]); std::mem::swap( &mut *app.world.get_mut::(child).unwrap(), diff --git a/crates/bevy_ui/src/update.rs b/crates/bevy_ui/src/update.rs index 05628555f1980..b0cf91aa6abb3 100644 --- a/crates/bevy_ui/src/update.rs +++ b/crates/bevy_ui/src/update.rs @@ -150,7 +150,7 @@ mod tests { struct Label(&'static str); fn node_with_transform(name: &'static str) -> (Label, Node, Transform) { - (Label(name), Node::default(), Transform::identity()) + (Label(name), Node::default(), Transform::IDENTITY) } fn node_without_transform(name: &'static str) -> (Label, Node) { diff --git a/examples/animation/custom_skinned_mesh.rs b/examples/animation/custom_skinned_mesh.rs index 94a657ffe5f90..4bf9035022439 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::IDENTITY, )) .id(); let joint_1 = commands .spawn_bundle(( AnimatedJoint, - Transform::identity(), - GlobalTransform::identity(), + Transform::IDENTITY, + GlobalTransform::IDENTITY, )) .id(); diff --git a/examples/ecs/component_change_detection.rs b/examples/ecs/component_change_detection.rs index 2d31302a3dd47..e3e986597ce35 100644 --- a/examples/ecs/component_change_detection.rs +++ b/examples/ecs/component_change_detection.rs @@ -18,7 +18,7 @@ struct MyComponent(f64); fn setup(mut commands: Commands) { commands.spawn().insert(MyComponent(0.)); - commands.spawn().insert(Transform::identity()); + commands.spawn().insert(Transform::IDENTITY); } fn change_component(time: Res