From 872505f5966d8d2dfc7934c151e0b7e44b7a5c4c Mon Sep 17 00:00:00 2001 From: Nova Date: Sat, 25 Nov 2023 10:43:08 -0500 Subject: [PATCH] fix(spatial): add spatial to children registry of parent on add_to --- src/nodes/spatial/mod.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/nodes/spatial/mod.rs b/src/nodes/spatial/mod.rs index becf2d56..0eb565e3 100644 --- a/src/nodes/spatial/mod.rs +++ b/src/nodes/spatial/mod.rs @@ -9,13 +9,14 @@ use color_eyre::eyre::{ensure, eyre, Result}; use glam::{vec3a, Mat4, Quat}; use mint::Vector3; use nanoid::nanoid; +use once_cell::sync::OnceCell; use parking_lot::Mutex; use serde::Deserialize; use stardust_xr::schemas::flex::{deserialize, serialize}; use stardust_xr::values::Transform; use std::fmt::Debug; use std::ptr; -use std::sync::{Arc, OnceLock, Weak}; +use std::sync::{Arc, Weak}; use stereokit::{bounds_grow_to_fit_box, Bounds}; static ZONEABLE_REGISTRY: Registry = Registry::new(); @@ -29,7 +30,7 @@ pub struct Spatial { pub(super) transform: Mutex, zone: Mutex>, children: Registry, - pub(super) bounding_box_calc: OnceLock Bounds>, + pub(super) bounding_box_calc: OnceCell Bounds>, } impl Spatial { @@ -43,7 +44,7 @@ impl Spatial { transform: Mutex::new(transform), zone: Mutex::new(Weak::new()), children: Registry::new(), - bounding_box_calc: OnceLock::default(), + bounding_box_calc: OnceCell::default(), }) } pub fn add_to( @@ -56,7 +57,7 @@ impl Spatial { node.spatial.get().is_none(), "Internal: Node already has a Spatial aspect!" ); - let spatial = Spatial::new(Arc::downgrade(node), parent, transform); + let spatial = Spatial::new(Arc::downgrade(node), parent.clone(), transform); node.add_local_method("get_bounding_box", Spatial::get_bounding_box_flex); node.add_local_method("get_transform", Spatial::get_transform_flex); node.add_local_signal("set_transform", Spatial::set_transform_flex); @@ -72,6 +73,9 @@ impl Spatial { if zoneable { ZONEABLE_REGISTRY.add_raw(&spatial); } + if let Some(parent) = parent { + parent.children.add_raw(&spatial); + } let _ = node.spatial.set(spatial.clone()); Ok(spatial) } @@ -88,7 +92,9 @@ impl Spatial { // the output bounds are probably way bigger than they need to be pub fn get_bounding_box(&self) -> Bounds { - let Some(node) = self.node() else {return Bounds::default()}; + let Some(node) = self.node() else { + return Bounds::default(); + }; let mut bounds = self .bounding_box_calc .get()