Skip to content

Commit

Permalink
Changing skeleton logic to new agreed upon bone names
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiaanOlij committed Mar 3, 2021
1 parent 874372c commit fa6bf66
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 101 deletions.
31 changes: 8 additions & 23 deletions demo/Main.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
[ext_resource path="res://Ground.tscn" type="PackedScene" id=2]
[ext_resource path="res://Table.tscn" type="PackedScene" id=3]
[ext_resource path="res://addons/godot-openxr/scenes/first_person_controller_vr.tscn" type="PackedScene" id=4]
[ext_resource path="res://addons/godot-openxr/scenes/left_hand.tscn" type="PackedScene" id=5]
[ext_resource path="res://addons/godot-openxr/scenes/right_hand.tscn" type="PackedScene" id=6]
[ext_resource path="res://addons/godot-openxr/scenes/right_hand_nodes.tscn" type="PackedScene" id=5]
[ext_resource path="res://addons/godot-openxr/scenes/left_hand_nodes.tscn" type="PackedScene" id=6]
[ext_resource path="res://addons/godot-openxr/scenes/XRPose.tscn" type="PackedScene" id=7]
[ext_resource path="res://addons/godot-xr-tools/objects/Viewport_2D_in_3D.tscn" type="PackedScene" id=8]
[ext_resource path="res://ControllerInfo.tscn" type="PackedScene" id=9]
Expand Down Expand Up @@ -55,23 +55,13 @@ visible = true

[node name="Info" parent="FPSController/LeftHandController" index="0" instance=ExtResource( 8 )]
transform = Transform( 1, 0, 0, 0, 0.939693, 0.34202, 0, -0.34202, 0.939693, 0.0483518, 0.0719222, 0 )
enabled = true
screen_size = Vector2( 0.16, 0.12 )
viewport_size = Vector2( 600, 400 )
transparent = true
scene = ExtResource( 9 )
collision_layer = 0

[node name="Function_Teleport" parent="FPSController/LeftHandController" index="1" instance=ExtResource( 10 )]
collision_mask = 1022
enabled = true
teleport_button = 15
can_teleport_color = Color( 0, 1, 0, 1 )
cant_teleport_color = Color( 1, 0, 0, 1 )
player_height = 1.8
player_radius = 0.4
strength = 5.0
max_slope = 20.0
camera = NodePath("../../ARVRCamera")

[node name="Function_pointer" parent="FPSController/LeftHandController" index="2" instance=ExtResource( 13 )]
Expand All @@ -85,10 +75,8 @@ visible = true

[node name="Info" parent="FPSController/RightHandController" index="0" instance=ExtResource( 8 )]
transform = Transform( 1, 0, 0, 0, 0.939693, 0.34202, 0, -0.34202, 0.939693, -0.0871993, 0.072, 0 )
enabled = true
screen_size = Vector2( 0.16, 0.12 )
viewport_size = Vector2( 600, 400 )
transparent = true
scene = ExtResource( 9 )
collision_layer = 0

Expand All @@ -103,12 +91,6 @@ show_target = true
collision_mask = 1024
collide_with_areas = true

