refactor(model): move into drawable
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
use super::{eventloop::EventLoop, scenegraph::Scenegraph};
|
use super::{eventloop::EventLoop, scenegraph::Scenegraph};
|
||||||
use crate::{
|
use crate::{
|
||||||
core::registry::Registry,
|
core::registry::Registry,
|
||||||
nodes::{data, fields, hmd, input, items, model, root::Root, spatial, startup},
|
nodes::{data, drawable, fields, hmd, input, items, root::Root, spatial, startup},
|
||||||
};
|
};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
@@ -67,7 +67,7 @@ impl Client {
|
|||||||
hmd::make_alias(&client);
|
hmd::make_alias(&client);
|
||||||
spatial::create_interface(&client);
|
spatial::create_interface(&client);
|
||||||
fields::create_interface(&client);
|
fields::create_interface(&client);
|
||||||
model::create_interface(&client);
|
drawable::create_interface(&client);
|
||||||
data::create_interface(&client);
|
data::create_interface(&client);
|
||||||
items::create_interface(&client);
|
items::create_interface(&client);
|
||||||
input::create_interface(&client);
|
input::create_interface(&client);
|
||||||
|
|||||||
@@ -7,13 +7,16 @@ use once_cell::sync::Lazy;
|
|||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
|
||||||
pub struct Registry<T>(Lazy<Mutex<FxHashMap<usize, Weak<T>>>>);
|
pub struct Registry<T: Send + Sync + ?Sized>(Lazy<Mutex<FxHashMap<usize, Weak<T>>>>);
|
||||||
|
|
||||||
impl<T: Send + Sync> Registry<T> {
|
impl<T: Send + Sync + ?Sized> Registry<T> {
|
||||||
pub const fn new() -> Self {
|
pub const fn new() -> Self {
|
||||||
Registry(Lazy::new(|| Mutex::new(FxHashMap::default())))
|
Registry(Lazy::new(|| Mutex::new(FxHashMap::default())))
|
||||||
}
|
}
|
||||||
pub fn add(&self, t: T) -> Arc<T> {
|
pub fn add(&self, t: T) -> Arc<T>
|
||||||
|
where
|
||||||
|
T: Sized,
|
||||||
|
{
|
||||||
let t_arc = Arc::new(t);
|
let t_arc = Arc::new(t);
|
||||||
self.add_raw(&t_arc);
|
self.add_raw(&t_arc);
|
||||||
t_arc
|
t_arc
|
||||||
@@ -21,7 +24,7 @@ impl<T: Send + Sync> Registry<T> {
|
|||||||
pub fn add_raw(&self, t: &Arc<T>) {
|
pub fn add_raw(&self, t: &Arc<T>) {
|
||||||
self.0
|
self.0
|
||||||
.lock()
|
.lock()
|
||||||
.insert(Arc::as_ptr(t) as usize, Arc::downgrade(t));
|
.insert(Arc::as_ptr(t) as *const () as usize, Arc::downgrade(t));
|
||||||
}
|
}
|
||||||
pub fn get_valid_contents(&self) -> Vec<Arc<T>> {
|
pub fn get_valid_contents(&self) -> Vec<Arc<T>> {
|
||||||
self.0
|
self.0
|
||||||
@@ -31,7 +34,9 @@ impl<T: Send + Sync> Registry<T> {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
pub fn remove(&self, t: &T) {
|
pub fn remove(&self, t: &T) {
|
||||||
self.0.lock().remove(&(ptr::addr_of!(*t) as usize));
|
self.0
|
||||||
|
.lock()
|
||||||
|
.remove(&(ptr::addr_of!(*t) as *const () as usize));
|
||||||
}
|
}
|
||||||
pub fn clear(&self) {
|
pub fn clear(&self) {
|
||||||
self.0.lock().clear();
|
self.0.lock().clear();
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ mod objects;
|
|||||||
mod wayland;
|
mod wayland;
|
||||||
|
|
||||||
use crate::core::destroy_queue;
|
use crate::core::destroy_queue;
|
||||||
use crate::nodes::model::{MODELS_TO_DROP, MODEL_REGISTRY};
|
use crate::nodes::drawable::model::MODELS_TO_DROP;
|
||||||
|
use crate::nodes::drawable::DRAWABLE_REGISTRY;
|
||||||
use crate::nodes::{hmd, input};
|
use crate::nodes::{hmd, input};
|
||||||
use crate::objects::input::mouse_pointer::MousePointer;
|
use crate::objects::input::mouse_pointer::MousePointer;
|
||||||
use crate::objects::input::sk_hand::SkHand;
|
use crate::objects::input::sk_hand::SkHand;
|
||||||
@@ -77,8 +78,8 @@ fn main() -> Result<()> {
|
|||||||
destroy_queue::clear();
|
destroy_queue::clear();
|
||||||
|
|
||||||
nodes::root::Root::logic_step(stereokit.time_elapsed());
|
nodes::root::Root::logic_step(stereokit.time_elapsed());
|
||||||
for model in MODEL_REGISTRY.get_valid_contents() {
|
for drawable in DRAWABLE_REGISTRY.get_valid_contents() {
|
||||||
model.draw(&stereokit, draw_ctx);
|
drawable.draw(&stereokit, draw_ctx);
|
||||||
}
|
}
|
||||||
MODELS_TO_DROP.lock().clear();
|
MODELS_TO_DROP.lock().clear();
|
||||||
|
|
||||||
|
|||||||
19
src/nodes/drawable/mod.rs
Normal file
19
src/nodes/drawable/mod.rs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
pub mod model;
|
||||||
|
|
||||||
|
use super::Node;
|
||||||
|
use crate::core::{client::Client, registry::Registry};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use stereokit::{lifecycle::DrawContext, StereoKit};
|
||||||
|
|
||||||
|
pub trait Drawable: Send + Sync {
|
||||||
|
fn draw(&self, sk: &StereoKit, draw_ctx: &DrawContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub static DRAWABLE_REGISTRY: Registry<dyn Drawable> = Registry::new();
|
||||||
|
|
||||||
|
pub fn create_interface(client: &Arc<Client>) {
|
||||||
|
let node = Node::create(client, "", "drawable", false);
|
||||||
|
node.add_local_signal("createModelFromFile", model::create_from_file);
|
||||||
|
node.add_local_signal("createModelFromResource", model::create_from_resource);
|
||||||
|
node.add_to_scenegraph();
|
||||||
|
}
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
use super::spatial::{get_spatial_parent_flex, Spatial};
|
use super::{Drawable, Node};
|
||||||
use super::Node;
|
|
||||||
use crate::core::client::Client;
|
use crate::core::client::Client;
|
||||||
use crate::core::registry::Registry;
|
use crate::core::registry::Registry;
|
||||||
use crate::core::resource::{NamespacedResourceID, ResourceID};
|
use crate::core::resource::{NamespacedResourceID, ResourceID};
|
||||||
|
use crate::nodes::drawable::DRAWABLE_REGISTRY;
|
||||||
|
use crate::nodes::spatial::{get_spatial_parent_flex, Spatial};
|
||||||
use anyhow::{anyhow, bail, ensure, Result};
|
use anyhow::{anyhow, bail, ensure, Result};
|
||||||
use flexbuffers::FlexBufferType;
|
use flexbuffers::FlexBufferType;
|
||||||
use glam::Mat4;
|
use glam::Mat4;
|
||||||
@@ -61,6 +62,7 @@ impl Model {
|
|||||||
};
|
};
|
||||||
node.add_local_signal("setMaterialParameter", Model::set_material_parameter);
|
node.add_local_signal("setMaterialParameter", Model::set_material_parameter);
|
||||||
let model_arc = MODEL_REGISTRY.add(model);
|
let model_arc = MODEL_REGISTRY.add(model);
|
||||||
|
DRAWABLE_REGISTRY.add_raw(&(model_arc.clone() as Arc<dyn Drawable>));
|
||||||
let _ = model_arc.pending_model_path.set(
|
let _ = model_arc.pending_model_path.set(
|
||||||
model_arc
|
model_arc
|
||||||
.resource_id
|
.resource_id
|
||||||
@@ -71,7 +73,40 @@ impl Model {
|
|||||||
Ok(model_arc)
|
Ok(model_arc)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(&self, sk: &StereoKit, draw_ctx: &DrawContext) {
|
fn set_material_parameter(
|
||||||
|
node: &Node,
|
||||||
|
_calling_client: Arc<Client>,
|
||||||
|
data: &[u8],
|
||||||
|
) -> Result<()> {
|
||||||
|
let model = node.model.get().unwrap();
|
||||||
|
let flex_vec = flexbuffers::Reader::get_root(data)?.get_vector()?;
|
||||||
|
let material_idx = flex_vec
|
||||||
|
.idx(0)
|
||||||
|
.get_u64()
|
||||||
|
.map_err(|_| anyhow!("Material ID is not a number!"))? as u32;
|
||||||
|
let parameter_name = flex_vec
|
||||||
|
.idx(1)
|
||||||
|
.get_str()
|
||||||
|
.map_err(|_| anyhow!("Parameter name is not a string!"))?;
|
||||||
|
|
||||||
|
let flex_parameter_value = flex_vec.idx(2);
|
||||||
|
let parameter_value = match flex_parameter_value.flexbuffer_type() {
|
||||||
|
FlexBufferType::String => {
|
||||||
|
MaterialParameter::Texture(PathBuf::from(flex_parameter_value.as_str()))
|
||||||
|
}
|
||||||
|
_ => bail!("Invalid parameter value type"),
|
||||||
|
};
|
||||||
|
|
||||||
|
model
|
||||||
|
.pending_material_parameters
|
||||||
|
.lock()
|
||||||
|
.insert((material_idx, parameter_name.to_string()), parameter_value);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Drawable for Model {
|
||||||
|
fn draw(&self, sk: &StereoKit, draw_ctx: &DrawContext) {
|
||||||
let sk_model = self
|
let sk_model = self
|
||||||
.sk_model
|
.sk_model
|
||||||
.get_or_try_init(|| {
|
.get_or_try_init(|| {
|
||||||
@@ -118,38 +153,6 @@ impl Model {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_material_parameter(
|
|
||||||
node: &Node,
|
|
||||||
_calling_client: Arc<Client>,
|
|
||||||
data: &[u8],
|
|
||||||
) -> Result<()> {
|
|
||||||
let model = node.model.get().unwrap();
|
|
||||||
let flex_vec = flexbuffers::Reader::get_root(data)?.get_vector()?;
|
|
||||||
let material_idx = flex_vec
|
|
||||||
.idx(0)
|
|
||||||
.get_u64()
|
|
||||||
.map_err(|_| anyhow!("Material ID is not a number!"))? as u32;
|
|
||||||
let parameter_name = flex_vec
|
|
||||||
.idx(1)
|
|
||||||
.get_str()
|
|
||||||
.map_err(|_| anyhow!("Parameter name is not a string!"))?;
|
|
||||||
|
|
||||||
let flex_parameter_value = flex_vec.idx(2);
|
|
||||||
let parameter_value = match flex_parameter_value.flexbuffer_type() {
|
|
||||||
FlexBufferType::String => {
|
|
||||||
MaterialParameter::Texture(PathBuf::from(flex_parameter_value.as_str()))
|
|
||||||
}
|
|
||||||
_ => bail!("Invalid parameter value type"),
|
|
||||||
};
|
|
||||||
|
|
||||||
model
|
|
||||||
.pending_material_parameters
|
|
||||||
.lock()
|
|
||||||
.insert((material_idx, parameter_name.to_string()), parameter_value);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
impl Drop for Model {
|
impl Drop for Model {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
@@ -157,16 +160,10 @@ impl Drop for Model {
|
|||||||
MODELS_TO_DROP.lock().push(model);
|
MODELS_TO_DROP.lock().push(model);
|
||||||
}
|
}
|
||||||
MODEL_REGISTRY.remove(self);
|
MODEL_REGISTRY.remove(self);
|
||||||
|
DRAWABLE_REGISTRY.remove(self);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_interface(client: &Arc<Client>) {
|
|
||||||
let node = Node::create(client, "", "drawable", false);
|
|
||||||
node.add_local_signal("createModelFromFile", create_from_file);
|
|
||||||
node.add_local_signal("createModelFromResource", create_from_resource);
|
|
||||||
node.add_to_scenegraph();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn create_from_file(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
|
pub fn create_from_file(_node: &Node, calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
|
||||||
let flex_vec = flexbuffers::Reader::get_root(data)?.get_vector()?;
|
let flex_vec = flexbuffers::Reader::get_root(data)?.get_vector()?;
|
||||||
let node = Node::create(
|
let node = Node::create(
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
pub mod alias;
|
pub mod alias;
|
||||||
pub mod data;
|
pub mod data;
|
||||||
|
pub mod drawable;
|
||||||
pub mod fields;
|
pub mod fields;
|
||||||
pub mod hmd;
|
pub mod hmd;
|
||||||
pub mod input;
|
pub mod input;
|
||||||
pub mod items;
|
pub mod items;
|
||||||
pub mod model;
|
|
||||||
pub mod root;
|
pub mod root;
|
||||||
pub mod spatial;
|
pub mod spatial;
|
||||||
pub mod startup;
|
pub mod startup;
|
||||||
@@ -28,10 +28,10 @@ use crate::core::registry::Registry;
|
|||||||
use self::alias::Alias;
|
use self::alias::Alias;
|
||||||
use self::data::{PulseReceiver, PulseSender};
|
use self::data::{PulseReceiver, PulseSender};
|
||||||
|
|
||||||
|
use self::drawable::model::Model;
|
||||||
use self::fields::Field;
|
use self::fields::Field;
|
||||||
use self::input::{InputHandler, InputMethod};
|
use self::input::{InputHandler, InputMethod};
|
||||||
use self::items::{Item, ItemAcceptor, ItemUI};
|
use self::items::{Item, ItemAcceptor, ItemUI};
|
||||||
use self::model::Model;
|
|
||||||
use self::spatial::Spatial;
|
use self::spatial::Spatial;
|
||||||
use self::startup::StartupSettings;
|
use self::startup::StartupSettings;
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use super::{shaders::PANEL_SHADER_BYTES, state::WaylandState};
|
use super::{shaders::PANEL_SHADER_BYTES, state::WaylandState};
|
||||||
use crate::{
|
use crate::{
|
||||||
core::{destroy_queue, registry::Registry},
|
core::{destroy_queue, registry::Registry},
|
||||||
nodes::model::Model,
|
nodes::drawable::model::Model,
|
||||||
};
|
};
|
||||||
use mint::Vector2;
|
use mint::Vector2;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
|
|||||||
Reference in New Issue
Block a user