From b8d17ac7cab6ef797efcc5e7049b1954361e1be2 Mon Sep 17 00:00:00 2001 From: Nova Date: Sun, 4 Feb 2024 11:04:16 -0500 Subject: [PATCH] refactor(fields): use idl --- codegen/src/lib.rs | 25 ++- src/nodes/fields/box.rs | 57 ++----- src/nodes/fields/cylinder.rs | 55 ++----- src/nodes/fields/mod.rs | 308 ++++++++++++++++++++--------------- src/nodes/fields/sphere.rs | 58 ++----- src/nodes/fields/torus.rs | 39 ++--- src/nodes/spatial/mod.rs | 53 ++---- src/nodes/spatial/zone.rs | 16 +- 8 files changed, 279 insertions(+), 332 deletions(-) diff --git a/codegen/src/lib.rs b/codegen/src/lib.rs index d89dc2d..7fc8495 100644 --- a/codegen/src/lib.rs +++ b/codegen/src/lib.rs @@ -150,6 +150,7 @@ fn generate_custom_struct(custom_struct: &CustomStruct) -> TokenStream { .fields .iter() .map(|a| generate_argument_decl(a, true)) + .map(|d| quote!(pub #d)) .reduce(|a, b| quote!(#a, #b)) .unwrap_or_default(); @@ -265,7 +266,7 @@ fn generate_member(member: &Member) -> TokenStream { let first_args = match member.side { Side::Server => { - quote!(_node: std::sync::Arc, _calling_client: std::sync::Arc, _fds: Vec) + quote!(_node: std::sync::Arc, _calling_client: std::sync::Arc) } Side::Client => quote!(_node: &crate::nodes::Node), }; @@ -312,7 +313,23 @@ fn generate_member(member: &Member) -> TokenStream { } } (Side::Server, MemberType::Signal) => { + let prefix = + if let Some(ArgumentType::Node { _type, return_info }) = &member.return_type { + if let Some(return_info) = return_info { + let parent_name = Ident::new( + &(name_str.to_case(Case::ScreamingSnake) + "_PARENT_PATH"), + Span::call_site(), + ); + let parent_path = &return_info.parent; + quote!(const #parent_name: &'static str = #parent_path;) + } else { + TokenStream::default() + } + } else { + TokenStream::default() + }; quote! { + #prefix #[doc = #description] fn #name(#argument_decls) -> color_eyre::eyre::Result<()>; } @@ -353,16 +370,14 @@ fn generate_handler(member: &Member) -> TokenStream { MemberType::Signal => quote! { node.add_local_signal(#member_name, |_node, _calling_client, _message| { #deserialize - let _fds = _message.fds; - Self::#member_name_ident(_node, _calling_client.clone(), _fds, #argument_uses) + Self::#member_name_ident(_node, _calling_client.clone(), #argument_uses) }); }, MemberType::Method => quote! { node.add_local_method(#member_name, |_node, _calling_client, _message, _method_response| { _method_response.wrap_async(async move { #deserialize - let _fds = _message.fds; - Self::#member_name_ident(_node, _calling_client.clone(), _fds, #argument_uses).await + Self::#member_name_ident(_node, _calling_client.clone(), #argument_uses).await }); }); }, diff --git a/src/nodes/fields/box.rs b/src/nodes/fields/box.rs index 6662b61..0e36ad9 100644 --- a/src/nodes/fields/box.rs +++ b/src/nodes/fields/box.rs @@ -1,14 +1,11 @@ -use super::{Field, FieldTrait, Node}; +use super::{get_field, BoxFieldAspect, Field, FieldTrait, Node}; use crate::core::client::Client; -use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial, Transform}; -use crate::nodes::Message; +use crate::nodes::fields::FieldAspect; +use crate::nodes::spatial::Spatial; use color_eyre::eyre::{ensure, Result}; use glam::{vec3, vec3a, Vec3, Vec3A}; use mint::Vector3; use parking_lot::Mutex; -use serde::Deserialize; -use stardust_xr::schemas::flex::deserialize; - use std::sync::Arc; pub struct BoxField { @@ -30,8 +27,8 @@ impl BoxField { space: node.spatial.get().unwrap().clone(), size: Mutex::new(size.into()), }; - box_field.add_field_methods(node); - node.add_local_signal("set_size", BoxField::set_size_flex); + ::add_node_members(node); + ::add_node_members(node); let field = Arc::new(Field::Box(box_field)); let _ = node.field.set(field.clone()); Ok(field) @@ -40,19 +37,6 @@ impl BoxField { pub fn set_size(&self, size: Vector3) { *self.size.lock() = size.into(); } - - pub fn set_size_flex( - node: Arc, - _calling_client: Arc, - message: Message, - ) -> Result<()> { - let Field::Box(box_field) = node.field.get().unwrap().as_ref() else { - return Ok(()); - }; - box_field.set_size(deserialize(message.as_ref())?); - - Ok(()) - } } impl FieldTrait for BoxField { @@ -70,25 +54,16 @@ impl FieldTrait for BoxField { self.space.as_ref() } } - -pub fn create_box_field_flex( - _node: Arc, - calling_client: Arc, - message: Message, -) -> Result<()> { - #[derive(Deserialize)] - struct CreateFieldInfo<'a> { - name: &'a str, - parent_path: &'a str, - transform: Transform, - size: Vector3, +impl BoxFieldAspect for BoxField { + fn set_size( + node: Arc, + _calling_client: Arc, + size: mint::Vector3, + ) -> Result<()> { + let Field::Box(this_field) = &*get_field(&node)? else { + return Ok(()); + }; + this_field.set_size(size.into()); + Ok(()) } - let info: CreateFieldInfo = deserialize(message.as_ref())?; - let node = Node::create_parent_name(&calling_client, "/field", info.name, true); - let parent = find_spatial_parent(&calling_client, info.parent_path)?; - let transform = parse_transform(info.transform, true, true, false); - let node = node.add_to_scenegraph()?; - Spatial::add_to(&node, Some(parent), transform, false)?; - BoxField::add_to(&node, info.size)?; - Ok(()) } diff --git a/src/nodes/fields/cylinder.rs b/src/nodes/fields/cylinder.rs index e2b55cc..2b48200 100644 --- a/src/nodes/fields/cylinder.rs +++ b/src/nodes/fields/cylinder.rs @@ -1,12 +1,10 @@ -use super::{Field, FieldTrait, Node}; +use super::{get_field, CylinderFieldAspect, Field, FieldTrait, Node}; use crate::core::client::Client; -use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial, Transform}; -use crate::nodes::Message; +use crate::nodes::fields::FieldAspect; +use crate::nodes::spatial::Spatial; use color_eyre::eyre::{ensure, Result}; use glam::{swizzles::*, vec2, Vec3A}; use portable_atomic::AtomicF32; -use serde::Deserialize; -use stardust_xr::schemas::flex::deserialize; use std::sync::atomic::Ordering; use std::sync::Arc; @@ -32,8 +30,8 @@ impl CylinderField { length: AtomicF32::new(length.abs()), radius: AtomicF32::new(radius.abs()), }; - cylinder_field.add_field_methods(node); - node.add_local_signal("set_size", CylinderField::set_size_flex); + ::add_node_members(node); + ::add_node_members(node); let _ = node.field.set(Arc::new(Field::Cylinder(cylinder_field))); Ok(()) } @@ -42,21 +40,7 @@ impl CylinderField { self.length.store(length.abs(), Ordering::Relaxed); self.radius.store(radius.abs(), Ordering::Relaxed); } - - pub fn set_size_flex( - node: Arc, - _calling_client: Arc, - message: Message, - ) -> Result<()> { - let Field::Cylinder(cylinder_field) = node.field.get().unwrap().as_ref() else { - return Ok(()); - }; - let (length, radius) = deserialize(message.as_ref())?; - cylinder_field.set_size(length, radius); - Ok(()) - } } - impl FieldTrait for CylinderField { fn local_distance(&self, p: Vec3A) -> f32 { let radius = self.radius.load(Ordering::Relaxed); @@ -69,26 +53,17 @@ impl FieldTrait for CylinderField { self.space.as_ref() } } - -pub fn create_cylinder_field_flex( - _node: Arc, - calling_client: Arc, - message: Message, -) -> Result<()> { - #[derive(Deserialize)] - struct CreateFieldInfo<'a> { - name: &'a str, - parent_path: &'a str, - transform: Transform, +impl CylinderFieldAspect for CylinderField { + fn set_size( + node: Arc, + _calling_client: Arc, length: f32, radius: f32, + ) -> Result<()> { + let Field::Cylinder(this_field) = &*get_field(&node)? else { + return Ok(()); + }; + this_field.set_size(length, radius); + Ok(()) } - let info: CreateFieldInfo = deserialize(message.as_ref())?; - let node = Node::create_parent_name(&calling_client, "/field", info.name, true); - let parent = find_spatial_parent(&calling_client, info.parent_path)?; - let transform = parse_transform(info.transform, true, true, false); - let node = node.add_to_scenegraph()?; - Spatial::add_to(&node, Some(parent), transform, false)?; - CylinderField::add_to(&node, info.length, info.radius)?; - Ok(()) } diff --git a/src/nodes/fields/mod.rs b/src/nodes/fields/mod.rs index eb0c2f9..c2275f7 100644 --- a/src/nodes/fields/mod.rs +++ b/src/nodes/fields/mod.rs @@ -3,25 +3,23 @@ mod cylinder; mod sphere; mod torus; -use self::cylinder::{create_cylinder_field_flex, CylinderField}; -use self::r#box::{create_box_field_flex, BoxField}; -use self::sphere::{create_sphere_field_flex, SphereField}; -use self::torus::{create_torus_field_flex, TorusField}; +use self::cylinder::CylinderField; +use self::r#box::BoxField; +use self::sphere::SphereField; +use self::torus::TorusField; use super::alias::AliasInfo; -use super::spatial::Spatial; -use super::{Message, Node}; +use super::spatial::{get_spatial, Spatial}; +use super::Node; use crate::core::client::Client; -use crate::core::scenegraph::MethodResponseSender; -use crate::nodes::spatial::find_reference_space; +use crate::create_interface; +use crate::nodes::spatial::Transform; use color_eyre::eyre::Result; -use glam::{vec2, vec3a, Vec3, Vec3A}; -use mint::Vector3; +use glam::{vec2, vec3a, Mat4, Vec3, Vec3A}; use once_cell::sync::Lazy; -use serde::{Deserialize, Serialize}; -use stardust_xr::schemas::flex::{deserialize, serialize}; use std::ops::Deref; +use std::os::fd::OwnedFd; use std::sync::Arc; // TODO: get SDFs working properly with non-uniform scale and so on, output distance relative to the spatial it's compared against @@ -31,13 +29,9 @@ pub static FIELD_ALIAS_INFO: Lazy = Lazy::new(|| AliasInfo { ..Default::default() }); +stardust_xr_server_codegen::codegen_field_protocol!(); + pub trait FieldTrait { - fn add_field_methods(&self, node: &Arc) { - node.add_local_method("distance", field_distance_flex); - node.add_local_method("normal", field_normal_flex); - node.add_local_method("closest_point", field_closest_point_flex); - node.add_local_method("ray_march", field_ray_march_flex); - } fn spatial_ref(&self) -> &Spatial; fn local_distance(&self, p: Vec3A) -> f32; @@ -83,6 +77,8 @@ pub trait FieldTrait { fn ray_march(&self, ray: Ray) -> RayMarchResult { let mut result = RayMarchResult { + ray_origin: ray.origin.into(), + ray_direction: ray.direction.into(), min_distance: f32::MAX, deepest_point_distance: 0_f32, ray_length: 0_f32, @@ -112,6 +108,76 @@ pub trait FieldTrait { result } } +impl FieldAspect for Fi { + fn distance( + node: Arc, + _calling_client: Arc, + space: Arc, + point: mint::Vector3, + ) -> impl std::future::Future)>> + Send + 'static { + async move { + let reference_space = get_spatial(&space, "Reference space")?; + let this_field = node.field.get().unwrap(); + + let distance = this_field.distance(reference_space.as_ref(), point.into()); + Ok((distance, Vec::new())) + } + } + + fn normal( + node: Arc, + _calling_client: Arc, + space: Arc, + point: mint::Vector3, + ) -> impl std::future::Future, Vec)>> + Send + 'static + { + async move { + let reference_space = get_spatial(&space, "Reference space")?; + let this_field = node.field.get().unwrap(); + + let normal = this_field.normal(reference_space.as_ref(), point.into(), 0.001); + Ok((normal.into(), Vec::new())) + } + } + + fn closest_point( + node: Arc, + _calling_client: Arc, + space: Arc, + point: mint::Vector3, + ) -> impl std::future::Future, Vec)>> + Send + 'static + { + async move { + let reference_space = get_spatial(&space, "Reference space")?; + let this_field = node.field.get().unwrap(); + + let closest_point = + this_field.closest_point(reference_space.as_ref(), point.into(), 0.001); + Ok((closest_point.into(), Vec::new())) + } + } + + fn ray_march( + node: Arc, + _calling_client: Arc, + space: Arc, + ray_origin: mint::Vector3, + ray_direction: mint::Vector3, + ) -> impl std::future::Future)>> + Send + 'static + { + async move { + let reference_space = get_spatial(&space, "Reference space")?; + let this_field = node.field.get().unwrap(); + + let ray_march = this_field.ray_march(Ray { + origin: ray_origin.into(), + direction: ray_direction.into(), + space: reference_space, + }); + Ok((ray_march, Vec::new())) + } + } +} pub struct Ray { pub origin: Vec3, @@ -119,14 +185,6 @@ pub struct Ray { pub space: Arc, } -#[derive(Debug, Serialize)] -pub struct RayMarchResult { - pub min_distance: f32, - pub deepest_point_distance: f32, - pub ray_length: f32, - pub ray_steps: u32, -} - // const MIN_RAY_STEPS: u32 = 0; const MAX_RAY_STEPS: u32 = 1000; @@ -136,107 +194,12 @@ const MAX_RAY_MARCH: f32 = f32::MAX; // const MIN_RAY_LENGTH: f32 = 0_f32; const MAX_RAY_LENGTH: f32 = 1000_f32; -fn field_distance_flex( - node: Arc, - calling_client: Arc, - message: Message, - response: MethodResponseSender, -) { - response.wrap_sync(move || { - #[derive(Deserialize)] - struct FieldInfoArgs<'a> { - reference_space_path: &'a str, - point: Vector3, - } - let args: FieldInfoArgs = deserialize(message.as_ref())?; - let reference_space = find_reference_space(&calling_client, args.reference_space_path)?; - - let distance = node - .field - .get() - .unwrap() - .distance(reference_space.as_ref(), args.point.into()); - Ok(serialize(distance)?.into()) - }); -} -fn field_normal_flex( - node: Arc, - calling_client: Arc, - message: Message, - response: MethodResponseSender, -) { - response.wrap_sync(move || { - #[derive(Deserialize)] - struct FieldInfoArgs<'a> { - reference_space_path: &'a str, - point: Vector3, - } - let args: FieldInfoArgs = deserialize(message.as_ref())?; - let reference_space = find_reference_space(&calling_client, args.reference_space_path)?; - - let normal = node.field.get().as_ref().unwrap().normal( - reference_space.as_ref(), - args.point.into(), - 0.001, - ); - Ok(serialize(mint::Vector3::from(normal))?.into()) - }); -} -fn field_closest_point_flex( - node: Arc, - calling_client: Arc, - message: Message, - response: MethodResponseSender, -) { - response.wrap_sync(move || { - #[derive(Deserialize)] - struct FieldInfoArgs<'a> { - reference_space_path: &'a str, - point: Vector3, - } - let args: FieldInfoArgs = deserialize(message.as_ref())?; - let reference_space = find_reference_space(&calling_client, args.reference_space_path)?; - - let closest_point = node.field.get().as_ref().unwrap().closest_point( - reference_space.as_ref(), - args.point.into(), - 0.001, - ); - Ok(serialize(mint::Vector3::from(closest_point))?.into()) - }); -} -fn field_ray_march_flex( - node: Arc, - calling_client: Arc, - message: Message, - response: MethodResponseSender, -) { - response.wrap_sync(move || { - #[derive(Deserialize)] - struct FieldInfoArgs<'a> { - reference_space_path: &'a str, - ray_origin: Vector3, - ray_direction: Vector3, - } - let args: FieldInfoArgs = deserialize(message.as_ref())?; - let reference_space = find_reference_space(&calling_client, args.reference_space_path)?; - - let ray_march_result = node.field.get().unwrap().ray_march(Ray { - origin: args.ray_origin.into(), - direction: args.ray_direction.into(), - space: reference_space, - }); - Ok(serialize(ray_march_result)?.into()) - }); -} - pub enum Field { Box(BoxField), Cylinder(CylinderField), Sphere(SphereField), Torus(TorusField), } - impl Deref for Field { type Target = dyn FieldTrait; fn deref(&self) -> &Self::Target { @@ -249,13 +212,102 @@ impl Deref for Field { } } -pub fn create_interface(client: &Arc) -> Result<()> { - let node = Node::create_parent_name(client, "", "field", false); - node.add_local_signal("create_box_field", create_box_field_flex); - node.add_local_signal("create_cylinder_field", create_cylinder_field_flex); - node.add_local_signal("create_sphere_field", create_sphere_field_flex); - node.add_local_signal("create_torus_field", create_torus_field_flex); - node.add_to_scenegraph().map(|_| ()) +create_interface!(FieldInterface, FieldInterfaceAspect, "/field"); +pub struct FieldInterface; +impl FieldInterfaceAspect for FieldInterface { + fn create_box_field( + _node: Arc, + calling_client: Arc, + name: String, + parent: Arc, + transform: Transform, + size: mint::Vector3, + ) -> Result<()> { + let transform = transform.to_mat4(true, true, false); + let parent = get_spatial(&parent, "Spatial parent")?; + let node = Node::create_parent_name( + &calling_client, + Self::CREATE_BOX_FIELD_PARENT_PATH, + &name, + true, + ) + .add_to_scenegraph()?; + Spatial::add_to(&node, Some(parent), transform, false)?; + BoxField::add_to(&node, size)?; + Ok(()) + } + + fn create_cylinder_field( + _node: Arc, + calling_client: Arc, + name: String, + parent: Arc, + transform: Transform, + length: f32, + radius: f32, + ) -> Result<()> { + let transform = transform.to_mat4(true, true, false); + let parent = get_spatial(&parent, "Spatial parent")?; + let node = Node::create_parent_name( + &calling_client, + Self::CREATE_CYLINDER_FIELD_PARENT_PATH, + &name, + true, + ) + .add_to_scenegraph()?; + Spatial::add_to(&node, Some(parent), transform, false)?; + CylinderField::add_to(&node, length, radius)?; + Ok(()) + } + + fn create_sphere_field( + _node: Arc, + calling_client: Arc, + name: String, + parent: Arc, + position: mint::Vector3, + radius: f32, + ) -> Result<()> { + let parent = get_spatial(&parent, "Spatial parent")?; + let node = Node::create_parent_name( + &calling_client, + Self::CREATE_SPHERE_FIELD_PARENT_PATH, + &name, + true, + ) + .add_to_scenegraph()?; + Spatial::add_to( + &node, + Some(parent), + Mat4::from_translation(position.into()), + false, + )?; + SphereField::add_to(&node, radius)?; + Ok(()) + } + + fn create_torus_field( + _node: Arc, + calling_client: Arc, + name: String, + parent: Arc, + transform: Transform, + radius_a: f32, + radius_b: f32, + ) -> Result<()> { + let transform = transform.to_mat4(true, true, false); + let parent = get_spatial(&parent, "Spatial parent")?; + let node = Node::create_parent_name( + &calling_client, + Self::CREATE_TORUS_FIELD_PARENT_PATH, + &name, + true, + ) + .add_to_scenegraph()?; + Spatial::add_to(&node, Some(parent), transform, false)?; + TorusField::add_to(&node, radius_a, radius_b)?; + Ok(()) + } } pub fn find_field(client: &Client, path: &str) -> Result> { diff --git a/src/nodes/fields/sphere.rs b/src/nodes/fields/sphere.rs index 0dcb447..ea35799 100644 --- a/src/nodes/fields/sphere.rs +++ b/src/nodes/fields/sphere.rs @@ -1,13 +1,10 @@ -use super::{Field, FieldTrait, Node}; +use super::{get_field, Field, FieldTrait, Node, SphereFieldAspect}; use crate::core::client::Client; -use crate::nodes::spatial::{find_spatial_parent, Spatial}; -use crate::nodes::Message; +use crate::nodes::fields::FieldAspect; +use crate::nodes::spatial::Spatial; use color_eyre::eyre::{ensure, Result}; -use glam::{Mat4, Vec3A}; -use mint::Vector3; +use glam::Vec3A; use portable_atomic::AtomicF32; -use serde::Deserialize; -use stardust_xr::schemas::flex::deserialize; use std::sync::atomic::Ordering; use std::sync::Arc; @@ -30,8 +27,8 @@ impl SphereField { space: node.spatial.get().unwrap().clone(), radius: AtomicF32::new(radius), }; - sphere_field.add_field_methods(node); - node.add_local_signal("set_radius", SphereField::set_radius_flex); + ::add_node_members(node); + ::add_node_members(node); let _ = node.field.set(Arc::new(Field::Sphere(sphere_field))); Ok(()) } @@ -39,18 +36,6 @@ impl SphereField { pub fn set_radius(&self, radius: f32) { self.radius.store(radius, Ordering::Relaxed); } - - pub fn set_radius_flex( - node: Arc, - _calling_client: Arc, - message: Message, - ) -> Result<()> { - let Field::Sphere(sphere_field) = node.field.get().unwrap().as_ref() else { - return Ok(()); - }; - sphere_field.set_radius(deserialize(message.as_ref())?); - Ok(()) - } } impl FieldTrait for SphereField { @@ -67,29 +52,12 @@ impl FieldTrait for SphereField { self.space.as_ref() } } - -pub fn create_sphere_field_flex( - _node: Arc, - calling_client: Arc, - message: Message, -) -> Result<()> { - #[derive(Deserialize)] - struct CreateFieldInfo<'a> { - name: &'a str, - parent_path: &'a str, - origin: Option>, - radius: f32, +impl SphereFieldAspect for SphereField { + fn set_radius(node: Arc, _calling_client: Arc, radius: f32) -> Result<()> { + let Field::Sphere(this_field) = &*get_field(&node)? else { + return Ok(()); + }; + this_field.set_radius(radius); + Ok(()) } - let info: CreateFieldInfo = deserialize(message.as_ref())?; - let node = Node::create_parent_name(&calling_client, "/field", info.name, true); - let parent = find_spatial_parent(&calling_client, info.parent_path)?; - let transform = Mat4::from_translation( - info.origin - .unwrap_or_else(|| Vector3::from([0.0; 3])) - .into(), - ); - let node = node.add_to_scenegraph()?; - Spatial::add_to(&node, Some(parent), transform, false)?; - SphereField::add_to(&node, info.radius)?; - Ok(()) } diff --git a/src/nodes/fields/torus.rs b/src/nodes/fields/torus.rs index 9ffab4a..a82e1d2 100644 --- a/src/nodes/fields/torus.rs +++ b/src/nodes/fields/torus.rs @@ -1,11 +1,11 @@ -use super::{Field, FieldTrait, Node}; +use super::{get_field, Field, FieldTrait, Node, TorusFieldAspect}; use crate::core::client::Client; -use crate::nodes::spatial::{find_spatial_parent, parse_transform, Spatial, Transform}; +use crate::nodes::fields::FieldAspect; +use crate::nodes::spatial::Spatial; use crate::nodes::Message; use color_eyre::eyre::{ensure, Result}; use glam::{swizzles::*, vec2, Vec3A}; use portable_atomic::AtomicF32; -use serde::Deserialize; use stardust_xr::schemas::flex::deserialize; use std::sync::atomic::Ordering; @@ -32,7 +32,8 @@ impl TorusField { radius_a: AtomicF32::new(radius_a.abs()), radius_b: AtomicF32::new(radius_b.abs()), }; - torus_field.add_field_methods(node); + ::add_node_members(node); + ::add_node_members(node); node.add_local_signal("set_size", TorusField::set_size_flex); let _ = node.field.set(Arc::new(Field::Torus(torus_field))); Ok(()) @@ -57,7 +58,6 @@ impl TorusField { Ok(()) } } - impl FieldTrait for TorusField { fn local_distance(&self, p: Vec3A) -> f32 { let radius_a = self.radius_a.load(Ordering::Relaxed); @@ -69,26 +69,17 @@ impl FieldTrait for TorusField { self.space.as_ref() } } - -pub fn create_torus_field_flex( - _node: Arc, - calling_client: Arc, - message: Message, -) -> Result<()> { - #[derive(Deserialize)] - struct CreateFieldInfo<'a> { - name: &'a str, - parent_path: &'a str, - transform: Transform, +impl TorusFieldAspect for TorusField { + fn set_size( + node: Arc, + _calling_client: Arc, radius_a: f32, radius_b: f32, + ) -> Result<()> { + let Field::Torus(this_field) = &*get_field(&node)? else { + return Ok(()); + }; + this_field.set_size(radius_a, radius_b); + Ok(()) } - let info: CreateFieldInfo = deserialize(message.as_ref())?; - let node = Node::create_parent_name(&calling_client, "/field", info.name, true); - let parent = find_spatial_parent(&calling_client, info.parent_path)?; - let transform = parse_transform(info.transform, true, true, false); - let node = node.add_to_scenegraph()?; - Spatial::add_to(&node, Some(parent), transform, false)?; - TorusField::add_to(&node, info.radius_a, info.radius_b)?; - Ok(()) } diff --git a/src/nodes/spatial/mod.rs b/src/nodes/spatial/mod.rs index 9e0e101..592eff0 100644 --- a/src/nodes/spatial/mod.rs +++ b/src/nodes/spatial/mod.rs @@ -19,24 +19,24 @@ use std::sync::{Arc, Weak}; use stereokit::{bounds_grow_to_fit_box, Bounds}; stardust_xr_server_codegen::codegen_spatial_protocol!(); -// impl Transform { -// pub fn to_mat4(self, position: bool, rotation: bool, scale: bool) -> Mat4 { -// let position = position -// .then_some(self.translation) -// .flatten() -// .unwrap_or_else(|| Vector3::from([0.0; 3])); -// let rotation = rotation -// .then_some(self.rotation) -// .flatten() -// .unwrap_or_else(|| Quat::IDENTITY.into()); -// let scale = scale -// .then_some(self.scale) -// .flatten() -// .unwrap_or_else(|| Vector3::from([1.0; 3])); +impl Transform { + pub fn to_mat4(self, position: bool, rotation: bool, scale: bool) -> Mat4 { + let position = position + .then_some(self.translation) + .flatten() + .unwrap_or_else(|| Vector3::from([0.0; 3])); + let rotation = rotation + .then_some(self.rotation) + .flatten() + .unwrap_or_else(|| Quat::IDENTITY.into()); + let scale = scale + .then_some(self.scale) + .flatten() + .unwrap_or_else(|| Vector3::from([1.0; 3])); -// Mat4::from_scale_rotation_translation(scale.into(), rotation.into(), position.into()) -// } -// } + Mat4::from_scale_rotation_translation(scale.into(), rotation.into(), position.into()) + } +} static ZONEABLE_REGISTRY: Registry = Registry::new(); @@ -247,7 +247,6 @@ impl SpatialAspect for Spatial { fn get_local_bounding_box( node: Arc, _calling_client: Arc, - _fds: Vec, ) -> impl std::future::Future)>> + Send + 'static { async move { let this_spatial = node @@ -267,7 +266,6 @@ impl SpatialAspect for Spatial { fn get_relative_bounding_box( node: Arc, _calling_client: Arc, - _fds: Vec, relative_to: Arc, ) -> impl std::future::Future)>> + Send + 'static { async move { @@ -303,7 +301,6 @@ impl SpatialAspect for Spatial { fn get_transform( node: Arc, _calling_client: Arc, - _fds: Vec, relative_to: Arc, ) -> impl std::future::Future)>> + Send + 'static { async move { @@ -331,7 +328,6 @@ impl SpatialAspect for Spatial { fn set_local_transform( node: Arc, _calling_client: Arc, - _fds: Vec, transform: Transform, ) -> Result<()> { let this_spatial = node @@ -344,7 +340,6 @@ impl SpatialAspect for Spatial { fn set_relative_transform( node: Arc, _calling_client: Arc, - _fds: Vec, relative_to: Arc, transform: Transform, ) -> Result<()> { @@ -361,7 +356,6 @@ impl SpatialAspect for Spatial { fn set_spatial_parent( node: Arc, _calling_client: Arc, - _fds: Vec, parent: Arc, ) -> Result<()> { let this_spatial = node @@ -377,7 +371,6 @@ impl SpatialAspect for Spatial { fn set_spatial_parent_in_place( node: Arc, _calling_client: Arc, - _fds: Vec, parent: Arc, ) -> Result<()> { let this_spatial = node @@ -390,12 +383,7 @@ impl SpatialAspect for Spatial { Ok(()) } - fn set_zoneable( - node: Arc, - _calling_client: Arc, - _fds: Vec, - zoneable: bool, - ) -> Result<()> { + fn set_zoneable(node: Arc, _calling_client: Arc, zoneable: bool) -> Result<()> { let spatial = node.spatial.get().unwrap(); if zoneable { ZONEABLE_REGISTRY.add_raw(spatial); @@ -458,9 +446,6 @@ pub fn find_spatial( pub fn find_spatial_parent(calling_client: &Arc, node_path: &str) -> Result> { find_spatial(calling_client, "Spatial parent", node_path) } -pub fn find_reference_space(calling_client: &Arc, node_path: &str) -> Result> { - find_spatial(calling_client, "Reference space", node_path) -} pub fn get_spatial(node: &Arc, node_name: &str) -> Result> { node.get_aspect(node_name, "spatial", |n| &n.spatial) .cloned() @@ -471,7 +456,6 @@ impl SpatialInterfaceAspect for SpatialInterface { fn create_spatial( _node: Arc, calling_client: Arc, - _fds: Vec, name: String, parent: Arc, transform: Transform, @@ -487,7 +471,6 @@ impl SpatialInterfaceAspect for SpatialInterface { fn create_zone( _node: Arc, calling_client: Arc, - _fds: Vec, name: String, parent: Arc, transform: Transform, diff --git a/src/nodes/spatial/zone.rs b/src/nodes/spatial/zone.rs index d5dde8c..422275c 100644 --- a/src/nodes/spatial/zone.rs +++ b/src/nodes/spatial/zone.rs @@ -10,10 +10,7 @@ use crate::{ use glam::vec3a; use parking_lot::Mutex; use rustc_hash::FxHashMap; -use std::{ - os::fd::OwnedFd, - sync::{Arc, Weak}, -}; +use std::sync::{Arc, Weak}; pub fn capture(spatial: &Arc, zone: &Arc) { let old_distance = spatial.zone_distance(); @@ -66,12 +63,7 @@ impl Zone { } } impl ZoneAspect for Zone { - #[doc = ""] - fn update( - node: Arc, - _calling_client: Arc, - _fds: Vec, - ) -> color_eyre::eyre::Result<()> { + fn update(node: Arc, _calling_client: Arc) -> color_eyre::eyre::Result<()> { let zone = node.zone.get().unwrap(); let Some(field) = zone.field.upgrade() else { return Err(color_eyre::eyre::eyre!("Zone's field has been destroyed")); @@ -140,11 +132,9 @@ impl ZoneAspect for Zone { Ok(()) } - #[doc = ""] fn capture( node: Arc, _calling_client: Arc, - _fds: Vec, spatial: Arc, ) -> color_eyre::eyre::Result<()> { let zone = node.zone.get().unwrap(); @@ -153,11 +143,9 @@ impl ZoneAspect for Zone { Ok(()) } - #[doc = ""] fn release( _node: Arc, _calling_client: Arc, - _fds: Vec, spatial: Arc, ) -> color_eyre::eyre::Result<()> { let spatial = get_spatial(&spatial, "Spatial")?;