Skip to content

Commit

Permalink
Merge pull request #10 from ForTehLose/prototype_locomotion
Browse files Browse the repository at this point in the history
Prototype locomotion and refactoring openXR tracking
  • Loading branch information
MalekiRe authored Sep 24, 2023
2 parents 5a48e2d + 6d5c1fa commit 6e472ae
Show file tree
Hide file tree
Showing 7 changed files with 398 additions and 21 deletions.
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@ linked = ["openxr/linked", "openxr/static"]
[dependencies]
anyhow = "1.0.75"
ash = "0.37.3"
bevy = { git = "https://github.com/awtterpip/bevy", default-features = false, features = ["bevy_render"] }
bevy = { git = "https://github.com/awtterpip/bevy", default-features = false, features = [
"bevy_render",
], rev = "ac28b11797c0a85b431ee4940c6afa434f712f7a" }
openxr = { version = "0.17.1", features = ["mint"] }
mint = "0.5.9"
wgpu = "0.16.0"
wgpu-core = { version = "0.16.0", features = ["vulkan"] }
wgpu-hal = "0.16.0"

[dev-dependencies]
bevy = { git = "https://github.com/awtterpip/bevy" }
bevy = { git = "https://github.com/awtterpip/bevy", rev = "ac28b11797c0a85b431ee4940c6afa434f712f7a" }
color-eyre = "0.6.2"

[[example]]
name = "xr"
path = "examples/xr.rs"

[profile.release]
debug = true
debug = true
31 changes: 31 additions & 0 deletions examples/xr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin};
use bevy::prelude::*;
use bevy::transform::components::Transform;
use bevy_openxr::xr_input::debug_gizmos::OpenXrDebugRenderer;
use bevy_openxr::xr_input::prototype_locomotion::{proto_locomotion, PrototypeLocomotionConfig};
use bevy_openxr::xr_input::trackers::{
OpenXRController, OpenXRLeftController, OpenXRRightController, OpenXRTracker,
};
use bevy_openxr::DefaultXrPlugins;

fn main() {
Expand All @@ -14,6 +18,9 @@ fn main() {
.add_plugins(LogDiagnosticsPlugin::default())
.add_plugins(FrameTimeDiagnosticsPlugin)
.add_systems(Startup, setup)
.add_systems(Update, proto_locomotion)
.add_systems(Startup, spawn_controllers_example)
.insert_resource(PrototypeLocomotionConfig::default())
.run();
}

Expand All @@ -36,6 +43,13 @@ fn setup(
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..default()
});
// cube
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 0.1 })),
material: materials.add(Color::rgb(0.8, 0.0, 0.0).into()),
transform: Transform::from_xyz(0.0, 0.5, 1.0),
..default()
});
// light
commands.spawn(PointLightBundle {
point_light: PointLight {
Expand All @@ -52,3 +66,20 @@ fn setup(
..default()
},));
}

fn spawn_controllers_example(mut commands: Commands) {
//left hand
commands.spawn((
OpenXRLeftController,
OpenXRController,
OpenXRTracker,
SpatialBundle::default(),
));
//right hand
commands.spawn((
OpenXRRightController,
OpenXRController,
OpenXRTracker,
SpatialBundle::default(),
));
}
73 changes: 59 additions & 14 deletions src/xr_input/debug_gizmos.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use bevy::prelude::{Color, Gizmos, Plugin, Quat, Res, Transform, Update, Vec2, Vec3};
use bevy::prelude::{
info, Color, Gizmos, GlobalTransform, Plugin, Quat, Query, Res, Transform, Update, Vec2, Vec3,
With, Without,
};

