look upon my bad code

This commit is contained in:
Jay Christy
2023-10-14 11:02:49 -04:00
parent 89f0e5367d
commit 1d434bb7a1
2 changed files with 138 additions and 46 deletions

View File

@@ -5,7 +5,7 @@ use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin};
use bevy::prelude::*; use bevy::prelude::*;
use bevy::transform::components::Transform; use bevy::transform::components::Transform;
use bevy_openxr::xr_input::hand::{spawn_hand_entities, update_hand_states, HandStatesResource, update_emulated_hand_skeletons, draw_hand_entities}; use bevy_openxr::xr_input::hand::OpenXrHandInput;
use bevy_openxr::xr_input::prototype_locomotion::{proto_locomotion, PrototypeLocomotionConfig}; use bevy_openxr::xr_input::prototype_locomotion::{proto_locomotion, PrototypeLocomotionConfig};
use bevy_openxr::xr_input::trackers::{ use bevy_openxr::xr_input::trackers::{
OpenXRController, OpenXRLeftController, OpenXRRightController, OpenXRTracker, OpenXRController, OpenXRLeftController, OpenXRRightController, OpenXRTracker,
@@ -23,13 +23,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_example)
.add_systems(Update, update_emulated_hand_skeletons)
.add_systems(PreUpdate, update_hand_states)
.add_systems(PostUpdate, draw_hand_entities)
.add_systems(Startup, spawn_hand_entities)
.insert_resource(PrototypeLocomotionConfig::default()) .insert_resource(PrototypeLocomotionConfig::default())
.insert_resource(HandStatesResource::default()) .add_systems(Startup, spawn_controllers_example)
.add_plugins(OpenXrHandInput)
.run(); .run();
} }

View File

