added tracker markers and change detection

This commit is contained in:
Jay Christy
2023-09-24 01:34:20 -04:00
parent 9e69777a45
commit 8cea603cb5
5 changed files with 172 additions and 37 deletions

View File

@@ -1,5 +1,6 @@
use bevy::prelude::{
info, Color, Gizmos, Plugin, Quat, Query, Res, Transform, Update, Vec2, Vec3, With,
info, Color, Gizmos, GlobalTransform, Plugin, Quat, Query, Res, Transform, Update, Vec2, Vec3,
With, Without,
};
use crate::{
@@ -9,9 +10,11 @@ use crate::{
use crate::xr_input::{
oculus_touch::{OculusController, OculusControllerRef},
Hand, QuatConv, TrackingRoot, Vec3Conv,
Hand, QuatConv, Vec3Conv,
};
use super::trackers::{OpenXRLeftController, OpenXRRightController, OpenXRTrackingRoot};
/// add debug renderer for controllers
#[derive(Default)]
pub struct OpenXrDebugRenderer;
@@ -29,7 +32,19 @@ pub fn draw_gizmos(
xr_input: Res<XrInput>,
instance: Res<XrInstance>,
session: Res<XrSession>,
tracking_root_query: Query<(&mut Transform, With<TrackingRoot>)>,
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();
@@ -56,21 +71,21 @@ pub fn draw_gizmos(
Err(_) => info!("too many tracking roots"),
}
//draw the hands
draw_hand_gizmo(&mut gizmos, &controller, Hand::Right, tracking_transform);
draw_hand_gizmo(&mut gizmos, &controller, Hand::Left, tracking_transform);
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,
tracking_root: &Transform,
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;
@@ -78,14 +93,9 @@ fn draw_hand_gizmo(
let grip_quat_offset = Quat::from_rotation_x(-1.4);
let face_quat_offset = Quat::from_rotation_x(1.05);
let controller_vec3 = tracking_root
.rotation
.mul_vec3(left_controller.0.pose.position.to_vec3())
+ tracking_root.translation;
let controller_quat = tracking_root
.rotation
.mul_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);
@@ -182,7 +192,6 @@ fn draw_hand_gizmo(
}
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;
@@ -191,16 +200,14 @@ fn draw_hand_gizmo(
let grip_quat_offset = Quat::from_rotation_x(-1.4);
let face_quat_offset = Quat::from_rotation_x(1.05);
let controller_vec3 = tracking_root
.rotation
.mul_vec3(right_controller.0.pose.position.to_vec3())
+ tracking_root.translation;
let controller_quat = tracking_root
.rotation
.mul_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,

View File

@@ -1,6 +1,7 @@
pub mod controllers;
pub mod debug_gizmos;
pub mod oculus_touch;
pub mod trackers;
pub mod xr_camera;
use crate::resources::XrSession;
@@ -10,12 +11,16 @@ 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::{default, Commands, Component, Plugin, PreUpdate, Quat, Res, Vec3};
use bevy::prelude::{
default, Commands, Component, Plugin, PreUpdate, Quat, Res, SpatialBundle, 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};
#[derive(Copy, Clone)]
pub struct OpenXrInput {
pub controller_type: XrControllerType,
@@ -26,9 +31,6 @@ pub enum Hand {
Right,
}
#[derive(Component)]
pub struct TrackingRoot;
impl OpenXrInput {
pub fn new(controller_type: XrControllerType) -> Self {
Self { controller_type }
@@ -58,9 +60,15 @@ impl Plugin for OpenXrInput {
fn setup_xr_cameras(mut commands: Commands) {
//this needs to do the whole xr tracking volume not just cameras
//get the root?
let tracking_root = commands.spawn((TransformBundle { ..default() }, TrackingRoot)).id();
let right = commands.spawn(XrCameraBundle::new(Eye::Right)).id();
let left = commands.spawn(XrCameraBundle::new(Eye::Left)).id();
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]);
}

18
src/xr_input/trackers.rs Normal file
View File

@@ -0,0 +1,18 @@
use bevy::prelude::Component;
#[derive(Component)]
pub struct OpenXRTrackingRoot;
#[derive(Component)]
pub struct OpenXRTracker;
#[derive(Component)]
pub struct OpenXRLeftEye;
#[derive(Component)]
pub struct OpenXRRightEye;
#[derive(Component)]
pub struct OpenXRHMD;
#[derive(Component)]
pub struct OpenXRLeftController;
#[derive(Component)]
pub struct OpenXRRightController;
#[derive(Component)]
pub struct OpenXRController;

View File

@@ -223,6 +223,7 @@ pub fn xr_camera_head_sync(
mut query: Query<(&mut Transform, &XrCameraType, &mut XRProjection)>,
) {
let mut f = || -> Option<()> {
//TODO calculate HMD position
for (mut transform, camera_type, mut xr_projection) in query.iter_mut() {
let view_idx = match camera_type {
XrCameraType::Xr(eye) => *eye as usize,