1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
use std::cmp::Ordering; use std::marker::PhantomData; use std::ops::{Add, Mul, Neg, Sub}; use fnv::{FnvHashMap, FnvHashSet}; use hibitset::BitSet; use mat32; use mat4; use num_traits::Float; use specs::{Entities, Entity, Join, ReadStorage, System, Write, WriteStorage}; use specs_guided_join::GuidedJoin; use type_name; use super::{ Child, Init, LocalTransform2D, LocalTransform3D, Transform2D, Transform3D, TransformGuide, }; pub struct TransformSystem<T> { depth: FnvHashMap<Entity, usize>, children: FnvHashMap<Entity, Entity>, guide: FnvHashSet<Entity>, new: Vec<Entity>, dead: BitSet, dirty: BitSet, _marker: PhantomData<T>, } impl<T> Default for TransformSystem<T> { #[inline(always)] fn default() -> Self { TransformSystem { depth: FnvHashMap::default(), children: FnvHashMap::default(), guide: FnvHashSet::default(), new: Vec::default(), dead: BitSet::default(), dirty: BitSet::default(), _marker: PhantomData, } } } impl<T> TransformSystem<T> { #[inline(always)] pub fn new() -> Self { Self::default() } } impl<T> TransformSystem<T> { #[inline(always)] pub fn name() -> &'static str { type_name::get::<Self>() } } impl<'system, T> System<'system> for TransformSystem<T> where T: 'static + Sync + Send + Float, for<'a, 'b> &'a T: Mul<&'b T, Output = T> + Neg<Output = T> + Add<&'b T, Output = T> + Sub<&'b T, Output = T>, { type SystemData = ( Entities<'system>, Write<'system, TransformGuide>, ReadStorage<'system, Child>, WriteStorage<'system, Init>, ReadStorage<'system, LocalTransform2D<T>>, ReadStorage<'system, LocalTransform3D<T>>, WriteStorage<'system, Transform2D<T>>, WriteStorage<'system, Transform3D<T>>, ); #[inline] fn run( &mut self, ( entities, mut guide, children, mut inits, locals_2d, locals_3d, mut globals_2d, mut globals_3d, ): Self::SystemData, ) { for (entity, _, child, _) in (&*entities, &locals_2d, &children, !&inits).join() { self.children.insert(entity, child.parent()); self.new.push(entity); self.guide.insert(entity); } for (entity, _, child, _) in (&*entities, &locals_3d, &children, !&inits).join() { self.children.insert(entity, child.parent()); self.new.push(entity); self.guide.insert(entity); } for (entity, _, _) in (&*entities, &locals_2d, !&inits).join() { self.guide.insert(entity); } for (entity, _, _) in (&*entities, &locals_3d, !&inits).join() { self.guide.insert(entity); } for (entity, _) in &self.children { if let Some(child) = children.get(*entity) { if !entities.is_alive(child.parent()) || self.dead.contains(child.parent().id()) { let _ = entities.delete(*entity); self.dead.add(entity.id()); self.guide.remove(entity); } } } let needs_sort = self.new.len() != 0; for entity in self.new.drain(..) { get_depth( &mut self.depth, &self.children, self.children.get(&entity).map(|p| *p), entity, 0, ); let _ = inits.insert(entity, Init); } for (entity, local, global, _) in (&*entities, &locals_2d, &mut globals_2d, !&children).join() { if local.is_dirty() { self.dirty.add(entity.id()); global.0 = local.matrix(); local.flag(false); } } for (entity, local, global, _) in (&*entities, &locals_3d, &mut globals_3d, !&children).join() { if local.is_dirty() { self.dirty.add(entity.id()); global.0 = local.matrix(); local.flag(false); } } if needs_sort { let mut removed = Vec::new(); let new_guide: Vec<Entity> = self .guide .iter() .map(|e| *e) .filter(|e| { if entities.is_alive(*e) { removed.push(*e); true } else { false } }) .collect(); for entity in removed { self.guide.remove(&entity); } guide.set(new_guide); guide.as_slice_mut().sort_by(|a, b| { if let Some(a) = self.depth.get(a) { if let Some(b) = self.depth.get(b) { a.cmp(b) } else { Ordering::Equal } } else { Ordering::Equal } }); } for (entity, local, child) in (&*entities, &locals_2d, &children).guided_join(guide.as_slice()) { let mut global_matrix = local.matrix(); if let Some(parent_global) = globals_2d.get(child.parent()) { let local_matrix = global_matrix.clone(); mat32::mul(&mut global_matrix, &parent_global.0, &local_matrix); } else if let Some(parent_global) = globals_3d.get(child.parent()) { let local_matrix = global_matrix.clone(); let mut parent_global_2d = mat32::new_identity(); mat32::set_mat4(&mut parent_global_2d, &parent_global.0); mat32::mul(&mut global_matrix, &parent_global_2d, &local_matrix); } let needs_global = if let Some(global_2d) = globals_2d.get_mut(entity) { global_2d.0 = global_matrix; false } else { true }; if needs_global { let _ = globals_2d.insert(entity, Transform2D::from(global_matrix)); } local.flag(false); child.flag(false); } for (entity, local, child) in (&*entities, &locals_3d, &children).guided_join(guide.as_slice()) { let mut global_matrix = local.matrix(); if let Some(parent_global) = globals_3d.get(child.parent()) { let local_matrix = global_matrix.clone(); mat4::mul(&mut global_matrix, &parent_global.0, &local_matrix); } else if let Some(parent_global) = globals_2d.get(child.parent()) { let local_matrix = global_matrix.clone(); let mut parent_global_3d = mat4::new_identity(); mat4::set_mat32(&mut parent_global_3d, &parent_global.0); mat4::mul(&mut global_matrix, &parent_global_3d, &local_matrix); } let needs_global = if let Some(global_3d) = globals_3d.get_mut(entity) { global_3d.0 = global_matrix; false } else { true }; if needs_global { let _ = globals_3d.insert(entity, Transform3D::from(global_matrix)); } local.flag(false); child.flag(false); } self.dead.clear(); self.dirty.clear(); } } #[inline] fn get_depth<'a>( depth: &'a mut FnvHashMap<Entity, usize>, children: &'a FnvHashMap<Entity, Entity>, parent: Option<Entity>, entity: Entity, current: usize, ) -> usize { if let Some(depth) = depth.get(&entity) { return *depth; } if let Some(parent) = parent { depth.insert(parent, current); get_depth( depth, children, children.get(&parent).map(|p| *p), entity, current + 1, ) } else { depth.insert(entity, current); current } } #[test] fn test_transform_system_name() { assert_eq!( TransformSystem::<f32>::name(), "transform_system::TransformSystem<f32>" ); struct Example; assert_eq!( TransformSystem::<Example>::name(), "transform_system::TransformSystem<transform_system::test_transform_system_name::Example>" ); }