oxygengine_composite_renderer/system/
transform.rs1use crate::{component::CompositeTransform, math::Mat2d, resource::CompositeTransformCache};
2use core::ecs::{
3 hierarchy::{Hierarchy, Parent},
4 Comp, Entity, Universe, World, WorldRef,
5};
6
7pub type CompositeTransformSystemResources<'a> = (
8 WorldRef,
9 &'a Hierarchy,
10 &'a mut CompositeTransformCache,
11 Comp<&'a Parent>,
12 Comp<&'a CompositeTransform>,
13);
14
15pub fn composite_transform_system(universe: &mut Universe) {
16 let (world, hierarchy, mut cache, ..) =
17 universe.query_resources::<CompositeTransformSystemResources>();
18
19 cache.clear();
20 for (entity, transform) in world
21 .query::<&CompositeTransform>()
22 .without::<&Parent>()
23 .iter()
24 {
25 let mat = transform.matrix();
26 cache.insert(entity, mat);
27 if let Some(children) = hierarchy.children(entity) {
28 for child in children {
29 if child != entity {
30 add_matrix(child, &world, mat, &hierarchy, &mut cache);
31 }
32 }
33 }
34 }
35}
36
37fn add_matrix(
38 child: Entity,
39 world: &World,
40 parent_matrix: Mat2d,
41 hierarchy: &Hierarchy,
42 cache: &mut CompositeTransformCache,
43) {
44 if let Ok(transform) = unsafe { world.get_unchecked::<&CompositeTransform>(child) } {
45 let mat = parent_matrix * transform.matrix();
46 cache.insert(child, mat);
47 if let Some(children) = hierarchy.children(child) {
48 for child in children {
49 add_matrix(child, world, mat, hierarchy, cache);
50 }
51 }
52 }
53}