feat(spatial): setTransform local signal

This commit is contained in:
Nova
2022-06-02 16:24:47 -04:00
parent 09588ab31d
commit be9296588e
4 changed files with 52 additions and 25 deletions

View File

@@ -9,8 +9,8 @@ version = "0.9.0"
libstardustxr = {path = "../libstardustxr-rs"}
anyhow = "1.0.57"
ctrlc = "3.2.2"
euler = {version = "0.4.0", features = ["mint"]}
flexbuffers = "2.0.0"
glam = {version = "0.20.5", features = ["mint"]}
mint = "0.5.9"
mio = {version = "0.8.3", features = ["net", "os-poll", "os-ext"]}
rccell = "0.1.3"

View File

@@ -3,10 +3,11 @@ use crate::nodes::spatial::Spatial;
use anyhow::Result;
use libstardustxr::scenegraph;
use libstardustxr::scenegraph::ScenegraphError;
use rccell::RcCell;
use rccell::{RcCell, WeakCell};
use std::collections::HashMap;
pub struct Scenegraph<'a> {
client: WeakCell<Client<'a>>,
pub spatial_nodes: HashMap<String, RcCell<Spatial<'a>>>,
}
@@ -15,6 +16,7 @@ impl<'a> Scenegraph<'a> {
// root: Spatial::new(Some(client), "/", Default::default()),
// hmd: Spatial::new(Some(client), "/hmd", Default::default()),
Scenegraph {
client: client.downgrade(),
spatial_nodes: HashMap::new(),
}
}
@@ -23,6 +25,7 @@ impl<'a> Scenegraph<'a> {
impl<'a> Default for Scenegraph<'a> {
fn default() -> Self {
Scenegraph {
client: WeakCell::new(),
spatial_nodes: HashMap::new(),
}
}
@@ -35,7 +38,7 @@ impl<'a> scenegraph::Scenegraph for Scenegraph<'a> {
.ok_or(ScenegraphError::NodeNotFound)?
.borrow()
.node
.send_local_signal(method, data)
.send_local_signal(self.client.upgrade().unwrap(), method, data)
.map_err(|_| ScenegraphError::MethodNotFound)
}
fn execute_method(
@@ -49,7 +52,7 @@ impl<'a> scenegraph::Scenegraph for Scenegraph<'a> {
.ok_or(ScenegraphError::NodeNotFound)?
.borrow()
.node
.execute_local_method(method, data)
.execute_local_method(self.client.upgrade().unwrap(), method, data)
.map_err(|_| ScenegraphError::MethodNotFound)
}
}

View File

@@ -3,8 +3,8 @@ use anyhow::{anyhow, ensure, Result};
use rccell::{RcCell, WeakCell};
use std::{collections::HashMap, vec::Vec};
pub type Signal<'a> = Box<dyn Fn(&[u8]) -> Result<()> + 'a>;
pub type Method<'a> = Box<dyn Fn(&[u8]) -> Result<Vec<u8>> + 'a>;
pub type Signal<'a> = Box<dyn Fn(RcCell<Client>, &[u8]) -> Result<()> + 'a>;
pub type Method<'a> = Box<dyn Fn(RcCell<Client>, &[u8]) -> Result<Vec<u8>> + 'a>;
pub struct Node<'a> {
client: WeakCell<Client<'a>>,
@@ -48,15 +48,25 @@ impl<'a> Node<'a> {
self.local_signals.insert(method.to_string(), signal);
}
pub fn send_local_signal(&self, method: &str, data: &[u8]) -> Result<()> {
pub fn send_local_signal(
&self,
calling_client: RcCell<Client>,
method: &str,
data: &[u8],
) -> Result<()> {
self.local_signals
.get(method)
.ok_or_else(|| anyhow!("Signal {} not found", method))?(data)
.ok_or_else(|| anyhow!("Signal {} not found", method))?(calling_client, data)
}
pub fn execute_local_method(&self, method: &str, data: &[u8]) -> Result<Vec<u8>> {
pub fn execute_local_method(
&self,
calling_client: RcCell<Client>,
method: &str,
data: &[u8],
) -> Result<Vec<u8>> {
self.local_methods
.get(method)
.ok_or_else(|| anyhow!("Method {} not found", method))?(data)
.ok_or_else(|| anyhow!("Method {} not found", method))?(calling_client, data)
}
pub fn send_remote_signal(&self, method: &str, data: &[u8]) -> Result<()> {
self.get_client()

View File

@@ -1,22 +1,21 @@
use super::core::Node;
use crate::core::client::Client;
use anyhow::{anyhow, Result};
// use euler::Mat4;
use glam::{Mat4, Quat, Vec3};
use libstardustxr::{flex_to_quat, flex_to_vec3};
use mint::RowMatrix4;
use rccell::{RcCell, WeakCell};
pub struct Spatial<'a> {
pub node: Node<'a>,
parent: WeakCell<Spatial<'a>>,
transform: RowMatrix4<f32>,
transform: Mat4,
}
impl<'a> Spatial<'a> {
pub fn new(
client: WeakCell<Client<'a>>,
path: &str,
transform: RowMatrix4<f32>,
transform: Mat4,
) -> Result<WeakCell<Self>> {
let spatial = RcCell::new(Spatial {
node: Node::from_path(client.clone(), path, true).unwrap(),
@@ -26,9 +25,10 @@ impl<'a> Spatial<'a> {
let weak_spatial = spatial.downgrade();
let captured_spatial = weak_spatial.clone();
let captured_client = client.clone();
// node_add_local_signal!(node, "setTransform", Spatial::set_transform_components);
spatial.borrow_mut().node.add_local_signal(
"setTransform",
Box::new(move |data| {
Box::new(move |calling_client, data| {
let root = flexbuffers::Reader::get_root(data)?;
let flex_vec = root.get_vector()?;
let spatial = captured_spatial
@@ -39,11 +39,19 @@ impl<'a> Spatial<'a> {
.borrow()
.get_scenegraph()
.spatial_nodes
.get(flex_vec.idx(1).as_str())
.ok_or(anyhow!("Spatial not found"))?;
.get(flex_vec.idx(0).as_str())
.ok_or(anyhow!("Spatial not found"))?
.clone();
let pos = flex_to_vec3!(flex_vec.idx(1));
let rot = flex_to_quat!(flex_vec.idx(2));
let scl = flex_to_vec3!(flex_vec.idx(3));
spatial.borrow_mut().set_transform_components(
client,
other_spatial,
pos.into(),
rot,
scl,
);
Ok(())
}),
);
@@ -57,18 +65,24 @@ impl<'a> Spatial<'a> {
Ok(weak_spatial)
}
pub fn local_transform(&self) -> RowMatrix4<f32> {
pub fn local_transform(&self) -> Mat4 {
self.transform
}
pub fn global_transform(&self) -> RowMatrix4<f32> {
todo!()
// match self.parent.upgrade() {
// Some(value) => Mat4::from(value.borrow().global_transform()) * self.transform,
// None => self.transform,
// }
pub fn global_transform(&self) -> Mat4 {
match self.parent.upgrade() {
Some(value) => value.borrow().global_transform() * self.transform,
None => self.transform,
}
}
pub fn set_transform_components(&mut self) {
pub fn set_transform_components(
&mut self,
calling_client: RcCell<Client>,
relative_space: RcCell<Spatial>,
pos: Option<mint::Vector3<f32>>,
rot: Option<mint::Quaternion<f32>>,
scl: Option<mint::Vector3<f32>>,
) {
todo!()
}