Merge pull request #91335 from devloglogan/xr-hand-modifier-update

Store previous relative transforms in `XRHandModifier3D`
This commit is contained in:
Rémi Verschelde 2024-04-30 17:03:53 +02:00
commit ec73f5631c
No known key found for this signature in database
GPG Key ID: C3336907360768E1
2 changed files with 27 additions and 0 deletions

View File

@ -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",
@ -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;
}
@ -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;
@ -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) {

View File

@ -73,6 +73,9 @@ private:
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);
};