openxr_hand.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /**************************************************************************/
  2. /* openxr_hand.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "openxr_hand.h"
  31. #include "../extensions/openxr_hand_tracking_extension.h"
  32. #include "../openxr_api.h"
  33. #include "scene/3d/skeleton_3d.h"
  34. #include "servers/xr_server.h"
  35. void OpenXRHand::_bind_methods() {
  36. ClassDB::bind_method(D_METHOD("set_hand", "hand"), &OpenXRHand::set_hand);
  37. ClassDB::bind_method(D_METHOD("get_hand"), &OpenXRHand::get_hand);
  38. ClassDB::bind_method(D_METHOD("set_hand_skeleton", "hand_skeleton"), &OpenXRHand::set_hand_skeleton);
  39. ClassDB::bind_method(D_METHOD("get_hand_skeleton"), &OpenXRHand::get_hand_skeleton);
  40. ClassDB::bind_method(D_METHOD("set_motion_range", "motion_range"), &OpenXRHand::set_motion_range);
  41. ClassDB::bind_method(D_METHOD("get_motion_range"), &OpenXRHand::get_motion_range);
  42. ClassDB::bind_method(D_METHOD("set_skeleton_rig", "skeleton_rig"), &OpenXRHand::set_skeleton_rig);
  43. ClassDB::bind_method(D_METHOD("get_skeleton_rig"), &OpenXRHand::get_skeleton_rig);
  44. ClassDB::bind_method(D_METHOD("set_bone_update", "bone_update"), &OpenXRHand::set_bone_update);
  45. ClassDB::bind_method(D_METHOD("get_bone_update"), &OpenXRHand::get_bone_update);
  46. ADD_PROPERTY(PropertyInfo(Variant::INT, "hand", PROPERTY_HINT_ENUM, "Left,Right"), "set_hand", "get_hand");
  47. ADD_PROPERTY(PropertyInfo(Variant::INT, "motion_range", PROPERTY_HINT_ENUM, "Unobstructed,Conform to controller"), "set_motion_range", "get_motion_range");
  48. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "hand_skeleton", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Skeleton3D"), "set_hand_skeleton", "get_hand_skeleton");
  49. ADD_PROPERTY(PropertyInfo(Variant::INT, "skeleton_rig", PROPERTY_HINT_ENUM, "OpenXR,Humanoid"), "set_skeleton_rig", "get_skeleton_rig");
  50. ADD_PROPERTY(PropertyInfo(Variant::INT, "bone_update", PROPERTY_HINT_ENUM, "Full,Rotation Only"), "set_bone_update", "get_bone_update");
  51. BIND_ENUM_CONSTANT(HAND_LEFT);
  52. BIND_ENUM_CONSTANT(HAND_RIGHT);
  53. BIND_ENUM_CONSTANT(HAND_MAX);
  54. BIND_ENUM_CONSTANT(MOTION_RANGE_UNOBSTRUCTED);
  55. BIND_ENUM_CONSTANT(MOTION_RANGE_CONFORM_TO_CONTROLLER);
  56. BIND_ENUM_CONSTANT(MOTION_RANGE_MAX);
  57. BIND_ENUM_CONSTANT(SKELETON_RIG_OPENXR);
  58. BIND_ENUM_CONSTANT(SKELETON_RIG_HUMANOID);
  59. BIND_ENUM_CONSTANT(SKELETON_RIG_MAX);
  60. BIND_ENUM_CONSTANT(BONE_UPDATE_FULL);
  61. BIND_ENUM_CONSTANT(BONE_UPDATE_ROTATION_ONLY);
  62. BIND_ENUM_CONSTANT(BONE_UPDATE_MAX);
  63. }
  64. OpenXRHand::OpenXRHand() {
  65. openxr_api = OpenXRAPI::get_singleton();
  66. hand_tracking_ext = OpenXRHandTrackingExtension::get_singleton();
  67. }
  68. void OpenXRHand::set_hand(Hands p_hand) {
  69. ERR_FAIL_INDEX(p_hand, HAND_MAX);
  70. hand = p_hand;
  71. }
  72. OpenXRHand::Hands OpenXRHand::get_hand() const {
  73. return hand;
  74. }
  75. void OpenXRHand::set_hand_skeleton(const NodePath &p_hand_skeleton) {
  76. hand_skeleton = p_hand_skeleton;
  77. // TODO if inside tree call _get_bones()
  78. }
  79. void OpenXRHand::set_motion_range(MotionRange p_motion_range) {
  80. ERR_FAIL_INDEX(p_motion_range, MOTION_RANGE_MAX);
  81. motion_range = p_motion_range;
  82. _set_motion_range();
  83. }
  84. OpenXRHand::MotionRange OpenXRHand::get_motion_range() const {
  85. return motion_range;
  86. }
  87. NodePath OpenXRHand::get_hand_skeleton() const {
  88. return hand_skeleton;
  89. }
  90. void OpenXRHand::_set_motion_range() {
  91. if (!hand_tracking_ext) {
  92. return;
  93. }
  94. XrHandJointsMotionRangeEXT xr_motion_range;
  95. switch (motion_range) {
  96. case MOTION_RANGE_UNOBSTRUCTED:
  97. xr_motion_range = XR_HAND_JOINTS_MOTION_RANGE_UNOBSTRUCTED_EXT;
  98. break;
  99. case MOTION_RANGE_CONFORM_TO_CONTROLLER:
  100. xr_motion_range = XR_HAND_JOINTS_MOTION_RANGE_CONFORMING_TO_CONTROLLER_EXT;
  101. break;
  102. default:
  103. xr_motion_range = XR_HAND_JOINTS_MOTION_RANGE_CONFORMING_TO_CONTROLLER_EXT;
  104. break;
  105. }
  106. hand_tracking_ext->set_motion_range(OpenXRHandTrackingExtension::HandTrackedHands(hand), xr_motion_range);
  107. }
  108. void OpenXRHand::set_skeleton_rig(SkeletonRig p_skeleton_rig) {
  109. ERR_FAIL_INDEX(p_skeleton_rig, SKELETON_RIG_MAX);
  110. skeleton_rig = p_skeleton_rig;
  111. }
  112. OpenXRHand::SkeletonRig OpenXRHand::get_skeleton_rig() const {
  113. return skeleton_rig;
  114. }
  115. void OpenXRHand::set_bone_update(BoneUpdate p_bone_update) {
  116. ERR_FAIL_INDEX(p_bone_update, BONE_UPDATE_MAX);
  117. bone_update = p_bone_update;
  118. }
  119. OpenXRHand::BoneUpdate OpenXRHand::get_bone_update() const {
  120. return bone_update;
  121. }
  122. Skeleton3D *OpenXRHand::get_skeleton() {
  123. if (!has_node(hand_skeleton)) {
  124. return nullptr;
  125. }
  126. Node *node = get_node(hand_skeleton);
  127. if (!node) {
  128. return nullptr;
  129. }
  130. Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(node);
  131. return skeleton;
  132. }
  133. void OpenXRHand::_get_joint_data() {
  134. // Table of bone names for different rig types.
  135. static const String bone_names[SKELETON_RIG_MAX][XR_HAND_JOINT_COUNT_EXT] = {
  136. // SKELETON_RIG_OPENXR bone names.
  137. {
  138. "Palm",
  139. "Wrist",
  140. "Thumb_Metacarpal",
  141. "Thumb_Proximal",
  142. "Thumb_Distal",
  143. "Thumb_Tip",
  144. "Index_Metacarpal",
  145. "Index_Proximal",
  146. "Index_Intermediate",
  147. "Index_Distal",
  148. "Index_Tip",
  149. "Middle_Metacarpal",
  150. "Middle_Proximal",
  151. "Middle_Intermediate",
  152. "Middle_Distal",
  153. "Middle_Tip",
  154. "Ring_Metacarpal",
  155. "Ring_Proximal",
  156. "Ring_Intermediate",
  157. "Ring_Distal",
  158. "Ring_Tip",
  159. "Little_Metacarpal",
  160. "Little_Proximal",
  161. "Little_Intermediate",
  162. "Little_Distal",
  163. "Little_Tip" },
  164. // SKELETON_RIG_HUMANOID bone names.
  165. {
  166. "Palm",
  167. "Hand",
  168. "ThumbMetacarpal",
  169. "ThumbProximal",
  170. "ThumbDistal",
  171. "ThumbTip",
  172. "IndexMetacarpal",
  173. "IndexProximal",
  174. "IndexIntermediate",
  175. "IndexDistal",
  176. "IndexTip",
  177. "MiddleMetacarpal",
  178. "MiddleProximal",
  179. "MiddleIntermediate",
  180. "MiddleDistal",
  181. "MiddleTip",
  182. "RingMetacarpal",
  183. "RingProximal",
  184. "RingIntermediate",
  185. "RingDistal",
  186. "RingTip",
  187. "LittleMetacarpal",
  188. "LittleProximal",
  189. "LittleIntermediate",
  190. "LittleDistal",
  191. "LittleTip" }
  192. };
  193. // Table of bone name formats for different rig types and left/right hands.
  194. static const String bone_name_formats[SKELETON_RIG_MAX][2] = {
  195. // SKELETON_RIG_OPENXR bone name format.
  196. { "<bone>_L", "<bone>_R" },
  197. // SKELETON_RIG_HUMANOID bone name format.
  198. { "Left<bone>", "Right<bone>" }
  199. };
  200. // reset JIC
  201. for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) {
  202. joints[i].bone = -1;
  203. joints[i].parent_joint = -1;
  204. }
  205. Skeleton3D *skeleton = get_skeleton();
  206. if (!skeleton) {
  207. return;
  208. }
  209. // Find the skeleton-bones associated with each OpenXR joint.
  210. int bones[XR_HAND_JOINT_COUNT_EXT];
  211. for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) {
  212. // Construct the expected bone name.
  213. String bone_name = bone_name_formats[skeleton_rig][hand].replace("<bone>", bone_names[skeleton_rig][i]);
  214. // Find the skeleton bone.
  215. bones[i] = skeleton->find_bone(bone_name);
  216. if (bones[i] == -1) {
  217. print_line("Couldn't obtain bone for", bone_name);
  218. }
  219. }
  220. // Assemble the OpenXR joint relationship to the available skeleton bones.
  221. for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) {
  222. // Get the skeleton bone (skip if not found).
  223. const int bone = bones[i];
  224. if (bone == -1) {
  225. continue;
  226. }
  227. // Find the parent skeleton-bone.
  228. const int parent_bone = skeleton->get_bone_parent(bone);
  229. if (parent_bone == -1) {
  230. // If no parent skeleton-bone exists then drive this relative to palm joint.
  231. joints[i].bone = bone;
  232. joints[i].parent_joint = XR_HAND_JOINT_PALM_EXT;
  233. continue;
  234. }
  235. // Find the OpenXR joint associated with the parent skeleton-bone.
  236. for (int j = 0; j < XR_HAND_JOINT_COUNT_EXT; ++j) {
  237. if (bones[j] == parent_bone) {
  238. // If a parent joint is found then drive this bone relative to it.
  239. joints[i].bone = bone;
  240. joints[i].parent_joint = j;
  241. break;
  242. }
  243. }
  244. }
  245. }
  246. void OpenXRHand::_update_skeleton() {
  247. if (openxr_api == nullptr || !openxr_api->is_initialized()) {
  248. return;
  249. } else if (hand_tracking_ext == nullptr || !hand_tracking_ext->get_active()) {
  250. return;
  251. }
  252. Skeleton3D *skeleton = get_skeleton();
  253. if (!skeleton) {
  254. return;
  255. }
  256. // Table of bone adjustments for different rig types
  257. static const Quaternion bone_adjustments[SKELETON_RIG_MAX] = {
  258. // SKELETON_RIG_OPENXR bone adjustment. This is an identity quaternion
  259. // because the incoming quaternions are already in OpenXR format.
  260. Quaternion(),
  261. // SKELETON_RIG_HUMANOID bone adjustment. This rotation performs:
  262. // OpenXR Z+ -> Godot Humanoid Y- (Back along the bone)
  263. // OpenXR Y+ -> Godot Humanoid Z- (Out the back of the hand)
  264. Quaternion(0.0, -Math_SQRT12, Math_SQRT12, 0.0),
  265. };
  266. // we cache our transforms so we can quickly calculate local transforms
  267. XRPose::TrackingConfidence confidences[XR_HAND_JOINT_COUNT_EXT];
  268. Quaternion quaternions[XR_HAND_JOINT_COUNT_EXT];
  269. Quaternion inv_quaternions[XR_HAND_JOINT_COUNT_EXT];
  270. Vector3 positions[XR_HAND_JOINT_COUNT_EXT];
  271. const Quaternion &rig_adjustment = bone_adjustments[skeleton_rig];
  272. const OpenXRHandTrackingExtension::HandTracker *hand_tracker = hand_tracking_ext->get_hand_tracker(OpenXRHandTrackingExtension::HandTrackedHands(hand));
  273. const float ws = XRServer::get_singleton()->get_world_scale();
  274. if (hand_tracker->is_initialized && hand_tracker->locations.isActive) {
  275. for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) {
  276. confidences[i] = XRPose::XR_TRACKING_CONFIDENCE_NONE;
  277. quaternions[i] = Quaternion();
  278. positions[i] = Vector3();
  279. const XrHandJointLocationEXT &location = hand_tracker->joint_locations[i];
  280. const XrPosef &pose = location.pose;
  281. if (location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT) {
  282. if (pose.orientation.x != 0 || pose.orientation.y != 0 || pose.orientation.z != 0 || pose.orientation.w != 0) {
  283. quaternions[i] = Quaternion(pose.orientation.x, pose.orientation.y, pose.orientation.z, pose.orientation.w) * rig_adjustment;
  284. inv_quaternions[i] = quaternions[i].inverse();
  285. if (location.locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT) {
  286. confidences[i] = XRPose::XR_TRACKING_CONFIDENCE_HIGH;
  287. positions[i] = Vector3(pose.position.x * ws, pose.position.y * ws, pose.position.z * ws);
  288. // TODO get inverse of position, we'll do this later. For now we're ignoring bone positions which generally works better anyway
  289. } else {
  290. confidences[i] = XRPose::XR_TRACKING_CONFIDENCE_LOW;
  291. }
  292. }
  293. }
  294. }
  295. if (confidences[XR_HAND_JOINT_PALM_EXT] != XRPose::XR_TRACKING_CONFIDENCE_NONE) {
  296. // Iterate over all the OpenXR joints.
  297. for (int joint = 0; joint < XR_HAND_JOINT_COUNT_EXT; joint++) {
  298. // Get the skeleton bone (skip if none).
  299. const int bone = joints[joint].bone;
  300. if (bone == -1) {
  301. continue;
  302. }
  303. // Calculate the relative relationship to the parent bone joint.
  304. const int parent_joint = joints[joint].parent_joint;
  305. const Quaternion q = inv_quaternions[parent_joint] * quaternions[joint];
  306. const Vector3 p = inv_quaternions[parent_joint].xform(positions[joint] - positions[parent_joint]);
  307. // Update the bone position if enabled by update mode.
  308. if (bone_update == BONE_UPDATE_FULL) {
  309. skeleton->set_bone_pose_position(joints[joint].bone, p);
  310. }
  311. // Always update the bone rotation.
  312. skeleton->set_bone_pose_rotation(joints[joint].bone, q);
  313. }
  314. // Transform the OpenXRHand to the skeleton pose.
  315. Transform3D t;
  316. t.basis = Basis(quaternions[XR_HAND_JOINT_PALM_EXT]);
  317. t.origin = positions[XR_HAND_JOINT_PALM_EXT];
  318. set_transform(t);
  319. // show it
  320. set_visible(true);
  321. } else {
  322. // hide it
  323. set_visible(false);
  324. }
  325. } else {
  326. // hide it
  327. set_visible(false);
  328. }
  329. }
  330. void OpenXRHand::_notification(int p_what) {
  331. switch (p_what) {
  332. case NOTIFICATION_ENTER_TREE: {
  333. _get_joint_data();
  334. set_process_internal(true);
  335. } break;
  336. case NOTIFICATION_EXIT_TREE: {
  337. set_process_internal(false);
  338. // reset
  339. for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) {
  340. joints[i].bone = -1;
  341. joints[i].parent_joint = -1;
  342. }
  343. } break;
  344. case NOTIFICATION_INTERNAL_PROCESS: {
  345. _update_skeleton();
  346. } break;
  347. default: {
  348. } break;
  349. }
  350. }