Updated Oculus controller to use the

This commit is contained in:
Schmarni
2023-11-10 21:56:03 +01:00
parent 1db2cb2dd7
commit ca15cb7cda
10 changed files with 505 additions and 366 deletions

View File

@@ -1,15 +1,27 @@
use bevy::{prelude::*, utils::HashMap};
use openxr as xr;
use xr::{Action, Binding, Posef};
use xr::{Action, Binding, Haptic, Posef};
use crate::resources::XrInstance;
use crate::resources::{XrInstance, XrSession};
pub fn setup_oxr_actions(world: &mut World, instance: Ref<XrInstance>) {
pub struct OpenXrActionsPlugin;
impl Plugin for OpenXrActionsPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(SetupActionSets {
sets: HashMap::new(),
});
app.add_systems(PostStartup, setup_oxr_actions);
}
}
pub fn setup_oxr_actions(world: &mut World) {
let actions = world.remove_resource::<SetupActionSets>().unwrap();
let instance = world.get_resource::<XrInstance>().unwrap();
let session = world.get_resource::<XrSession>().unwrap();
let left_path = instance.string_to_path("/user/hand/left").unwrap();
let right_path = instance.string_to_path("/user/hand/right").unwrap();
let hands = [left_path, right_path];
let actions = world.remove_resource::<SetupActionSets>().unwrap();
let mut action_sets = ActionSets { sets: default() };
let mut action_bindings: HashMap<&'static str, Vec<xr::Path>> = HashMap::new();
let mut a_iter = actions.sets.into_iter();
@@ -31,7 +43,7 @@ pub fn setup_oxr_actions(world: &mut World, instance: Ref<XrInstance>) {
ActionType::Bool => TypedAction::Bool(match action.handednes {
ActionHandednes::Single => oxr_action_set
.create_action(action_name, action.pretty_name, &[])
.unwrap(),
.expect(action.pretty_name),
ActionHandednes::Double => oxr_action_set
.create_action(action_name, action.pretty_name, &hands)
.unwrap(),
@@ -44,6 +56,14 @@ pub fn setup_oxr_actions(world: &mut World, instance: Ref<XrInstance>) {
.create_action(action_name, action.pretty_name, &hands)
.unwrap(),
}),
ActionType::Haptic => TypedAction::Haptic(match action.handednes {
ActionHandednes::Single => oxr_action_set
.create_action(action_name, action.pretty_name, &[])
.unwrap(),
ActionHandednes::Double => oxr_action_set
.create_action(action_name, action.pretty_name, &hands)
.unwrap(),
}),
};
actions.insert(action_name, typed_action);
for (device_path, bindings) in action.bindings.into_iter() {
@@ -60,6 +80,7 @@ pub fn setup_oxr_actions(world: &mut World, instance: Ref<XrInstance>) {
ActionSet {
oxr_action_set,
actions,
enabled: true,
},
);
}
@@ -77,6 +98,7 @@ pub fn setup_oxr_actions(world: &mut World, instance: Ref<XrInstance>) {
TypedAction::F32(a) => Binding::new(a, binding),
TypedAction::Bool(a) => Binding::new(a, binding),
TypedAction::PoseF(a) => Binding::new(a, binding),
TypedAction::Haptic(a) => Binding::new(a, binding),
})
.collect::<Vec<_>>(),
)
@@ -86,6 +108,17 @@ pub fn setup_oxr_actions(world: &mut World, instance: Ref<XrInstance>) {
.suggest_interaction_profile_bindings(instance.string_to_path(dev).unwrap(), &bindings)
.unwrap();
}
session
.attach_action_sets(
&action_sets
.sets
.iter()
.map(|(_, set)| &set.oxr_action_set)
.collect::<Vec<_>>(),
)
.unwrap();
world.insert_resource(action_sets);
}
pub enum ActionHandednes {
@@ -97,12 +130,14 @@ pub enum ActionType {
F32,
Bool,
PoseF,
Haptic,
}
pub enum TypedAction {
F32(Action<f32>),
Bool(Action<bool>),
PoseF(Action<Posef>),
Haptic(Action<Haptic>),
}
pub struct SetupAction {
@@ -136,19 +171,30 @@ impl SetupActionSet {
},
);
}
pub fn suggest_binding(
&mut self,
action_name: &'static str,
device_path: &'static str,
action_path: &'static str,
) {
self.actions
.get_mut(action_name)
.unwrap()
.bindings
.entry(device_path)
.or_default()
.push(action_path);
pub fn suggest_binding(&mut self, device_path: &'static str, bindings: &[XrBinding]) {
for binding in bindings {
self.actions
.get_mut(binding.action)
.ok_or(anyhow::anyhow!("Missing Action: {}", binding.action))
.unwrap()
.bindings
.entry(device_path)
.or_default()
.push(binding.path);
}
}
}
pub struct XrBinding {
action: &'static str,
path: &'static str,
}
impl XrBinding {
pub fn new(action_name: &'static str, binding_path: &'static str) -> XrBinding {
XrBinding {
action: action_name,
path: binding_path,
}
}
}
@@ -178,6 +224,7 @@ impl SetupActionSets {
pub struct ActionSet {
oxr_action_set: xr::ActionSet,
enabled: bool,
actions: HashMap<&'static str, TypedAction>,
}
@@ -185,3 +232,74 @@ pub struct ActionSet {
pub struct ActionSets {
sets: HashMap<&'static str, ActionSet>,
}
impl ActionSets {
pub fn get_action_f32(
&self,
action_set: &'static str,
action_name: &'static str,
) -> anyhow::Result<&Action<f32>> {
let action = self
.sets
.get(action_set)
.ok_or(anyhow::anyhow!("Action Set Not Found!"))?
.actions
.get(action_name)
.ok_or(anyhow::anyhow!("Action Not Found!"))?;
match action {
TypedAction::F32(a) => Ok(a),
_ => anyhow::bail!("wrong action type"),
}
}
pub fn get_action_bool(
&self,
action_set: &'static str,
action_name: &'static str,
) -> anyhow::Result<&Action<bool>> {
let action = self
.sets
.get(action_set)
.ok_or(anyhow::anyhow!("Action Set Not Found!"))?
.actions
.get(action_name)
.ok_or(anyhow::anyhow!("Action Not Found!"))?;
match action {
TypedAction::Bool(a) => Ok(a),
_ => anyhow::bail!("wrong action type"),
}
}
pub fn get_action_posef(
&self,
action_set: &'static str,
action_name: &'static str,
) -> anyhow::Result<&Action<Posef>> {
let action = self
.sets
.get(action_set)
.ok_or(anyhow::anyhow!("Action Set Not Found!"))?
.actions
.get(action_name)
.ok_or(anyhow::anyhow!("Action Not Found!"))?;
match action {
TypedAction::PoseF(a) => Ok(a),
_ => anyhow::bail!("wrong action type"),
}
}
pub fn get_action_haptic(
&self,
action_set: &'static str,
action_name: &'static str,
) -> anyhow::Result<&Action<Haptic>> {
let action = self
.sets
.get(action_set)
.ok_or(anyhow::anyhow!("Action Set Not Found!"))?
.actions
.get(action_name)
.ok_or(anyhow::anyhow!("Action Not Found!"))?;
match action {
TypedAction::Haptic(a) => Ok(a),
_ => anyhow::bail!("wrong action type"),
}
}
}

View File

@@ -14,6 +14,7 @@ use crate::xr_input::{
};
use super::{
actions::ActionSets,
handtracking::{HandTrackingRef, HandTrackingTracker},
trackers::{OpenXRLeftController, OpenXRRightController, OpenXRTrackingRoot},
QuatConv,
@@ -54,44 +55,48 @@ pub fn draw_gizmos(
Without<OpenXRLeftController>,
Without<OpenXRTrackingRoot>,
)>,
hand_tracking: Res<HandTrackingTracker>,
hand_tracking: Option<Res<HandTrackingTracker>>,
action_sets: Res<ActionSets>,
) {
let handtracking_ref = hand_tracking.get_ref(&xr_input, &frame_state);
if let Some(joints) = handtracking_ref.get_left_poses() {
for joint in joints {
let p = joint.pose.position;
let r = joint.pose.orientation;
let quat = r.to_quat();
let trans = Transform::from_rotation(quat);
gizmos.circle(
(p.x, p.y, p.z).into(),
trans.forward(),
joint.radius,
Color::ORANGE_RED,
);
if let Some(hand_tracking) = hand_tracking {
let handtracking_ref = hand_tracking.get_ref(&xr_input, &frame_state);
if let Some(joints) = handtracking_ref.get_left_poses() {
for joint in joints {
let p = joint.pose.position;
let r = joint.pose.orientation;
let quat = r.to_quat();
let trans = Transform::from_rotation(quat);
gizmos.circle(
(p.x, p.y, p.z).into(),
trans.forward(),
joint.radius,
Color::ORANGE_RED,
);
}
} else {
info!("left_hand_poses returned None");
}
} else {
info!("left_hand_poses returned None");
}
if let Some(joints) = handtracking_ref.get_right_poses() {
for joint in joints {
let p = joint.pose.position;
let r = joint.pose.orientation;
let quat = r.to_quat();
let trans = Transform::from_rotation(quat);
gizmos.circle(
(p.x, p.y, p.z).into(),
trans.forward(),
joint.radius,
Color::LIME_GREEN,
);
if let Some(joints) = handtracking_ref.get_right_poses() {
for joint in joints {
let p = joint.pose.position;
let r = joint.pose.orientation;
let quat = r.to_quat();
let trans = Transform::from_rotation(quat);
gizmos.circle(
(p.x, p.y, p.z).into(),
trans.forward(),
joint.radius,
Color::LIME_GREEN,
);
}
return;
}
return;
}
//lock frame
let frame_state = *frame_state.lock().unwrap();
//get controller
let controller = oculus_controller.get_ref(&instance, &session, &frame_state, &xr_input);
let controller = oculus_controller.get_ref(&session, &frame_state, &xr_input, &action_sets);
info!("{}",controller.x_button());
//tracking root?
let mut tracking_transform = &Transform::IDENTITY;
let root = tracking_root_query.get_single();

View File

@@ -14,6 +14,7 @@ use crate::{
};
use super::{
actions::ActionSets,
hand_poses::get_simulated_open_hand_transforms,
handtracking::HandTrackingTracker,
oculus_touch::OculusController,
@@ -363,6 +364,7 @@ pub fn update_hand_states(
xr_input: Res<XrInput>,
instance: Res<XrInstance>,
session: Res<XrSession>,
action_sets: Res<ActionSets>,
) {
match hand_states_option {
Some(mut hands) => {
@@ -370,7 +372,7 @@ pub fn update_hand_states(
let frame_state = *frame_state.lock().unwrap();
//get controller
let controller =
oculus_controller.get_ref(&instance, &session, &frame_state, &xr_input);
oculus_controller.get_ref(&session, &frame_state, &xr_input, &action_sets);
//right hand
let squeeze = controller.squeeze(Hand::Right);

View File

@@ -1,28 +1,30 @@
pub mod actions;
pub mod controllers;
pub mod debug_gizmos;
pub mod hand;
pub mod hand_poses;
pub mod handtracking;
pub mod interactions;
pub mod oculus_touch;
pub mod prototype_locomotion;
pub mod trackers;
pub mod xr_camera;
pub mod hand_poses;
pub mod hand;
pub mod handtracking;
pub mod actions;
use crate::resources::XrSession;
use crate::xr_begin_frame;
use crate::xr_input::controllers::XrControllerType;
use crate::xr_input::oculus_touch::{setup_oculus_controller, ActionSets};
use crate::xr_input::oculus_touch::setup_oculus_controller;
use crate::xr_input::xr_camera::{xr_camera_head_sync, Eye, XRProjection, XrCameraBundle};
use bevy::app::{App, PostUpdate, Startup};
use bevy::log::warn;
use bevy::prelude::{BuildChildren, IntoSystemConfigs, Component};
use bevy::prelude::{BuildChildren, Component, IntoSystemConfigs};
use bevy::prelude::{Commands, Plugin, PreUpdate, Quat, Res, SpatialBundle, Update, Vec3};
use bevy::render::camera::CameraProjectionPlugin;
use bevy::render::view::{update_frusta, VisibilitySystems};
use bevy::transform::TransformSystem;
use self::actions::{setup_oxr_actions, OpenXrActionsPlugin};
use self::oculus_touch::post_action_setup_oculus_controller;
use self::trackers::{
adopt_open_xr_trackers, update_open_xr_controllers, OpenXRLeftEye, OpenXRRightEye,
OpenXRTrackingRoot,
@@ -47,6 +49,11 @@ impl OpenXrInput {
impl Plugin for OpenXrInput {
fn build(&self, app: &mut App) {
app.add_plugins(CameraProjectionPlugin::<XRProjection>::default());
app.add_plugins(OpenXrActionsPlugin);
app.add_systems(
PreUpdate,
(post_action_setup_oculus_controller.after(setup_oxr_actions),),
);
match self.controller_type {
XrControllerType::OculusTouch => {
app.add_systems(Startup, setup_oculus_controller);
@@ -54,7 +61,7 @@ impl Plugin for OpenXrInput {
}
//adopt any new trackers
app.add_systems(PreUpdate, adopt_open_xr_trackers);
app.add_systems(PreUpdate, action_set_system);
// app.add_systems(PreUpdate, action_set_system);
app.add_systems(PreUpdate, xr_camera_head_sync.after(xr_begin_frame));
//update controller trackers
app.add_systems(Update, update_open_xr_controllers);
@@ -83,19 +90,19 @@ fn setup_xr_cameras(mut commands: Commands) {
commands.entity(tracking_root).push_children(&[right, left]);
}
fn action_set_system(action_sets: Res<ActionSets>, session: Res<XrSession>) {
let mut active_action_sets = vec![];
for i in &action_sets.0 {
active_action_sets.push(openxr::ActiveActionSet::new(i));
}
//info!("action sets: {:#?}", action_sets.0.len());
match session.sync_actions(&active_action_sets) {
Err(err) => {
warn!("{}", err);
}
_ => {}
}
}
// fn action_set_system(action_sets: Res<ActionSets>, session: Res<XrSession>) {
// let mut active_action_sets = vec![];
// for i in &action_sets.0 {
// active_action_sets.push(openxr::ActiveActionSet::new(i));
// }
// //info!("action sets: {:#?}", action_sets.0.len());
// match session.sync_actions(&active_action_sets) {
// Err(err) => {
// warn!("{}", err);
// }
// _ => {}
// }
// }
pub trait Vec3Conv {
fn to_vec3(&self) -> Vec3;

View File

@@ -2,39 +2,81 @@ use crate::input::XrInput;
use crate::resources::{XrInstance, XrSession};
use crate::xr_input::controllers::{Handed, Touchable};
use crate::xr_input::Hand;
use bevy::prelude::{Commands, Res, Resource};
use bevy::prelude::{Commands, Res, ResMut, Resource};
use openxr::{
Action, ActionSet, AnyGraphics, Binding, FrameState, Haptic, Instance, Path, Posef, Session,
Space, SpaceLocation, SpaceVelocity,
};
use std::convert::identity;
use std::sync::OnceLock;
pub fn setup_oculus_controller(
mut commands: Commands,
use super::actions::{ActionHandednes, ActionSets, ActionType, SetupActionSets, XrBinding};
pub fn post_action_setup_oculus_controller(
action_sets: Res<ActionSets>,
mut controller: ResMut<OculusController>,
instance: Res<XrInstance>,
session: Res<XrSession>,
) {
let mut action_sets = vec![];
let oculus_controller = OculusController::new(
Instance::clone(&instance),
Session::clone(&session),
&mut action_sets,
)
.unwrap();
session
.attach_action_sets(&action_sets.iter().map(|a| a).collect::<Vec<_>>())
let s = Session::<AnyGraphics>::clone(&session);
let left_path = instance.string_to_path("/user/hand/left").unwrap();
let right_path = instance.string_to_path("/user/hand/right").unwrap();
let grip_action = action_sets
.get_action_posef("oculus_input", "hand_pose")
.unwrap();
let aim_action = action_sets
.get_action_posef("oculus_input", "pointer_pose")
.unwrap();
controller.grip_space = Some(Handed {
left: grip_action
.create_space(
s.clone(),
left_path,
Posef::IDENTITY,
)
.unwrap(),
right: grip_action
.create_space(
s.clone(),
right_path,
Posef::IDENTITY,
)
.unwrap(),
});
controller.aim_space = Some(Handed {
left: aim_action
.create_space(
s.clone(),
left_path,
Posef::IDENTITY,
)
.unwrap(),
right: aim_action
.create_space(
s.clone(),
right_path,
Posef::IDENTITY,
)
.unwrap(),
})
}
pub fn setup_oculus_controller(
mut commands: Commands,
instance: Res<XrInstance>,
mut action_sets: ResMut<SetupActionSets>,
) {
let oculus_controller = OculusController::new(action_sets).unwrap();
init_subaction_path(&instance);
commands.insert_resource(oculus_controller);
commands.insert_resource(ActionSets(action_sets));
}
#[derive(Resource, Clone)]
pub struct ActionSets(pub Vec<ActionSet>);
// #[derive(Resource, Clone)]
// pub struct ActionSets(pub Vec<ActionSet>);
pub struct OculusControllerRef<'a> {
oculus_controller: &'a OculusController,
instance: &'a Instance,
action_sets: &'a ActionSets,
session: &'a Session<AnyGraphics>,
frame_state: &'a FrameState,
xr_input: &'a XrInput,
@@ -58,11 +100,11 @@ pub fn subaction_path(hand: Hand) -> Path {
impl OculusControllerRef<'_> {
pub fn grip_space(&self, hand: Hand) -> (SpaceLocation, SpaceVelocity) {
match hand {
Hand::Left => self.oculus_controller.grip_space.left.relate(
Hand::Left => self.oculus_controller.grip_space.as_ref().unwrap().left.relate(
&self.xr_input.stage,
self.frame_state.predicted_display_time,
),
Hand::Right => self.oculus_controller.grip_space.right.relate(
Hand::Right => self.oculus_controller.grip_space.as_ref().unwrap().right.relate(
&self.xr_input.stage,
self.frame_state.predicted_display_time,
),
@@ -71,11 +113,11 @@ impl OculusControllerRef<'_> {
}
pub fn aim_space(&self, hand: Hand) -> (SpaceLocation, SpaceVelocity) {
match hand {
Hand::Left => self.oculus_controller.aim_space.left.relate(
Hand::Left => self.oculus_controller.aim_space.as_ref().unwrap().left.relate(
&self.xr_input.stage,
self.frame_state.predicted_display_time,
),
Hand::Right => self.oculus_controller.aim_space.right.relate(
Hand::Right => self.oculus_controller.aim_space.as_ref().unwrap().right.relate(
&self.xr_input.stage,
self.frame_state.predicted_display_time,
),
@@ -83,102 +125,107 @@ impl OculusControllerRef<'_> {
.unwrap()
}
pub fn squeeze(&self, hand: Hand) -> f32 {
let action = &self.oculus_controller.squeeze;
let action = &self
.action_sets
.get_action_f32("oculus_input", "squeeze")
.unwrap();
action
.state(&self.session, subaction_path(hand))
.unwrap()
.current_state
}
pub fn trigger(&self, hand: Hand) -> f32 {
self.oculus_controller
.trigger
.inner
self.action_sets
.get_action_f32("oculus_input", "trigger")
.unwrap()
.state(&self.session, subaction_path(hand))
.unwrap()
.current_state
}
pub fn trigger_touched(&self, hand: Hand) -> bool {
self.oculus_controller
.trigger
.touch
self.action_sets
.get_action_bool("oculus_input", "trigger_touched")
.unwrap()
.state(&self.session, subaction_path(hand))
.unwrap()
.current_state
}
pub fn x_button(&self) -> bool {
self.oculus_controller
.x_button
.inner
self.action_sets
.get_action_bool("oculus_input", "x_button")
.unwrap()
.state(&self.session, Path::NULL)
.unwrap()
.current_state
}
pub fn x_button_touched(&self) -> bool {
self.oculus_controller
.x_button
.touch
self.action_sets
.get_action_bool("oculus_input", "x_button_touch")
.unwrap()
.state(&self.session, Path::NULL)
.unwrap()
.current_state
}
pub fn y_button(&self) -> bool {
self.oculus_controller
.y_button
.inner
self.action_sets
.get_action_bool("oculus_input", "y_button")
.unwrap()
.state(&self.session, Path::NULL)
.unwrap()
.current_state
}
pub fn y_button_touched(&self) -> bool {
self.oculus_controller
.y_button
.touch
self.action_sets
.get_action_bool("oculus_input", "y_button_touch")
.unwrap()
.state(&self.session, Path::NULL)
.unwrap()
.current_state
}
pub fn menu_button(&self) -> bool {
self.oculus_controller
.menu_button
self.action_sets
.get_action_bool("oculus_input", "menu_button")
.unwrap()
.state(&self.session, Path::NULL)
.unwrap()
.current_state
}
pub fn a_button(&self) -> bool {
self.oculus_controller
.a_button
.inner
self.action_sets
.get_action_bool("oculus_input", "a_button")
.unwrap()
.state(&self.session, Path::NULL)
.unwrap()
.current_state
}
pub fn a_button_touched(&self) -> bool {
self.oculus_controller
.a_button
.touch
self.action_sets
.get_action_bool("oculus_input", "a_button_touch")
.unwrap()
.state(&self.session, Path::NULL)
.unwrap()
.current_state
}
pub fn b_button(&self) -> bool {
self.oculus_controller
.b_button
.inner
self.action_sets
.get_action_bool("oculus_input", "b_button")
.unwrap()
.state(&self.session, Path::NULL)
.unwrap()
.current_state
}
pub fn b_button_touched(&self) -> bool {
self.oculus_controller
.b_button
.touch
self.action_sets
.get_action_bool("oculus_input", "b_button_touch")
.unwrap()
.state(&self.session, Path::NULL)
.unwrap()
.current_state
}
pub fn thumbstick_touch(&self, hand: Hand) -> bool {
self.oculus_controller
.thumbstick_touch
self.action_sets
.get_action_bool("oculus_input", "thumbstick_touch")
.unwrap()
.state(&self.session, subaction_path(hand))
.unwrap()
.current_state
@@ -186,28 +233,32 @@ impl OculusControllerRef<'_> {
pub fn thumbstick(&self, hand: Hand) -> Thumbstick {
Thumbstick {
x: self
.oculus_controller
.thumbstick_x
.action_sets
.get_action_f32("oculus_input", "thumbstick_x")
.unwrap()
.state(&self.session, subaction_path(hand))
.unwrap()
.current_state,
y: self
.oculus_controller
.thumbstick_y
.action_sets
.get_action_f32("oculus_input", "thumbstick_y")
.unwrap()
.state(&self.session, subaction_path(hand))
.unwrap()
.current_state,
click: self
.oculus_controller
.thumbstick_click
.action_sets
.get_action_bool("oculus_input", "thumbstick_click")
.unwrap()
.state(&self.session, subaction_path(hand))
.unwrap()
.current_state,
}
}
pub fn thumbrest_touch(&self, hand: Hand) -> bool {
self.oculus_controller
.thumbrest_touch
self.action_sets
.get_action_bool("oculus_input", "thumbrest_touch")
.unwrap()
.state(&self.session, subaction_path(hand))
.unwrap()
.current_state
@@ -224,244 +275,197 @@ pub struct Thumbstick {
impl OculusController {
pub fn get_ref<'a>(
&'a self,
instance: &'a Instance,
session: &'a Session<AnyGraphics>,
frame_state: &'a FrameState,
xr_input: &'a XrInput,
action_sets: &'a ActionSets,
) -> OculusControllerRef {
OculusControllerRef {
oculus_controller: self,
instance,
session,
frame_state,
xr_input,
action_sets,
}
}
}
#[derive(Resource)]
pub struct OculusController {
pub grip_space: Handed<Space>,
pub aim_space: Handed<Space>,
pub grip_pose: Action<Posef>,
pub aim_pose: Action<Posef>,
pub squeeze: Action<f32>,
pub trigger: Touchable<f32>,
pub haptic_feedback: Action<Haptic>,
pub x_button: Touchable<bool>,
pub y_button: Touchable<bool>,
pub menu_button: Action<bool>,
pub a_button: Touchable<bool>,
pub b_button: Touchable<bool>,
pub thumbstick_x: Action<f32>,
pub thumbstick_y: Action<f32>,
pub thumbstick_touch: Action<bool>,
pub thumbstick_click: Action<bool>,
pub thumbrest_touch: Action<bool>,
pub grip_space: Option<Handed<Space>>,
pub aim_space: Option<Handed<Space>>,
}
impl OculusController {
pub fn new(
instance: Instance,
session: Session<AnyGraphics>,
action_sets: &mut Vec<ActionSet>,
) -> anyhow::Result<Self> {
pub fn new(mut action_sets: ResMut<SetupActionSets>) -> anyhow::Result<Self> {
let action_set =
instance.create_action_set("oculus_input", "Oculus Touch Controller Input", 0)?;
init_subaction_path(&instance);
let left_path = instance.string_to_path("/user/hand/left").unwrap();
let right_path = instance.string_to_path("/user/hand/right").unwrap();
let hands = [left_path, right_path];
let grip_pose = action_set.create_action::<Posef>("hand_pose", "Hand Pose", &hands)?;
let aim_pose = action_set.create_action::<Posef>("pointer_pose", "Pointer Pose", &hands)?;
action_sets.add_action_set("oculus_input", "Oculus Touch Controller Input", 0);
action_set.new_action(
"hand_pose",
"Hand Pose",
ActionType::PoseF,
ActionHandednes::Double,
);
action_set.new_action(
"pointer_pose",
"Pointer Pose",
ActionType::PoseF,
ActionHandednes::Double,
);
action_set.new_action(
"squeeze",
"Grip Pull",
ActionType::F32,
ActionHandednes::Double,
);
action_set.new_action(
"trigger",
"Trigger Pull",
ActionType::F32,
ActionHandednes::Double,
);
action_set.new_action(
"trigger_touched",
"Trigger Touch",
ActionType::Bool,
ActionHandednes::Double,
);
action_set.new_action(
"haptic_feedback",
"Haptic Feedback",
ActionType::Haptic,
ActionHandednes::Double,
);
action_set.new_action(
"x_button",
"X Button",
ActionType::Bool,
ActionHandednes::Single,
);
action_set.new_action(
"x_button_touch",
"X Button Touch",
ActionType::Bool,
ActionHandednes::Single,
);
action_set.new_action(
"y_button",
"Y Button",
ActionType::Bool,
ActionHandednes::Single,
);
action_set.new_action(
"y_button_touch",
"Y Button Touch",
ActionType::Bool,
ActionHandednes::Single,
);
action_set.new_action(
"a_button",
"A Button",
ActionType::Bool,
ActionHandednes::Single,
);
action_set.new_action(
"a_button_touch",
"A Button Touch",
ActionType::Bool,
ActionHandednes::Single,
);
action_set.new_action(
"b_button",
"B Button",
ActionType::Bool,
ActionHandednes::Single,
);
action_set.new_action(
"b_button_touch",
"B Button Touch",
ActionType::Bool,
ActionHandednes::Single,
);
action_set.new_action(
"menu_button",
"Menu Button",
ActionType::Bool,
ActionHandednes::Single,
);
action_set.new_action(
"thumbstick_x",
"Thumbstick X",
ActionType::F32,
ActionHandednes::Double,
);
action_set.new_action(
"thumbstick_y",
"Thumbstick y",
ActionType::F32,
ActionHandednes::Double,
);
action_set.new_action(
"thumbstick_touch",
"Thumbstick Touch",
ActionType::Bool,
ActionHandednes::Double,
);
action_set.new_action(
"thumbstick_click",
"Thumbstick Click",
ActionType::Bool,
ActionHandednes::Double,
);
action_set.new_action(
"thumbrest_touch",
"Thumbrest Touch",
ActionType::Bool,
ActionHandednes::Double,
);
let this = OculusController {
grip_space: Handed {
left: grip_pose.create_space(session.clone(), left_path, Posef::IDENTITY)?,
right: grip_pose.create_space(session.clone(), right_path, Posef::IDENTITY)?,
},
aim_space: Handed {
left: aim_pose.create_space(session.clone(), left_path, Posef::IDENTITY)?,
right: aim_pose.create_space(session.clone(), right_path, Posef::IDENTITY)?,
},
grip_pose,
aim_pose,
squeeze: action_set.create_action("squeeze", "Grip Pull", &hands)?,
trigger: Touchable {
inner: action_set.create_action("trigger", "Trigger Pull", &hands)?,
touch: action_set.create_action("trigger_touched", "Trigger Touch", &hands)?,
},
haptic_feedback: action_set.create_action(
"haptic_feedback",
"Haptic Feedback",
&hands,
)?,
x_button: Touchable {
inner: action_set.create_action("x_button", "X Button", &[])?,
touch: action_set.create_action("x_button_touch", "X Button Touch", &[])?,
},
y_button: Touchable {
inner: action_set.create_action("y_button", "Y Button", &[])?,
touch: action_set.create_action("y_button_touch", "Y Button Touch", &[])?,
},
menu_button: action_set.create_action("menu_button", "Menu Button", &[])?,
a_button: Touchable {
inner: action_set.create_action("a_button", "A Button", &[])?,
touch: action_set.create_action("a_button_touch", "A Button Touch", &[])?,
},
b_button: Touchable {
inner: action_set.create_action("b_button", "B Button", &[])?,
touch: action_set.create_action("b_button_touch", "B Button Touch", &[])?,
},
thumbstick_x: action_set.create_action("thumbstick_x", "Thumbstick X", &hands)?,
thumbstick_y: action_set.create_action("thumbstick_y", "Thumbstick Y", &hands)?,
thumbstick_touch: action_set.create_action(
"thumbstick_touch",
"Thumbstick Touch",
&hands,
)?,
thumbstick_click: action_set.create_action(
"thumbstick_click",
"Thumbstick Click",
&hands,
)?,
thumbrest_touch: action_set.create_action(
"thumbrest_touch",
"Thumbrest Touch",
&hands,
)?,
grip_space: None,
aim_space: None,
};
let i = instance;
i.suggest_interaction_profile_bindings(
i.string_to_path("/interaction_profiles/oculus/touch_controller")?,
action_set.suggest_binding(
"/interaction_profiles/oculus/touch_controller",
&[
Binding::new(
&this.grip_pose,
i.string_to_path("/user/hand/left/input/grip/pose")?,
XrBinding::new("hand_pose", "/user/hand/left/input/grip/pose"),
XrBinding::new("hand_pose", "/user/hand/right/input/grip/pose"),
XrBinding::new("pointer_pose", "/user/hand/left/input/aim/pose"),
XrBinding::new("pointer_pose", "/user/hand/right/input/aim/pose"),
XrBinding::new("squeeze", "/user/hand/left/input/squeeze/value"),
XrBinding::new("squeeze", "/user/hand/right/input/squeeze/value"),
XrBinding::new("trigger", "/user/hand/left/input/trigger/value"),
XrBinding::new("trigger", "/user/hand/right/input/trigger/value"),
XrBinding::new("trigger_touched", "/user/hand/left/input/trigger/touch"),
XrBinding::new("trigger_touched", "/user/hand/right/input/trigger/touch"),
XrBinding::new("haptic_feedback", "/user/hand/left/output/haptic"),
XrBinding::new("haptic_feedback", "/user/hand/right/output/haptic"),
XrBinding::new("x_button", "/user/hand/left/input/x/click"),
XrBinding::new("x_button_touch", "/user/hand/left/input/x/touch"),
XrBinding::new("y_button", "/user/hand/left/input/y/click"),
XrBinding::new("y_button_touch", "/user/hand/left/input/y/touch"),
XrBinding::new("a_button", "/user/hand/right/input/a/click"),
XrBinding::new("a_button_touch", "/user/hand/right/input/a/touch"),
XrBinding::new("b_button", "/user/hand/right/input/b/click"),
XrBinding::new("b_button_touch", "/user/hand/right/input/b/touch"),
XrBinding::new("menu_button", "/user/hand/left/input/menu/click"),
XrBinding::new("thumbstick_x", "/user/hand/left/input/thumbstick/x"),
XrBinding::new("thumbstick_y", "/user/hand/left/input/thumbstick/y"),
XrBinding::new("thumbstick_x", "/user/hand/right/input/thumbstick/x"),
XrBinding::new("thumbstick_y", "/user/hand/right/input/thumbstick/y"),
XrBinding::new("thumbstick_click", "/user/hand/left/input/thumbstick/click"),
XrBinding::new(
"thumbstick_click",
"/user/hand/right/input/thumbstick/click",
),
Binding::new(
&this.grip_pose,
i.string_to_path("/user/hand/right/input/grip/pose")?,
),
Binding::new(
&this.aim_pose,
i.string_to_path("/user/hand/left/input/aim/pose")?,
),
Binding::new(
&this.aim_pose,
i.string_to_path("/user/hand/left/input/aim/pose")?,
),
Binding::new(
&this.squeeze,
i.string_to_path("/user/hand/left/input/squeeze/value")?,
),
Binding::new(
&this.squeeze,
i.string_to_path("/user/hand/right/input/squeeze/value")?,
),
Binding::new(
&this.trigger.inner,
i.string_to_path("/user/hand/right/input/trigger/value")?,
),
Binding::new(
&this.trigger.inner,
i.string_to_path("/user/hand/left/input/trigger/value")?,
),
Binding::new(
&this.trigger.touch,
i.string_to_path("/user/hand/right/input/trigger/touch")?,
),
Binding::new(
&this.trigger.touch,
i.string_to_path("/user/hand/left/input/trigger/touch")?,
),
Binding::new(
&this.haptic_feedback,
i.string_to_path("/user/hand/right/output/haptic")?,
),
Binding::new(
&this.haptic_feedback,
i.string_to_path("/user/hand/left/output/haptic")?,
),
Binding::new(
&this.x_button.inner,
i.string_to_path("/user/hand/left/input/x/click")?,
),
Binding::new(
&this.x_button.touch,
i.string_to_path("/user/hand/left/input/x/touch")?,
),
Binding::new(
&this.y_button.inner,
i.string_to_path("/user/hand/left/input/y/click")?,
),
Binding::new(
&this.y_button.touch,
i.string_to_path("/user/hand/left/input/y/touch")?,
),
Binding::new(
&this.menu_button,
i.string_to_path("/user/hand/left/input/menu/click")?,
),
Binding::new(
&this.a_button.inner,
i.string_to_path("/user/hand/right/input/a/click")?,
),
Binding::new(
&this.a_button.touch,
i.string_to_path("/user/hand/right/input/a/touch")?,
),
Binding::new(
&this.b_button.inner,
i.string_to_path("/user/hand/right/input/b/click")?,
),
Binding::new(
&this.b_button.touch,
i.string_to_path("/user/hand/right/input/b/touch")?,
),
Binding::new(
&this.thumbstick_x,
i.string_to_path("/user/hand/left/input/thumbstick/x")?,
),
Binding::new(
&this.thumbstick_x,
i.string_to_path("/user/hand/right/input/thumbstick/x")?,
),
Binding::new(
&this.thumbstick_y,
i.string_to_path("/user/hand/left/input/thumbstick/y")?,
),
Binding::new(
&this.thumbstick_y,
i.string_to_path("/user/hand/right/input/thumbstick/y")?,
),
Binding::new(
&this.thumbstick_click,
i.string_to_path("/user/hand/left/input/thumbstick/click")?,
),
Binding::new(
&this.thumbstick_click,
i.string_to_path("/user/hand/right/input/thumbstick/click")?,
),
Binding::new(
&this.thumbstick_touch,
i.string_to_path("/user/hand/left/input/thumbstick/touch")?,
),
Binding::new(
&this.thumbstick_touch,
i.string_to_path("/user/hand/right/input/thumbstick/touch")?,
),
Binding::new(
&this.thumbrest_touch,
i.string_to_path("/user/hand/left/input/thumbrest/touch")?,
),
Binding::new(
&this.thumbrest_touch,
i.string_to_path("/user/hand/right/input/thumbrest/touch")?,
XrBinding::new("thumbstick_touch", "/user/hand/left/input/thumbstick/touch"),
XrBinding::new(
"thumbstick_touch",
"/user/hand/right/input/thumbstick/touch",
),
XrBinding::new("thumbrest_touch", "/user/hand/left/input/thumbrest/touch"),
XrBinding::new("thumbrest_touch", "/user/hand/right/input/thumbrest/touch"),
],
)?;
action_sets.push(action_set);
);
Ok(this)
}
}

View File

@@ -11,7 +11,8 @@ use crate::{
};
use super::{
oculus_touch::OculusController, trackers::OpenXRTrackingRoot, Hand, QuatConv, Vec3Conv,
actions::ActionSets, oculus_touch::OculusController, trackers::OpenXRTrackingRoot, Hand,
QuatConv, Vec3Conv,
};
pub enum LocomotionType {
@@ -67,6 +68,7 @@ pub fn proto_locomotion(
views: ResMut<XrViews>,
mut gizmos: Gizmos,
config_option: Option<ResMut<PrototypeLocomotionConfig>>,
action_sets: Res<ActionSets>,
) {
match config_option {
Some(_) => (),
@@ -80,7 +82,7 @@ pub fn proto_locomotion(
//lock frame
let frame_state = *frame_state.lock().unwrap();
//get controller
let controller = oculus_controller.get_ref(&instance, &session, &frame_state, &xr_input);
let controller = oculus_controller.get_ref(&session, &frame_state, &xr_input, &action_sets);
let root = tracking_root_query.get_single_mut();
match root {
Ok(mut position) => {

View File

@@ -8,7 +8,7 @@ use crate::{
resources::{XrFrameState, XrInstance, XrSession},
};
use super::{oculus_touch::OculusController, Hand, QuatConv, Vec3Conv};
use super::{actions::ActionSets, oculus_touch::OculusController, Hand, QuatConv, Vec3Conv};
#[derive(Component)]
pub struct OpenXRTrackingRoot;
@@ -62,14 +62,14 @@ pub fn update_open_xr_controllers(
Without<OpenXRLeftController>,
)>,
frame_state: Res<XrFrameState>,
instance: Res<XrInstance>,
xr_input: Res<XrInput>,
session: Res<XrSession>,
action_sets: Res<ActionSets>,
) {
//lock dat frame?
let frame_state = *frame_state.lock().unwrap();
//get controller
let controller = oculus_controller.get_ref(&instance, &session, &frame_state, &xr_input);
let controller = oculus_controller.get_ref(&session, &frame_state, &xr_input, &action_sets);
//get left controller
let left_grip_space = controller.grip_space(Hand::Left);
let left_aim_space = controller.aim_space(Hand::Left);