Skip to content

Commit

Permalink
fix(spatial): add spatial to children registry of parent on add_to
Browse files Browse the repository at this point in the history
  • Loading branch information
technobaboo committed Nov 25, 2023
1 parent b4acbdb commit 872505f
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/nodes/spatial/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Spatial> = Registry::new();
Expand All @@ -29,7 +30,7 @@ pub struct Spatial {
pub(super) transform: Mutex<Mat4>,
zone: Mutex<Weak<Zone>>,
children: Registry<Spatial>,
pub(super) bounding_box_calc: OnceLock<fn(&Node) -> Bounds>,
pub(super) bounding_box_calc: OnceCell<fn(&Node) -> Bounds>,
}

impl Spatial {
Expand All @@ -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(
Expand All @@ -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);
Expand All @@ -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)
}
Expand All @@ -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()
Expand Down

0 comments on commit 872505f

Please sign in to comment.