Skip to content

Commit

Permalink
moved more stuff around
Browse files Browse the repository at this point in the history
  • Loading branch information
ForTehLose committed Sep 24, 2023
1 parent 85dc35a commit 012a778
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 68 deletions.
66 changes: 2 additions & 64 deletions examples/xr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use bevy_openxr::xr_input::debug_gizmos::OpenXrDebugRenderer;
use bevy_openxr::xr_input::oculus_touch::OculusController;
use bevy_openxr::xr_input::prototype_locomotion::{proto_locomotion, PrototypeLocomotionConfig};
use bevy_openxr::xr_input::trackers::{
OpenXRController, OpenXRLeftController, OpenXRRightController, OpenXRTracker,
OpenXRTrackingRoot, adopt_open_xr_trackers,
adopt_open_xr_trackers, OpenXRController, OpenXRLeftController, OpenXRRightController,
OpenXRTracker, OpenXRTrackingRoot,
};
use bevy_openxr::xr_input::{Hand, QuatConv, Vec3Conv};
use bevy_openxr::DefaultXrPlugins;
Expand All @@ -29,8 +29,6 @@ fn main() {
.add_systems(Startup, setup)
.add_systems(Update, proto_locomotion)
.add_systems(Startup, spawn_controllers_example)
.add_systems(Update, update_open_xr_controllers)
.add_systems(Update, adopt_open_xr_trackers)
.insert_resource(PrototypeLocomotionConfig::default())
.run();
}
Expand Down Expand Up @@ -85,72 +83,12 @@ fn spawn_controllers_example(mut commands: Commands) {
OpenXRController,
OpenXRTracker,
SpatialBundle::default(),
// PbrBundle {
// mesh: meshes.add(Mesh::from(shape::Cube { size: 0.1 })),
// material: materials.add(Color::RED.into()),
// transform: Transform::from_xyz(0.0, 0.5, 1.0),
// ..default()
// },
));
//right hand
commands.spawn((
OpenXRRightController,
OpenXRController,
OpenXRTracker,
SpatialBundle::default(),
// PbrBundle {
// mesh: meshes.add(Mesh::from(shape::Cube { size: 0.1 })),
// material: materials.add(Color::BLUE.into()),
// transform: Transform::from_xyz(0.0, 0.5, 1.0),
// ..default()
// },
));
}

fn update_open_xr_controllers(
oculus_controller: Res<OculusController>,
mut left_controller_query: Query<(
&mut Transform,
With<OpenXRLeftController>,
Without<OpenXRRightController>,
)>,
mut right_controller_query: Query<(
&mut Transform,
With<OpenXRRightController>,
Without<OpenXRLeftController>,
)>,
frame_state: Res<XrFrameState>,
instance: Res<XrInstance>,
xr_input: Res<XrInput>,
session: Res<XrSession>,
) {
//lock dat frame?
let frame_state = *frame_state.lock().unwrap();
//get controller
let controller = oculus_controller.get_ref(&instance, &session, &frame_state, &xr_input);
//get left controller
let left = controller.grip_space(Hand::Left);
let left_postion = left.0.pose.position.to_vec3();

left_controller_query
.get_single_mut()
.unwrap()
.0
.translation = left_postion;

left_controller_query.get_single_mut().unwrap().0.rotation = left.0.pose.orientation.to_quat();
//get right controller
let right = controller.grip_space(Hand::Right);
let right_postion = right.0.pose.position.to_vec3();

right_controller_query
.get_single_mut()
.unwrap()
.0
.translation = right_postion;

right_controller_query.get_single_mut().unwrap().0.rotation =
right.0.pose.orientation.to_quat();
}


13 changes: 10 additions & 3 deletions src/xr_input/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pub mod controllers;
pub mod debug_gizmos;
pub mod oculus_touch;
pub mod trackers;
pub mod prototype_locomotion;
pub mod trackers;
pub mod xr_camera;

use crate::resources::XrSession;
Expand All @@ -13,14 +13,17 @@ use crate::xr_input::xr_camera::{xr_camera_head_sync, Eye, XRProjection, XrCamer
use bevy::app::{App, PostUpdate, Startup};
use bevy::log::warn;
use bevy::prelude::{
default, Commands, Component, Plugin, PreUpdate, Quat, Res, SpatialBundle, Vec3,
default, Commands, Component, Plugin, PreUpdate, Quat, Res, SpatialBundle, Update, Vec3,
};
use bevy::prelude::{BuildChildren, IntoSystemConfigs};
use bevy::render::camera::CameraProjectionPlugin;
use bevy::render::view::{update_frusta, VisibilitySystems};
use bevy::transform::{TransformBundle, TransformSystem};

use self::trackers::{OpenXRLeftEye, OpenXRRightEye, OpenXRTrackingRoot};
use self::trackers::{
adopt_open_xr_trackers, update_open_xr_controllers, OpenXRLeftEye, OpenXRRightEye,
OpenXRTrackingRoot,
};

#[derive(Copy, Clone)]
pub struct OpenXrInput {
Expand All @@ -46,8 +49,12 @@ impl Plugin for OpenXrInput {
app.add_systems(Startup, setup_oculus_controller);
}
}
//adopt any new trackers
app.add_systems(PreUpdate, adopt_open_xr_trackers);
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);
app.add_systems(
PostUpdate,
update_frusta::<XRProjection>
Expand Down
53 changes: 52 additions & 1 deletion src/xr_input/trackers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use bevy::prelude::{info, Added, BuildChildren, Commands, Component, Entity, Query, With};
use bevy::prelude::{info, Added, BuildChildren, Commands, Component, Entity, Query, With, Res, Transform, Without};

use crate::{resources::{XrFrameState, XrInstance, XrSession}, input::XrInput};

use super::{oculus_touch::OculusController, Hand, Vec3Conv, QuatConv};

#[derive(Component)]
pub struct OpenXRTrackingRoot;
Expand Down Expand Up @@ -34,3 +38,50 @@ pub fn adopt_open_xr_trackers(
Err(_) => info!("root isnt spawned yet?"),
}
}

pub fn update_open_xr_controllers(
oculus_controller: Res<OculusController>,
mut left_controller_query: Query<(
&mut Transform,
With<OpenXRLeftController>,
Without<OpenXRRightController>,
)>,
mut right_controller_query: Query<(
&mut Transform,
With<OpenXRRightController>,
Without<OpenXRLeftController>,
)>,
frame_state: Res<XrFrameState>,
instance: Res<XrInstance>,
xr_input: Res<XrInput>,
session: Res<XrSession>,
) {
//lock dat frame?
let frame_state = *frame_state.lock().unwrap();
//get controller
let controller = oculus_controller.get_ref(&instance, &session, &frame_state, &xr_input);
//get left controller
let left = controller.grip_space(Hand::Left);
let left_postion = left.0.pose.position.to_vec3();

left_controller_query
.get_single_mut()
.unwrap()
.0
.translation = left_postion;

left_controller_query.get_single_mut().unwrap().0.rotation = left.0.pose.orientation.to_quat();
//get right controller
let right = controller.grip_space(Hand::Right);
let right_postion = right.0.pose.position.to_vec3();

right_controller_query
.get_single_mut()
.unwrap()
.0
.translation = right_postion;

right_controller_query.get_single_mut().unwrap().0.rotation =
right.0.pose.orientation.to_quat();
}

0 comments on commit 012a778

Please sign in to comment.