feat: basic spatial closure
This commit is contained in:
@@ -9,8 +9,10 @@ version = "0.9.0"
|
|||||||
libstardustxr = {path = "../libstardustxr-rs"}
|
libstardustxr = {path = "../libstardustxr-rs"}
|
||||||
anyhow = "1.0.57"
|
anyhow = "1.0.57"
|
||||||
ctrlc = "3.2.2"
|
ctrlc = "3.2.2"
|
||||||
|
euler = {version = "0.4.0", features = ["mint"]}
|
||||||
|
flexbuffers = "2.0.0"
|
||||||
|
mint = "0.5.9"
|
||||||
mio = {version = "0.8.3", features = ["net", "os-poll", "os-ext"]}
|
mio = {version = "0.8.3", features = ["net", "os-poll", "os-ext"]}
|
||||||
rccell = "0.1.3"
|
rccell = "0.1.3"
|
||||||
slab = "0.4.6"
|
slab = "0.4.6"
|
||||||
thiserror = "1.0.31"
|
thiserror = "1.0.31"
|
||||||
vek = "0.15.8"
|
|
||||||
|
|||||||
@@ -1,17 +1,19 @@
|
|||||||
use crate::core::client::Client;
|
use crate::core::client::Client;
|
||||||
|
use crate::core::scenegraph::Scenegraph;
|
||||||
use anyhow::{anyhow, ensure, Result};
|
use anyhow::{anyhow, ensure, Result};
|
||||||
use libstardustxr::messenger::Messenger;
|
use libstardustxr::messenger::Messenger;
|
||||||
use std::{collections::HashMap, rc::Weak, vec::Vec};
|
use std::{collections::HashMap, rc::Weak, vec::Vec};
|
||||||
|
|
||||||
pub type Signal<'a> = dyn Fn(&[u8]) + 'a;
|
pub type Signal<'a> = Box<dyn Fn(&[u8]) -> Result<()> + 'a>;
|
||||||
pub type Method<'a> = dyn Fn(&[u8]) -> Vec<u8> + 'a;
|
pub type Method<'a> = Box<dyn Fn(&[u8]) -> Result<Vec<u8>> + 'a>;
|
||||||
|
|
||||||
pub struct Node<'a> {
|
pub struct Node<'a> {
|
||||||
path: String,
|
path: String,
|
||||||
trailing_slash_pos: usize,
|
trailing_slash_pos: usize,
|
||||||
messenger: Weak<Messenger<'a>>,
|
messenger: Weak<Messenger<'a>>,
|
||||||
local_signals: HashMap<String, Box<Signal<'a>>>,
|
scenegraph: Option<&'a Scenegraph<'a>>,
|
||||||
local_methods: HashMap<String, Box<Method<'a>>>,
|
local_signals: HashMap<String, Signal<'a>>,
|
||||||
|
local_methods: HashMap<String, Method<'a>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Node<'a> {
|
impl<'a> Node<'a> {
|
||||||
@@ -22,7 +24,11 @@ impl<'a> Node<'a> {
|
|||||||
self.path.as_str()
|
self.path.as_str()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_path(client: Option<&Client<'a>>, path: &str) -> Result<Node<'a>> {
|
pub fn from_path(
|
||||||
|
client: Option<&Client<'a>>,
|
||||||
|
path: &str,
|
||||||
|
destroyable: bool,
|
||||||
|
) -> Result<Node<'a>> {
|
||||||
ensure!(path.starts_with('/'), "Invalid path {}", path);
|
ensure!(path.starts_with('/'), "Invalid path {}", path);
|
||||||
let mut weak_messenger = Weak::default();
|
let mut weak_messenger = Weak::default();
|
||||||
if let Some(c) = client.as_ref() {
|
if let Some(c) = client.as_ref() {
|
||||||
@@ -34,22 +40,25 @@ impl<'a> Node<'a> {
|
|||||||
.rfind('/')
|
.rfind('/')
|
||||||
.ok_or_else(|| anyhow!("Invalid path {}", path))?,
|
.ok_or_else(|| anyhow!("Invalid path {}", path))?,
|
||||||
messenger: weak_messenger,
|
messenger: weak_messenger,
|
||||||
|
scenegraph: client.as_ref().map(|f| f.scenegraph.as_ref().unwrap()),
|
||||||
local_signals: HashMap::new(),
|
local_signals: HashMap::new(),
|
||||||
local_methods: HashMap::new(),
|
local_methods: HashMap::new(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn add_local_signal(&mut self, method: &str, signal: Signal<'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, method: &str, data: &[u8]) -> Result<()> {
|
||||||
self.local_signals
|
self.local_signals
|
||||||
.get(method)
|
.get(method)
|
||||||
.ok_or_else(|| anyhow!("Signal {} not found", method))?(data);
|
.ok_or_else(|| anyhow!("Signal {} not found", method))?(data)
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
pub fn execute_local_method(&self, method: &str, data: &[u8]) -> Result<Vec<u8>> {
|
pub fn execute_local_method(&self, method: &str, data: &[u8]) -> Result<Vec<u8>> {
|
||||||
Ok(self
|
self.local_methods
|
||||||
.local_methods
|
|
||||||
.get(method)
|
.get(method)
|
||||||
.ok_or_else(|| anyhow!("Method {} not found", method))?(data))
|
.ok_or_else(|| anyhow!("Method {} not found", method))?(data)
|
||||||
}
|
}
|
||||||
pub fn send_remote_signal(&self, method: &str, data: &[u8]) -> Result<()> {
|
pub fn send_remote_signal(&self, method: &str, data: &[u8]) -> Result<()> {
|
||||||
self.messenger
|
self.messenger
|
||||||
|
|||||||
@@ -1,44 +1,73 @@
|
|||||||
use super::core::Node;
|
use super::core::Node;
|
||||||
use crate::core::client::Client;
|
use crate::core::client::Client;
|
||||||
|
use anyhow::{anyhow, Result};
|
||||||
|
use euler::Mat4;
|
||||||
|
use libstardustxr::{flex_to_quat, flex_to_vec3};
|
||||||
|
use mint::RowMatrix4;
|
||||||
use rccell::{RcCell, WeakCell};
|
use rccell::{RcCell, WeakCell};
|
||||||
use vek::mat::repr_c::row_major::Mat4;
|
|
||||||
|
|
||||||
pub struct Spatial<'a> {
|
pub struct Spatial<'a> {
|
||||||
pub node: Node<'a>,
|
pub node: Node<'a>,
|
||||||
parent: WeakCell<Spatial<'a>>,
|
parent: WeakCell<Spatial<'a>>,
|
||||||
transform: Mat4<f32>,
|
transform: RowMatrix4<f32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Spatial<'a> {
|
impl<'a> Spatial<'a> {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
client: Option<&mut Client<'a>>,
|
client: Option<&mut Client<'a>>,
|
||||||
path: &str,
|
path: &str,
|
||||||
transform: Mat4<f32>,
|
transform: RowMatrix4<f32>,
|
||||||
) -> WeakCell<Self> {
|
) -> Result<WeakCell<Self>> {
|
||||||
let spatial = RcCell::new(Spatial {
|
let mut spatial = Spatial {
|
||||||
node: Node::from_path(client.as_deref(), path).unwrap(),
|
node: Node::from_path(client.as_deref(), path, true).unwrap(),
|
||||||
parent: WeakCell::new(),
|
parent: WeakCell::new(),
|
||||||
transform,
|
transform,
|
||||||
});
|
};
|
||||||
let weak_spatial = spatial.downgrade();
|
let spatial_cell = RcCell::new(spatial);
|
||||||
client
|
let weak_spatial = spatial_cell.downgrade();
|
||||||
.unwrap()
|
if let Some(client) = client {
|
||||||
.scenegraph
|
let weak_spatial = weak_spatial.clone();
|
||||||
.as_mut()
|
spatial.node.add_local_signal(
|
||||||
.unwrap()
|
"setTransform",
|
||||||
.spatial_nodes
|
Box::new(|data| {
|
||||||
.insert(path.to_string(), spatial);
|
let root = flexbuffers::Reader::get_root(data).unwrap();
|
||||||
weak_spatial
|
let flex_vec = root.get_vector().unwrap();
|
||||||
|
let spatial = client
|
||||||
|
.scenegraph
|
||||||
|
.as_ref()
|
||||||
|
.unwrap()
|
||||||
|
.spatial_nodes
|
||||||
|
.get(flex_vec.idx(1).as_str())
|
||||||
|
.ok_or(anyhow!("Spatial not found"))?;
|
||||||
|
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));
|
||||||
|
Ok(())
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
client
|
||||||
|
.scenegraph
|
||||||
|
.as_mut()
|
||||||
|
.unwrap()
|
||||||
|
.spatial_nodes
|
||||||
|
.insert(path.to_string(), spatial_cell);
|
||||||
|
}
|
||||||
|
Ok(weak_spatial)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn local_transform(&self) -> Mat4<f32> {
|
pub fn local_transform(&self) -> RowMatrix4<f32> {
|
||||||
self.transform
|
self.transform
|
||||||
}
|
}
|
||||||
pub fn global_transform(&self) -> Mat4<f32> {
|
pub fn global_transform(&self) -> RowMatrix4<f32> {
|
||||||
match self.parent.upgrade() {
|
todo!()
|
||||||
Some(value) => value.borrow().global_transform() * self.transform,
|
// match self.parent.upgrade() {
|
||||||
None => self.transform,
|
// Some(value) => Mat4::from(value.borrow().global_transform()) * self.transform,
|
||||||
}
|
// None => self.transform,
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_transform_components(&mut self) {
|
||||||
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub fn relative_transform(&self, space: WeakCell<Spatial>) {}
|
// pub fn relative_transform(&self, space: WeakCell<Spatial>) {}
|
||||||
|
|||||||
Reference in New Issue
Block a user