added tracker markers and change detection
This commit is contained in:
111
examples/xr.rs
111
examples/xr.rs
@@ -9,7 +9,11 @@ use bevy_openxr::input::XrInput;
|
|||||||
use bevy_openxr::resources::{XrFrameState, XrInstance, XrSession, XrViews};
|
use bevy_openxr::resources::{XrFrameState, XrInstance, XrSession, XrViews};
|
||||||
use bevy_openxr::xr_input::debug_gizmos::OpenXrDebugRenderer;
|
use bevy_openxr::xr_input::debug_gizmos::OpenXrDebugRenderer;
|
||||||
use bevy_openxr::xr_input::oculus_touch::OculusController;
|
use bevy_openxr::xr_input::oculus_touch::OculusController;
|
||||||
use bevy_openxr::xr_input::{Hand, QuatConv, TrackingRoot, Vec3Conv};
|
use bevy_openxr::xr_input::trackers::{
|
||||||
|
OpenXRController, OpenXRLeftController, OpenXRRightController, OpenXRTracker,
|
||||||
|
OpenXRTrackingRoot,
|
||||||
|
};
|
||||||
|
use bevy_openxr::xr_input::{Hand, QuatConv, Vec3Conv};
|
||||||
use bevy_openxr::DefaultXrPlugins;
|
use bevy_openxr::DefaultXrPlugins;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@@ -23,6 +27,9 @@ fn main() {
|
|||||||
.add_plugins(FrameTimeDiagnosticsPlugin)
|
.add_plugins(FrameTimeDiagnosticsPlugin)
|
||||||
.add_systems(Startup, setup)
|
.add_systems(Startup, setup)
|
||||||
.add_systems(Update, proto_locomotion)
|
.add_systems(Update, proto_locomotion)
|
||||||
|
.add_systems(Startup, spawn_controllers)
|
||||||
|
.add_systems(Update, update_hands)
|
||||||
|
.add_systems(Update, adopt_open_xr_trackers)
|
||||||
.run();
|
.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,9 +95,106 @@ struct RotationTimer {
|
|||||||
timer: Timer,
|
timer: Timer,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn spawn_controllers(
|
||||||
|
mut commands: Commands,
|
||||||
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||||
|
) {
|
||||||
|
//left hand
|
||||||
|
commands.spawn((
|
||||||
|
OpenXRLeftController,
|
||||||
|
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_hands(
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn adopt_open_xr_trackers(
|
||||||
|
query: Query<(Entity), Added<OpenXRTracker>>,
|
||||||
|
mut commands: Commands,
|
||||||
|
tracking_root_query: Query<(Entity, With<OpenXRTrackingRoot>)>,
|
||||||
|
) {
|
||||||
|
let root = tracking_root_query.get_single();
|
||||||
|
match root {
|
||||||
|
Ok(thing) => {
|
||||||
|
// info!("root is");
|
||||||
|
for tracker in query.iter() {
|
||||||
|
info!("we got a new tracker");
|
||||||
|
commands.entity(thing.0).add_child(tracker);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => info!("root isnt spawned yet?"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn proto_locomotion(
|
fn proto_locomotion(
|
||||||
time: Res<Time>,
|
time: Res<Time>,
|
||||||
mut tracking_root_query: Query<(&mut Transform, With<TrackingRoot>)>,
|
mut tracking_root_query: Query<(&mut Transform, With<OpenXRTrackingRoot>)>,
|
||||||
oculus_controller: Res<OculusController>,
|
oculus_controller: Res<OculusController>,
|
||||||
frame_state: Res<XrFrameState>,
|
frame_state: Res<XrFrameState>,
|
||||||
xr_input: Res<XrInput>,
|
xr_input: Res<XrInput>,
|
||||||
@@ -162,7 +266,6 @@ fn proto_locomotion(
|
|||||||
//apply rotation
|
//apply rotation
|
||||||
let v = views.lock().unwrap();
|
let v = views.lock().unwrap();
|
||||||
let views = v.get(0);
|
let views = v.get(0);
|
||||||
let views = v.get(0);
|
|
||||||
match views {
|
match views {
|
||||||
Some(view) => {
|
Some(view) => {
|
||||||
let mut hmd_translation = view.pose.position.to_vec3();
|
let mut hmd_translation = view.pose.position.to_vec3();
|
||||||
@@ -176,8 +279,6 @@ fn proto_locomotion(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
RotationType::Snap => {
|
RotationType::Snap => {
|
||||||
//yup even more math i cant remember
|
|
||||||
|
|
||||||
//get timer
|
//get timer
|
||||||
match rotation_timer {
|
match rotation_timer {
|
||||||
Some(mut timer) => {
|
Some(mut timer) => {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use bevy::prelude::{
|
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::{
|
use crate::{
|
||||||
@@ -9,9 +10,11 @@ use crate::{
|
|||||||
|
|
||||||
use crate::xr_input::{
|
use crate::xr_input::{
|
||||||
oculus_touch::{OculusController, OculusControllerRef},
|
oculus_touch::{OculusController, OculusControllerRef},
|
||||||
Hand, QuatConv, TrackingRoot, Vec3Conv,
|
Hand, QuatConv, Vec3Conv,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use super::trackers::{OpenXRLeftController, OpenXRRightController, OpenXRTrackingRoot};
|
||||||
|
|
||||||
/// add debug renderer for controllers
|
/// add debug renderer for controllers
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct OpenXrDebugRenderer;
|
pub struct OpenXrDebugRenderer;
|
||||||
@@ -29,7 +32,19 @@ pub fn draw_gizmos(
|
|||||||
xr_input: Res<XrInput>,
|
xr_input: Res<XrInput>,
|
||||||
instance: Res<XrInstance>,
|
instance: Res<XrInstance>,
|
||||||
session: Res<XrSession>,
|
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
|
//lock frame
|
||||||
let frame_state = *frame_state.lock().unwrap();
|
let frame_state = *frame_state.lock().unwrap();
|
||||||
@@ -56,21 +71,21 @@ pub fn draw_gizmos(
|
|||||||
Err(_) => info!("too many tracking roots"),
|
Err(_) => info!("too many tracking roots"),
|
||||||
}
|
}
|
||||||
//draw the hands
|
//draw the hands
|
||||||
draw_hand_gizmo(&mut gizmos, &controller, Hand::Right, tracking_transform);
|
let left_transform = left_controller_query.get_single().unwrap().0;
|
||||||
draw_hand_gizmo(&mut gizmos, &controller, Hand::Left, tracking_transform);
|
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(
|
fn draw_hand_gizmo(
|
||||||
gizmos: &mut Gizmos,
|
gizmos: &mut Gizmos,
|
||||||
controller: &OculusControllerRef<'_>,
|
controller: &OculusControllerRef<'_>,
|
||||||
hand: Hand,
|
hand: Hand,
|
||||||
tracking_root: &Transform,
|
hand_transform: &GlobalTransform,
|
||||||
) {
|
) {
|
||||||
match hand {
|
match hand {
|
||||||
Hand::Left => {
|
Hand::Left => {
|
||||||
//get left controller
|
|
||||||
let left_controller = controller.grip_space(Hand::Left);
|
|
||||||
|
|
||||||
let left_color = Color::YELLOW_GREEN;
|
let left_color = Color::YELLOW_GREEN;
|
||||||
let off_color = Color::BLUE;
|
let off_color = Color::BLUE;
|
||||||
let touch_color = Color::GREEN;
|
let touch_color = Color::GREEN;
|
||||||
@@ -78,14 +93,9 @@ fn draw_hand_gizmo(
|
|||||||
|
|
||||||
let grip_quat_offset = Quat::from_rotation_x(-1.4);
|
let grip_quat_offset = Quat::from_rotation_x(-1.4);
|
||||||
let face_quat_offset = Quat::from_rotation_x(1.05);
|
let face_quat_offset = Quat::from_rotation_x(1.05);
|
||||||
|
let trans = hand_transform.compute_transform();
|
||||||
let controller_vec3 = tracking_root
|
let controller_vec3 = trans.translation;
|
||||||
.rotation
|
let controller_quat = trans.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 face_quat = controller_quat.mul_quat(face_quat_offset);
|
let face_quat = controller_quat.mul_quat(face_quat_offset);
|
||||||
let face_quat_normal = face_quat.mul_vec3(Vec3::Z);
|
let face_quat_normal = face_quat.mul_vec3(Vec3::Z);
|
||||||
|
|
||||||
@@ -182,7 +192,6 @@ fn draw_hand_gizmo(
|
|||||||
}
|
}
|
||||||
Hand::Right => {
|
Hand::Right => {
|
||||||
//get right controller
|
//get right controller
|
||||||
let right_controller = controller.grip_space(Hand::Right);
|
|
||||||
let right_color = Color::YELLOW_GREEN;
|
let right_color = Color::YELLOW_GREEN;
|
||||||
let off_color = Color::BLUE;
|
let off_color = Color::BLUE;
|
||||||
let touch_color = Color::GREEN;
|
let touch_color = Color::GREEN;
|
||||||
@@ -191,16 +200,14 @@ fn draw_hand_gizmo(
|
|||||||
let grip_quat_offset = Quat::from_rotation_x(-1.4);
|
let grip_quat_offset = Quat::from_rotation_x(-1.4);
|
||||||
let face_quat_offset = Quat::from_rotation_x(1.05);
|
let face_quat_offset = Quat::from_rotation_x(1.05);
|
||||||
|
|
||||||
let controller_vec3 = tracking_root
|
let trans = hand_transform.compute_transform();
|
||||||
.rotation
|
let controller_vec3 = trans.translation;
|
||||||
.mul_vec3(right_controller.0.pose.position.to_vec3())
|
let controller_quat = trans.rotation;
|
||||||
+ tracking_root.translation;
|
|
||||||
let controller_quat = tracking_root
|
|
||||||
.rotation
|
|
||||||
.mul_quat(right_controller.0.pose.orientation.to_quat());
|
|
||||||
let face_quat = controller_quat.mul_quat(face_quat_offset);
|
let face_quat = controller_quat.mul_quat(face_quat_offset);
|
||||||
let face_quat_normal = face_quat.mul_vec3(Vec3::Z);
|
let face_quat_normal = face_quat.mul_vec3(Vec3::Z);
|
||||||
|
|
||||||
|
let squeeze = controller.squeeze(Hand::Right);
|
||||||
|
//info!("{:?}", squeeze);
|
||||||
//grip
|
//grip
|
||||||
gizmos.rect(
|
gizmos.rect(
|
||||||
controller_vec3,
|
controller_vec3,
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
pub mod controllers;
|
pub mod controllers;
|
||||||
pub mod debug_gizmos;
|
pub mod debug_gizmos;
|
||||||
pub mod oculus_touch;
|
pub mod oculus_touch;
|
||||||
|
pub mod trackers;
|
||||||
pub mod xr_camera;
|
pub mod xr_camera;
|
||||||
|
|
||||||
use crate::resources::XrSession;
|
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 crate::xr_input::xr_camera::{xr_camera_head_sync, Eye, XRProjection, XrCameraBundle};
|
||||||
use bevy::app::{App, PostUpdate, Startup};
|
use bevy::app::{App, PostUpdate, Startup};
|
||||||
use bevy::log::warn;
|
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::prelude::{BuildChildren, IntoSystemConfigs};
|
||||||
use bevy::render::camera::CameraProjectionPlugin;
|
use bevy::render::camera::CameraProjectionPlugin;
|
||||||
use bevy::render::view::{update_frusta, VisibilitySystems};
|
use bevy::render::view::{update_frusta, VisibilitySystems};
|
||||||
use bevy::transform::{TransformBundle, TransformSystem};
|
use bevy::transform::{TransformBundle, TransformSystem};
|
||||||
|
|
||||||
|
use self::trackers::{OpenXRLeftEye, OpenXRRightEye, OpenXRTrackingRoot};
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone)]
|
||||||
pub struct OpenXrInput {
|
pub struct OpenXrInput {
|
||||||
pub controller_type: XrControllerType,
|
pub controller_type: XrControllerType,
|
||||||
@@ -26,9 +31,6 @@ pub enum Hand {
|
|||||||
Right,
|
Right,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
|
||||||
pub struct TrackingRoot;
|
|
||||||
|
|
||||||
impl OpenXrInput {
|
impl OpenXrInput {
|
||||||
pub fn new(controller_type: XrControllerType) -> Self {
|
pub fn new(controller_type: XrControllerType) -> Self {
|
||||||
Self { controller_type }
|
Self { controller_type }
|
||||||
@@ -58,9 +60,15 @@ impl Plugin for OpenXrInput {
|
|||||||
fn setup_xr_cameras(mut commands: Commands) {
|
fn setup_xr_cameras(mut commands: Commands) {
|
||||||
//this needs to do the whole xr tracking volume not just cameras
|
//this needs to do the whole xr tracking volume not just cameras
|
||||||
//get the root?
|
//get the root?
|
||||||
let tracking_root = commands.spawn((TransformBundle { ..default() }, TrackingRoot)).id();
|
let tracking_root = commands
|
||||||
let right = commands.spawn(XrCameraBundle::new(Eye::Right)).id();
|
.spawn((SpatialBundle::default(), OpenXRTrackingRoot))
|
||||||
let left = commands.spawn(XrCameraBundle::new(Eye::Left)).id();
|
.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]);
|
commands.entity(tracking_root).push_children(&[right, left]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
18
src/xr_input/trackers.rs
Normal file
18
src/xr_input/trackers.rs
Normal 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;
|
||||||
@@ -223,6 +223,7 @@ pub fn xr_camera_head_sync(
|
|||||||
mut query: Query<(&mut Transform, &XrCameraType, &mut XRProjection)>,
|
mut query: Query<(&mut Transform, &XrCameraType, &mut XRProjection)>,
|
||||||
) {
|
) {
|
||||||
let mut f = || -> Option<()> {
|
let mut f = || -> Option<()> {
|
||||||
|
//TODO calculate HMD position
|
||||||
for (mut transform, camera_type, mut xr_projection) in query.iter_mut() {
|
for (mut transform, camera_type, mut xr_projection) in query.iter_mut() {
|
||||||
let view_idx = match camera_type {
|
let view_idx = match camera_type {
|
||||||
XrCameraType::Xr(eye) => *eye as usize,
|
XrCameraType::Xr(eye) => *eye as usize,
|
||||||
|
|||||||
Reference in New Issue
Block a user