Merge pull request #17 from ForTehLose/hands

Hands
This commit is contained in:
ForTehLose
2023-10-17 22:17:13 -05:00
committed by GitHub
4 changed files with 1636 additions and 6 deletions

View File

@@ -1,9 +1,13 @@
use bevy::diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin};
use bevy::prelude::*;
use bevy::transform::components::Transform;
use bevy_openxr::input::XrInput;
use bevy_openxr::resources::{XrFrameState, XrInstance, XrSession};
use bevy_openxr::xr_input::debug_gizmos::OpenXrDebugRenderer;
use bevy_openxr::xr_input::hand::{OpenXrHandInput, HandInputDebugRenderer};
use bevy_openxr::xr_input::interactions::{
draw_interaction_gizmos, draw_socket_gizmos, interactions, socket_interactions,
update_interactable_states, InteractionEvent, Touched, XRDirectInteractor, XRInteractable,
@@ -23,13 +27,15 @@ fn main() {
info!("Running `openxr-6dof` skill");
App::new()
.add_plugins(DefaultXrPlugins)
.add_plugins(OpenXrDebugRenderer) //new debug renderer adds gizmos to
//.add_plugins(OpenXrDebugRenderer) //new debug renderer adds gizmos to
.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())
.add_systems(Startup, spawn_controllers_example)
.add_plugins(OpenXrHandInput)
.add_plugins(HandInputDebugRenderer)
.add_systems(
Update,
draw_interaction_gizmos.after(update_interactable_states),
@@ -88,7 +94,14 @@ fn setup(
});
// camera
commands.spawn((Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
transform: Transform::from_xyz(0.25, 1.25, 0.0).looking_at(
Vec3 {
x: -0.548,
y: -0.161,
z: -0.137,
},
Vec3::Y,
),
..default()
},));
//simple interactable

1095
src/xr_input/hand.rs Normal file

File diff suppressed because it is too large Load Diff

520
src/xr_input/hand_poses.rs Normal file
View File

