Skip to content

Commit

Permalink
feat(objects): add palm
Browse files Browse the repository at this point in the history
  • Loading branch information
technobaboo committed Jul 18, 2024
1 parent 6350444 commit f639a16
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
22 changes: 21 additions & 1 deletion src/objects/input/sk_hand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::nodes::{
spatial::Spatial,
Node,
};
use crate::objects::{ObjectHandle, SpatialRef};
use color_eyre::eyre::Result;
use glam::{Mat4, Quat, Vec3};
use serde::{Deserialize, Serialize};
Expand All @@ -16,6 +17,7 @@ use std::sync::Arc;
use stereokit_rust::sk::{DisplayMode, MainThreadToken, Sk};
use stereokit_rust::system::{HandJoint, HandSource, Handed, Input, LinePoint, Lines};
use stereokit_rust::util::Color128;
use zbus::Connection;

fn convert_joint(joint: HandJoint) -> Joint {
Joint {
Expand All @@ -34,13 +36,23 @@ struct HandDatamap {

pub struct SkHand {
_node: OwnedNode,
palm_spatial: Arc<Spatial>,
palm_object: ObjectHandle<SpatialRef>,
handed: Handed,
input: Arc<InputMethod>,
capture: Option<Arc<InputHandler>>,
datamap: HandDatamap,
}
impl SkHand {
pub fn new(handed: Handed) -> Result<Self> {
pub fn new(connection: &Connection, handed: Handed) -> Result<Self> {
let (palm_spatial, palm_object) = SpatialRef::create(
connection,
&("/org/stardustxr/Hand/".to_string()
+ match handed {
Handed::Left => "left",
_ => "right",
} + "/palm"),
);
let _node = Node::generate(&INTERNAL_CLIENT, false).add_to_scenegraph_owned()?;
Spatial::add_to(&_node.0, None, Mat4::IDENTITY, false);
let hand = InputDataType::Hand(Hand {
Expand All @@ -53,6 +65,8 @@ impl SkHand {
Input::hand_visible(handed, false);
Ok(SkHand {
_node,
palm_spatial,
palm_object,
handed,
input,
capture: None,
Expand Down Expand Up @@ -93,6 +107,12 @@ impl SkHand {
hand.palm.radius =
(sk_hand.fingers[2][0].radius + sk_hand.fingers[2][1].radius) * 0.5;

self.palm_spatial
.set_local_transform(Mat4::from_rotation_translation(
hand.palm.rotation.into(),
hand.palm.position.into(),
));

hand.wrist.position = Vec3::from(sk_hand.wrist.position).into();
hand.wrist.rotation = Quat::from(sk_hand.wrist.orientation).into();
hand.wrist.radius =
Expand Down
31 changes: 18 additions & 13 deletions src/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,26 @@ impl ServerObjects {
});
}

tokio::task::spawn({
let connection = connection.clone();
async move {
connection
.request_name("org.stardustxr.Controllers")
.await
.unwrap();
connection
.request_name("org.stardustxr.Hands")
.await
.unwrap();
}
});

let inputs = if sk.get_active_display_mode() == DisplayMode::MixedReality {
tokio::task::spawn({
let connection = connection.clone();
async move {
connection
.request_name("org.stardustxr.Controllers")
.await
.unwrap();
}
});
Inputs::XR {
controller_left: SkController::new(&connection, Handed::Left).unwrap(),
controller_right: SkController::new(&connection, Handed::Right).unwrap(),
hand_left: SkHand::new(Handed::Left).unwrap(),
hand_right: SkHand::new(Handed::Right).unwrap(),
hand_left: SkHand::new(&connection, Handed::Left).unwrap(),
hand_right: SkHand::new(&connection, Handed::Right).unwrap(),
eye_pointer: Device::has_eye_gaze()
.then(EyePointer::new)
.transpose()
Expand Down Expand Up @@ -130,8 +135,8 @@ impl ServerObjects {
// }
if Input::key(Key::F8).is_just_inactive() {
self.inputs = Inputs::Hands {
left: SkHand::new(Handed::Left).unwrap(),
right: SkHand::new(Handed::Right).unwrap(),
left: SkHand::new(&self.connection, Handed::Left).unwrap(),
right: SkHand::new(&self.connection, Handed::Right).unwrap(),
};
}
}
Expand Down

0 comments on commit f639a16

Please sign in to comment.