refactor(fields): use idl

This commit is contained in:
Nova
2024-02-04 11:04:16 -05:00
parent ed28914f86
commit b8d17ac7ca
8 changed files with 279 additions and 332 deletions

View File

@@ -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<crate::nodes::Node>, _calling_client: std::sync::Arc<crate::core::client::Client>, _fds: Vec<std::os::fd::OwnedFd>)
quote!(_node: std::sync::Arc<crate::nodes::Node>, _calling_client: std::sync::Arc<crate::core::client::Client>)
}
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
});
});
},

View File

@@ -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);
<BoxField as FieldAspect>::add_node_members(node);
<BoxField as BoxFieldAspect>::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<f32>) {
*self.size.lock() = size.into();
}
pub fn set_size_flex(
node: Arc<Node>,
_calling_client: Arc<Client>,
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<Node>,
calling_client: Arc<Client>,
message: Message,
) -> Result<()> {
#[derive(Deserialize)]
struct CreateFieldInfo<'a> {
name: &'a str,
parent_path: &'a str,
transform: Transform,
size: Vector3<f32>,
impl BoxFieldAspect for BoxField {
fn set_size(
node: Arc<Node>,
_calling_client: Arc<Client>,
size: mint::Vector3<f32>,
) -> 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(())
}

View File

@@ -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);
<CylinderField as FieldAspect>::add_node_members(node);
<CylinderField as CylinderFieldAspect>::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<Node>,
_calling_client: Arc<Client>,
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<Node>,
calling_client: Arc<Client>,
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<Node>,
_calling_client: Arc<Client>,
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(())
}

View File