@@ -0,0 +1,520 @@
use bevy::prelude::{Quat, Transform, Vec3};
use openxr::{Posef, Quaternionf, Vector3f};
use super::Hand;
pub fn get_simulated_open_hand_transforms(hand: Hand) -> [Transform; 26] {
let test_hand_bones: [Vec3; 26] = [
Vec3 {
x: 0.0,
y: 0.0,
z: 0.0,
}, //palm
Vec3 {
x: 0.0,
y: 0.0,
z: -0.04,
}, //wrist
Vec3 {
x: -0.02,
y: 0.00,
z: 0.015,
}, //thumb
Vec3 {
x: 0.0,
y: 0.0,
z: 0.03,
},
Vec3 {
x: 0.0,
y: 0.0,
z: 0.024,
},
Vec3 {
x: 0.0,
y: 0.0,
z: 0.024,
},
Vec3 {
x: -0.01,
y: -0.015,
z: 0.0155,
}, //index
Vec3 {
x: 0.0,
y: 0.0,
z: 0.064,
},
Vec3 {
x: 0.0,
y: 0.0,
z: 0.037,
},
Vec3 {
x: 0.0,
y: 0.0,
z: 0.02,
},
Vec3 {
x: 0.0,
y: 0.0,
z: 0.01,
},
Vec3 {
x: 0.0,
y: -0.02,
z: 0.016,
}, //middle
Vec3 {
x: 0.0,
y: 0.0,
z: 0.064,
},
Vec3 {
x: 0.0,
y: 0.0,
z: 0.037,
},
Vec3 {
x: 0.0,
y: 0.0,
z: 0.02,
},
Vec3 {
x: 0.0,
y: 0.0,
z: 0.01,
},
Vec3 {
x: 0.01,
y: -0.015,
z: 0.015,
}, //ring
Vec3 {
x: 0.0,
y: 0.0,
z: 0.064,
},
Vec3 {
x: 0.0,
y: 0.0,
z: 0.037,
},
Vec3 {
x: 0.0,
y: 0.0,
z: 0.02,
},
Vec3 {
x: 0.0,
y: 0.0,
z: 0.01,
},
Vec3 {
x: 0.02,
y: -0.01,
z: 0.015,
}, //little
Vec3 {
x: 0.0,
y: 0.0,
z: 0.064,
},
Vec3 {
x: 0.0,
y: 0.0,
z: 0.037,
},
Vec3 {
x: 0.0,
y: 0.0,
z: 0.02,
},
Vec3 {
x: 0.0,
y: 0.0,
z: 0.01,
},
];
let result = bones_to_transforms(test_hand_bones, hand);
return result;
}
fn bones_to_transforms(hand_bones: [Vec3; 26], hand: Hand) -> [Transform; 26] {
match hand {
Hand::Left => {
let mut result_array: [Transform; 26] = [Transform::default(); 26];
for (place, data) in result_array.iter_mut().zip(hand_bones.iter()) {
*place = Transform {
translation: Vec3 {
x: -data.x,
y: -data.y,
z: -data.z,
},
rotation: Quat::IDENTITY,
scale: Vec3::splat(1.0),
}
}
return result_array;
}
Hand::Right => {
let mut result_array: [Transform; 26] = [Transform::default(); 26];
for (place, data) in result_array.iter_mut().zip(hand_bones.iter()) {
*place = Transform {
translation: Vec3 {
x: data.x,
y: -data.y,
z: -data.z,
},
rotation: Quat::IDENTITY,
scale: Vec3::splat(1.0),
}
}
return result_array;
}
}
}
pub fn get_test_hand_pose_array() -> [Posef; 26] {
let test_hand_pose: [Posef; 26] = [
Posef {
position: Vector3f {
x: 0.0,
y: 0.0,
z: 0.0,
},
orientation: Quaternionf {
x: -0.267,
y: 0.849,
z: 0.204,
w: 0.407,
},
}, //palm
Posef {
position: Vector3f {
x: 0.02,
y: -0.040,
z: -0.015,
},
orientation: Quaternionf {
x: -0.267,
y: 0.849,
z: 0.204,
w: 0.407,
},
},
Posef {
position: Vector3f {
x: 0.019,
y: -0.037,
z: 0.011,
},
orientation: Quaternionf {
x: -0.744,
y: -0.530,
z: 0.156,
w: -0.376,
},
},
Posef {
position: Vector3f {
x: 0.015,
y: -0.014,
z: 0.047,
},
orientation: Quaternionf {
x: -0.786,
y: -0.550,
z: 0.126,
w: -0.254,
},
},
Posef {
position: Vector3f {
x: 0.004,
y: 0.003,
z: 0.068,
},
orientation: Quaternionf {
x: -0.729,
y: -0.564,
z: 0.027,
w: -0.387,
},
},
Posef {
position: Vector3f {
x: -0.009,
y: 0.011,
z: 0.072,
},
orientation: Quaternionf {
x: -0.585,
y: -0.548,
z: -0.140,
w: -0.582,
},
},
Posef {
position: Vector3f {
x: 0.027,
y: -0.021,
z: 0.001,
},
orientation: Quaternionf {
x: -0.277,
y: -0.826,
z: 0.317,
w: -0.376,
},
},
Posef {
position: Vector3f {
x: -0.002,
y: 0.026,
z: 0.034,
},
orientation: Quaternionf {
x: -0.277,
y: -0.826,
z: 0.317,
w: -0.376,
},
},
Posef {
position: Vector3f {
x: -0.023,
y: 0.049,
z: 0.055,
},
orientation: Quaternionf {
x: -0.244,
y: -0.843,
z: 0.256,
w: -0.404,
},
},
Posef {
position: Vector3f {
x: -0.037,
y: 0.059,
z: 0.067,
},
orientation: Quaternionf {
x: -0.200,
y: -0.866,
z: 0.165,
w: -0.428,
},
},
Posef {
position: Vector3f {
x: -0.045,
y: 0.063,
z: 0.073,
},
orientation: Quaternionf {
x: -0.172,
y: -0.874,
z: 0.110,
w: -0.440,
},
},
Posef {
position: Vector3f {
x: 0.021,
y: -0.017,
z: -0.007,
},
orientation: Quaternionf {
x: -0.185,
y: -0.817,
z: 0.370,
w: -0.401,
},
},
Posef {
position: Vector3f {
x: -0.011,
y: 0.029,
z: 0.018,
},
orientation: Quaternionf {
x: -0.185,
y: -0.817,
z: 0.370,
w: -0.401,
},
},
Posef {
position: Vector3f {
x: -0.034,
y: 0.06,
z: 0.033,
},
orientation: Quaternionf {
x: -0.175,
y: -0.809,
z: 0.371,
w: -0.420,
},
},
Posef {
position: Vector3f {
x: -0.051,
y: 0.072,
z: 0.045,
},
orientation: Quaternionf {
x: -0.109,
y: -0.856,
z: 0.245,
w: -0.443,
},
},
Posef {
position: Vector3f {
x: -0.06,
y: 0.077,
z: 0.051,
},
orientation: Quaternionf {
x: -0.075,
y: -0.871,
z: 0.180,
w: -0.450,
},
},
Posef {
position: Vector3f {
x: 0.013,
y: -0.017,
z: -0.015,
},
orientation: Quaternionf {
x: -0.132,
y: -0.786,
z: 0.408,
w: -0.445,
},
},
Posef {
position: Vector3f {
x: -0.02,
y: 0.025,
z: 0.0,
},
orientation: Quaternionf {
x: -0.132,
y: -0.786,
z: 0.408,
w: -0.445,
},
},
Posef {
position: Vector3f {
x: -0.042,
y: 0.055,
z: 0.007,
},
orientation: Quaternionf {
x: -0.131,
y: -0.762,
z: 0.432,
w: -0.464,
},
},
Posef {
position: Vector3f {
x: -0.06,
y: 0.069,
z: 0.015,
},
orientation: Quaternionf {
x: -0.071,
y: -0.810,
z: 0.332,
w: -0.477,
},
},
Posef {
position: Vector3f {
x: -0.069,
y: 0.075,
z: 0.02,
},
orientation: Quaternionf {
x: -0.029,
y: -0.836,
z: 0.260,
w: -0.482,
},
},
Posef {
position: Vector3f {
x: 0.004,
y: -0.022,
z: -0.022,
},
orientation: Quaternionf {
x: -0.060,
y: -0.749,
z: 0.481,
w: -0.452,
},
},
Posef {
position: Vector3f {
x: -0.028,
y: 0.018,
z: -0.015,
},
orientation: Quaternionf {
x: -0.060,
y: -0.749,
z: 0.481,
w: -0.452,
},
},
Posef {
position: Vector3f {
x: -0.046,
y: 0.042,
z: -0.017,
},
orientation: Quaternionf {
x: -0.061,
y: -0.684,
z: 0.534,
w: -0.493,
},
},
Posef {
position: Vector3f {
x: -0.059,
y: 0.053,
z: -0.015,
},
orientation: Quaternionf {
x: 0.002,
y: -0.745,
z: 0.444,
w: -0.498,
},
},
Posef {
position: Vector3f {
x: -0.068,
y: 0.059,
z: -0.013,
},
orientation: Quaternionf {
x: 0.045,
y: -0.780,
z: 0.378,
w: -0.496,
},
},
];
return test_hand_pose;
}

View File

@@ -5,6 +5,8 @@ pub mod oculus_touch;
pub mod prototype_locomotion;
pub mod trackers;
pub mod xr_camera;
pub mod hand_poses;
pub mod hand;
use crate::resources::XrSession;
use crate::xr_begin_frame;
@@ -13,7 +15,7 @@ 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::{BuildChildren, IntoSystemConfigs};
use bevy::prelude::{BuildChildren, IntoSystemConfigs, Component};
use bevy::prelude::{Commands, Plugin, PreUpdate, Quat, Res, SpatialBundle, Update, Vec3};
use bevy::render::camera::CameraProjectionPlugin;
use bevy::render::view::{update_frusta, VisibilitySystems};
@@ -28,7 +30,7 @@ use self::trackers::{
pub struct OpenXrInput {
pub controller_type: XrControllerType,
}
#[derive(Clone, Copy, Debug, Ord, PartialOrd, Eq, PartialEq)]
#[derive(Clone, Copy, Debug, Ord, PartialOrd, Eq, PartialEq, Component)]
pub enum Hand {
Left,
Right,