refactor(scenegraph+node): 1 node type with components

This commit is contained in:
Nova
2022-06-03 22:33:40 -04:00
parent be9296588e
commit efe870c193
3 changed files with 46 additions and 50 deletions

View File

@@ -1,5 +1,5 @@
use crate::core::client::Client; use crate::core::client::Client;
use crate::nodes::spatial::Spatial; use crate::nodes::core::Node;
use anyhow::Result; use anyhow::Result;
use libstardustxr::scenegraph; use libstardustxr::scenegraph;
use libstardustxr::scenegraph::ScenegraphError; use libstardustxr::scenegraph::ScenegraphError;
@@ -8,7 +8,7 @@ use std::collections::HashMap;
pub struct Scenegraph<'a> { pub struct Scenegraph<'a> {
client: WeakCell<Client<'a>>, client: WeakCell<Client<'a>>,
pub spatial_nodes: HashMap<String, RcCell<Spatial<'a>>>, pub nodes: HashMap<String, RcCell<Node<'a>>>,
} }
impl<'a> Scenegraph<'a> { impl<'a> Scenegraph<'a> {
@@ -17,7 +17,7 @@ impl<'a> Scenegraph<'a> {
// hmd: Spatial::new(Some(client), "/hmd", Default::default()), // hmd: Spatial::new(Some(client), "/hmd", Default::default()),
Scenegraph { Scenegraph {
client: client.downgrade(), client: client.downgrade(),
spatial_nodes: HashMap::new(), nodes: HashMap::new(),
} }
} }
} }
@@ -26,18 +26,17 @@ impl<'a> Default for Scenegraph<'a> {
fn default() -> Self { fn default() -> Self {
Scenegraph { Scenegraph {
client: WeakCell::new(), client: WeakCell::new(),
spatial_nodes: HashMap::new(), nodes: HashMap::new(),
} }
} }
} }
impl<'a> scenegraph::Scenegraph for Scenegraph<'a> { impl<'a> scenegraph::Scenegraph for Scenegraph<'a> {
fn send_signal(&self, path: &str, method: &str, data: &[u8]) -> Result<(), ScenegraphError> { fn send_signal(&self, path: &str, method: &str, data: &[u8]) -> Result<(), ScenegraphError> {
self.spatial_nodes self.nodes
.get(path) .get(path)
.ok_or(ScenegraphError::NodeNotFound)? .ok_or(ScenegraphError::NodeNotFound)?
.borrow() .borrow()
.node
.send_local_signal(self.client.upgrade().unwrap(), method, data) .send_local_signal(self.client.upgrade().unwrap(), method, data)
.map_err(|_| ScenegraphError::MethodNotFound) .map_err(|_| ScenegraphError::MethodNotFound)
} }
@@ -47,11 +46,10 @@ impl<'a> scenegraph::Scenegraph for Scenegraph<'a> {
method: &str, method: &str,
data: &[u8], data: &[u8],
) -> Result<Vec<u8>, ScenegraphError> { ) -> Result<Vec<u8>, ScenegraphError> {
self.spatial_nodes self.nodes
.get(path) .get(path)
.ok_or(ScenegraphError::NodeNotFound)? .ok_or(ScenegraphError::NodeNotFound)?
.borrow() .borrow()
.node
.execute_local_method(self.client.upgrade().unwrap(), method, data) .execute_local_method(self.client.upgrade().unwrap(), method, data)
.map_err(|_| ScenegraphError::MethodNotFound) .map_err(|_| ScenegraphError::MethodNotFound)
} }

View File

@@ -1,4 +1,5 @@
use crate::core::client::Client; use crate::core::client::Client;
use crate::nodes::spatial::Spatial;
use anyhow::{anyhow, ensure, Result}; use anyhow::{anyhow, ensure, Result};
use rccell::{RcCell, WeakCell}; use rccell::{RcCell, WeakCell};
use std::{collections::HashMap, vec::Vec}; use std::{collections::HashMap, vec::Vec};
@@ -13,6 +14,8 @@ pub struct Node<'a> {
local_signals: HashMap<String, Signal<'a>>, local_signals: HashMap<String, Signal<'a>>,
local_methods: HashMap<String, Method<'a>>, local_methods: HashMap<String, Method<'a>>,
destroyable: bool, destroyable: bool,
pub spatial: Option<Spatial<'a>>,
} }
impl<'a> Node<'a> { impl<'a> Node<'a> {
@@ -41,6 +44,7 @@ impl<'a> Node<'a> {
local_signals: HashMap::new(), local_signals: HashMap::new(),
local_methods: HashMap::new(), local_methods: HashMap::new(),
destroyable, destroyable,
spatial: None,
}) })
} }
@@ -54,9 +58,11 @@ impl<'a> Node<'a> {
method: &str, method: &str,
data: &[u8], data: &[u8],
) -> Result<()> { ) -> Result<()> {
self.local_signals let signal = self
.local_signals
.get(method) .get(method)
.ok_or_else(|| anyhow!("Signal {} not found", method))?(calling_client, data) .ok_or_else(|| anyhow!("Signal {} not found", method))?;
signal(calling_client, data)
} }
pub fn execute_local_method( pub fn execute_local_method(
&self, &self,

View File

@@ -1,68 +1,58 @@
use super::core::Node; use super::core::Node;
use crate::core::client::Client; use crate::core::client::Client;
use anyhow::{anyhow, Result}; use anyhow::{anyhow, ensure, Result};
use glam::{Mat4, Quat, Vec3}; use glam::{Mat4, Quat, Vec3};
use libstardustxr::{flex_to_quat, flex_to_vec3}; use libstardustxr::{flex_to_quat, flex_to_vec3};
use rccell::{RcCell, WeakCell}; use rccell::{RcCell, WeakCell};
pub struct Spatial<'a> { pub struct Spatial<'a> {
pub node: Node<'a>, node: WeakCell<Node<'a>>,
parent: WeakCell<Spatial<'a>>, parent: WeakCell<Node<'a>>,
transform: Mat4, transform: Mat4,
} }
impl<'a> Spatial<'a> { impl<'a> Spatial<'a> {
pub fn new( pub fn new(node: RcCell<Node<'a>>, transform: Mat4) -> Self {
client: WeakCell<Client<'a>>, let spatial = Spatial {
path: &str, node: node.downgrade(),
transform: Mat4,
) -> Result<WeakCell<Self>> {
let spatial = RcCell::new(Spatial {
node: Node::from_path(client.clone(), path, true).unwrap(),
parent: WeakCell::new(), parent: WeakCell::new(),
transform, transform,
}); };
let weak_spatial = spatial.downgrade(); let node_captured = node.clone();
let captured_spatial = weak_spatial.clone(); node.borrow_mut().add_local_signal(
let captured_client = client.clone();
// node_add_local_signal!(node, "setTransform", Spatial::set_transform_components);
spatial.borrow_mut().node.add_local_signal(
"setTransform", "setTransform",
Box::new(move |calling_client, data| { Box::new(move |calling_client, data| {
let root = flexbuffers::Reader::get_root(data)?; let root = flexbuffers::Reader::get_root(data)?;
let flex_vec = root.get_vector()?; let flex_vec = root.get_vector()?;
let spatial = captured_spatial // let node = node.
.upgrade() let client = node_captured
.ok_or(anyhow!("Invalid spatial"))?; .borrow()
let client = captured_client.upgrade().ok_or(anyhow!("Invalid client"))?; .get_client()
let other_spatial = client .ok_or(anyhow!("Node somehow has no client!"))?;
let other_spatial = calling_client
.borrow() .borrow()
.get_scenegraph() .get_scenegraph()
.spatial_nodes .nodes
.get(flex_vec.idx(0).as_str()) .get(flex_vec.idx(0).as_str())
.ok_or(anyhow!("Spatial not found"))? .ok_or(anyhow!("Spatial node not found"))?
.clone(); .clone();
ensure!(
other_spatial.borrow().spatial.is_some(),
"Node is not a Spatial!"
);
let pos = flex_to_vec3!(flex_vec.idx(1)); let pos = flex_to_vec3!(flex_vec.idx(1));
let rot = flex_to_quat!(flex_vec.idx(2)); let rot = flex_to_quat!(flex_vec.idx(2));
let scl = flex_to_vec3!(flex_vec.idx(3)); let scl = flex_to_vec3!(flex_vec.idx(3));
spatial.borrow_mut().set_transform_components( node_captured
client, .borrow_mut()
other_spatial, .spatial
pos.into(), .as_mut()
rot, .unwrap()
scl, .set_transform_components(client, other_spatial, pos.into(), rot, scl);
);
Ok(()) Ok(())
}), }),
); );
client spatial
.upgrade()
.unwrap()
.borrow_mut()
.get_scenegraph_mut()
.spatial_nodes
.insert(path.to_string(), spatial);
Ok(weak_spatial)
} }
pub fn local_transform(&self) -> Mat4 { pub fn local_transform(&self) -> Mat4 {
@@ -70,7 +60,9 @@ impl<'a> Spatial<'a> {
} }
pub fn global_transform(&self) -> Mat4 { pub fn global_transform(&self) -> Mat4 {
match self.parent.upgrade() { match self.parent.upgrade() {
Some(value) => value.borrow().global_transform() * self.transform, Some(value) => {
value.borrow().spatial.as_ref().unwrap().global_transform() * self.transform
}
None => self.transform, None => self.transform,
} }
} }
@@ -78,7 +70,7 @@ impl<'a> Spatial<'a> {
pub fn set_transform_components( pub fn set_transform_components(
&mut self, &mut self,
calling_client: RcCell<Client>, calling_client: RcCell<Client>,
relative_space: RcCell<Spatial>, relative_space: RcCell<Node>,
pos: Option<mint::Vector3<f32>>, pos: Option<mint::Vector3<f32>>,
rot: Option<mint::Quaternion<f32>>, rot: Option<mint::Quaternion<f32>>,
scl: Option<mint::Vector3<f32>>, scl: Option<mint::Vector3<f32>>,