Path: blob/master/modules/openxr/scene/openxr_hand.cpp
10278 views
/**************************************************************************/1/* openxr_hand.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "openxr_hand.h"3132#include "../extensions/openxr_hand_tracking_extension.h"33#include "../openxr_api.h"3435#include "scene/3d/skeleton_3d.h"36#include "servers/xr_server.h"3738void OpenXRHand::_bind_methods() {39ClassDB::bind_method(D_METHOD("set_hand", "hand"), &OpenXRHand::set_hand);40ClassDB::bind_method(D_METHOD("get_hand"), &OpenXRHand::get_hand);4142ClassDB::bind_method(D_METHOD("set_hand_skeleton", "hand_skeleton"), &OpenXRHand::set_hand_skeleton);43ClassDB::bind_method(D_METHOD("get_hand_skeleton"), &OpenXRHand::get_hand_skeleton);4445ClassDB::bind_method(D_METHOD("set_motion_range", "motion_range"), &OpenXRHand::set_motion_range);46ClassDB::bind_method(D_METHOD("get_motion_range"), &OpenXRHand::get_motion_range);4748ClassDB::bind_method(D_METHOD("set_skeleton_rig", "skeleton_rig"), &OpenXRHand::set_skeleton_rig);49ClassDB::bind_method(D_METHOD("get_skeleton_rig"), &OpenXRHand::get_skeleton_rig);5051ClassDB::bind_method(D_METHOD("set_bone_update", "bone_update"), &OpenXRHand::set_bone_update);52ClassDB::bind_method(D_METHOD("get_bone_update"), &OpenXRHand::get_bone_update);5354ADD_PROPERTY(PropertyInfo(Variant::INT, "hand", PROPERTY_HINT_ENUM, "Left,Right"), "set_hand", "get_hand");55ADD_PROPERTY(PropertyInfo(Variant::INT, "motion_range", PROPERTY_HINT_ENUM, "Unobstructed,Conform to controller"), "set_motion_range", "get_motion_range");56ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "hand_skeleton", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Skeleton3D"), "set_hand_skeleton", "get_hand_skeleton");57ADD_PROPERTY(PropertyInfo(Variant::INT, "skeleton_rig", PROPERTY_HINT_ENUM, "OpenXR,Humanoid"), "set_skeleton_rig", "get_skeleton_rig");58ADD_PROPERTY(PropertyInfo(Variant::INT, "bone_update", PROPERTY_HINT_ENUM, "Full,Rotation Only"), "set_bone_update", "get_bone_update");5960BIND_ENUM_CONSTANT(HAND_LEFT);61BIND_ENUM_CONSTANT(HAND_RIGHT);62BIND_ENUM_CONSTANT(HAND_MAX);6364BIND_ENUM_CONSTANT(MOTION_RANGE_UNOBSTRUCTED);65BIND_ENUM_CONSTANT(MOTION_RANGE_CONFORM_TO_CONTROLLER);66BIND_ENUM_CONSTANT(MOTION_RANGE_MAX);6768BIND_ENUM_CONSTANT(SKELETON_RIG_OPENXR);69BIND_ENUM_CONSTANT(SKELETON_RIG_HUMANOID);70BIND_ENUM_CONSTANT(SKELETON_RIG_MAX);7172BIND_ENUM_CONSTANT(BONE_UPDATE_FULL);73BIND_ENUM_CONSTANT(BONE_UPDATE_ROTATION_ONLY);74BIND_ENUM_CONSTANT(BONE_UPDATE_MAX);75}7677OpenXRHand::OpenXRHand() {78openxr_api = OpenXRAPI::get_singleton();79hand_tracking_ext = OpenXRHandTrackingExtension::get_singleton();80}8182void OpenXRHand::set_hand(Hands p_hand) {83ERR_FAIL_INDEX(p_hand, HAND_MAX);8485hand = p_hand;86}8788OpenXRHand::Hands OpenXRHand::get_hand() const {89return hand;90}9192void OpenXRHand::set_hand_skeleton(const NodePath &p_hand_skeleton) {93hand_skeleton = p_hand_skeleton;9495// TODO if inside tree call _get_bones()96}9798void OpenXRHand::set_motion_range(MotionRange p_motion_range) {99ERR_FAIL_INDEX(p_motion_range, MOTION_RANGE_MAX);100motion_range = p_motion_range;101102_set_motion_range();103}104105OpenXRHand::MotionRange OpenXRHand::get_motion_range() const {106return motion_range;107}108109NodePath OpenXRHand::get_hand_skeleton() const {110return hand_skeleton;111}112113void OpenXRHand::_set_motion_range() {114if (!hand_tracking_ext) {115return;116}117118XrHandJointsMotionRangeEXT xr_motion_range;119switch (motion_range) {120case MOTION_RANGE_UNOBSTRUCTED:121xr_motion_range = XR_HAND_JOINTS_MOTION_RANGE_UNOBSTRUCTED_EXT;122break;123case MOTION_RANGE_CONFORM_TO_CONTROLLER:124xr_motion_range = XR_HAND_JOINTS_MOTION_RANGE_CONFORMING_TO_CONTROLLER_EXT;125break;126default:127xr_motion_range = XR_HAND_JOINTS_MOTION_RANGE_CONFORMING_TO_CONTROLLER_EXT;128break;129}130131hand_tracking_ext->set_motion_range(OpenXRHandTrackingExtension::HandTrackedHands(hand), xr_motion_range);132}133134void OpenXRHand::set_skeleton_rig(SkeletonRig p_skeleton_rig) {135ERR_FAIL_INDEX(p_skeleton_rig, SKELETON_RIG_MAX);136137skeleton_rig = p_skeleton_rig;138}139140OpenXRHand::SkeletonRig OpenXRHand::get_skeleton_rig() const {141return skeleton_rig;142}143144void OpenXRHand::set_bone_update(BoneUpdate p_bone_update) {145ERR_FAIL_INDEX(p_bone_update, BONE_UPDATE_MAX);146147bone_update = p_bone_update;148}149150OpenXRHand::BoneUpdate OpenXRHand::get_bone_update() const {151return bone_update;152}153154Skeleton3D *OpenXRHand::get_skeleton() {155if (!has_node(hand_skeleton)) {156return nullptr;157}158159Node *node = get_node(hand_skeleton);160if (!node) {161return nullptr;162}163164Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(node);165return skeleton;166}167168void OpenXRHand::_get_joint_data() {169// Table of bone names for different rig types.170static const String bone_names[SKELETON_RIG_MAX][XR_HAND_JOINT_COUNT_EXT] = {171// SKELETON_RIG_OPENXR bone names.172{173"Palm",174"Wrist",175"Thumb_Metacarpal",176"Thumb_Proximal",177"Thumb_Distal",178"Thumb_Tip",179"Index_Metacarpal",180"Index_Proximal",181"Index_Intermediate",182"Index_Distal",183"Index_Tip",184"Middle_Metacarpal",185"Middle_Proximal",186"Middle_Intermediate",187"Middle_Distal",188"Middle_Tip",189"Ring_Metacarpal",190"Ring_Proximal",191"Ring_Intermediate",192"Ring_Distal",193"Ring_Tip",194"Little_Metacarpal",195"Little_Proximal",196"Little_Intermediate",197"Little_Distal",198"Little_Tip" },199200// SKELETON_RIG_HUMANOID bone names.201{202"Palm",203"Hand",204"ThumbMetacarpal",205"ThumbProximal",206"ThumbDistal",207"ThumbTip",208"IndexMetacarpal",209"IndexProximal",210"IndexIntermediate",211"IndexDistal",212"IndexTip",213"MiddleMetacarpal",214"MiddleProximal",215"MiddleIntermediate",216"MiddleDistal",217"MiddleTip",218"RingMetacarpal",219"RingProximal",220"RingIntermediate",221"RingDistal",222"RingTip",223"LittleMetacarpal",224"LittleProximal",225"LittleIntermediate",226"LittleDistal",227"LittleTip" }228};229230// Table of bone name formats for different rig types and left/right hands.231static const String bone_name_formats[SKELETON_RIG_MAX][2] = {232// SKELETON_RIG_OPENXR bone name format.233{ "<bone>_L", "<bone>_R" },234235// SKELETON_RIG_HUMANOID bone name format.236{ "Left<bone>", "Right<bone>" }237};238239// reset JIC240for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) {241joints[i].bone = -1;242joints[i].parent_joint = -1;243}244245Skeleton3D *skeleton = get_skeleton();246if (!skeleton) {247return;248}249250// Find the skeleton-bones associated with each OpenXR joint.251int bones[XR_HAND_JOINT_COUNT_EXT];252for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) {253// Construct the expected bone name.254String bone_name = bone_name_formats[skeleton_rig][hand].replace("<bone>", bone_names[skeleton_rig][i]);255256// Find the skeleton bone.257bones[i] = skeleton->find_bone(bone_name);258if (bones[i] == -1) {259print_line("Couldn't obtain bone for", bone_name);260}261}262263// Assemble the OpenXR joint relationship to the available skeleton bones.264for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) {265// Get the skeleton bone (skip if not found).266const int bone = bones[i];267if (bone == -1) {268continue;269}270271// Find the parent skeleton-bone.272const int parent_bone = skeleton->get_bone_parent(bone);273if (parent_bone == -1) {274// If no parent skeleton-bone exists then drive this relative to palm joint.275joints[i].bone = bone;276joints[i].parent_joint = XR_HAND_JOINT_PALM_EXT;277continue;278}279280// Find the OpenXR joint associated with the parent skeleton-bone.281for (int j = 0; j < XR_HAND_JOINT_COUNT_EXT; ++j) {282if (bones[j] == parent_bone) {283// If a parent joint is found then drive this bone relative to it.284joints[i].bone = bone;285joints[i].parent_joint = j;286break;287}288}289}290}291292void OpenXRHand::_update_skeleton() {293if (openxr_api == nullptr || !openxr_api->is_initialized()) {294return;295} else if (hand_tracking_ext == nullptr || !hand_tracking_ext->get_active()) {296return;297}298299Skeleton3D *skeleton = get_skeleton();300if (!skeleton) {301return;302}303304// Table of bone adjustments for different rig types305static const Quaternion bone_adjustments[SKELETON_RIG_MAX] = {306// SKELETON_RIG_OPENXR bone adjustment. This is an identity quaternion307// because the incoming quaternions are already in OpenXR format.308Quaternion(),309310// SKELETON_RIG_HUMANOID bone adjustment. This rotation performs:311// OpenXR Z+ -> Godot Humanoid Y- (Back along the bone)312// OpenXR Y+ -> Godot Humanoid Z- (Out the back of the hand)313Quaternion(0.0, -Math::SQRT12, Math::SQRT12, 0.0),314};315316// we cache our transforms so we can quickly calculate local transforms317XRPose::TrackingConfidence confidences[XR_HAND_JOINT_COUNT_EXT];318Quaternion quaternions[XR_HAND_JOINT_COUNT_EXT];319Quaternion inv_quaternions[XR_HAND_JOINT_COUNT_EXT];320Vector3 positions[XR_HAND_JOINT_COUNT_EXT];321322const Quaternion &rig_adjustment = bone_adjustments[skeleton_rig];323const OpenXRHandTrackingExtension::HandTracker *hand_tracker = hand_tracking_ext->get_hand_tracker(OpenXRHandTrackingExtension::HandTrackedHands(hand));324const float ws = XRServer::get_singleton()->get_world_scale();325326if (hand_tracker->is_initialized && hand_tracker->locations.isActive) {327for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) {328confidences[i] = XRPose::XR_TRACKING_CONFIDENCE_NONE;329quaternions[i] = Quaternion();330positions[i] = Vector3();331332const XrHandJointLocationEXT &location = hand_tracker->joint_locations[i];333const XrPosef &pose = location.pose;334335if (location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT) {336if (pose.orientation.x != 0 || pose.orientation.y != 0 || pose.orientation.z != 0 || pose.orientation.w != 0) {337quaternions[i] = Quaternion(pose.orientation.x, pose.orientation.y, pose.orientation.z, pose.orientation.w) * rig_adjustment;338inv_quaternions[i] = quaternions[i].inverse();339340if (location.locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT) {341confidences[i] = XRPose::XR_TRACKING_CONFIDENCE_HIGH;342positions[i] = Vector3(pose.position.x * ws, pose.position.y * ws, pose.position.z * ws);343344// TODO get inverse of position, we'll do this later. For now we're ignoring bone positions which generally works better anyway345} else {346confidences[i] = XRPose::XR_TRACKING_CONFIDENCE_LOW;347}348}349}350}351352if (confidences[XR_HAND_JOINT_PALM_EXT] != XRPose::XR_TRACKING_CONFIDENCE_NONE) {353// Iterate over all the OpenXR joints.354for (int joint = 0; joint < XR_HAND_JOINT_COUNT_EXT; joint++) {355// Get the skeleton bone (skip if none).356const int bone = joints[joint].bone;357if (bone == -1) {358continue;359}360361// Calculate the relative relationship to the parent bone joint.362const int parent_joint = joints[joint].parent_joint;363const Quaternion q = inv_quaternions[parent_joint] * quaternions[joint];364const Vector3 p = inv_quaternions[parent_joint].xform(positions[joint] - positions[parent_joint]);365366// Update the bone position if enabled by update mode.367if (bone_update == BONE_UPDATE_FULL) {368skeleton->set_bone_pose_position(joints[joint].bone, p);369}370371// Always update the bone rotation.372skeleton->set_bone_pose_rotation(joints[joint].bone, q);373}374375// Transform the OpenXRHand to the skeleton pose.376Transform3D t;377t.basis = Basis(quaternions[XR_HAND_JOINT_PALM_EXT]);378t.origin = positions[XR_HAND_JOINT_PALM_EXT];379set_transform(t);380381// show it382set_visible(true);383} else {384// hide it385set_visible(false);386}387} else {388// hide it389set_visible(false);390}391}392393void OpenXRHand::_notification(int p_what) {394switch (p_what) {395case NOTIFICATION_ENTER_TREE: {396_get_joint_data();397398set_process_internal(true);399} break;400case NOTIFICATION_EXIT_TREE: {401set_process_internal(false);402403// reset404for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) {405joints[i].bone = -1;406joints[i].parent_joint = -1;407}408} break;409case NOTIFICATION_INTERNAL_PROCESS: {410_update_skeleton();411} break;412default: {413} break;414}415}416417418