2023-10-18 20:07:51 +00:00
|
|
|
//! System parameter for computing up-to-date [`GlobalTransform`]s.
|
|
|
|
|
|
|
|
use bevy_ecs::{
|
|
|
|
prelude::Entity,
|
|
|
|
query::QueryEntityError,
|
|
|
|
system::{Query, SystemParam},
|
|
|
|
};
|
|
|
|
use bevy_hierarchy::{HierarchyQueryExt, Parent};
|
2024-12-06 17:03:55 +00:00
|
|
|
use thiserror::Error;
|
2023-10-18 20:07:51 +00:00
|
|
|
|
|
|
|
use crate::components::{GlobalTransform, Transform};
|
|
|
|
|
|
|
|
/// System parameter for computing up-to-date [`GlobalTransform`]s.
|
|
|
|
///
|
|
|
|
/// Computing an entity's [`GlobalTransform`] can be expensive so it is recommended
|
|
|
|
/// you use the [`GlobalTransform`] component stored on the entity, unless you need
|
|
|
|
/// a [`GlobalTransform`] that reflects the changes made to any [`Transform`]s since
|
|
|
|
/// the last time the transform propagation systems ran.
|
|
|
|
#[derive(SystemParam)]
|
|
|
|
pub struct TransformHelper<'w, 's> {
|
|
|
|
parent_query: Query<'w, 's, &'static Parent>,
|
|
|
|
transform_query: Query<'w, 's, &'static Transform>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'w, 's> TransformHelper<'w, 's> {
|
|
|
|
/// Computes the [`GlobalTransform`] of the given entity from the [`Transform`] component on it and its ancestors.
|
|
|
|
pub fn compute_global_transform(
|
|
|
|
&self,
|
|
|
|
entity: Entity,
|
|
|
|
) -> Result<GlobalTransform, ComputeGlobalTransformError> {
|
|
|
|
let transform = self
|
|
|
|
.transform_query
|
|
|
|
.get(entity)
|
|
|
|
.map_err(|err| map_error(err, false))?;
|
|
|
|
|
|
|
|
let mut global_transform = GlobalTransform::from(*transform);
|
|
|
|
|
|
|
|
for entity in self.parent_query.iter_ancestors(entity) {
|
|
|
|
let transform = self
|
|
|
|
.transform_query
|
|
|
|
.get(entity)
|
|
|
|
.map_err(|err| map_error(err, true))?;
|
|
|
|
|
|
|
|
global_transform = *transform * global_transform;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(global_transform)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn map_error(err: QueryEntityError, ancestor: bool) -> ComputeGlobalTransformError {
|
|
|
|
use ComputeGlobalTransformError::*;
|
|
|
|
match err {
|
2024-09-26 13:31:11 +00:00
|
|
|
QueryEntityError::QueryDoesNotMatch(entity, _) => MissingTransform(entity),
|
2023-10-18 20:07:51 +00:00
|
|
|
QueryEntityError::NoSuchEntity(entity) => {
|
|
|
|
if ancestor {
|
|
|
|
MalformedHierarchy(entity)
|
|
|
|
} else {
|
|
|
|
NoSuchEntity(entity)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QueryEntityError::AliasedMutability(_) => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Error returned by [`TransformHelper::compute_global_transform`].
|
2024-12-06 17:03:55 +00:00
|
|
|
#[derive(Debug, Error)]
|
2023-10-18 20:07:51 +00:00
|
|
|
pub enum ComputeGlobalTransformError {
|
|
|
|
/// The entity or one of its ancestors is missing the [`Transform`] component.
|
2024-12-06 17:03:55 +00:00
|
|
|
#[error("The entity {0:?} or one of its ancestors is missing the `Transform` component")]
|
2023-10-18 20:07:51 +00:00
|
|
|
MissingTransform(Entity),
|
|
|
|
/// The entity does not exist.
|
2024-12-06 17:03:55 +00:00
|
|
|
#[error("The entity {0:?} does not exist")]
|
2023-10-18 20:07:51 +00:00
|
|
|
NoSuchEntity(Entity),
|
|
|
|
/// An ancestor is missing.
|
|
|
|
/// This probably means that your hierarchy has been improperly maintained.
|
2024-12-06 17:03:55 +00:00
|
|
|
#[error("The ancestor {0:?} is missing")]
|
2023-10-18 20:07:51 +00:00
|
|
|
MalformedHierarchy(Entity),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2024-09-27 00:59:59 +00:00
|
|
|
use core::f32::consts::TAU;
|
2023-10-18 20:07:51 +00:00
|
|
|
|
|
|
|
use bevy_app::App;
|
|
|
|
use bevy_ecs::system::SystemState;
|
2024-07-01 14:29:39 +00:00
|
|
|
use bevy_hierarchy::BuildChildren;
|
2023-10-18 20:07:51 +00:00
|
|
|
use bevy_math::{Quat, Vec3};
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
components::{GlobalTransform, Transform},
|
|
|
|
helper::TransformHelper,
|
2024-05-31 16:40:36 +00:00
|
|
|
plugins::TransformPlugin,
|
2023-10-18 20:07:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn match_transform_propagation_systems() {
|
|
|
|
// Single transform
|
|
|
|
match_transform_propagation_systems_inner(vec![Transform::from_translation(Vec3::X)
|
|
|
|
.with_rotation(Quat::from_rotation_y(TAU / 4.))
|
|
|
|
.with_scale(Vec3::splat(2.))]);
|
|
|
|
|
|
|
|
// Transform hierarchy
|
|
|
|
match_transform_propagation_systems_inner(vec![
|
|
|
|
Transform::from_translation(Vec3::X)
|
|
|
|
.with_rotation(Quat::from_rotation_y(TAU / 4.))
|
|
|
|
.with_scale(Vec3::splat(2.)),
|
|
|
|
Transform::from_translation(Vec3::Y)
|
|
|
|
.with_rotation(Quat::from_rotation_z(TAU / 3.))
|
|
|
|
.with_scale(Vec3::splat(1.5)),
|
|
|
|
Transform::from_translation(Vec3::Z)
|
|
|
|
.with_rotation(Quat::from_rotation_x(TAU / 2.))
|
|
|
|
.with_scale(Vec3::splat(0.3)),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn match_transform_propagation_systems_inner(transforms: Vec<Transform>) {
|
|
|
|
let mut app = App::new();
|
|
|
|
app.add_plugins(TransformPlugin);
|
|
|
|
|
|
|
|
let mut entity = None;
|
|
|
|
|
|
|
|
for transform in transforms {
|
2024-09-27 17:06:48 +00:00
|
|
|
let mut e = app.world_mut().spawn(transform);
|
2023-10-18 20:07:51 +00:00
|
|
|
|
|
|
|
if let Some(entity) = entity {
|
|
|
|
e.set_parent(entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
entity = Some(e.id());
|
|
|
|
}
|
|
|
|
|
|
|
|
let leaf_entity = entity.unwrap();
|
|
|
|
|
|
|
|
app.update();
|
|
|
|
|
2024-03-31 03:16:10 +00:00
|
|
|
let transform = *app.world().get::<GlobalTransform>(leaf_entity).unwrap();
|
2023-10-18 20:07:51 +00:00
|
|
|
|
2024-03-31 03:16:10 +00:00
|
|
|
let mut state = SystemState::<TransformHelper>::new(app.world_mut());
|
|
|
|
let helper = state.get(app.world());
|
2023-10-18 20:07:51 +00:00
|
|
|
|
|
|
|
let computed_transform = helper.compute_global_transform(leaf_entity).unwrap();
|
|
|
|
|
|
|
|
approx::assert_abs_diff_eq!(transform.affine(), computed_transform.affine());
|
|
|
|
}
|
|
|
|
}
|