Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store previous relative transforms in XRHandModifier3D #91335

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions scene/3d/xr_hand_modifier_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ void XRHandModifier3D::_get_joint_data() {
return;
}

if (has_stored_previous_transforms) {
previous_relative_transforms.clear();
has_stored_previous_transforms = false;
}

// Table of bone names for different rig types.
static const String bone_names[XRHandTracker::HAND_JOINT_MAX] = {
"Palm",
Expand Down Expand Up @@ -196,6 +201,18 @@ void XRHandModifier3D::_process_modification() {

// Skip if no tracking data
if (!tracker->get_has_tracking_data()) {
if (!has_stored_previous_transforms) {
return;
}

// Apply previous relative transforms if they are stored.
for (int joint = 0; joint < XRHandTracker::HAND_JOINT_MAX; joint++) {
if (bone_update == BONE_UPDATE_FULL) {
skeleton->set_bone_pose_position(joints[joint].bone, previous_relative_transforms[joint].origin);
}

skeleton->set_bone_pose_rotation(joints[joint].bone, Quaternion(previous_relative_transforms[joint].basis));
}
return;
}

Expand Down Expand Up @@ -223,6 +240,12 @@ void XRHandModifier3D::_process_modification() {
return;
}

if (!has_stored_previous_transforms) {
previous_relative_transforms.resize(XRHandTracker::HAND_JOINT_MAX);
has_stored_previous_transforms = true;
}
Transform3D *previous_relative_transforms_ptr = previous_relative_transforms.ptrw();

for (int joint = 0; joint < XRHandTracker::HAND_JOINT_MAX; joint++) {
// Get the skeleton bone (skip if none).
const int bone = joints[joint].bone;
Expand All @@ -233,6 +256,7 @@ void XRHandModifier3D::_process_modification() {
// Calculate the relative relationship to the parent bone joint.
const int parent_joint = joints[joint].parent_joint;
const Transform3D relative_transform = inv_transforms[parent_joint] * transforms[joint];
previous_relative_transforms_ptr[joint] = relative_transform;

// Update the bone position if enabled by update mode.
if (bone_update == BONE_UPDATE_FULL) {
Expand Down
3 changes: 3 additions & 0 deletions scene/3d/xr_hand_modifier_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class XRHandModifier3D : public SkeletonModifier3D {
BoneUpdate bone_update = BONE_UPDATE_FULL;
JointData joints[XRHandTracker::HAND_JOINT_MAX];

bool has_stored_previous_transforms = false;
Vector<Transform3D> previous_relative_transforms;

void _get_joint_data();
void _tracker_changed(StringName p_tracker_name, XRServer::TrackerType p_tracker_type);
};
Expand Down
Loading