
# Objective - Hierarchy tools are not just used for `Transform`: they are also used for scenes. - In the future there's interest in using them for other features, such as visiibility inheritance. - The fact that these tools are found in `bevy_transform` causes a great deal of user and developer confusion - Fixes #2758. ## Solution - Split `bevy_transform` into two! - Make everything work again. Note that this is a very tightly scoped PR: I *know* there are code quality and docs issues that existed in bevy_transform that I've just moved around. We should fix those in a seperate PR and try to merge this ASAP to reduce the bitrot involved in splitting an entire crate. ## Frustrations The API around `GlobalTransform` is a mess: we have massive code and docs duplication, no link between the two types and no clear way to extend this to other forms of inheritance. In the medium-term, I feel pretty strongly that `GlobalTransform` should be replaced by something like `Inherited<Transform>`, which lives in `bevy_hierarchy`: - avoids code duplication - makes the inheritance pattern extensible - links the types at the type-level - allows us to remove all references to inheritance from `bevy_transform`, making it more useful as a standalone crate and cleaning up its docs ## Additional context - double-blessed by @cart in https://github.com/bevyengine/bevy/issues/4141#issuecomment-1063592414 and https://github.com/bevyengine/bevy/issues/2758#issuecomment-913810963 - preparation for more advanced / cleaner hierarchy tools: go read https://github.com/bevyengine/rfcs/pull/53 ! - originally attempted by @finegeometer in #2789. It was a great idea, just needed more discussion! Co-authored-by: Carter Anderson <mcanders1@gmail.com>
110 lines
3.9 KiB
Rust
110 lines
3.9 KiB
Rust
#![warn(missing_docs)]
|
|
#![doc = include_str!("../README.md")]
|
|
|
|
/// The basic components of the transform crate
|
|
pub mod components;
|
|
mod systems;
|
|
pub use crate::systems::transform_propagate_system;
|
|
|
|
#[doc(hidden)]
|
|
pub mod prelude {
|
|
#[doc(hidden)]
|
|
pub use crate::{components::*, TransformBundle, TransformPlugin};
|
|
}
|
|
|
|
use bevy_app::prelude::*;
|
|
use bevy_ecs::prelude::*;
|
|
use bevy_hierarchy::HierarchySystem;
|
|
use prelude::{GlobalTransform, Transform};
|
|
|
|
/// A [`Bundle`] of the [`Transform`] and [`GlobalTransform`]
|
|
/// [`Component`](bevy_ecs::component::Component)s, which describe the position of an entity.
|
|
///
|
|
/// * To place or move an entity, you should set its [`Transform`].
|
|
/// * To get the global position of an entity, you should get its [`GlobalTransform`].
|
|
/// * For transform hierarchies to work correctly, you must have both a [`Transform`] and a [`GlobalTransform`].
|
|
/// * You may use the [`TransformBundle`] to guarantee this.
|
|
///
|
|
/// ## [`Transform`] and [`GlobalTransform`]
|
|
///
|
|
/// [`Transform`] is the position of an entity relative to its parent position, or the reference
|
|
/// frame if it doesn't have a parent.
|
|
///
|
|
/// [`GlobalTransform`] is the position of an entity relative to the reference frame.
|
|
///
|
|
/// [`GlobalTransform`] is updated from [`Transform`] in the system
|
|
/// [`transform_propagate_system`].
|
|
///
|
|
/// 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(Bundle, Clone, Copy, Debug, Default)]
|
|
pub struct TransformBundle {
|
|
/// The transform of the entity.
|
|
pub local: Transform,
|
|
/// The global transform of the entity.
|
|
pub global: GlobalTransform,
|
|
}
|
|
|
|
impl TransformBundle {
|
|
/// Creates a new [`TransformBundle`] from a [`Transform`].
|
|
///
|
|
/// This initializes [`GlobalTransform`] as identity, to be updated later by the
|
|
/// [`CoreStage::PostUpdate`](crate::CoreStage::PostUpdate) stage.
|
|
#[inline]
|
|
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(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl From<Transform> for TransformBundle {
|
|
#[inline]
|
|
fn from(transform: Transform) -> Self {
|
|
Self::from_transform(transform)
|
|
}
|
|
}
|
|
/// Label enum for the systems relating to transform propagation
|
|
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)]
|
|
pub enum TransformSystem {
|
|
/// Propagates changes in transform to childrens' [`GlobalTransform`](crate::components::GlobalTransform)
|
|
TransformPropagate,
|
|
}
|
|
|
|
/// The base plugin for handling [`Transform`] components
|
|
#[derive(Default)]
|
|
pub struct TransformPlugin;
|
|
|
|
impl Plugin for TransformPlugin {
|
|
fn build(&self, app: &mut App) {
|
|
app.register_type::<Transform>()
|
|
.register_type::<GlobalTransform>()
|
|
// Adding these to startup ensures the first update is "correct"
|
|
.add_startup_system_to_stage(
|
|
StartupStage::PostStartup,
|
|
systems::transform_propagate_system
|
|
.label(TransformSystem::TransformPropagate)
|
|
.after(HierarchySystem::ParentUpdate),
|
|
)
|
|
.add_system_to_stage(
|
|
CoreStage::PostUpdate,
|
|
systems::transform_propagate_system
|
|
.label(TransformSystem::TransformPropagate)
|
|
.after(HierarchySystem::ParentUpdate),
|
|
);
|
|
}
|
|
}
|