openxr_hand.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. ADD_PROPERTY(PropertyInfo(Variant::INT, "hand", PROPERTY_HINT_ENUM, "Left,Right"), "set_hand", "get_hand");
  43. ADD_PROPERTY(PropertyInfo(Variant::INT, "motion_range", PROPERTY_HINT_ENUM, "Unobstructed,Conform to controller"), "set_motion_range", "get_motion_range");
  44. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "hand_skeleton", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Skeleton3D"), "set_hand_skeleton", "get_hand_skeleton");
  45. BIND_ENUM_CONSTANT(HAND_LEFT);
  46. BIND_ENUM_CONSTANT(HAND_RIGHT);
  47. BIND_ENUM_CONSTANT(HAND_MAX);
  48. BIND_ENUM_CONSTANT(MOTION_RANGE_UNOBSTRUCTED);
  49. BIND_ENUM_CONSTANT(MOTION_RANGE_CONFORM_TO_CONTROLLER);
  50. BIND_ENUM_CONSTANT(MOTION_RANGE_MAX);
  51. }
  52. OpenXRHand::OpenXRHand() {
  53. openxr_api = OpenXRAPI::get_singleton();
  54. hand_tracking_ext = OpenXRHandTrackingExtension::get_singleton();
  55. }
  56. void OpenXRHand::set_hand(const Hands p_hand) {
  57. ERR_FAIL_INDEX(p_hand, HAND_MAX);
  58. hand = p_hand;
  59. }
  60. OpenXRHand::Hands OpenXRHand::get_hand() const {
  61. return hand;
  62. }
  63. void OpenXRHand::set_hand_skeleton(const NodePath &p_hand_skeleton) {
  64. hand_skeleton = p_hand_skeleton;
  65. // TODO if inside tree call _get_bones()
  66. }
  67. void OpenXRHand::set_motion_range(const MotionRange p_motion_range) {
  68. ERR_FAIL_INDEX(p_motion_range, MOTION_RANGE_MAX);
  69. motion_range = p_motion_range;
  70. _set_motion_range();
  71. }
  72. OpenXRHand::MotionRange OpenXRHand::get_motion_range() const {
  73. return motion_range;
  74. }
  75. NodePath OpenXRHand::get_hand_skeleton() const {
  76. return hand_skeleton;
  77. }
  78. void OpenXRHand::_set_motion_range() {
  79. if (!hand_tracking_ext) {
  80. return;
  81. }
  82. XrHandJointsMotionRangeEXT xr_motion_range;
  83. switch (motion_range) {
  84. case MOTION_RANGE_UNOBSTRUCTED:
  85. xr_motion_range = XR_HAND_JOINTS_MOTION_RANGE_UNOBSTRUCTED_EXT;
  86. break;
  87. case MOTION_RANGE_CONFORM_TO_CONTROLLER:
  88. xr_motion_range = XR_HAND_JOINTS_MOTION_RANGE_CONFORMING_TO_CONTROLLER_EXT;
  89. break;
  90. default:
  91. xr_motion_range = XR_HAND_JOINTS_MOTION_RANGE_CONFORMING_TO_CONTROLLER_EXT;
  92. break;
  93. }
  94. hand_tracking_ext->set_motion_range(OpenXRHandTrackingExtension::HandTrackedHands(hand), xr_motion_range);
  95. }
  96. Skeleton3D *OpenXRHand::get_skeleton() {
  97. if (!has_node(hand_skeleton)) {
  98. return nullptr;
  99. }
  100. Node *node = get_node(hand_skeleton);
  101. if (!node) {
  102. return nullptr;
  103. }
  104. Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(node);
  105. return skeleton;
  106. }
  107. void OpenXRHand::_get_bones() {
  108. const char *bone_names[XR_HAND_JOINT_COUNT_EXT] = {
  109. "Palm",
  110. "Wrist",
  111. "Thumb_Metacarpal",
  112. "Thumb_Proximal",
  113. "Thumb_Distal",
  114. "Thumb_Tip",
  115. "Index_Metacarpal",
  116. "Index_Proximal",
  117. "Index_Intermediate",
  118. "Index_Distal",
  119. "Index_Tip",
  120. "Middle_Metacarpal",
  121. "Middle_Proximal",
  122. "Middle_Intermediate",
  123. "Middle_Distal",
  124. "Middle_Tip",
  125. "Ring_Metacarpal",
  126. "Ring_Proximal",
  127. "Ring_Intermediate",
  128. "Ring_Distal",
  129. "Ring_Tip",
  130. "Little_Metacarpal",
  131. "Little_Proximal",
  132. "Little_Intermediate",
  133. "Little_Distal",
  134. "Little_Tip",
  135. };
  136. // reset JIC
  137. for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) {
  138. bones[i] = -1;
  139. }
  140. Skeleton3D *skeleton = get_skeleton();
  141. if (!skeleton) {
  142. return;
  143. }
  144. // We cast to spatials which should allow us to use any subclass of that.
  145. for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) {
  146. String bone_name = bone_names[i];
  147. if (hand == 0) {
  148. bone_name += String("_L");
  149. } else {
  150. bone_name += String("_R");
  151. }
  152. bones[i] = skeleton->find_bone(bone_name);
  153. if (bones[i] == -1) {
  154. print_line("Couldn't obtain bone for", bone_name);
  155. }
  156. }
  157. }
  158. void OpenXRHand::_update_skeleton() {
  159. if (openxr_api == nullptr || !openxr_api->is_initialized()) {
  160. return;
  161. } else if (hand_tracking_ext == nullptr || !hand_tracking_ext->get_active()) {
  162. return;
  163. }
  164. Skeleton3D *skeleton = get_skeleton();
  165. if (!skeleton) {
  166. return;
  167. }
  168. // we cache our transforms so we can quickly calculate local transforms
  169. XRPose::TrackingConfidence confidences[XR_HAND_JOINT_COUNT_EXT];
  170. Quaternion quaternions[XR_HAND_JOINT_COUNT_EXT];
  171. Quaternion inv_quaternions[XR_HAND_JOINT_COUNT_EXT];
  172. Vector3 positions[XR_HAND_JOINT_COUNT_EXT];
  173. const OpenXRHandTrackingExtension::HandTracker *hand_tracker = hand_tracking_ext->get_hand_tracker(OpenXRHandTrackingExtension::HandTrackedHands(hand));
  174. const float ws = XRServer::get_singleton()->get_world_scale();
  175. if (hand_tracker->is_initialized && hand_tracker->locations.isActive) {
  176. for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) {
  177. confidences[i] = XRPose::XR_TRACKING_CONFIDENCE_NONE;
  178. quaternions[i] = Quaternion();
  179. positions[i] = Vector3();
  180. const XrHandJointLocationEXT &location = hand_tracker->joint_locations[i];
  181. const XrPosef &pose = location.pose;
  182. if (location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT) {
  183. if (pose.orientation.x != 0 || pose.orientation.y != 0 || pose.orientation.z != 0 || pose.orientation.w != 0) {
  184. quaternions[i] = Quaternion(pose.orientation.x, pose.orientation.y, pose.orientation.z, pose.orientation.w);
  185. inv_quaternions[i] = quaternions[i].inverse();
  186. if (location.locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT) {
  187. confidences[i] = XRPose::XR_TRACKING_CONFIDENCE_HIGH;
  188. positions[i] = Vector3(pose.position.x * ws, pose.position.y * ws, pose.position.z * ws);
  189. // TODO get inverse of position, we'll do this later. For now we're ignoring bone positions which generally works better anyway
  190. } else {
  191. confidences[i] = XRPose::XR_TRACKING_CONFIDENCE_LOW;
  192. }
  193. }
  194. }
  195. }
  196. if (confidences[XR_HAND_JOINT_PALM_EXT] != XRPose::XR_TRACKING_CONFIDENCE_NONE) {
  197. // now update our skeleton
  198. for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) {
  199. if (bones[i] != -1) {
  200. int bone = bones[i];
  201. int parent = skeleton->get_bone_parent(bone);
  202. // Get our target quaternion
  203. Quaternion q = quaternions[i];
  204. // Get our target position
  205. Vector3 p = positions[i];
  206. // get local translation, parent should already be processed
  207. if (parent == -1) {
  208. // use our palm location here, that is what we are tracking
  209. q = inv_quaternions[XR_HAND_JOINT_PALM_EXT] * q;
  210. p = inv_quaternions[XR_HAND_JOINT_PALM_EXT].xform(p - positions[XR_HAND_JOINT_PALM_EXT]);
  211. } else {
  212. int found = false;
  213. for (int b = 0; b < XR_HAND_JOINT_COUNT_EXT && !found; b++) {
  214. if (bones[b] == parent) {
  215. q = inv_quaternions[b] * q;
  216. p = inv_quaternions[b].xform(p - positions[b]);
  217. found = true;
  218. }
  219. }
  220. }
  221. // and set our pose
  222. skeleton->set_bone_pose_position(bones[i], p);
  223. skeleton->set_bone_pose_rotation(bones[i], q);
  224. }
  225. }
  226. Transform3D t;
  227. t.basis = Basis(quaternions[XR_HAND_JOINT_PALM_EXT]);
  228. t.origin = positions[XR_HAND_JOINT_PALM_EXT];
  229. set_transform(t);
  230. // show it
  231. set_visible(true);
  232. } else {
  233. // hide it
  234. set_visible(false);
  235. }
  236. } else {
  237. // hide it
  238. set_visible(false);
  239. }
  240. }
  241. void OpenXRHand::_notification(int p_what) {
  242. switch (p_what) {
  243. case NOTIFICATION_ENTER_TREE: {
  244. _get_bones();
  245. set_process_internal(true);
  246. } break;
  247. case NOTIFICATION_EXIT_TREE: {
  248. set_process_internal(false);
  249. // reset
  250. for (int i = 0; i < XR_HAND_JOINT_COUNT_EXT; i++) {
  251. bones[i] = -1;
  252. }
  253. } break;
  254. case NOTIFICATION_INTERNAL_PROCESS: {
  255. _update_skeleton();
  256. } break;
  257. default: {
  258. } break;
  259. }
  260. }