@@ -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<AliasInfo> = Lazy::new(|| AliasInfo {
..Default::default()
});
stardust_xr_server_codegen::codegen_field_protocol!();
pub trait FieldTrait {
fn add_field_methods(&self, node: &Arc<Node>) {
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<Fi: FieldTrait> FieldAspect for Fi {
fn distance(
node: Arc<Node>,
_calling_client: Arc<Client>,
space: Arc<Node>,
point: mint::Vector3<f32>,
) -> impl std::future::Future<Output = Result<(f32, Vec<OwnedFd>)>> + 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<Node>,
_calling_client: Arc<Client>,
space: Arc<Node>,
point: mint::Vector3<f32>,
) -> impl std::future::Future<Output = Result<(mint::Vector3<f32>, Vec<OwnedFd>)>> + 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<Node>,
_calling_client: Arc<Client>,
space: Arc<Node>,
point: mint::Vector3<f32>,
) -> impl std::future::Future<Output = Result<(mint::Vector3<f32>, Vec<OwnedFd>)>> + 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<Node>,
_calling_client: Arc<Client>,
space: Arc<Node>,
ray_origin: mint::Vector3<f32>,
ray_direction: mint::Vector3<f32>,
) -> impl std::future::Future<Output = Result<(RayMarchResult, Vec<OwnedFd>)>> + 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<Spatial>,
}
#[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<Node>,
calling_client: Arc<Client>,
message: Message,
response: MethodResponseSender,
) {
response.wrap_sync(move || {
#[derive(Deserialize)]
struct FieldInfoArgs<'a> {
reference_space_path: &'a str,
point: Vector3<f32>,
}
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<Node>,
calling_client: Arc<Client>,
message: Message,
response: MethodResponseSender,
) {
response.wrap_sync(move || {
#[derive(Deserialize)]
struct FieldInfoArgs<'a> {
reference_space_path: &'a str,
point: Vector3<f32>,
}
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<Node>,
calling_client: Arc<Client>,
message: Message,
response: MethodResponseSender,
) {
response.wrap_sync(move || {
#[derive(Deserialize)]
struct FieldInfoArgs<'a> {
reference_space_path: &'a str,
point: Vector3<f32>,
}
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<Node>,
calling_client: Arc<Client>,
message: Message,
response: MethodResponseSender,
) {
response.wrap_sync(move || {
#[derive(Deserialize)]
struct FieldInfoArgs<'a> {
reference_space_path: &'a str,
ray_origin: Vector3<f32>,
ray_direction: Vector3<f32>,
}
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<Client>) -> 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<Node>,
calling_client: Arc<Client>,
name: String,
parent: Arc<Node>,
transform: Transform,
size: mint::Vector3<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_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<Node>,
calling_client: Arc<Client>,
name: String,
parent: Arc<Node>,
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<Node>,
calling_client: Arc<Client>,
name: String,
parent: Arc<Node>,
position: mint::Vector3<f32>,
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<Node>,
calling_client: Arc<Client>,
name: String,
parent: Arc<Node>,
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<Arc<Field>> {

View File

@@ -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);
<SphereField as FieldAspect>::add_node_members(node);
<SphereField as SphereFieldAspect>::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<Node>,
_calling_client: Arc<Client>,
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<Node>,
calling_client: Arc<Client>,
message: Message,
) -> Result<()> {
#[derive(Deserialize)]
struct CreateFieldInfo<'a> {
name: &'a str,
parent_path: &'a str,
origin: Option<Vector3<f32>>,
radius: f32,
impl SphereFieldAspect for SphereField {
fn set_radius(node: Arc<Node>, _calling_client: Arc<Client>, 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(())
}

View File

@@ -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);
<TorusField as FieldAspect>::add_node_members(node);
<TorusField as TorusFieldAspect>::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<Node>,
calling_client: Arc<Client>,
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<Node>,
_calling_client: Arc<Client>,
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(())
}

View File

@@ -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<Spatial> = Registry::new();
@@ -247,7 +247,6 @@ impl SpatialAspect for Spatial {
fn get_local_bounding_box(
node: Arc<Node>,
_calling_client: Arc<Client>,
_fds: Vec<OwnedFd>,
) -> impl std::future::Future<Output = Result<(BoundingBox, Vec<OwnedFd>)>> + Send + 'static {
async move {
let this_spatial = node
@@ -267,7 +266,6 @@ impl SpatialAspect for Spatial {
fn get_relative_bounding_box(
node: Arc<Node>,
_calling_client: Arc<Client>,
_fds: Vec<OwnedFd>,
relative_to: Arc<Node>,
) -> impl std::future::Future<Output = Result<(BoundingBox, Vec<OwnedFd>)>> + Send + 'static {
async move {
@@ -303,7 +301,6 @@ impl SpatialAspect for Spatial {
fn get_transform(
node: Arc<Node>,
_calling_client: Arc<Client>,
_fds: Vec<OwnedFd>,
relative_to: Arc<Node>,
) -> impl std::future::Future<Output = Result<(Transform, Vec<OwnedFd>)>> + Send + 'static {
async move {
@@ -331,7 +328,6 @@ impl SpatialAspect for Spatial {
fn set_local_transform(
node: Arc<Node>,
_calling_client: Arc<Client>,
_fds: Vec<OwnedFd>,
transform: Transform,
) -> Result<()> {
let this_spatial = node
@@ -344,7 +340,6 @@ impl SpatialAspect for Spatial {
fn set_relative_transform(
node: Arc<Node>,
_calling_client: Arc<Client>,
_fds: Vec<OwnedFd>,
relative_to: Arc<Node>,
transform: Transform,
) -> Result<()> {
@@ -361,7 +356,6 @@ impl SpatialAspect for Spatial {
fn set_spatial_parent(
node: Arc<Node>,
_calling_client: Arc<Client>,
_fds: Vec<OwnedFd>,
parent: Arc<Node>,
) -> Result<()> {
let this_spatial = node
@@ -377,7 +371,6 @@ impl SpatialAspect for Spatial {
fn set_spatial_parent_in_place(
node: Arc<Node>,
_calling_client: Arc<Client>,
_fds: Vec<OwnedFd>,
parent: Arc<Node>,
) -> Result<()> {
let this_spatial = node
@@ -390,12 +383,7 @@ impl SpatialAspect for Spatial {
Ok(())
}
fn set_zoneable(
node: Arc<Node>,
_calling_client: Arc<Client>,
_fds: Vec<OwnedFd>,
zoneable: bool,
) -> Result<()> {
fn set_zoneable(node: Arc<Node>, _calling_client: Arc<Client>, 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<Client>, node_path: &str) -> Result<Arc<Spatial>> {
find_spatial(calling_client, "Spatial parent", node_path)
}
pub fn find_reference_space(calling_client: &Arc<Client>, node_path: &str) -> Result<Arc<Spatial>> {
find_spatial(calling_client, "Reference space", node_path)
}
pub fn get_spatial(node: &Arc<Node>, node_name: &str) -> Result<Arc<Spatial>> {
node.get_aspect(node_name, "spatial", |n| &n.spatial)
.cloned()
@@ -471,7 +456,6 @@ impl SpatialInterfaceAspect for SpatialInterface {
fn create_spatial(
_node: Arc<Node>,
calling_client: Arc<Client>,
_fds: Vec<OwnedFd>,
name: String,
parent: Arc<Node>,
transform: Transform,
@@ -487,7 +471,6 @@ impl SpatialInterfaceAspect for SpatialInterface {
fn create_zone(
_node: Arc<Node>,
calling_client: Arc<Client>,
_fds: Vec<OwnedFd>,
name: String,
parent: Arc<Node>,
transform: Transform,

View File

@@ -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<Spatial>, zone: &Arc<Zone>) {
let old_distance = spatial.zone_distance();
@@ -66,12 +63,7 @@ impl Zone {
}
}
impl ZoneAspect for Zone {
#[doc = ""]
fn update(
node: Arc<Node>,
_calling_client: Arc<Client>,
_fds: Vec<OwnedFd>,
) -> color_eyre::eyre::Result<()> {
fn update(node: Arc<Node>, _calling_client: Arc<Client>) -> 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<Node>,
_calling_client: Arc<Client>,
_fds: Vec<OwnedFd>,
spatial: Arc<Node>,
) -> color_eyre::eyre::Result<()> {
let zone = node.zone.get().unwrap();
@@ -153,11 +143,9 @@ impl ZoneAspect for Zone {
Ok(())
}
#[doc = ""]
fn release(
_node: Arc<Node>,
_calling_client: Arc<Client>,
_fds: Vec<OwnedFd>,
spatial: Arc<Node>,
) -> color_eyre::eyre::Result<()> {
let spatial = get_spatial(&spatial, "Spatial")?;