Skip to content

Commit b42f426

Browse files
committed
Add associated constant IDENTITY to Transform and friends. (#5340)
# Objective Since `identity` is a const fn that takes no arguments it seems logical to make it an associated constant. This is also more in line with types from glam (eg. `Quat::IDENTITY`). ## Migration Guide The method `identity()` on `Transform`, `GlobalTransform` and `TransformBundle` has been deprecated. Use the associated constant `IDENTITY` instead. Co-authored-by: devil-ira <[email protected]>
1 parent ed773db commit b42f426

File tree

14 files changed

+65
-79
lines changed

14 files changed

+65
-79
lines changed

crates/bevy_gltf/src/loader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ async fn load_gltf<'a, 'b>(
466466

467467
world
468468
.spawn()
469-
.insert_bundle(SpatialBundle::visible_identity())
469+
.insert_bundle(SpatialBundle::VISIBLE_IDENTITY)
470470
.with_children(|parent| {
471471
for node in scene.nodes() {
472472
let result = load_node(
@@ -1169,7 +1169,7 @@ mod test {
11691169
GltfNode {
11701170
children: vec![],
11711171
mesh: None,
1172-
transform: bevy_transform::prelude::Transform::identity(),
1172+
transform: bevy_transform::prelude::Transform::IDENTITY,
11731173
}
11741174
}
11751175
}

crates/bevy_pbr/src/light.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ pub fn update_point_light_frusta(
14501450
Mat4::perspective_infinite_reverse_rh(std::f32::consts::FRAC_PI_2, 1.0, POINT_LIGHT_NEAR_Z);
14511451
let view_rotations = CUBE_MAP_FACES
14521452
.iter()
1453-
.map(|CubeMapFace { target, up }| Transform::identity().looking_at(*target, *up))
1453+
.map(|CubeMapFace { target, up }| Transform::IDENTITY.looking_at(*target, *up))
14541454
.collect::<Vec<_>>();
14551455

14561456
for (entity, transform, point_light, mut cubemap_frusta) in &mut views {

crates/bevy_pbr/src/render/light.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ pub fn prepare_lights(
781781
Mat4::perspective_infinite_reverse_rh(std::f32::consts::FRAC_PI_2, 1.0, POINT_LIGHT_NEAR_Z);
782782
let cube_face_rotations = CUBE_MAP_FACES
783783
.iter()
784-
.map(|CubeMapFace { target, up }| Transform::identity().looking_at(*target, *up))
784+
.map(|CubeMapFace { target, up }| Transform::IDENTITY.looking_at(*target, *up))
785785
.collect::<Vec<_>>();
786786

787787
global_light_meta.entity_to_index.clear();

crates/bevy_render/src/spatial_bundle.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,23 @@ impl SpatialBundle {
3333
pub const fn from_transform(transform: Transform) -> Self {
3434
SpatialBundle {
3535
transform,
36-
// Note: `..Default::default()` cannot be used here, because it isn't const
37-
..Self::visible_identity()
36+
..Self::VISIBLE_IDENTITY
3837
}
3938
}
4039

41-
/// Creates a new identity [`SpatialBundle`], with no translation, rotation, and a scale of 1
42-
/// on all axes.
43-
#[inline]
44-
pub const fn visible_identity() -> Self {
45-
SpatialBundle {
46-
transform: Transform::identity(),
47-
global_transform: GlobalTransform::identity(),
48-
visibility: Visibility::visible(),
49-
computed: ComputedVisibility::not_visible(),
50-
}
51-
}
40+
/// A visible [`SpatialBundle`], with no translation, rotation, and a scale of 1 on all axes.
41+
pub const VISIBLE_IDENTITY: Self = SpatialBundle {
42+
visibility: Visibility::VISIBLE,
43+
computed: ComputedVisibility::INVISIBLE,
44+
transform: Transform::IDENTITY,
45+
global_transform: GlobalTransform::IDENTITY,
46+
};
47+
48+
/// An invisible [`SpatialBundle`], with no translation, rotation, and a scale of 1 on all axes.
49+
pub const INVISIBLE_IDENTITY: Self = SpatialBundle {
50+
visibility: Visibility::INVISIBLE,
51+
..Self::VISIBLE_IDENTITY
52+
};
5253
}
5354

5455
impl From<Transform> for SpatialBundle {

crates/bevy_render/src/view/visibility/mod.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@ pub struct Visibility {
3737

3838
impl Default for Visibility {
3939
fn default() -> Self {
40-
Self::visible()
40+
Visibility::VISIBLE
4141
}
4242
}
4343

4444
impl Visibility {
45-
/// Creates a new [`Visibility`], set as visible
46-
pub const fn visible() -> Self {
47-
Self { is_visible: true }
48-
}
45+
/// A [`Visibility`], set as visible.
46+
pub const VISIBLE: Self = Visibility { is_visible: true };
47+
48+
/// A [`Visibility`], set as invisible.
49+
pub const INVISIBLE: Self = Visibility { is_visible: false };
4950
}
5051

5152
/// Algorithmically-computed indication of whether an entity is visible and should be extracted for rendering
@@ -58,18 +59,16 @@ pub struct ComputedVisibility {
5859

5960
impl Default for ComputedVisibility {
6061
fn default() -> Self {
61-
Self::not_visible()
62+
Self::INVISIBLE
6263
}
6364
}
6465

6566
impl ComputedVisibility {
66-
/// Creates a new [`ComputedVisibility`], set as not visible
67-
pub const fn not_visible() -> Self {
68-
Self {
69-
is_visible_in_hierarchy: false,
70-
is_visible_in_view: false,
71-
}
72-
}
67+
/// A [`ComputedVisibility`], set as invisible.
68+
pub const INVISIBLE: Self = ComputedVisibility {
69+
is_visible_in_hierarchy: false,
70+
is_visible_in_view: false,
71+
};
7372

7473
/// 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`]
7574
/// are true. This is the canonical method to call to determine if an entity should be drawn.

crates/bevy_transform/src/components/global_transform.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ macro_rules! impl_local_axis {
4646
}
4747

4848
impl GlobalTransform {
49+
/// An identity [`GlobalTransform`] that maps all points in space to themselves.
50+
pub const IDENTITY: Self = Self(Affine3A::IDENTITY);
51+
4952
#[doc(hidden)]
5053
#[inline]
5154
pub fn from_xyz(x: f32, y: f32, z: f32) -> Self {
@@ -105,12 +108,6 @@ impl GlobalTransform {
105108
self.0.to_scale_rotation_translation()
106109
}
107110

108-
/// Creates a new identity [`GlobalTransform`], that maps all points in space to themselves.
109-
#[inline]
110-
pub const fn identity() -> Self {
111-
Self(Affine3A::IDENTITY)
112-
}
113-
114111
impl_local_axis!(right, left, X);
115112
impl_local_axis!(up, down, Y);
116113
impl_local_axis!(back, forward, Z);
@@ -154,7 +151,7 @@ impl GlobalTransform {
154151

155152
impl Default for GlobalTransform {
156153
fn default() -> Self {
157-
Self::identity()
154+
Self::IDENTITY
158155
}
159156
}
160157

crates/bevy_transform/src/components/transform.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ pub struct Transform {
3838
}
3939

4040
impl Transform {
41+
/// An identity [`Transform`] with no translation, rotation, and a scale of 1 on all axes.
42+
pub const IDENTITY: Self = Transform {
43+
translation: Vec3::ZERO,
44+
rotation: Quat::IDENTITY,
45+
scale: Vec3::ONE,
46+
};
47+
4148
/// Creates a new [`Transform`] at the position `(x, y, z)`. In 2d, the `z` component
4249
/// is used for z-ordering elements: higher `z`-value will be in front of lower
4350
/// `z`-value.
@@ -46,17 +53,6 @@ impl Transform {
4653
Self::from_translation(Vec3::new(x, y, z))
4754
}
4855

49-
/// Creates a new identity [`Transform`], with no translation, rotation, and a scale of 1 on
50-
/// all axes.
51-
#[inline]
52-
pub const fn identity() -> Self {
53-
Transform {
54-
translation: Vec3::ZERO,
55-
rotation: Quat::IDENTITY,
56-
scale: Vec3::ONE,
57-
}
58-
}
59-
6056
/// Extracts the translation, rotation, and scale from `matrix`. It must be a 3d affine
6157
/// transformation matrix.
6258
#[inline]
@@ -76,7 +72,7 @@ impl Transform {
7672
pub const fn from_translation(translation: Vec3) -> Self {
7773
Transform {
7874
translation,
79-
..Self::identity()
75+
..Self::IDENTITY
8076
}
8177
}
8278

@@ -86,7 +82,7 @@ impl Transform {
8682
pub const fn from_rotation(rotation: Quat) -> Self {
8783
Transform {
8884
rotation,
89-
..Self::identity()
85+
..Self::IDENTITY
9086
}
9187
}
9288

@@ -96,7 +92,7 @@ impl Transform {
9692
pub const fn from_scale(scale: Vec3) -> Self {
9793
Transform {
9894
scale,
99-
..Self::identity()
95+
..Self::IDENTITY
10096
}
10197
}
10298

@@ -335,7 +331,7 @@ impl Transform {
335331

336332
impl Default for Transform {
337333
fn default() -> Self {
338-
Self::identity()
334+
Self::IDENTITY
339335
}
340336
}
341337

crates/bevy_transform/src/lib.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ pub struct TransformBundle {
4646
}
4747

4848
impl TransformBundle {
49+
/// An identity [`TransformBundle`] with no translation, rotation, and a scale of 1 on all axes.
50+
pub const IDENTITY: Self = TransformBundle {
51+
local: Transform::IDENTITY,
52+
global: GlobalTransform::IDENTITY,
53+
};
54+
4955
/// Creates a new [`TransformBundle`] from a [`Transform`].
5056
///
5157
/// This initializes [`GlobalTransform`] as identity, to be updated later by the
@@ -54,18 +60,7 @@ impl TransformBundle {
5460
pub const fn from_transform(transform: Transform) -> Self {
5561
TransformBundle {
5662
local: transform,
57-
// Note: `..Default::default()` cannot be used here, because it isn't const
58-
..Self::identity()
59-
}
60-
}
61-
62-
/// Creates a new identity [`TransformBundle`], with no translation, rotation, and a scale of 1
63-
/// on all axes.
64-
#[inline]
65-
pub const fn identity() -> Self {
66-
TransformBundle {
67-
local: Transform::identity(),
68-
global: GlobalTransform::identity(),
63+
..Self::IDENTITY
6964
}
7065
}
7166
}

crates/bevy_transform/src/systems.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,14 +297,12 @@ mod test {
297297
.world
298298
.spawn()
299299
.insert(Transform::from_translation(translation))
300-
.insert(GlobalTransform::default())
300+
.insert(GlobalTransform::IDENTITY)
301301
.with_children(|builder| {
302302
child = builder
303-
.spawn_bundle((Transform::identity(), GlobalTransform::default()))
303+
.spawn_bundle(TransformBundle::IDENTITY)
304304
.with_children(|builder| {
305-
grandchild = builder
306-
.spawn_bundle((Transform::identity(), GlobalTransform::default()))
307-
.id();
305+
grandchild = builder.spawn_bundle(TransformBundle::IDENTITY).id();
308306
})
309307
.id();
310308
})
@@ -338,11 +336,11 @@ mod test {
338336
let mut grandchild = Entity::from_raw(0);
339337
let child = world
340338
.spawn()
341-
.insert_bundle((Transform::identity(), GlobalTransform::default()))
339+
.insert_bundle(TransformBundle::IDENTITY)
342340
.with_children(|builder| {
343341
grandchild = builder
344342
.spawn()
345-
.insert_bundle((Transform::identity(), GlobalTransform::default()))
343+
.insert_bundle(TransformBundle::IDENTITY)
346344
.id();
347345
})
348346
.id();
@@ -357,7 +355,7 @@ mod test {
357355

358356
app.world
359357
.spawn()
360-
.insert_bundle((Transform::default(), GlobalTransform::default()))
358+
.insert_bundle(TransformBundle::IDENTITY)
361359
.push_children(&[child]);
362360
std::mem::swap(
363361
&mut *app.world.get_mut::<Parent>(child).unwrap(),

crates/bevy_ui/src/update.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ mod tests {
150150
struct Label(&'static str);
151151

152152
fn node_with_transform(name: &'static str) -> (Label, Node, Transform) {
153-
(Label(name), Node::default(), Transform::identity())
153+
(Label(name), Node::default(), Transform::IDENTITY)
154154
}
155155

156156
fn node_without_transform(name: &'static str) -> (Label, Node) {

examples/animation/animated_transform.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ fn setup(
125125
.insert_bundle((planet, player))
126126
.with_children(|p| {
127127
// This entity is just used for animation, but doesn't display anything
128-
p.spawn_bundle(SpatialBundle::default())
128+
p.spawn_bundle(SpatialBundle::VISIBLE_IDENTITY)
129129
// Add the Name component
130130
.insert(orbit_controller)
131131
.with_children(|p| {

examples/animation/custom_skinned_mesh.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,14 @@ fn setup(
123123
let joint_0 = commands
124124
.spawn_bundle((
125125
Transform::from_xyz(i as f32 * 1.5, 0.0, 0.0),
126-
GlobalTransform::identity(),
126+
GlobalTransform::IDENTITY,
127127
))
128128
.id();
129129
let joint_1 = commands
130130
.spawn_bundle((
131131
AnimatedJoint,
132-
Transform::identity(),
133-
GlobalTransform::identity(),
132+
Transform::IDENTITY,
133+
GlobalTransform::IDENTITY,
134134
))
135135
.id();
136136

examples/ecs/component_change_detection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ struct MyComponent(f64);
1818

1919
fn setup(mut commands: Commands) {
2020
commands.spawn().insert(MyComponent(0.));
21-
commands.spawn().insert(Transform::identity());
21+
commands.spawn().insert(Transform::IDENTITY);
2222
}
2323

2424
fn change_component(time: Res<Time>, mut query: Query<(Entity, &mut MyComponent)>) {

examples/scene/scene.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ fn save_scene_system(world: &mut World) {
8484
scene_world.spawn().insert_bundle((
8585
component_b,
8686
ComponentA { x: 1.0, y: 2.0 },
87-
Transform::identity(),
87+
Transform::IDENTITY,
8888
));
8989
scene_world
9090
.spawn()

0 commit comments

Comments
 (0)