refactored grips as well

This commit is contained in:
ForTehLose
2024-09-03 15:13:08 -04:00
parent fa9b797610
commit f30c590cc7
3 changed files with 141 additions and 111 deletions

View File

@@ -1,45 +1,20 @@
//! A simple 3D scene with light shining over a cube sitting on a plane.
use std::ops::Deref;
use bevy::prelude::*;
use bevy_mod_openxr::{
action_binding::{OxrSendActionBindings, OxrSuggestActionBinding},
action_set_attaching::OxrAttachActionSet,
action_set_syncing::{OxrActionSetSyncSet, OxrSyncActionSet},
add_xr_plugins,
helper_traits::{ToQuat, ToVec3},
resources::{OxrFrameState, OxrInstance, Pipelined},
session::OxrSession,
spaces::{OxrSpaceExt, OxrSpaceLocationFlags, OxrSpaceSyncSet},
};
use bevy_mod_xr::{
session::{session_available, session_running, XrSessionCreated, XrTrackingRoot},
spaces::{XrPrimaryReferenceSpace, XrReferenceSpace, XrSpace},
types::XrPose,
};
use bevy_mod_openxr::add_xr_plugins;
use bevy_mod_xr::session::{XrSessionCreated, XrTrackingRoot};
use bevy_xr_utils::tracking_utils::{
TrackingUtilitiesPlugin, XRTrackedLocalFloor, XRTrackedStage, XRTrackedView,
TrackingUtilitiesPlugin, XRTrackedLeftGrip, XRTrackedLocalFloor, XRTrackedRightGrip,
XRTrackedStage, XRTrackedView,
};
use openxr::Posef;
fn main() {
let mut app = App::new();
app.add_plugins(add_xr_plugins(DefaultPlugins));
app.add_systems(Startup, setup);
//create bindings
app.add_systems(OxrSendActionBindings, suggest_action_bindings);
//sync actions
app.add_systems(
PreUpdate,
sync_actions
.before(OxrActionSetSyncSet)
.run_if(session_running),
);
//things?
app.add_systems(XrSessionCreated, spawn_hands);
app.add_systems(XrSessionCreated, attach_set);
app.add_systems(Startup, create_actions.run_if(session_available));
//tracking utils plugin
app.add_plugins(TrackingUtilitiesPlugin);
@@ -82,72 +57,12 @@ fn setup(
});
}
#[derive(Resource)]
struct ControllerActions {
set: openxr::ActionSet,
left: openxr::Action<Posef>,
right: openxr::Action<Posef>,
}
fn suggest_action_bindings(
actions: Res<ControllerActions>,
mut bindings: EventWriter<OxrSuggestActionBinding>,
) {
bindings.send(OxrSuggestActionBinding {
action: actions.left.as_raw(),
interaction_profile: "/interaction_profiles/oculus/touch_controller".into(),
bindings: vec!["/user/hand/left/input/grip/pose".into()],
});
bindings.send(OxrSuggestActionBinding {
action: actions.right.as_raw(),
interaction_profile: "/interaction_profiles/oculus/touch_controller".into(),
bindings: vec!["/user/hand/right/input/grip/pose".into()],
});
}
fn sync_actions(actions: Res<ControllerActions>, mut sync: EventWriter<OxrSyncActionSet>) {
sync.send(OxrSyncActionSet(actions.set.clone()));
}
fn attach_set(actions: Res<ControllerActions>, mut attach: EventWriter<OxrAttachActionSet>) {
attach.send(OxrAttachActionSet(actions.set.clone()));
}
fn create_actions(instance: Res<OxrInstance>, mut cmds: Commands) {
let set = instance.create_action_set("hands", "Hands", 0).unwrap();
let left = set
.create_action("left_pose", "Left Hand Grip Pose", &[])
.unwrap();
let right = set
.create_action("right_pose", "Right Hand Grip Pose", &[])
.unwrap();
cmds.insert_resource(ControllerActions { set, left, right })
}
fn spawn_hands(
actions: Res<ControllerActions>,
mut cmds: Commands,
root: Query<Entity, With<XrTrackingRoot>>,
session: Res<OxrSession>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// This is a demonstation of how to integrate with the openxr crate, the right space is the
// recommended way
let left_space = XrSpace::from_openxr_space(
actions
.left
.create_space(
session.deref().deref().clone(),
openxr::Path::NULL,
Posef::IDENTITY,
)
.unwrap(),
);
let right_space = session
.create_action_space(&actions.right, openxr::Path::NULL, XrPose::IDENTITY)
.unwrap();
let left = cmds
.spawn((
PbrBundle {
@@ -156,20 +71,19 @@ fn spawn_hands(
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..default()
},
left_space,
XRTrackedLeftGrip,
))
.id();
let right = cmds
.spawn((
let bundle = (
PbrBundle {
mesh: meshes.add(Cuboid::new(0.1, 0.1, 0.05)),
material: materials.add(Color::srgb_u8(124, 144, 255)),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..default()
},
right_space,
))
.id();
XRTrackedRightGrip,
);
let right = cmds.spawn(bundle).id();
//head?
let head = cmds
@@ -211,4 +125,3 @@ fn spawn_hands(
cmds.entity(root.single())
.push_children(&[left, right, head, local_floor]);
}

View File

@@ -140,7 +140,7 @@ impl OxrSpaceVelocityFlags {
}
#[allow(clippy::type_complexity)]
fn update_space_transforms(
pub fn update_space_transforms(
session: Res<OxrSession>,
default_ref_space: Res<XrPrimaryReferenceSpace>,
pipelined: Option<Res<Pipelined>>,

View File

@@ -1,13 +1,19 @@
use std::ops::Deref;
use bevy::prelude::*;
use bevy_mod_openxr::{
action_binding::{OxrSendActionBindings, OxrSuggestActionBinding},
action_set_attaching::OxrAttachActionSet,
action_set_syncing::{OxrActionSetSyncSet, OxrSyncActionSet},
helper_traits::{ToQuat, ToVec3},
resources::{OxrFrameState, Pipelined},
resources::{OxrFrameState, OxrInstance, Pipelined},
session::OxrSession,
spaces::{OxrSpaceLocationFlags, OxrSpaceSyncSet},
spaces::{update_space_transforms, OxrSpaceExt, OxrSpaceLocationFlags, OxrSpaceSyncSet},
};
use bevy_mod_xr::{
session::{session_running, XrSessionCreated, XrTrackingRoot},
spaces::{XrPrimaryReferenceSpace, XrReferenceSpace},
session::{session_available, session_running, XrSessionCreated, XrTrackingRoot},
spaces::{XrPrimaryReferenceSpace, XrReferenceSpace, XrSpace},
types::XrPose,
};
use openxr::Posef;
@@ -25,7 +31,7 @@ pub struct XRTrackedView;
pub struct XRTrackedLeftGrip;
#[derive(Component)]
pub struct XRTRackedRightGrip;
pub struct XRTrackedRightGrip;
pub struct TrackingUtilitiesPlugin;
@@ -60,6 +66,28 @@ impl Plugin for TrackingUtilitiesPlugin {
PreUpdate,
update_local_floor.after(update_local_floor_transforms),
);
//bindings
app.add_systems(OxrSendActionBindings, suggest_action_bindings);
//sync actions
app.add_systems(
PreUpdate,
sync_actions
.before(OxrActionSetSyncSet)
.run_if(session_running),
);
//attach sets
app.add_systems(XrSessionCreated, attach_set);
//create actions
app.add_systems(Startup, create_actions.run_if(session_available));
//left grip
// .add_systems(
// PreUpdate,
// update_space_transforms
// .in_set(OxrSpaceSyncSet)
// .run_if(session_running),
// )
app.add_systems(PreUpdate, update_left_grip.after(update_space_transforms));
app.add_systems(PreUpdate, update_right_grip.after(update_space_transforms));
}
}
@@ -167,6 +195,44 @@ fn update_local_floor(
}
}
//left grip
#[derive(Component)]
struct LeftGrip;
fn update_left_grip(
mut left_grip: Query<&mut Transform, (With<LeftGrip>, Without<XRTrackedLeftGrip>)>,
mut tracked_left_grip: Query<&mut Transform, (With<XRTrackedLeftGrip>, Without<LeftGrip>)>,
) {
let head_transform = left_grip.get_single_mut();
match head_transform {
Ok(head) => {
for (mut transform) in &mut tracked_left_grip {
*transform = head.clone();
}
}
Err(_) => (),
}
}
//right grip
#[derive(Component)]
struct RightGrip;
fn update_right_grip(
mut right_grip: Query<&mut Transform, (With<RightGrip>, Without<XRTrackedRightGrip>)>,
mut tracked_right_grip: Query<&mut Transform, (With<XRTrackedRightGrip>, Without<RightGrip>)>,
) {
let head_transform = right_grip.get_single_mut();
match head_transform {
Ok(head) => {
for (mut transform) in &mut tracked_right_grip {
*transform = head.clone();
}
}
Err(_) => (),
}
}
//tracking rig
#[derive(Resource)]
struct ControllerActions {
@@ -176,9 +242,9 @@ struct ControllerActions {
}
fn spawn_tracking_rig(
// actions: Res<ControllerActions>,
actions: Res<ControllerActions>,
mut cmds: Commands,
// root: Query<Entity, With<XrTrackingRoot>>,
root: Query<Entity, With<XrTrackingRoot>>,
session: Res<OxrSession>,
) {
//head
@@ -189,4 +255,55 @@ fn spawn_tracking_rig(
.spawn((SpatialBundle { ..default() }, HeadXRSpace(head_space)))
.id();
let local_floor = cmds.spawn((SpatialBundle { ..default() }, LocalFloor)).id();
let left_space = session
.create_action_space(&actions.left, openxr::Path::NULL, XrPose::IDENTITY)
.unwrap();
let right_space = session
.create_action_space(&actions.right, openxr::Path::NULL, XrPose::IDENTITY)
.unwrap();
let left = cmds
.spawn((SpatialBundle { ..default() }, left_space, LeftGrip))
.id();
let right = cmds
.spawn((SpatialBundle { ..default() }, right_space, RightGrip))
.id();
}
//bindings
//TODO figure out how to make these better
fn suggest_action_bindings(
actions: Res<ControllerActions>,
mut bindings: EventWriter<OxrSuggestActionBinding>,
) {
bindings.send(OxrSuggestActionBinding {
action: actions.left.as_raw(),
interaction_profile: "/interaction_profiles/oculus/touch_controller".into(),
bindings: vec!["/user/hand/left/input/grip/pose".into()],
});
bindings.send(OxrSuggestActionBinding {
action: actions.right.as_raw(),
interaction_profile: "/interaction_profiles/oculus/touch_controller".into(),
bindings: vec!["/user/hand/right/input/grip/pose".into()],
});
}
fn sync_actions(actions: Res<ControllerActions>, mut sync: EventWriter<OxrSyncActionSet>) {
sync.send(OxrSyncActionSet(actions.set.clone()));
}
fn attach_set(actions: Res<ControllerActions>, mut attach: EventWriter<OxrAttachActionSet>) {
attach.send(OxrAttachActionSet(actions.set.clone()));
}
fn create_actions(instance: Res<OxrInstance>, mut cmds: Commands) {
let set = instance.create_action_set("hands", "Hands", 0).unwrap();
let left = set
.create_action("left_pose", "Left Hand Grip Pose", &[])
.unwrap();
let right = set
.create_action("right_pose", "Right Hand Grip Pose", &[])
.unwrap();
cmds.insert_resource(ControllerActions { set, left, right })
}