2022-01-14 18:47:48 +00:00
|
|
|
#![warn(missing_docs)]
|
2022-11-21 18:18:38 +00:00
|
|
|
#![warn(clippy::undocumented_unsafe_blocks)]
|
2021-12-18 22:59:55 +00:00
|
|
|
#![doc = include_str!("../README.md")]
|
|
|
|
|
2023-01-12 18:46:11 +00:00
|
|
|
pub mod commands;
|
2022-01-14 18:47:48 +00:00
|
|
|
/// The basic components of the transform crate
|
2019-12-02 09:31:07 +00:00
|
|
|
pub mod components;
|
2022-03-15 01:54:05 +00:00
|
|
|
mod systems;
|
2019-12-02 09:31:07 +00:00
|
|
|
|
2022-11-02 20:40:45 +00:00
|
|
|
#[doc(hidden)]
|
2019-12-02 09:31:07 +00:00
|
|
|
pub mod prelude {
|
2022-11-02 20:40:45 +00:00
|
|
|
#[doc(hidden)]
|
2023-01-12 18:46:11 +00:00
|
|
|
pub use crate::{
|
|
|
|
commands::BuildChildrenTransformExt, components::*, TransformBundle, TransformPlugin,
|
|
|
|
};
|
2020-07-10 04:18:35 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 21:20:37 +00:00
|
|
|
use bevy_app::prelude::*;
|
2022-03-15 01:54:05 +00:00
|
|
|
use bevy_ecs::prelude::*;
|
2022-09-19 16:12:11 +00:00
|
|
|
use bevy_hierarchy::ValidParentCheckPlugin;
|
2022-03-15 01:54:05 +00:00
|
|
|
use prelude::{GlobalTransform, Transform};
|
2023-01-31 01:47:00 +00:00
|
|
|
use systems::{propagate_transforms, sync_simple_transforms};
|
2020-07-16 23:32:39 +00:00
|
|
|
|
2022-02-06 01:07:55 +00:00
|
|
|
/// 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`].
|
2022-07-18 23:27:30 +00:00
|
|
|
/// * To get the global transform of an entity, you should get its [`GlobalTransform`].
|
2022-02-06 01:07:55 +00:00
|
|
|
/// * 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
|
2022-03-15 01:54:05 +00:00
|
|
|
/// frame if it doesn't have a parent.
|
2022-02-06 01:07:55 +00:00
|
|
|
///
|
|
|
|
/// [`GlobalTransform`] is the position of an entity relative to the reference frame.
|
|
|
|
///
|
2022-11-21 18:18:38 +00:00
|
|
|
/// [`GlobalTransform`] is updated from [`Transform`] in the systems labeled
|
|
|
|
/// [`TransformPropagate`](crate::TransformSystem::TransformPropagate).
|
2022-02-06 01:07:55 +00:00
|
|
|
///
|
|
|
|
/// This system runs in stage [`CoreStage::PostUpdate`](crate::CoreStage::PostUpdate). If you
|
2022-07-21 20:46:54 +00:00
|
|
|
/// update the [`Transform`] of an entity in this stage or after, you will notice a 1 frame lag
|
2022-02-06 01:07:55 +00:00
|
|
|
/// 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 {
|
2022-08-30 22:10:24 +00:00
|
|
|
/// 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,
|
|
|
|
};
|
|
|
|
|
2022-02-06 01:07:55 +00:00
|
|
|
/// 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,
|
2022-08-30 22:10:24 +00:00
|
|
|
..Self::IDENTITY
|
2022-02-06 01:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Transform> for TransformBundle {
|
|
|
|
#[inline]
|
|
|
|
fn from(transform: Transform) -> Self {
|
|
|
|
Self::from_transform(transform)
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 01:54:05 +00:00
|
|
|
/// Label enum for the systems relating to transform propagation
|
2021-02-18 21:20:37 +00:00
|
|
|
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemLabel)]
|
|
|
|
pub enum TransformSystem {
|
2022-07-21 20:46:54 +00:00
|
|
|
/// Propagates changes in transform to children's [`GlobalTransform`](crate::components::GlobalTransform)
|
2021-02-18 21:20:37 +00:00
|
|
|
TransformPropagate,
|
|
|
|
}
|
|
|
|
|
2023-01-11 21:12:01 +00:00
|
|
|
/// Transform propagation system set for third party plugins use
|
|
|
|
pub fn transform_propagate_system_set() -> SystemSet {
|
|
|
|
SystemSet::new()
|
|
|
|
.with_system(systems::sync_simple_transforms)
|
|
|
|
.with_system(systems::propagate_transforms)
|
|
|
|
}
|
|
|
|
|
2022-03-15 01:54:05 +00:00
|
|
|
/// The base plugin for handling [`Transform`] components
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct TransformPlugin;
|
|
|
|
|
2020-08-08 03:22:17 +00:00
|
|
|
impl Plugin for TransformPlugin {
|
2021-07-27 20:21:06 +00:00
|
|
|
fn build(&self, app: &mut App) {
|
2022-03-15 01:54:05 +00:00
|
|
|
app.register_type::<Transform>()
|
2020-11-28 00:39:59 +00:00
|
|
|
.register_type::<GlobalTransform>()
|
2022-09-19 16:12:11 +00:00
|
|
|
.add_plugin(ValidParentCheckPlugin::<GlobalTransform>::default())
|
2022-07-10 20:29:06 +00:00
|
|
|
// add transform systems to startup so the first update is "correct"
|
2021-02-17 02:00:12 +00:00
|
|
|
.add_startup_system_to_stage(
|
2021-02-18 21:20:37 +00:00
|
|
|
StartupStage::PostStartup,
|
2023-01-31 01:47:00 +00:00
|
|
|
sync_simple_transforms
|
|
|
|
.label(TransformSystem::TransformPropagate)
|
|
|
|
// FIXME: https://github.com/bevyengine/bevy/issues/4381
|
|
|
|
// These systems cannot access the same entities,
|
|
|
|
// due to subtle query filtering that is not yet correctly computed in the ambiguity detector
|
|
|
|
.ambiguous_with(propagate_transforms),
|
2022-11-21 18:18:38 +00:00
|
|
|
)
|
|
|
|
.add_startup_system_to_stage(
|
|
|
|
StartupStage::PostStartup,
|
2023-01-31 01:47:00 +00:00
|
|
|
propagate_transforms.label(TransformSystem::TransformPropagate),
|
2022-11-21 18:18:38 +00:00
|
|
|
)
|
|
|
|
.add_system_to_stage(
|
|
|
|
CoreStage::PostUpdate,
|
2023-01-31 01:47:00 +00:00
|
|
|
sync_simple_transforms
|
|
|
|
.label(TransformSystem::TransformPropagate)
|
|
|
|
.ambiguous_with(propagate_transforms),
|
2020-11-29 22:07:47 +00:00
|
|
|
)
|
2020-11-17 02:18:00 +00:00
|
|
|
.add_system_to_stage(
|
2021-02-18 21:20:37 +00:00
|
|
|
CoreStage::PostUpdate,
|
2023-01-31 01:47:00 +00:00
|
|
|
propagate_transforms.label(TransformSystem::TransformPropagate),
|
2020-11-17 02:18:00 +00:00
|
|
|
);
|
2020-07-16 23:32:39 +00:00
|
|
|
}
|
2019-12-02 09:31:07 +00:00
|
|
|
}
|