From f30c590cc77435f7a8db9426d6a0a0c2da836bcd Mon Sep 17 00:00:00 2001 From: ForTehLose Date: Tue, 3 Sep 2024 15:13:08 -0400 Subject: [PATCH] refactored grips as well --- .../bevy_openxr/examples/common_entities.rs | 119 +++------------- crates/bevy_openxr/src/openxr/spaces.rs | 2 +- crates/bevy_xr_utils/src/tracking_utils.rs | 131 +++++++++++++++++- 3 files changed, 141 insertions(+), 111 deletions(-) diff --git a/crates/bevy_openxr/examples/common_entities.rs b/crates/bevy_openxr/examples/common_entities.rs index 595b2bf..be14890 100644 --- a/crates/bevy_openxr/examples/common_entities.rs +++ b/crates/bevy_openxr/examples/common_entities.rs @@ -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, - right: openxr::Action, -} - -fn suggest_action_bindings( - actions: Res, - mut bindings: EventWriter, -) { - 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, mut sync: EventWriter) { - sync.send(OxrSyncActionSet(actions.set.clone())); -} - -fn attach_set(actions: Res, mut attach: EventWriter) { - attach.send(OxrAttachActionSet(actions.set.clone())); -} - -fn create_actions(instance: Res, 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, mut cmds: Commands, root: Query>, - session: Res, mut meshes: ResMut>, mut materials: ResMut>, ) { - // 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, - )) - .id(); - let right = cmds - .spawn(( - 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, + XRTrackedLeftGrip, )) .id(); + 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() + }, + 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]); } - diff --git a/crates/bevy_openxr/src/openxr/spaces.rs b/crates/bevy_openxr/src/openxr/spaces.rs index 028cc42..2e80425 100644 --- a/crates/bevy_openxr/src/openxr/spaces.rs +++ b/crates/bevy_openxr/src/openxr/spaces.rs @@ -140,7 +140,7 @@ impl OxrSpaceVelocityFlags { } #[allow(clippy::type_complexity)] -fn update_space_transforms( +pub fn update_space_transforms( session: Res, default_ref_space: Res, pipelined: Option>, diff --git a/crates/bevy_xr_utils/src/tracking_utils.rs b/crates/bevy_xr_utils/src/tracking_utils.rs index 655a3e9..00f6a77 100644 --- a/crates/bevy_xr_utils/src/tracking_utils.rs +++ b/crates/bevy_xr_utils/src/tracking_utils.rs @@ -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, Without)>, + mut tracked_left_grip: Query<&mut Transform, (With, Without)>, +) { + 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, Without)>, + mut tracked_right_grip: Query<&mut Transform, (With, Without)>, +) { + 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, + actions: Res, mut cmds: Commands, - // root: Query>, + root: Query>, session: Res, ) { //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, + mut bindings: EventWriter, +) { + 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, mut sync: EventWriter) { + sync.send(OxrSyncActionSet(actions.set.clone())); +} + +fn attach_set(actions: Res, mut attach: EventWriter) { + attach.send(OxrAttachActionSet(actions.set.clone())); +} + +fn create_actions(instance: Res, 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 }) }