openxr_hand_tracking_extension.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. request_extensions[XR_EXT_HAND_TRACKING_EXTENSION_NAME] = &hand_tracking_ext;
  51. request_extensions[XR_EXT_HAND_JOINTS_MOTION_RANGE_EXTENSION_NAME] = &hand_motion_range_ext;
  52. return request_extensions;
  53. }
  54. void OpenXRHandTrackingExtension::on_instance_created(const XrInstance p_instance) {
  55. if (hand_tracking_ext) {
  56. EXT_INIT_XR_FUNC(xrCreateHandTrackerEXT);
  57. EXT_INIT_XR_FUNC(xrDestroyHandTrackerEXT);
  58. EXT_INIT_XR_FUNC(xrLocateHandJointsEXT);
  59. hand_tracking_ext = xrCreateHandTrackerEXT_ptr && xrDestroyHandTrackerEXT_ptr && xrLocateHandJointsEXT_ptr;
  60. }
  61. }
  62. void OpenXRHandTrackingExtension::on_session_destroyed() {
  63. cleanup_hand_tracking();
  64. }
  65. void OpenXRHandTrackingExtension::on_instance_destroyed() {
  66. xrCreateHandTrackerEXT_ptr = nullptr;
  67. xrDestroyHandTrackerEXT_ptr = nullptr;
  68. xrLocateHandJointsEXT_ptr = nullptr;
  69. }
  70. void *OpenXRHandTrackingExtension::set_system_properties_and_get_next_pointer(void *p_next_pointer) {
  71. if (!hand_tracking_ext) {
  72. // not supported...
  73. return p_next_pointer;
  74. }
  75. handTrackingSystemProperties = {
  76. XR_TYPE_SYSTEM_HAND_TRACKING_PROPERTIES_EXT, // type
  77. p_next_pointer, // next
  78. false, // supportsHandTracking
  79. };
  80. return &handTrackingSystemProperties;
  81. }
  82. void OpenXRHandTrackingExtension::on_state_ready() {
  83. if (!handTrackingSystemProperties.supportsHandTracking) {
  84. // not supported...
  85. return;
  86. }
  87. // Setup our hands and reset data
  88. for (int i = 0; i < OPENXR_MAX_TRACKED_HANDS; i++) {
  89. // we'll do this later
  90. hand_trackers[i].is_initialized = false;
  91. hand_trackers[i].hand_tracker = XR_NULL_HANDLE;
  92. hand_trackers[i].locations.isActive = false;
  93. for (int j = 0; j < XR_HAND_JOINT_COUNT_EXT; j++) {
  94. hand_trackers[i].joint_locations[j] = { 0, { { 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0 } }, 0.0 };
  95. hand_trackers[i].joint_velocities[j] = { 0, { 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0 } };
  96. }
  97. }
  98. }
  99. void OpenXRHandTrackingExtension::on_process() {
  100. if (!handTrackingSystemProperties.supportsHandTracking) {
  101. // not supported...
  102. return;
  103. }
  104. // process our hands
  105. const XrTime time = OpenXRAPI::get_singleton()->get_next_frame_time(); // This data will be used for the next frame we render
  106. if (time == 0) {
  107. // we don't have timing info yet, or we're skipping a frame...
  108. return;
  109. }
  110. XrResult result;
  111. for (int i = 0; i < OPENXR_MAX_TRACKED_HANDS; i++) {
  112. if (hand_trackers[i].hand_tracker == XR_NULL_HANDLE) {
  113. XrHandTrackerCreateInfoEXT createInfo = {
  114. XR_TYPE_HAND_TRACKER_CREATE_INFO_EXT, // type
  115. nullptr, // next
  116. i == 0 ? XR_HAND_LEFT_EXT : XR_HAND_RIGHT_EXT, // hand
  117. XR_HAND_JOINT_SET_DEFAULT_EXT, // handJointSet
  118. };
  119. result = xrCreateHandTrackerEXT(OpenXRAPI::get_singleton()->get_session(), &createInfo, &hand_trackers[i].hand_tracker);
  120. if (XR_FAILED(result)) {
  121. // not successful? then we do nothing.
  122. print_line("OpenXR: Failed to obtain hand tracking information [", OpenXRAPI::get_singleton()->get_error_string(result), "]");
  123. hand_trackers[i].is_initialized = false;
  124. } else {
  125. void *next_pointer = nullptr;
  126. hand_trackers[i].velocities.type = XR_TYPE_HAND_JOINT_VELOCITIES_EXT;
  127. hand_trackers[i].velocities.next = next_pointer;
  128. hand_trackers[i].velocities.jointCount = XR_HAND_JOINT_COUNT_EXT;
  129. hand_trackers[i].velocities.jointVelocities = hand_trackers[i].joint_velocities;
  130. next_pointer = &hand_trackers[i].velocities;
  131. hand_trackers[i].locations.type = XR_TYPE_HAND_JOINT_LOCATIONS_EXT;
  132. hand_trackers[i].locations.next = next_pointer;
  133. hand_trackers[i].locations.isActive = false;
  134. hand_trackers[i].locations.jointCount = XR_HAND_JOINT_COUNT_EXT;
  135. hand_trackers[i].locations.jointLocations = hand_trackers[i].joint_locations;
  136. hand_trackers[i].is_initialized = true;
  137. }
  138. }
  139. if (hand_trackers[i].is_initialized) {
  140. void *next_pointer = nullptr;
  141. XrHandJointsMotionRangeInfoEXT motionRangeInfo;
  142. if (hand_motion_range_ext) {
  143. motionRangeInfo.type = XR_TYPE_HAND_JOINTS_MOTION_RANGE_INFO_EXT;
  144. motionRangeInfo.next = next_pointer;
  145. motionRangeInfo.handJointsMotionRange = hand_trackers[i].motion_range;
  146. next_pointer = &motionRangeInfo;
  147. }
  148. XrHandJointsLocateInfoEXT locateInfo = {
  149. XR_TYPE_HAND_JOINTS_LOCATE_INFO_EXT, // type
  150. next_pointer, // next
  151. OpenXRAPI::get_singleton()->get_play_space(), // baseSpace
  152. time, // time
  153. };
  154. result = xrLocateHandJointsEXT(hand_trackers[i].hand_tracker, &locateInfo, &hand_trackers[i].locations);
  155. if (XR_FAILED(result)) {
  156. // not successful? then we do nothing.
  157. print_line("OpenXR: Failed to get tracking for hand", i, "[", OpenXRAPI::get_singleton()->get_error_string(result), "]");
  158. continue;
  159. }
  160. // For some reason an inactive controller isn't coming back as inactive but has coordinates either as NAN or very large
  161. const XrPosef &palm = hand_trackers[i].joint_locations[XR_HAND_JOINT_PALM_EXT].pose;
  162. if (
  163. !hand_trackers[i].locations.isActive || isnan(palm.position.x) || palm.position.x < -1000000.00 || palm.position.x > 1000000.00) {
  164. hand_trackers[i].locations.isActive = false; // workaround, make sure its inactive
  165. }
  166. }
  167. }
  168. }
  169. void OpenXRHandTrackingExtension::on_state_stopping() {
  170. // cleanup
  171. cleanup_hand_tracking();
  172. }
  173. void OpenXRHandTrackingExtension::cleanup_hand_tracking() {
  174. XRServer *xr_server = XRServer::get_singleton();
  175. ERR_FAIL_NULL(xr_server);
  176. for (int i = 0; i < OPENXR_MAX_TRACKED_HANDS; i++) {
  177. if (hand_trackers[i].hand_tracker != XR_NULL_HANDLE) {
  178. xrDestroyHandTrackerEXT(hand_trackers[i].hand_tracker);
  179. hand_trackers[i].is_initialized = false;
  180. hand_trackers[i].hand_tracker = XR_NULL_HANDLE;
  181. }
  182. }
  183. }
  184. bool OpenXRHandTrackingExtension::get_active() {
  185. return handTrackingSystemProperties.supportsHandTracking;
  186. }
  187. const OpenXRHandTrackingExtension::HandTracker *OpenXRHandTrackingExtension::get_hand_tracker(HandTrackedHands p_hand) const {
  188. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, nullptr);
  189. return &hand_trackers[p_hand];
  190. }
  191. XrHandJointsMotionRangeEXT OpenXRHandTrackingExtension::get_motion_range(HandTrackedHands p_hand) const {
  192. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, XR_HAND_JOINTS_MOTION_RANGE_MAX_ENUM_EXT);
  193. return hand_trackers[p_hand].motion_range;
  194. }
  195. void OpenXRHandTrackingExtension::set_motion_range(HandTrackedHands p_hand, XrHandJointsMotionRangeEXT p_motion_range) {
  196. ERR_FAIL_UNSIGNED_INDEX(p_hand, OPENXR_MAX_TRACKED_HANDS);
  197. hand_trackers[p_hand].motion_range = p_motion_range;
  198. }
  199. XrSpaceLocationFlags OpenXRHandTrackingExtension::get_hand_joint_location_flags(HandTrackedHands p_hand, XrHandJointEXT p_joint) const {
  200. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, XrSpaceLocationFlags(0));
  201. ERR_FAIL_UNSIGNED_INDEX_V(p_joint, XR_HAND_JOINT_COUNT_EXT, XrSpaceLocationFlags(0));
  202. if (!hand_trackers[p_hand].is_initialized) {
  203. return XrSpaceLocationFlags(0);
  204. }
  205. const XrHandJointLocationEXT &location = hand_trackers[p_hand].joint_locations[p_joint];
  206. return location.locationFlags;
  207. }
  208. Quaternion OpenXRHandTrackingExtension::get_hand_joint_rotation(HandTrackedHands p_hand, XrHandJointEXT p_joint) const {
  209. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, Quaternion());
  210. ERR_FAIL_UNSIGNED_INDEX_V(p_joint, XR_HAND_JOINT_COUNT_EXT, Quaternion());
  211. if (!hand_trackers[p_hand].is_initialized) {
  212. return Quaternion();
  213. }
  214. const XrHandJointLocationEXT &location = hand_trackers[p_hand].joint_locations[p_joint];
  215. return Quaternion(location.pose.orientation.x, location.pose.orientation.y, location.pose.orientation.z, location.pose.orientation.w);
  216. }
  217. Vector3 OpenXRHandTrackingExtension::get_hand_joint_position(HandTrackedHands p_hand, XrHandJointEXT p_joint) const {
  218. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, Vector3());
  219. ERR_FAIL_UNSIGNED_INDEX_V(p_joint, XR_HAND_JOINT_COUNT_EXT, Vector3());
  220. if (!hand_trackers[p_hand].is_initialized) {
  221. return Vector3();
  222. }
  223. const XrHandJointLocationEXT &location = hand_trackers[p_hand].joint_locations[p_joint];
  224. return Vector3(location.pose.position.x, location.pose.position.y, location.pose.position.z);
  225. }
  226. float OpenXRHandTrackingExtension::get_hand_joint_radius(HandTrackedHands p_hand, XrHandJointEXT p_joint) const {
  227. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, 0.0);
  228. ERR_FAIL_UNSIGNED_INDEX_V(p_joint, XR_HAND_JOINT_COUNT_EXT, 0.0);
  229. if (!hand_trackers[p_hand].is_initialized) {
  230. return 0.0;
  231. }
  232. return hand_trackers[p_hand].joint_locations[p_joint].radius;
  233. }
  234. XrSpaceVelocityFlags OpenXRHandTrackingExtension::get_hand_joint_velocity_flags(HandTrackedHands p_hand, XrHandJointEXT p_joint) const {
  235. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, XrSpaceVelocityFlags(0));
  236. ERR_FAIL_UNSIGNED_INDEX_V(p_joint, XR_HAND_JOINT_COUNT_EXT, XrSpaceVelocityFlags(0));
  237. if (!hand_trackers[p_hand].is_initialized) {
  238. return XrSpaceVelocityFlags(0);
  239. }
  240. const XrHandJointVelocityEXT &velocity = hand_trackers[p_hand].joint_velocities[p_joint];
  241. return velocity.velocityFlags;
  242. }
  243. Vector3 OpenXRHandTrackingExtension::get_hand_joint_linear_velocity(HandTrackedHands p_hand, XrHandJointEXT p_joint) const {
  244. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, Vector3());
  245. ERR_FAIL_UNSIGNED_INDEX_V(p_joint, XR_HAND_JOINT_COUNT_EXT, Vector3());
  246. if (!hand_trackers[p_hand].is_initialized) {
  247. return Vector3();
  248. }
  249. const XrHandJointVelocityEXT &velocity = hand_trackers[p_hand].joint_velocities[p_joint];
  250. return Vector3(velocity.linearVelocity.x, velocity.linearVelocity.y, velocity.linearVelocity.z);
  251. }
  252. Vector3 OpenXRHandTrackingExtension::get_hand_joint_angular_velocity(HandTrackedHands p_hand, XrHandJointEXT p_joint) const {
  253. ERR_FAIL_UNSIGNED_INDEX_V(p_hand, OPENXR_MAX_TRACKED_HANDS, Vector3());
  254. ERR_FAIL_UNSIGNED_INDEX_V(p_joint, XR_HAND_JOINT_COUNT_EXT, Vector3());
  255. if (!hand_trackers[p_hand].is_initialized) {
  256. return Vector3();
  257. }
  258. const XrHandJointVelocityEXT &velocity = hand_trackers[p_hand].joint_velocities[p_joint];
  259. return Vector3(velocity.angularVelocity.x, velocity.angularVelocity.y, velocity.angularVelocity.z);
  260. }