use crate::{
input::XrInput,
Expand All @@ -7,9 +10,11 @@ use crate::{

use crate::xr_input::{
oculus_touch::{OculusController, OculusControllerRef},
Hand, QuatConv, Vec3Conv,
Hand,
};

use super::trackers::{OpenXRLeftController, OpenXRRightController, OpenXRTrackingRoot};

/// add debug renderer for controllers
#[derive(Default)]
pub struct OpenXrDebugRenderer;
Expand All @@ -27,32 +32,70 @@ pub fn draw_gizmos(
xr_input: Res<XrInput>,
instance: Res<XrInstance>,
session: Res<XrSession>,
tracking_root_query: Query<(&mut Transform, With<OpenXRTrackingRoot>)>,
left_controller_query: Query<(
&GlobalTransform,
With<OpenXRLeftController>,
Without<OpenXRRightController>,
Without<OpenXRTrackingRoot>,
)>,
right_controller_query: Query<(
&GlobalTransform,
With<OpenXRRightController>,
Without<OpenXRLeftController>,
Without<OpenXRTrackingRoot>,
)>,
) {
//lock frame
let frame_state = *frame_state.lock().unwrap();
//get controller
let controller = oculus_controller.get_ref(&instance, &session, &frame_state, &xr_input);
//tracking root?
let mut tracking_transform = &Transform::IDENTITY;
let root = tracking_root_query.get_single();
match root {
Ok(position) => {
gizmos.circle(
position.0.translation
+ Vec3 {
x: 0.0,
y: 0.01,
z: 0.0,
},
Vec3::Y,
0.2,
Color::RED,
);
tracking_transform = position.0;
}
Err(_) => info!("too many tracking roots"),
}
//draw the hands
draw_hand_gizmo(&mut gizmos, &controller, Hand::Right);
draw_hand_gizmo(&mut gizmos, &controller, Hand::Left);
let left_transform = left_controller_query.get_single().unwrap().0;
let right_transform = right_controller_query.get_single().unwrap().0;

draw_hand_gizmo(&mut gizmos, &controller, Hand::Right, right_transform);
draw_hand_gizmo(&mut gizmos, &controller, Hand::Left, left_transform);
}

fn draw_hand_gizmo(gizmos: &mut Gizmos, controller: &OculusControllerRef<'_>, hand: Hand) {
fn draw_hand_gizmo(
gizmos: &mut Gizmos,
controller: &OculusControllerRef<'_>,
hand: Hand,
hand_transform: &GlobalTransform,
) {
match hand {
Hand::Left => {
//get left controller
let left_controller = controller.grip_space(Hand::Left);

let left_color = Color::YELLOW_GREEN;
let off_color = Color::BLUE;
let touch_color = Color::GREEN;
let pressed_color = Color::RED;

let grip_quat_offset = Quat::from_rotation_x(-1.4);
let face_quat_offset = Quat::from_rotation_x(1.05);

let controller_vec3 = left_controller.0.pose.position.to_vec3();
let controller_quat = left_controller.0.pose.orientation.to_quat();
let trans = hand_transform.compute_transform();
let controller_vec3 = trans.translation;
let controller_quat = trans.rotation;
let face_quat = controller_quat.mul_quat(face_quat_offset);
let face_quat_normal = face_quat.mul_vec3(Vec3::Z);

Expand Down Expand Up @@ -149,7 +192,6 @@ fn draw_hand_gizmo(gizmos: &mut Gizmos, controller: &OculusControllerRef<'_>, ha
}
Hand::Right => {
//get right controller
let right_controller = controller.grip_space(Hand::Right);
let right_color = Color::YELLOW_GREEN;
let off_color = Color::BLUE;
let touch_color = Color::GREEN;
Expand All @@ -158,11 +200,14 @@ fn draw_hand_gizmo(gizmos: &mut Gizmos, controller: &OculusControllerRef<'_>, ha
let grip_quat_offset = Quat::from_rotation_x(-1.4);
let face_quat_offset = Quat::from_rotation_x(1.05);

let controller_vec3 = right_controller.0.pose.position.to_vec3();
let controller_quat = right_controller.0.pose.orientation.to_quat();
let trans = hand_transform.compute_transform();
let controller_vec3 = trans.translation;
let controller_quat = trans.rotation;
let face_quat = controller_quat.mul_quat(face_quat_offset);
let face_quat_normal = face_quat.mul_vec3(Vec3::Z);

let squeeze = controller.squeeze(Hand::Right);
//info!("{:?}", squeeze);
//grip
gizmos.rect(
controller_vec3,
Expand Down
30 changes: 26 additions & 4 deletions src/xr_input/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pub mod controllers;
pub mod debug_gizmos;
pub mod oculus_touch;
pub mod prototype_locomotion;
pub mod trackers;
pub mod xr_camera;

use crate::resources::XrSession;
Expand All @@ -10,12 +12,17 @@ use crate::xr_input::oculus_touch::{setup_oculus_controller, ActionSets};
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::IntoSystemConfigs;
use bevy::prelude::{Commands, Plugin, PreUpdate, Quat, Res, Vec3};
use bevy::prelude::{BuildChildren, 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::trackers::{
adopt_open_xr_trackers, update_open_xr_controllers, OpenXRLeftEye, OpenXRRightEye,
OpenXRTrackingRoot,
};

#[derive(Copy, Clone)]
pub struct OpenXrInput {
pub controller_type: XrControllerType,
Expand All @@ -25,6 +32,7 @@ pub enum Hand {
Left,
Right,
}

impl OpenXrInput {
pub fn new(controller_type: XrControllerType) -> Self {
Self { controller_type }
Expand All @@ -39,8 +47,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 All @@ -52,8 +64,18 @@ impl Plugin for OpenXrInput {
}

fn setup_xr_cameras(mut commands: Commands) {
commands.spawn(XrCameraBundle::new(Eye::Right));
commands.spawn(XrCameraBundle::new(Eye::Left));
//this needs to do the whole xr tracking volume not just cameras
//get the root?
let tracking_root = commands
.spawn((SpatialBundle::default(), OpenXRTrackingRoot))
.id();
let right = commands
.spawn((XrCameraBundle::new(Eye::Right), OpenXRRightEye))
.id();
let left = commands
.spawn((XrCameraBundle::new(Eye::Left), OpenXRLeftEye))
.id();
commands.entity(tracking_root).push_children(&[right, left]);
}

fn action_set_system(action_sets: Res<ActionSets>, session: Res<XrSession>) {
Expand Down
Loading

0 comments on commit 6e472ae

Please sign in to comment.