refactor(spatial): store parent as Rc<Spatial>
This commit is contained in:
@@ -4,19 +4,19 @@ use anyhow::{anyhow, bail, ensure, Result};
|
|||||||
use glam::Mat4;
|
use glam::Mat4;
|
||||||
use libstardustxr::{flex_to_quat, flex_to_vec3};
|
use libstardustxr::{flex_to_quat, flex_to_vec3};
|
||||||
use rccell::{RcCell, WeakCell};
|
use rccell::{RcCell, WeakCell};
|
||||||
use std::cell::Cell;
|
use std::cell::{Cell, RefCell};
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
pub struct Spatial<'a> {
|
pub struct Spatial<'a> {
|
||||||
node: WeakCell<Node<'a>>,
|
node: WeakCell<Node<'a>>,
|
||||||
parent: WeakCell<Node<'a>>,
|
parent: RefCell<Option<Rc<Spatial<'a>>>>,
|
||||||
transform: Cell<Mat4>,
|
transform: Cell<Mat4>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Spatial<'a> {
|
impl<'a> Spatial<'a> {
|
||||||
pub fn add_to(
|
pub fn add_to(
|
||||||
node: RcCell<Node<'a>>,
|
node: RcCell<Node<'a>>,
|
||||||
parent: WeakCell<Node<'a>>,
|
parent: Option<Rc<Spatial<'a>>>,
|
||||||
transform: Mat4,
|
transform: Mat4,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
if node.borrow_mut().spatial.is_none() {
|
if node.borrow_mut().spatial.is_none() {
|
||||||
@@ -24,7 +24,7 @@ impl<'a> Spatial<'a> {
|
|||||||
}
|
}
|
||||||
let spatial = Spatial {
|
let spatial = Spatial {
|
||||||
node: node.downgrade(),
|
node: node.downgrade(),
|
||||||
parent,
|
parent: RefCell::new(parent),
|
||||||
transform: Cell::new(transform),
|
transform: Cell::new(transform),
|
||||||
};
|
};
|
||||||
node.borrow_mut()
|
node.borrow_mut()
|
||||||
@@ -61,10 +61,8 @@ impl<'a> Spatial<'a> {
|
|||||||
self.transform.get()
|
self.transform.get()
|
||||||
}
|
}
|
||||||
pub fn global_transform(&self) -> Mat4 {
|
pub fn global_transform(&self) -> Mat4 {
|
||||||
match self.parent.upgrade() {
|
match self.parent.borrow().clone() {
|
||||||
Some(value) => {
|
Some(value) => value.global_transform() * self.transform.get(),
|
||||||
value.borrow().spatial.as_ref().unwrap().global_transform() * self.transform.get()
|
|
||||||
}
|
|
||||||
None => self.transform.get(),
|
None => self.transform.get(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -97,6 +95,10 @@ pub fn create_spatial_flex(_node: &Node, calling_client: Rc<Client>, data: &[u8]
|
|||||||
flex_vec.idx(0).get_str()?,
|
flex_vec.idx(0).get_str()?,
|
||||||
true,
|
true,
|
||||||
);
|
);
|
||||||
|
let parent = calling_client
|
||||||
|
.get_scenegraph()
|
||||||
|
.get_node(flex_vec.idx(1).as_str())
|
||||||
|
.and_then(|node| node.borrow().spatial.clone());
|
||||||
let transform = Mat4::from_scale_rotation_translation(
|
let transform = Mat4::from_scale_rotation_translation(
|
||||||
flex_to_vec3!(flex_vec.idx(4))
|
flex_to_vec3!(flex_vec.idx(4))
|
||||||
.ok_or_else(|| anyhow!("Scale not found"))?
|
.ok_or_else(|| anyhow!("Scale not found"))?
|
||||||
@@ -109,6 +111,6 @@ pub fn create_spatial_flex(_node: &Node, calling_client: Rc<Client>, data: &[u8]
|
|||||||
.into(),
|
.into(),
|
||||||
);
|
);
|
||||||
let spatial_rc = calling_client.get_scenegraph().add_node(spatial);
|
let spatial_rc = calling_client.get_scenegraph().add_node(spatial);
|
||||||
Spatial::add_to(spatial_rc, WeakCell::new(), transform)?;
|
Spatial::add_to(spatial_rc, parent, transform)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user