|
| 1 | +//! System parameter for computing up-to-date [`GlobalTransform`]s. |
| 2 | +
|
| 3 | +use bevy_ecs::{ |
| 4 | + prelude::Entity, |
| 5 | + query::QueryEntityError, |
| 6 | + system::{Query, SystemParam}, |
| 7 | +}; |
| 8 | +use bevy_hierarchy::{HierarchyQueryExt, Parent}; |
| 9 | + |
| 10 | +use crate::components::{GlobalTransform, Transform}; |
| 11 | + |
| 12 | +/// System parameter for computing up-to-date [`GlobalTransform`]s. |
| 13 | +/// |
| 14 | +/// Computing an entity's [`GlobalTransform`] can be expensive so it is recommended |
| 15 | +/// you use the [`GlobalTransform`] component stored on the entity, unless you need |
| 16 | +/// a [`GlobalTransform`] that reflects the changes made to any [`Transform`]s since |
| 17 | +/// the last time the transform propagation systems ran. |
| 18 | +#[derive(SystemParam)] |
| 19 | +pub struct TransformHelper<'w, 's> { |
| 20 | + parent_query: Query<'w, 's, &'static Parent>, |
| 21 | + transform_query: Query<'w, 's, &'static Transform>, |
| 22 | +} |
| 23 | + |
| 24 | +impl<'w, 's> TransformHelper<'w, 's> { |
| 25 | + /// Computes the [`GlobalTransform`] of the given entity from the [`Transform`] component on it and its ancestors. |
| 26 | + pub fn compute_global_transform( |
| 27 | + &self, |
| 28 | + entity: Entity, |
| 29 | + ) -> Result<GlobalTransform, ComputeGlobalTransformError> { |
| 30 | + let transform = self |
| 31 | + .transform_query |
| 32 | + .get(entity) |
| 33 | + .map_err(|err| map_error(err, false))?; |
| 34 | + |
| 35 | + let mut global_transform = GlobalTransform::from(*transform); |
| 36 | + |
| 37 | + for entity in self.parent_query.iter_ancestors(entity) { |
| 38 | + let transform = self |
| 39 | + .transform_query |
| 40 | + .get(entity) |
| 41 | + .map_err(|err| map_error(err, true))?; |
| 42 | + |
| 43 | + global_transform = *transform * global_transform; |
| 44 | + } |
| 45 | + |
| 46 | + Ok(global_transform) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +fn map_error(err: QueryEntityError, ancestor: bool) -> ComputeGlobalTransformError { |
| 51 | + use ComputeGlobalTransformError::*; |
| 52 | + match err { |
| 53 | + QueryEntityError::QueryDoesNotMatch(entity) => MissingTransform(entity), |
| 54 | + QueryEntityError::NoSuchEntity(entity) => { |
| 55 | + if ancestor { |
| 56 | + MalformedHierarchy(entity) |
| 57 | + } else { |
| 58 | + NoSuchEntity(entity) |
| 59 | + } |
| 60 | + } |
| 61 | + QueryEntityError::AliasedMutability(_) => unreachable!(), |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/// Error returned by [`TransformHelper::compute_global_transform`]. |
| 66 | +#[derive(Debug)] |
| 67 | +pub enum ComputeGlobalTransformError { |
| 68 | + /// The entity or one of its ancestors is missing the [`Transform`] component. |
| 69 | + MissingTransform(Entity), |
| 70 | + /// The entity does not exist. |
| 71 | + NoSuchEntity(Entity), |
| 72 | + /// An ancestor is missing. |
| 73 | + /// This probably means that your hierarchy has been improperly maintained. |
| 74 | + MalformedHierarchy(Entity), |
| 75 | +} |
| 76 | + |
| 77 | +#[cfg(test)] |
| 78 | +mod tests { |
| 79 | + use std::f32::consts::TAU; |
| 80 | + |
| 81 | + use bevy_app::App; |
| 82 | + use bevy_ecs::system::SystemState; |
| 83 | + use bevy_hierarchy::BuildWorldChildren; |
| 84 | + use bevy_math::{Quat, Vec3}; |
| 85 | + |
| 86 | + use crate::{ |
| 87 | + components::{GlobalTransform, Transform}, |
| 88 | + helper::TransformHelper, |
| 89 | + TransformBundle, TransformPlugin, |
| 90 | + }; |
| 91 | + |
| 92 | + #[test] |
| 93 | + fn match_transform_propagation_systems() { |
| 94 | + // Single transform |
| 95 | + match_transform_propagation_systems_inner(vec![Transform::from_translation(Vec3::X) |
| 96 | + .with_rotation(Quat::from_rotation_y(TAU / 4.)) |
| 97 | + .with_scale(Vec3::splat(2.))]); |
| 98 | + |
| 99 | + // Transform hierarchy |
| 100 | + match_transform_propagation_systems_inner(vec![ |
| 101 | + Transform::from_translation(Vec3::X) |
| 102 | + .with_rotation(Quat::from_rotation_y(TAU / 4.)) |
| 103 | + .with_scale(Vec3::splat(2.)), |
| 104 | + Transform::from_translation(Vec3::Y) |
| 105 | + .with_rotation(Quat::from_rotation_z(TAU / 3.)) |
| 106 | + .with_scale(Vec3::splat(1.5)), |
| 107 | + Transform::from_translation(Vec3::Z) |
| 108 | + .with_rotation(Quat::from_rotation_x(TAU / 2.)) |
| 109 | + .with_scale(Vec3::splat(0.3)), |
| 110 | + ]); |
| 111 | + } |
| 112 | + |
| 113 | + fn match_transform_propagation_systems_inner(transforms: Vec<Transform>) { |
| 114 | + let mut app = App::new(); |
| 115 | + app.add_plugins(TransformPlugin); |
| 116 | + |
| 117 | + let mut entity = None; |
| 118 | + |
| 119 | + for transform in transforms { |
| 120 | + let mut e = app.world.spawn(TransformBundle::from(transform)); |
| 121 | + |
| 122 | + if let Some(entity) = entity { |
| 123 | + e.set_parent(entity); |
| 124 | + } |
| 125 | + |
| 126 | + entity = Some(e.id()); |
| 127 | + } |
| 128 | + |
| 129 | + let leaf_entity = entity.unwrap(); |
| 130 | + |
| 131 | + app.update(); |
| 132 | + |
| 133 | + let transform = *app.world.get::<GlobalTransform>(leaf_entity).unwrap(); |
| 134 | + |
| 135 | + let mut state = SystemState::<TransformHelper>::new(&mut app.world); |
| 136 | + let helper = state.get(&app.world); |
| 137 | + |
| 138 | + let computed_transform = helper.compute_global_transform(leaf_entity).unwrap(); |
| 139 | + |
| 140 | + approx::assert_abs_diff_eq!(transform.affine(), computed_transform.affine()); |
| 141 | + } |
| 142 | +} |
0 commit comments