[node name="Left_hand" parent="FPSController" instance=ExtResource( 5 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.5, 1, -0.5 )

[node name="Right_hand" parent="FPSController" instance=ExtResource( 6 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.5, 1, -0.5 )

[node name="Left_grip_pose" parent="FPSController" instance=ExtResource( 7 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.5, 1, -0.5 )
action = "godot/grip_pose"
Expand All @@ -127,17 +109,20 @@ transform = Transform( 1, 0, 0, 0, -1.62921e-07, -1, 0, 1, -1.62921e-07, 0, 0, 0
mesh = SubResource( 3 )
material/0 = null

[node name="Left_hand" parent="FPSController" instance=ExtResource( 6 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -0.5, 1, -0.5 )

[node name="Right_hand" parent="FPSController" instance=ExtResource( 5 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.5, 1, -0.5 )

[node name="Table" parent="." instance=ExtResource( 3 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -6 )

[node name="Ground" parent="." instance=ExtResource( 2 )]

[node name="Screen" parent="." instance=ExtResource( 8 )]
transform = Transform( 0.843251, 0, -0.53752, 0, 1, 0, 0.53752, 0, 0.843251, 1.92678, 1.5, -2.47671 )
enabled = true
screen_size = Vector2( 3, 2 )
viewport_size = Vector2( 600, 400 )
transparent = true
scene = ExtResource( 12 )
collision_layer = 1024

Expand Down
2 changes: 1 addition & 1 deletion demo/addons/godot-xr-tools/assets/LeftHandBlendTree.tres
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ nodes/SetGrip/position = Vector2( 360, 40 )
nodes/SetIndex/node = SubResource( 7 )
nodes/SetIndex/position = Vector2( 360, 240 )
nodes/output/position = Vector2( 1020, 80 )
node_connections = [ "IndexTimeScale", 0, "SetIndex", "Blend2", 0, "GripTimeScale", "Blend2", 1, "IndexTimeScale", "GripTimeScale", 0, "SetGrip", "SetIndex", 0, "Animation 2", "SetGrip", 0, "GripAnimation", "output", 0, "Blend2" ]
node_connections = [ "output", 0, "Blend2", "SetGrip", 0, "GripAnimation", "Blend2", 0, "GripTimeScale", "Blend2", 1, "IndexTimeScale", "IndexTimeScale", 0, "SetIndex", "SetIndex", 0, "Animation 2", "GripTimeScale", 0, "SetGrip" ]
4 changes: 4 additions & 0 deletions demo/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

config_version=4

_global_script_classes=[ ]
_global_script_class_icons={
}

[application]

config/name="Godot OpenXR demo"
Expand Down
139 changes: 62 additions & 77 deletions src/openxr/OpenXRSkeleton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,39 +37,46 @@ void OpenXRSkeleton::_init() {

void OpenXRSkeleton::_ready() {
const char *bone_names[XR_HAND_JOINT_COUNT_EXT] = {
".",
"Palm",
"Wrist",
"ThumbMetacarpal",
"ThumbProximal",
"ThumbDistal",
"ThumbTip",
"IndexMetacarpal",
"IndexProximal",
"IndexIntermediate",
"IndexDistal",
"IndexTip",
"MiddleMetacarpal",
"MiddleProximal",
"MiddleIntermediate",
"MiddleDistal",
"MiddleTip",
"RingMetacarpal",
"RingProximal",
"RingIntermediate",
"RingDistal",
"RingTip",
"LittleMetacarpal",
"LittleProximal",
"LittleIntermediate",
"LittleDistal",
"LittleTip",
"Thumb_Metacarpal",
"Thumb_Proximal",
"Thumb_Distal",
"Thumb_Tip",
"Index_Metacarpal",
"Index_Proximal",
"Index_Intermediate",
"Index_Distal",
"Index_Tip",
"Middle_Metacarpal",
"Middle_Proximal",
"Middle_Intermediate",
"Middle_Distal",
"Middle_Tip",
"Ring_Metacarpal",
"Ring_Proximal",
"Ring_Intermediate",
"Ring_Distal",
"Ring_Tip",
"Little_Metacarpal",
"Little_Proximal",
"Little_Intermediate",
"Little_Distal",
"Little_Tip",
};

// We cast to spatials which should allow us to use any subclass of that.
for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) {
bones[i] = find_bone(bone_names[i]);
char bone_name[250];
if (hand == 0) {
sprintf(bone_name, "%s_L", bone_names[i]);
} else {
sprintf(bone_name, "%s_R", bone_names[i]);
}

bones[i] = find_bone(bone_name);
if (bones[i] == -1) {
printf("Couldn't obtain bone for %s\n", bone_names[i]);
Godot::print("Couldn't obtain bone for {0}", bone_name);
}
}
}
Expand All @@ -81,63 +88,41 @@ void OpenXRSkeleton::_physics_process(float delta) {
return;
}

const int parents[XR_HAND_JOINT_COUNT_EXT]{
-1, // XR_HAND_JOINT_PALM_EXT = 0,
0, // XR_HAND_JOINT_WRIST_EXT = 1,
1, // XR_HAND_JOINT_THUMB_METACARPAL_EXT = 2,
2, // XR_HAND_JOINT_THUMB_PROXIMAL_EXT = 3,
3, // XR_HAND_JOINT_THUMB_DISTAL_EXT = 4,
4, // XR_HAND_JOINT_THUMB_TIP_EXT = 5,
1, // XR_HAND_JOINT_INDEX_METACARPAL_EXT = 6,
6, // XR_HAND_JOINT_INDEX_PROXIMAL_EXT = 7,
7, // XR_HAND_JOINT_INDEX_INTERMEDIATE_EXT = 8,
8, // XR_HAND_JOINT_INDEX_DISTAL_EXT = 9,
9, // XR_HAND_JOINT_INDEX_TIP_EXT = 10,
1, // XR_HAND_JOINT_MIDDLE_METACARPAL_EXT = 11,
11, // XR_HAND_JOINT_MIDDLE_PROXIMAL_EXT = 12,
12, // XR_HAND_JOINT_MIDDLE_INTERMEDIATE_EXT = 13,
13, // XR_HAND_JOINT_MIDDLE_DISTAL_EXT = 14,
14, // XR_HAND_JOINT_MIDDLE_TIP_EXT = 15,
1, // XR_HAND_JOINT_RING_METACARPAL_EXT = 16,
16, // XR_HAND_JOINT_RING_PROXIMAL_EXT = 17,
17, // XR_HAND_JOINT_RING_INTERMEDIATE_EXT = 18,
18, // XR_HAND_JOINT_RING_DISTAL_EXT = 19,
19, // XR_HAND_JOINT_RING_TIP_EXT = 20,
1, // XR_HAND_JOINT_LITTLE_METACARPAL_EXT = 21,
21, // XR_HAND_JOINT_LITTLE_PROXIMAL_EXT = 22,
22, // XR_HAND_JOINT_LITTLE_INTERMEDIATE_EXT = 23,
23, // XR_HAND_JOINT_LITTLE_DISTAL_EXT = 24,
24, // XR_HAND_JOINT_LITTLE_TIP_EXT = 25,
};

// we cache the inverse of our transforms so we can quickly calculate local transforms
// we cache our transforms so we can quickly calculate local transforms
Transform transforms[XR_HAND_JOINT_COUNT_EXT];
Transform inv_transforms[XR_HAND_JOINT_COUNT_EXT];

const HandTracker *hand_tracker = openxr_api->get_hand_tracker(hand);
const float ws = ARVRServer::get_singleton()->get_world_scale();

if (hand_tracker->is_initialised && hand_tracker->locations.isActive) {
// get our transforms
for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) {
const XrPosef &pose = hand_tracker->joint_locations[i].pose;
transforms[i] = openxr_api->transform_from_pose(pose, ws);
inv_transforms[i] = transforms[i].inverse();
}

// now update our skeleton
for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) {
if (i == 0) {
// just get our transform, we place our mesh with OpenXRPose
const XrPosef &pose = hand_tracker->joint_locations[i].pose;
Transform t = openxr_api->transform_from_pose(pose, ws);

inv_transforms[i] = t.inverse();
} else if (bones[i] == -1) {
// missing bone...
inv_transforms[i] = Transform();
} else if (parents[i] != -1) {
const XrPosef &pose = hand_tracker->joint_locations[i].pose;

Transform t = openxr_api->transform_from_pose(pose, ws);
inv_transforms[i] = t.inverse();

// get local translation
if (parents[i] != -1) {
// parent should already be processed
int parent = parents[i];
t = inv_transforms[parent] * t;
if (bones[i] != -1) {
int bone = bones[i];
int parent = get_bone_parent(bone);

Transform t = transforms[i];

// get local translation, parent should already be processed
if (parent == -1) {
// use our palm location here, that is what we are tracking
t = inv_transforms[XR_HAND_JOINT_PALM_EXT] * t;
} else {
int found = false;
for (int b = 0; b < XR_HAND_JOINT_COUNT_EXT && !found; b++) {
if (bones[b] == parent) {
t = inv_transforms[b] * t;
found = true;
}
}
}

// get difference with rest
Expand Down

0 comments on commit fa6bf66

Please sign in to comment.