1- use crate :: components:: { GlobalTransform , Transform3d , TransformTreeChanged } ;
1+ use crate :: components:: { GlobalTransform , Transform2d , Transform3d , TransformTreeChanged } ;
22use bevy_ecs:: prelude:: * ;
33#[ cfg( feature = "std" ) ]
44pub use parallel:: propagate_parent_transforms;
@@ -9,33 +9,35 @@ pub use serial::propagate_parent_transforms;
99///
1010/// Third party plugins should ensure that this is used in concert with
1111/// [`propagate_parent_transforms`] and [`mark_dirty_trees`].
12- pub fn sync_simple_transforms (
12+ pub fn sync_simple_transforms < T > (
1313 mut query : ParamSet < (
1414 Query <
15- ( & Transform3d , & mut GlobalTransform ) ,
15+ ( & T , & mut GlobalTransform ) ,
1616 (
17- Or < ( Changed < Transform3d > , Added < GlobalTransform > ) > ,
17+ Or < ( Changed < T > , Added < GlobalTransform > ) > ,
1818 Without < ChildOf > ,
1919 Without < Children > ,
2020 ) ,
2121 > ,
22- Query < ( Ref < Transform3d > , & mut GlobalTransform ) , ( Without < ChildOf > , Without < Children > ) > ,
22+ Query < ( Ref < T > , & mut GlobalTransform ) , ( Without < ChildOf > , Without < Children > ) > ,
2323 ) > ,
2424 mut orphaned : RemovedComponents < ChildOf > ,
25- ) {
25+ ) where
26+ T : Copy + Clone + Component + Into < GlobalTransform > ,
27+ {
2628 // Update changed entities.
2729 query
2830 . p0 ( )
2931 . par_iter_mut ( )
3032 . for_each ( |( transform, mut global_transform) | {
31- * global_transform = GlobalTransform :: from ( * transform) ;
33+ * global_transform = ( * transform) . into ( ) ;
3234 } ) ;
3335 // Update orphaned entities.
3436 let mut query = query. p1 ( ) ;
3537 let mut iter = query. iter_many_mut ( orphaned. read ( ) ) ;
3638 while let Some ( ( transform, mut global_transform) ) = iter. fetch_next ( ) {
3739 if !transform. is_changed ( ) && !global_transform. is_added ( ) {
38- * global_transform = GlobalTransform :: from ( * transform) ;
40+ * global_transform = ( * transform) . into ( ) ;
3941 }
4042 }
4143}
@@ -48,6 +50,7 @@ pub fn mark_dirty_trees(
4850 Entity ,
4951 Or < (
5052 Changed < Transform3d > ,
53+ Changed < Transform2d > ,
5154 Changed < ChildOf > ,
5255 Added < GlobalTransform > ,
5356 ) > ,
@@ -107,7 +110,11 @@ mod serial {
107110 > ,
108111 mut orphaned : RemovedComponents < ChildOf > ,
109112 transform_query : Query <
110- ( Ref < Transform3d > , & mut GlobalTransform , Option < & Children > ) ,
113+ (
114+ AnyOf < ( Ref < Transform3d > , Ref < Transform2d > ) > ,
115+ & mut GlobalTransform ,
116+ Option < & Children > ,
117+ ) ,
111118 With < ChildOf > ,
112119 > ,
113120 child_query : Query < ( Entity , Ref < ChildOf > ) , With < GlobalTransform > > ,
@@ -120,7 +127,7 @@ mod serial {
120127 |( entity, children, transform, mut global_transform) | {
121128 let changed = transform. is_changed ( ) || global_transform. is_added ( ) || orphaned_entities. binary_search ( & entity) . is_ok ( ) ;
122129 if changed {
123- * global_transform = GlobalTransform :: from ( * transform) ;
130+ * global_transform = local_to_global ( transform) ;
124131 }
125132
126133 for ( child, child_of) in child_query. iter_many ( children) {
@@ -175,7 +182,11 @@ mod serial {
175182 unsafe fn propagate_recursive (
176183 parent : & GlobalTransform ,
177184 transform_query : & Query <
178- ( Ref < Transform3d > , & mut GlobalTransform , Option < & Children > ) ,
185+ (
186+ AnyOf < ( Ref < Transform3d > , Ref < Transform2d > ) > ,
187+ & mut GlobalTransform ,
188+ Option < & Children > ,
189+ ) ,
179190 With < ChildOf > ,
180191 > ,
181192 child_query : & Query < ( Entity , Ref < ChildOf > ) , With < GlobalTransform > > ,
@@ -252,6 +263,7 @@ mod serial {
252263/// the serial version.
253264#[ cfg( feature = "std" ) ]
254265mod parallel {
266+ use super :: local_to_global;
255267 use crate :: prelude:: * ;
256268 // TODO: this implementation could be used in no_std if there are equivalents of these.
257269 use alloc:: { sync:: Arc , vec:: Vec } ;
@@ -273,7 +285,12 @@ mod parallel {
273285 pub fn propagate_parent_transforms (
274286 mut queue : Local < WorkQueue > ,
275287 mut roots : Query <
276- ( Entity , Ref < Transform3d > , & mut GlobalTransform , & Children ) ,
288+ (
289+ Entity ,
290+ AnyOf < ( Ref < Transform3d > , Ref < Transform2d > ) > ,
291+ & mut GlobalTransform ,
292+ & Children ,
293+ ) ,
277294 ( Without < ChildOf > , Changed < TransformTreeChanged > ) ,
278295 > ,
279296 nodes : NodeQuery ,
@@ -282,7 +299,7 @@ mod parallel {
282299 roots. par_iter_mut ( ) . for_each_init (
283300 || queue. local_queue . borrow_local_mut ( ) ,
284301 |outbox, ( parent, transform, mut parent_transform, children) | {
285- * parent_transform = GlobalTransform :: from ( * transform) ;
302+ * parent_transform = local_to_global ( transform) ;
286303
287304 // SAFETY: the parent entities passed into this function are taken from iterating
288305 // over the root entity query. Queries iterate over disjoint entities, preventing
@@ -460,7 +477,8 @@ mod parallel {
460477
461478 // Transform prop is expensive - this helps avoid updating entire subtrees if
462479 // the GlobalTransform is unchanged, at the cost of an added equality check.
463- global_transform. set_if_neq ( p_global_transform. mul_transform ( * transform) ) ;
480+ let child_global = local_to_global ( transform) ;
481+ global_transform. set_if_neq ( * p_global_transform * child_global) ;
464482
465483 children. map ( |children| {
466484 // Only continue propagation if the entity has children.
@@ -499,7 +517,7 @@ mod parallel {
499517 (
500518 Entity ,
501519 (
502- Ref < ' static , Transform3d > ,
520+ AnyOf < ( Ref < ' static , Transform3d > , Ref < ' static , Transform2d > ) > ,
503521 Mut < ' static , GlobalTransform > ,
504522 Ref < ' static , TransformTreeChanged > ,
505523 ) ,
@@ -557,6 +575,16 @@ mod parallel {
557575 }
558576}
559577
578+ fn local_to_global (
579+ any_of : ( Option < Ref < ' _ , Transform3d > > , Option < Ref < ' _ , Transform2d > > ) ,
580+ ) -> GlobalTransform {
581+ match any_of {
582+ ( Some ( transform_3d) , _) => GlobalTransform :: from ( * transform_3d) ,
583+ ( _, Some ( transform_2d) ) => GlobalTransform :: from ( * transform_2d) ,
584+ _ => unreachable ! ( ) ,
585+ }
586+ }
587+
560588#[ cfg( test) ]
561589mod test {
562590 use alloc:: { vec, vec:: Vec } ;
0 commit comments