Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Add associated constant IDENTITY to Transform and friends. #5340

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/bevy_gltf/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -1169,7 +1169,7 @@ mod test {
GltfNode {
children: vec![],
mesh: None,
transform: bevy_transform::prelude::Transform::identity(),
transform: bevy_transform::prelude::Transform::IDENTITY,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();

for (entity, transform, point_light, mut cubemap_frusta) in &mut views {
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/render/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();

global_light_meta.entity_to_index.clear();
Expand Down
27 changes: 14 additions & 13 deletions crates/bevy_render/src/spatial_bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,23 @@ 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::INVISIBLE,
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,
..Self::VISIBLE_IDENTITY
};
}

impl From<Transform> for SpatialBundle {
Expand Down
25 changes: 12 additions & 13 deletions crates/bevy_render/src/view/visibility/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -58,18 +59,16 @@ pub struct ComputedVisibility {

impl Default for ComputedVisibility {
fn default() -> Self {
Self::not_visible()
Self::INVISIBLE
}
}

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,
}
}
/// A [`ComputedVisibility`], set as invisible.
pub const INVISIBLE: 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.
Expand Down
11 changes: 4 additions & 7 deletions crates/bevy_transform/src/components/global_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -105,12 +108,6 @@ impl GlobalTransform {
self.0.to_scale_rotation_translation()
}

/// Creates a new identity [`GlobalTransform`], that maps all points in space to themselves.
#[inline]
pub const fn identity() -> Self {
Self(Affine3A::IDENTITY)
}

impl_local_axis!(right, left, X);
impl_local_axis!(up, down, Y);
impl_local_axis!(back, forward, Z);
Expand Down Expand Up @@ -154,7 +151,7 @@ impl GlobalTransform {

impl Default for GlobalTransform {
fn default() -> Self {
Self::identity()
Self::IDENTITY
}
}

Expand Down
26 changes: 11 additions & 15 deletions crates/bevy_transform/src/components/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -46,17 +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]
pub const fn identity() -> Self {
Transform {
translation: Vec3::ZERO,
rotation: Quat::IDENTITY,
scale: Vec3::ONE,
}
}

/// Extracts the translation, rotation, and scale from `matrix`. It must be a 3d affine
/// transformation matrix.
#[inline]
Expand All @@ -76,7 +72,7 @@ impl Transform {
pub const fn from_translation(translation: Vec3) -> Self {
Transform {
translation,
..Self::identity()
..Self::IDENTITY
}
}

Expand All @@ -86,7 +82,7 @@ impl Transform {
pub const fn from_rotation(rotation: Quat) -> Self {
Transform {
rotation,
..Self::identity()
..Self::IDENTITY
}
}

Expand All @@ -96,7 +92,7 @@ impl Transform {
pub const fn from_scale(scale: Vec3) -> Self {
Transform {
scale,
..Self::identity()
..Self::IDENTITY
}
}

Expand Down Expand Up @@ -335,7 +331,7 @@ impl Transform {

impl Default for Transform {
fn default() -> Self {
Self::identity()
Self::IDENTITY
}
}

Expand Down
19 changes: 7 additions & 12 deletions crates/bevy_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = TransformBundle {
local: Transform::IDENTITY,
global: GlobalTransform::IDENTITY,
};

/// Creates a new [`TransformBundle`] from a [`Transform`].
///
/// This initializes [`GlobalTransform`] as identity, to be updated later by the
Expand All @@ -54,18 +60,7 @@ 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()
}
}

/// Creates a new identity [`TransformBundle`], with no translation, rotation, and a scale of 1
/// on all axes.
#[inline]
pub const fn identity() -> Self {
TransformBundle {
local: Transform::identity(),
global: GlobalTransform::identity(),
..Self::IDENTITY
}
}
}
Expand Down
14 changes: 6 additions & 8 deletions crates/bevy_transform/src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})
Expand Down Expand Up @@ -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();
Expand All @@ -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::<Parent>(child).unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ui/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion examples/animation/animated_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ fn setup(
.insert_bundle((planet, player))
.with_children(|p| {
// This entity is just used for animation, but doesn't display anything
p.spawn_bundle(SpatialBundle::default())
p.spawn_bundle(SpatialBundle::VISIBLE_IDENTITY)
// Add the Name component
.insert(orbit_controller)
.with_children(|p| {
Expand Down
6 changes: 3 additions & 3 deletions examples/animation/custom_skinned_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion examples/ecs/component_change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Time>, mut query: Query<(Entity, &mut MyComponent)>) {
Expand Down
2 changes: 1 addition & 1 deletion examples/scene/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn save_scene_system(world: &mut World) {
scene_world.spawn().insert_bundle((
component_b,
ComponentA { x: 1.0, y: 2.0 },
Transform::identity(),
Transform::IDENTITY,
));
scene_world
.spawn()
Expand Down