openxr_hand_tracking_extension.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /**************************************************************************/
  2. /* openxr_hand_tracking_extension.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_tracking_extension.h"
  31. #include "../openxr_api.h"
  32. #include "core/config/project_settings.h"
  33. #include "core/string/print_string.h"
  34. #include "servers/xr_server.h"
  35. #include <openxr/openxr.h>
  36. OpenXRHandTrackingExtension *OpenXRHandTrackingExtension::singleton = nullptr;
  37. OpenXRHandTrackingExtension *OpenXRHandTrackingExtension::get_singleton() {
  38. return singleton;
  39. }
  40. OpenXRHandTrackingExtension::OpenXRHandTrackingExtension() {
  41. singleton = this;
  42. // Make sure this is cleared until we actually request it
  43. handTrackingSystemProperties.supportsHandTracking = false;
  44. }
  45. OpenXRHandTrackingExtension::~OpenXRHandTrackingExtension() {
  46. singleton = nullptr;
  47. }
  48. HashMap<String, bool *> OpenXRHandTrackingExtension::get_requested_extensions() {
  49. HashMap<String, bool *> request_extensions;
  50. unobstructed_data_source = GLOBAL_GET("xr/openxr/extensions/hand_tracking_unobstructed_data_source");
  51. controller_data_source = GLOBAL_GET("xr/openxr/extensions/hand_tracking_controller_data_source");
  52. request_extensions[XR_EXT_HAND_TRACKING_EXTENSION_NAME] = &hand_tracking_ext;
  53. request_extensions[XR_EXT_HAND_JOINTS_MOTION_RANGE_EXTENSION_NAME] = &hand_motion_range_ext;
  54. if (unobstructed_data_source || controller_data_source) {
  55. request_extensions[XR_EXT_HAND_TRACKING_DATA_SOURCE_EXTENSION_NAME] = &hand_tracking_source_ext;
  56. }
  57. return request_extensions;
  58. }
  59. void OpenXRHandTrackingExtension::on_instance_created(const XrInstance p_instance) {
  60. if (hand_tracking_ext) {
  61. EXT_INIT_XR_FUNC(xrCreateHandTrackerEXT);
  62. EXT_INIT_XR_FUNC(xrDestroyHandTrackerEXT);
  63. EXT_INIT_XR_FUNC(xrLocateHandJointsEXT);
  64. hand_tracking_ext = xrCreateHandTrackerEXT_ptr && xrDestroyHandTrackerEXT_ptr && xrLocateHandJointsEXT_ptr;
  65. }
  66. }
  67. void OpenXRHandTrackingExtension::on_session_destroyed() {
  68. cleanup_hand_tracking();
  69. }
  70. void OpenXRHandTrackingExtension::on_instance_destroyed() {
  71. xrCreateHandTrackerEXT_ptr = nullptr;
  72. xrDestroyHandTrackerEXT_ptr = nullptr;
  73. xrLocateHandJointsEXT_ptr = nullptr;
  74. }
  75. void *OpenXRHandTrackingExtension::set_system_properties_and_get_next_pointer(void *p_next_pointer) {
  76. if (!hand_tracking_ext) {
  77. // not supported...
  78. return p_next_pointer;
  79. }
  80. handTrackingSystemProperties = {
  81. XR_TYPE_SYSTEM_HAND_TRACKING_PROPERTIES_EXT, // type
  82. p_next_pointer, // next
  83. false, // supportsHandTracking
  84. };
  85. return &handTrackingSystemProperties;
  86. }
  87. void OpenXRHandTrackingExtension::on_state_ready() {
  88. if (!handTrackingSystemProperties.supportsHandTracking) {
  89. // not supported...
  90. return;
  91. }
  92. // Setup our hands and reset data
  93. for (int i = 0; i < OPENXR_MAX_TRACKED_HANDS; i++) {
  94. // we'll do this later
  95. hand_trackers[i].is_initialized = false;
  96. hand_trackers[i].hand_tracker = XR_NULL_HANDLE;
  97. hand_trackers[i].locations.isActive = false;
  98. for (int j = 0; j < XR_HAND_JOINT_COUNT_EXT; j++) {
  99. hand_trackers[i].joint_locations[j] = { 0, { { 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0 } }, 0.0 };
  100. hand_trackers[i].joint_velocities[j] = { 0, { 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0 } };
  101. }
  102. }
  103. }
  104. void OpenXRHandTrackingExtension::on_process() {
  105. if (!handTrackingSystemProperties.supportsHandTracking) {
  106. // not supported...
  107. return;
  108. }
  109. // process our hands
  110. const XrTime time = OpenXRAPI::get_singleton()->get_predicted_display_time();
  111. if (time == 0) {
  112. // we don't have timing info yet, or we're skipping a frame...
  113. return;
  114. }
  115. XrResult result;
  116. for (int i = 0; i < OPENXR_MAX_TRACKED_HANDS; i++) {
  117. if (hand_trackers[i].hand_tracker == XR_NULL_HANDLE) {
  118. void *next_pointer = nullptr;
  119. // Originally not all XR runtimes supported hand tracking data sourced both from controllers and normal hand tracking.
  120. // With this extension we can indicate we wish to accept input from either or both sources.
  121. // This functionality is subject to the abilities of the XR runtime and requires the data source extension.
  122. // Note: If the data source extension is not available, no guarantees can be made on what the XR runtime supports.
  123. uint32_t data_source_count = 0;
  124. XrHandTrackingDataSourceEXT data_sources[2];
  125. if (unobstructed_data_source) {
  126. data_sources[data_source_count++] = XR_HAND_TRACKING_DATA_SOURCE_UNOBSTRUCTED_EXT;
  127. }
  128. if (controller_data_source) {
  129. data_sources[data_source_count++] = XR_HAND_TRACKING_DATA_SOURCE_CONTROLLER_EXT;
  130. }
  131. XrHandTrackingDataSourceInfoEXT data_source_info = { XR_TYPE_HAND_TRACKING_DATA_SOURCE_INFO_EXT, next_pointer, data_source_count, data_sources };
  132. if (hand_tracking_source_ext) {
  133. // If supported include this info
  134. next_pointer = &data_source_info;
  135. }
  136. XrHandTrackerCreateInfoEXT create_info = {
  137. XR_TYPE_HAND_TRACKER_CREATE_INFO_EXT, // type
  138. next_pointer, // next
  139. i == 0 ? XR_HAND_LEFT_EXT : XR_HAND_RIGHT_EXT, // hand
  140. XR_HAND_JOINT_SET_DEFAULT_EXT, // handJointSet
  141. };
  142. result = xrCreateHandTrackerEXT(OpenXRAPI::get_singleton()->get_session(), &create_info, &hand_trackers[i].hand_tracker);
  143. if (XR_FAILED(result)) {
  144. // not successful? then we do nothing.
  145. print_line("OpenXR: Failed to obtain hand tracking information [", OpenXRAPI::get_singleton()->get_error_string(result), "]");
  146. hand_trackers[i].is_initialized = false;
  147. } else {
  148. next_pointer = nullptr;
  149. hand_trackers[i].velocities.type = XR_TYPE_HAND_JOINT_VELOCITIES_EXT;
  150. hand_trackers[i].velocities.next = next_pointer;
  151. hand_trackers[i].velocities.jointCount = XR_HAND_JOINT_COUNT_EXT;
  152. hand_trackers[i].velocities.jointVelocities = hand_trackers[i].joint_velocities;
  153. next_pointer = &hand_trackers[i].velocities;
  154. if (hand_tracking_source_ext) {
  155. hand_trackers[i].data_source.type = XR_TYPE_HAND_TRACKING_DATA_SOURCE_STATE_EXT;
  156. hand_trackers[i].data_source.next = next_pointer;
  157. hand_trackers[i].data_source.isActive = false;
  158. hand_trackers[i].data_source.dataSource = XrHandTrackingDataSourceEXT(0);
  159. next_pointer = &hand_trackers[i].data_source;
  160. }
  161. // Needed for vendor hand tracking extensions implemented from GDExtension.
  162. for (OpenXRExtensionWrapper *wrapper : OpenXRAPI::get_singleton()->get_registered_extension_wrappers()) {
  163. void *np = wrapper->set_hand_joint_locations_and_get_next_pointer(i, next_pointer);
  164. if (np != nullptr) {
  165. next_pointer = np;
  166. }
  167. }
  168. hand_trackers[i].locations.type = XR_TYPE_HAND_JOINT_LOCATIONS_EXT;
  169. hand_trackers[i].locations.next = next_pointer;
  170. hand_trackers[i].locations.isActive = false;
  171. hand_trackers[i].locations.jointCount = XR_HAND_JOINT_COUNT_EXT;
  172. hand_trackers[i].locations.jointLocations = hand_trackers[i].joint_locations;
  173. Ref<XRHandTracker> godot_tracker;
  174. godot_tracker.instantiate();
  175. godot_tracker->set_tracker_hand(i == 0 ? XRPositionalTracker::TRACKER_HAND_LEFT : XRPositionalTracker::TRACKER_HAND_RIGHT);
  176. godot_tracker->set_tracker_name(i == 0 ? "/user/hand_tracker/left" : "/user/hand_tracker/right");
  177. XRServer::get_singleton()->add_tracker(godot_tracker);
  178. hand_trackers[i].godot_tracker = godot_tracker;
  179. hand_trackers[i].is_initialized = true;
  180. }
  181. }
  182. if (hand_trackers[i].is_initialized) {
  183. Ref<XRHandTracker> godot_tracker = hand_trackers[i].godot_tracker;
  184. void *next_pointer = nullptr;
  185. XrHandJointsMotionRangeInfoEXT motion_range_info = { XR_TYPE_HAND_JOINTS_MOTION_RANGE_INFO_EXT, next_pointer, hand_trackers[i].motion_range };
  186. if (hand_motion_range_ext) {
  187. next_pointer = &motion_range_info;
  188. }
  189. XrHandJointsLocateInfoEXT locateInfo = {
  190. XR_TYPE_HAND_JOINTS_LOCATE_INFO_EXT, // type
  191. next_pointer, // next
  192. OpenXRAPI::get_singleton()->get_play_space(), // baseSpace
  193. time, // time
  194. };
  195. result = xrLocateHandJointsEXT(hand_trackers[i].hand_tracker, &locateInfo, &hand_trackers[i].locations);
  196. if (XR_FAILED(result)) {
  197. // not successful? then we do nothing.
  198. print_line("OpenXR: Failed to get tracking for hand", i, "[", OpenXRAPI::get_singleton()->get_error_string(result), "]");
  199. godot_tracker->set_hand_tracking_source(XRHandTracker::HAND_TRACKING_SOURCE_UNKNOWN);
  200. godot_tracker->set_has_tracking_data(false);
  201. godot_tracker->invalidate_pose("default");
  202. continue;
  203. }
  204. // For some reason an inactive controller isn't coming back as inactive but has coordinates either as NAN or very large
  205. const XrPosef &palm = hand_trackers[i].joint_locations[XR_HAND_JOINT_PALM_EXT].pose;
  206. if (!hand_trackers[i].locations.isActive || isnan(palm.position.x) || palm.position.x < -1000000.00 || palm.position.x > 1000000.00) {
  207. hand_trackers[i].locations.isActive = false; // workaround, make sure its inactive
  208. }
  209. if (hand_trackers[i].locations.isActive) {
  210. // SKELETON_RIG_HUMANOID bone adjustment. This rotation performs:
  211. // OpenXR Z+ -> Godot Humanoid Y- (Back along the bone)
  212. // OpenXR Y+ -> Godot Humanoid Z- (Out the back of the hand)
  213. const Quaternion bone_adjustment(0.0, -Math_SQRT12, Math_SQRT12, 0.0);
  214. for (int joint = 0; joint < XR_HAND_JOINT_COUNT_EXT; joint++) {
  215. const XrHandJointLocationEXT &location = hand_trackers[i].joint_locations[joint];
  216. const XrHandJointVelocityEXT &velocity = hand_trackers[i].joint_velocities[joint];
  217. const XrPosef &pose = location.pose;
  218. Transform3D transform;
  219. Vector3 linear_velocity;
  220. Vector3 angular_velocity;
  221. BitField<XRHandTracker::HandJointFlags> flags;
  222. if (location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_VALID_BIT) {
  223. if (pose.orientation.x != 0 || pose.orientation.y != 0 || pose.orientation.z != 0 || pose.orientation.w != 0) {
  224. flags.set_flag(XRHandTracker::HAND_JOINT_FLAG_ORIENTATION_VALID);
  225. transform.basis = Basis(Quaternion(pose.orientation.x, pose.orientation.y, pose.orientation.z, pose.orientation.w) * bone_adjustment);
  226. }
  227. }
  228. if (location.locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT) {
  229. flags.set_flag(XRHandTracker::HAND_JOINT_FLAG_POSITION_VALID);
  230. transform.origin = Vector3(pose.position.x, pose.position.y, pose.position.z);
  231. }
  232. if (location.locationFlags & XR_SPACE_LOCATION_ORIENTATION_TRACKED_BIT) {
  233. flags.set_flag(XRHandTracker::HAND_JOINT_FLAG_ORIENTATION_TRACKED);
  234. }
  235. if (location.locationFlags & XR_SPACE_LOCATION_POSITION_TRACKED_BIT) {
  236. flags.set_flag(XRHandTracker::HAND_JOINT_FLAG_POSITION_TRACKED);
  237. }
  238. if (location.locationFlags & XR_SPACE_VELOCITY_LINEAR_VALID_BIT) {
  239. flags.set_flag(XRHandTracker::HAND_JOINT_FLAG_LINEAR_VELOCITY_VALID);
  240. linear_velocity = Vector3(velocity.linearVelocity.x, velocity.linearVelocity.y, velocity.linearVelocity.z);
  241. godot_tracker->set_hand_joint_linear_velocity((XRHandTracker::HandJoint)joint, linear_velocity);
  242. }
  243. if (location.locationFlags & XR_SPACE_VELOCITY_ANGULAR_VALID_BIT) {
  244. flags.set_flag(XRHandTracker::HAND_JOINT_FLAG_ANGULAR_VELOCITY_VALID);
  245. angular_velocity = Vector3(velocity.angularVelocity.x, velocity.angularVelocity.y, velocity.angularVelocity.z);
  246. godot_tracker->set_hand_joint_angular_velocity((XRHandTracker::HandJoint)joint, angular_velocity);
  247. }
  248. godot_tracker->set_hand_joint_flags((XRHandTracker::HandJoint)joint, flags);
  249. godot_tracker->set_hand_joint_transform((XRHandTracker::HandJoint)joint, transform);
  250. godot_tracker->set_hand_joint_radius((XRHandTracker::HandJoint)joint, location.radius);
  251. if (joint == XR_HAND_JOINT_PALM_EXT) {
  252. if (location.locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT) {
  253. XrHandTrackingDataSourceStateEXT &data_source = hand_trackers[i].data_source;
  254. XRHandTracker::HandTrackingSource source = XRHandTracker::HAND_TRACKING_SOURCE_UNKNOWN;
  255. if (hand_tracking_source_ext) {
  256. if (!data_source.isActive) {
  257. source = XRHandTracker::HAND_TRACKING_SOURCE_NOT_TRACKED;
  258. } else if (data_source.dataSource == XR_HAND_TRACKING_DATA_SOURCE_UNOBSTRUCTED_EXT) {
  259. source = XRHandTracker::HAND_TRACKING_SOURCE_UNOBSTRUCTED;
  260. } else if (data_source.dataSource == XR_HAND_TRACKING_DATA_SOURCE_CONTROLLER_EXT) {
  261. source = XRHandTracker::HAND_TRACKING_SOURCE_CONTROLLER;
  262. } else {
  263. // Data source shouldn't be active, if new data sources are added to OpenXR we need to enable them.
  264. WARN_PRINT_ONCE("Unknown active data source found!");
  265. source = XRHandTracker::HAND_TRACKING_SOURCE_UNKNOWN;
  266. }
  267. }
  268. godot_tracker->set_hand_tracking_source(source);
  269. godot_tracker->set_has_tracking_data(true);
  270. godot_tracker->set_pose("default", transform, linear_velocity, angular_velocity);
  271. } else {
  272. godot_tracker->set_hand_tracking_source(hand_tracking_source_ext ? XRHandTracker::HAND_TRACKING_SOURCE_NOT_TRACKED : XRHandTracker::HAND_TRACKING_SOURCE_UNKNOWN);
  273. godot_tracker->set_has_tracking_data(false);
  274. godot_tracker->invalidate_pose("default");
  275. }
  276. }
  277. }
  278. } else {
  279. godot_tracker->set_hand_tracking_source(hand_tracking_source_ext ? XRHandTracker::HAND_TRACKING_SOURCE_NOT_TRACKED : XRHandTracker::HAND_TRACKING_SOURCE_UNKNOWN);
  280. godot_tracker->set_has_tracking_data(false);
  281. godot_tracker->invalidate_pose("default");
  282. }
  283. }
  284. }
  285. }
  286. void OpenXRHandTrackingExtension::on_state_stopping() {
  287. // cleanup
  288. cleanup_hand_tracking();
  289. }
  290. void OpenXRHandTrackingExtension::cleanup_hand_tracking() {
  291. XRServer *xr_server = XRServer::get_singleton();
  292. ERR_FAIL_NULL(xr_server);
  293. for (int i = 0; i < OPENXR_MAX_TRACKED_HANDS; i++) {
  294. if (hand_trackers[i].hand_tracker != XR_NULL_HANDLE) {
  295. xrDestroyHandTrackerEXT(hand_trackers[i].hand_tracker);
  296. hand_trackers[i].is_initialized = false;
  297. hand_trackers[i].hand_tracker = XR_NULL_HANDLE;
  298. XRServer::get_singleton()->remove_tracker(hand_trackers[i].godot_tracker);
  299. }
  300. }
  301. }
  302. bool OpenXRHandTrackingExtension::get_active() {
  303. return handTrackingSystemProperties.supportsHandTracking;
  304. }
  305. const OpenXRHandTrackingExtension::HandTracker *OpenXRHandTrackingExtension::get_hand_tracker(HandTrackedHands p_hand) const {
  306. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, nullptr);
  307. return &hand_trackers[p_hand];
  308. }
  309. XrHandJointsMotionRangeEXT OpenXRHandTrackingExtension::get_motion_range(HandTrackedHands p_hand) const {
  310. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, XR_HAND_JOINTS_MOTION_RANGE_MAX_ENUM_EXT);
  311. return hand_trackers[p_hand].motion_range;
  312. }
  313. OpenXRHandTrackingExtension::HandTrackedSource OpenXRHandTrackingExtension::get_hand_tracking_source(HandTrackedHands p_hand) const {
  314. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, OPENXR_SOURCE_UNKNOWN);
  315. if (hand_tracking_source_ext) {
  316. if (!hand_trackers[p_hand].data_source.isActive) {
  317. return OPENXR_SOURCE_NOT_TRACKED;
  318. } else if (hand_trackers[p_hand].data_source.dataSource == XR_HAND_TRACKING_DATA_SOURCE_UNOBSTRUCTED_EXT) {
  319. return OPENXR_SOURCE_UNOBSTRUCTED;
  320. } else if (hand_trackers[p_hand].data_source.dataSource == XR_HAND_TRACKING_DATA_SOURCE_CONTROLLER_EXT) {
  321. return OPENXR_SOURCE_CONTROLLER;
  322. } else {
  323. // Data source shouldn't be active, if new data sources are added to OpenXR we need to enable them.
  324. WARN_PRINT_ONCE("Unknown active data source found!");
  325. return OPENXR_SOURCE_UNKNOWN;
  326. }
  327. }
  328. return OPENXR_SOURCE_UNKNOWN;
  329. }
  330. void OpenXRHandTrackingExtension::set_motion_range(HandTrackedHands p_hand, XrHandJointsMotionRangeEXT p_motion_range) {
  331. ERR_FAIL_UNSIGNED_INDEX(p_hand, OPENXR_MAX_TRACKED_HANDS);
  332. hand_trackers[p_hand].motion_range = p_motion_range;
  333. }
  334. XrSpaceLocationFlags OpenXRHandTrackingExtension::get_hand_joint_location_flags(HandTrackedHands p_hand, XrHandJointEXT p_joint) const {
  335. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, XrSpaceLocationFlags(0));
  336. ERR_FAIL_UNSIGNED_INDEX_V(p_joint, XR_HAND_JOINT_COUNT_EXT, XrSpaceLocationFlags(0));
  337. if (!hand_trackers[p_hand].is_initialized) {
  338. return XrSpaceLocationFlags(0);
  339. }
  340. const XrHandJointLocationEXT &location = hand_trackers[p_hand].joint_locations[p_joint];
  341. return location.locationFlags;
  342. }
  343. Quaternion OpenXRHandTrackingExtension::get_hand_joint_rotation(HandTrackedHands p_hand, XrHandJointEXT p_joint) const {
  344. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, Quaternion());
  345. ERR_FAIL_UNSIGNED_INDEX_V(p_joint, XR_HAND_JOINT_COUNT_EXT, Quaternion());
  346. if (!hand_trackers[p_hand].is_initialized) {
  347. return Quaternion();
  348. }
  349. const XrHandJointLocationEXT &location = hand_trackers[p_hand].joint_locations[p_joint];
  350. return Quaternion(location.pose.orientation.x, location.pose.orientation.y, location.pose.orientation.z, location.pose.orientation.w);
  351. }
  352. Vector3 OpenXRHandTrackingExtension::get_hand_joint_position(HandTrackedHands p_hand, XrHandJointEXT p_joint) const {
  353. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, Vector3());
  354. ERR_FAIL_UNSIGNED_INDEX_V(p_joint, XR_HAND_JOINT_COUNT_EXT, Vector3());
  355. if (!hand_trackers[p_hand].is_initialized) {
  356. return Vector3();
  357. }
  358. const XrHandJointLocationEXT &location = hand_trackers[p_hand].joint_locations[p_joint];
  359. return Vector3(location.pose.position.x, location.pose.position.y, location.pose.position.z);
  360. }
  361. float OpenXRHandTrackingExtension::get_hand_joint_radius(HandTrackedHands p_hand, XrHandJointEXT p_joint) const {
  362. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, 0.0);
  363. ERR_FAIL_UNSIGNED_INDEX_V(p_joint, XR_HAND_JOINT_COUNT_EXT, 0.0);
  364. if (!hand_trackers[p_hand].is_initialized) {
  365. return 0.0;
  366. }
  367. return hand_trackers[p_hand].joint_locations[p_joint].radius;
  368. }
  369. XrSpaceVelocityFlags OpenXRHandTrackingExtension::get_hand_joint_velocity_flags(HandTrackedHands p_hand, XrHandJointEXT p_joint) const {
  370. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, XrSpaceVelocityFlags(0));
  371. ERR_FAIL_UNSIGNED_INDEX_V(p_joint, XR_HAND_JOINT_COUNT_EXT, XrSpaceVelocityFlags(0));
  372. if (!hand_trackers[p_hand].is_initialized) {
  373. return XrSpaceVelocityFlags(0);
  374. }
  375. const XrHandJointVelocityEXT &velocity = hand_trackers[p_hand].joint_velocities[p_joint];
  376. return velocity.velocityFlags;
  377. }
  378. Vector3 OpenXRHandTrackingExtension::get_hand_joint_linear_velocity(HandTrackedHands p_hand, XrHandJointEXT p_joint) const {
  379. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, Vector3());
  380. ERR_FAIL_UNSIGNED_INDEX_V(p_joint, XR_HAND_JOINT_COUNT_EXT, Vector3());
  381. if (!hand_trackers[p_hand].is_initialized) {
  382. return Vector3();
  383. }
  384. const XrHandJointVelocityEXT &velocity = hand_trackers[p_hand].joint_velocities[p_joint];
  385. return Vector3(velocity.linearVelocity.x, velocity.linearVelocity.y, velocity.linearVelocity.z);
  386. }
  387. Vector3 OpenXRHandTrackingExtension::get_hand_joint_angular_velocity(HandTrackedHands p_hand, XrHandJointEXT p_joint) const {
  388. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, Vector3());
  389. ERR_FAIL_UNSIGNED_INDEX_V(p_joint, XR_HAND_JOINT_COUNT_EXT, Vector3());
  390. if (!hand_trackers[p_hand].is_initialized) {
  391. return Vector3();
  392. }
  393. const XrHandJointVelocityEXT &velocity = hand_trackers[p_hand].joint_velocities[p_joint];
  394. return Vector3(velocity.angularVelocity.x, velocity.angularVelocity.y, velocity.angularVelocity.z);
  395. }