@@ -1,11 +1,51 @@
use std::f32::consts::PI; use std::f32::consts::PI;
use bevy::prelude::{Entity, Resource, Component, default, SpatialBundle, Commands, Res, ResMut, info, Transform, Query, Quat, Vec3, GlobalTransform, With, Gizmos, Color}; use bevy::prelude::{
default, info, Color, Commands, Component, Entity, Gizmos, GlobalTransform, Plugin, PostUpdate,
PreUpdate, Quat, Query, Res, ResMut, Resource, SpatialBundle, Startup, Transform, Update, Vec3,
With,
};
use openxr::{HandJoint, Posef}; use openxr::{HandJoint, Posef};
use crate::{resources::{XrFrameState, XrInstance, XrSession}, input::XrInput, xr_input::Vec3Conv}; use crate::{
input::XrInput,
resources::{XrFrameState, XrInstance, XrSession},
xr_input::Vec3Conv,
};
use super::{trackers::{OpenXRTracker, OpenXRRightController, OpenXRLeftController}, Hand, oculus_touch::OculusController, hand_poses::get_simulated_open_hand_transforms}; use super::{
hand_poses::get_simulated_open_hand_transforms,
oculus_touch::OculusController,
trackers::{OpenXRLeftController, OpenXRRightController, OpenXRTracker},
Hand,
};
/// add debug renderer for controllers
#[derive(Default)]
pub struct OpenXrHandInput;
impl Plugin for OpenXrHandInput {
fn build(&self, app: &mut bevy::prelude::App) {
app.add_systems(Update, update_hand_skeletons)
.add_systems(PreUpdate, update_hand_states)
.add_systems(PostUpdate, draw_hand_entities)
.add_systems(Startup, spawn_hand_entities)
.insert_resource(HandStatesResource::default())
.insert_resource(HandInputSource::default());
}
}
#[derive(Resource)]
pub enum HandInputSource {
Emulated,
OpenXr,
}
impl Default for HandInputSource {
fn default() -> Self {
HandInputSource::Emulated
}
}
#[derive(Resource, Default)] #[derive(Resource, Default)]
pub struct HandsResource { pub struct HandsResource {
@@ -25,7 +65,15 @@ pub struct HandResource {
impl Default for HandResource { impl Default for HandResource {
fn default() -> Self { fn default() -> Self {
Self { palm: Entity::PLACEHOLDER, wrist: Entity::PLACEHOLDER, thumb: Default::default(), index: Default::default(), middle: Default::default(), ring: Default::default(), little: Default::default() } Self {
palm: Entity::PLACEHOLDER,
wrist: Entity::PLACEHOLDER,
thumb: Default::default(),
index: Default::default(),
middle: Default::default(),
ring: Default::default(),
little: Default::default(),
}
} }
} }
@@ -38,7 +86,12 @@ pub struct ThumbResource {
impl Default for ThumbResource { impl Default for ThumbResource {
fn default() -> Self { fn default() -> Self {
Self { metacarpal: Entity::PLACEHOLDER, proximal: Entity::PLACEHOLDER, distal: Entity::PLACEHOLDER, tip: Entity::PLACEHOLDER } Self {
metacarpal: Entity::PLACEHOLDER,
proximal: Entity::PLACEHOLDER,
distal: Entity::PLACEHOLDER,
tip: Entity::PLACEHOLDER,
}
} }
} }
pub struct IndexResource { pub struct IndexResource {
@@ -51,7 +104,13 @@ pub struct IndexResource {
impl Default for IndexResource { impl Default for IndexResource {
fn default() -> Self { fn default() -> Self {
Self { metacarpal: Entity::PLACEHOLDER, proximal: Entity::PLACEHOLDER, intermediate: Entity::PLACEHOLDER, distal: Entity::PLACEHOLDER, tip: Entity::PLACEHOLDER } Self {
metacarpal: Entity::PLACEHOLDER,
proximal: Entity::PLACEHOLDER,
intermediate: Entity::PLACEHOLDER,
distal: Entity::PLACEHOLDER,
tip: Entity::PLACEHOLDER,
}
} }
} }
pub struct MiddleResource { pub struct MiddleResource {
@@ -63,7 +122,13 @@ pub struct MiddleResource {
} }
impl Default for MiddleResource { impl Default for MiddleResource {
fn default() -> Self { fn default() -> Self {
Self { metacarpal: Entity::PLACEHOLDER, proximal: Entity::PLACEHOLDER, intermediate: Entity::PLACEHOLDER, distal: Entity::PLACEHOLDER, tip: Entity::PLACEHOLDER } Self {
metacarpal: Entity::PLACEHOLDER,
proximal: Entity::PLACEHOLDER,
intermediate: Entity::PLACEHOLDER,
distal: Entity::PLACEHOLDER,
tip: Entity::PLACEHOLDER,
}
} }
} }
pub struct RingResource { pub struct RingResource {
@@ -75,7 +140,13 @@ pub struct RingResource {
} }
impl Default for RingResource { impl Default for RingResource {
fn default() -> Self { fn default() -> Self {
Self { metacarpal: Entity::PLACEHOLDER, proximal: Entity::PLACEHOLDER, intermediate: Entity::PLACEHOLDER, distal: Entity::PLACEHOLDER, tip: Entity::PLACEHOLDER } Self {
metacarpal: Entity::PLACEHOLDER,
proximal: Entity::PLACEHOLDER,
intermediate: Entity::PLACEHOLDER,
distal: Entity::PLACEHOLDER,
tip: Entity::PLACEHOLDER,
}
} }
} }
pub struct LittleResource { pub struct LittleResource {
@@ -87,7 +158,13 @@ pub struct LittleResource {
} }
impl Default for LittleResource { impl Default for LittleResource {
fn default() -> Self { fn default() -> Self {
Self { metacarpal: Entity::PLACEHOLDER, proximal: Entity::PLACEHOLDER, intermediate: Entity::PLACEHOLDER, distal: Entity::PLACEHOLDER, tip: Entity::PLACEHOLDER } Self {
metacarpal: Entity::PLACEHOLDER,
proximal: Entity::PLACEHOLDER,
intermediate: Entity::PLACEHOLDER,
distal: Entity::PLACEHOLDER,
tip: Entity::PLACEHOLDER,
}
} }
} }
@@ -176,7 +253,9 @@ pub fn spawn_hand_entities(mut commands: Commands) {
HandBone::IndexTip => hand_resource.right.index.tip = boneid, HandBone::IndexTip => hand_resource.right.index.tip = boneid,
HandBone::MiddleMetacarpal => hand_resource.right.middle.metacarpal = boneid, HandBone::MiddleMetacarpal => hand_resource.right.middle.metacarpal = boneid,
HandBone::MiddleProximal => hand_resource.right.middle.proximal = boneid, HandBone::MiddleProximal => hand_resource.right.middle.proximal = boneid,
HandBone::MiddleIntermediate => hand_resource.right.middle.intermediate = boneid, HandBone::MiddleIntermediate => {
hand_resource.right.middle.intermediate = boneid
}
HandBone::MiddleDistal => hand_resource.right.middle.distal = boneid, HandBone::MiddleDistal => hand_resource.right.middle.distal = boneid,
HandBone::MiddleTip => hand_resource.right.middle.tip = boneid, HandBone::MiddleTip => hand_resource.right.middle.tip = boneid,
HandBone::RingMetacarpal => hand_resource.right.ring.metacarpal = boneid, HandBone::RingMetacarpal => hand_resource.right.ring.metacarpal = boneid,
@@ -186,7 +265,9 @@ pub fn spawn_hand_entities(mut commands: Commands) {
HandBone::RingTip => hand_resource.right.ring.tip = boneid, HandBone::RingTip => hand_resource.right.ring.tip = boneid,
HandBone::LittleMetacarpal => hand_resource.right.little.metacarpal = boneid, HandBone::LittleMetacarpal => hand_resource.right.little.metacarpal = boneid,
HandBone::LittleProximal => hand_resource.right.little.proximal = boneid, HandBone::LittleProximal => hand_resource.right.little.proximal = boneid,
HandBone::LittleIntermediate => hand_resource.right.little.intermediate = boneid, HandBone::LittleIntermediate => {
hand_resource.right.little.intermediate = boneid
}
HandBone::LittleDistal => hand_resource.right.little.distal = boneid, HandBone::LittleDistal => hand_resource.right.little.distal = boneid,
HandBone::LittleTip => hand_resource.right.little.tip = boneid, HandBone::LittleTip => hand_resource.right.little.tip = boneid,
}, },
@@ -915,13 +996,17 @@ fn log_hand(hand_pose: [Posef; 26]) {
); );
} }
pub fn update_hand_skeletons(
pub fn update_emulated_hand_skeletons(
right_controller_query: Query<(&GlobalTransform, With<OpenXRRightController>)>, right_controller_query: Query<(&GlobalTransform, With<OpenXRRightController>)>,
left_controller_query: Query<(&GlobalTransform, With<OpenXRLeftController>)>, left_controller_query: Query<(&GlobalTransform, With<OpenXRLeftController>)>,
hand_states_option: Option<ResMut<HandStatesResource>>, hand_states_option: Option<ResMut<HandStatesResource>>,
mut hand_bone_query: Query<(&mut Transform, &HandBone, &Hand)>, mut hand_bone_query: Query<(&mut Transform, &HandBone, &Hand)>,
input_source: Option<Res<HandInputSource>>,
) { ) {
match input_source {
Some(res) => match *res {
HandInputSource::Emulated => {
info!("hand input source is emulated");
match hand_states_option { match hand_states_option {
Some(hands) => { Some(hands) => {
let left_hand_transform = left_controller_query let left_hand_transform = left_controller_query
@@ -950,6 +1035,17 @@ pub fn update_emulated_hand_skeletons(
None => info!("hand states resource not initialized yet"), None => info!("hand states resource not initialized yet"),
} }
} }
HandInputSource::OpenXr => {
info!("hand input source is open XR: this is not implemented yet");
return;
}
},
None => {
info!("hand input source not initialized");
return;
}
}
}
pub fn draw_hand_entities(mut gizmos: Gizmos, query: Query<(&Transform, &HandBone)>) { pub fn draw_hand_entities(mut gizmos: Gizmos, query: Query<(&Transform, &HandBone)>) {
for (transform, hand_bone) in query.iter() { for (transform, hand_bone) in query.iter() {