xr_hand_modifier_3d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /**************************************************************************/
  2. /* xr_hand_modifier_3d.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 "xr_hand_modifier_3d.h"
  31. #include "core/config/project_settings.h"
  32. #include "servers/xr/xr_pose.h"
  33. #include "servers/xr_server.h"
  34. void XRHandModifier3D::_bind_methods() {
  35. ClassDB::bind_method(D_METHOD("set_hand_tracker", "tracker_name"), &XRHandModifier3D::set_hand_tracker);
  36. ClassDB::bind_method(D_METHOD("get_hand_tracker"), &XRHandModifier3D::get_hand_tracker);
  37. ClassDB::bind_method(D_METHOD("set_bone_update", "bone_update"), &XRHandModifier3D::set_bone_update);
  38. ClassDB::bind_method(D_METHOD("get_bone_update"), &XRHandModifier3D::get_bone_update);
  39. ADD_PROPERTY(PropertyInfo(Variant::STRING, "hand_tracker", PROPERTY_HINT_ENUM_SUGGESTION, "/user/hand_tracker/left,/user/hand_tracker/right"), "set_hand_tracker", "get_hand_tracker");
  40. ADD_PROPERTY(PropertyInfo(Variant::INT, "bone_update", PROPERTY_HINT_ENUM, "Full,Rotation Only"), "set_bone_update", "get_bone_update");
  41. BIND_ENUM_CONSTANT(BONE_UPDATE_FULL);
  42. BIND_ENUM_CONSTANT(BONE_UPDATE_ROTATION_ONLY);
  43. BIND_ENUM_CONSTANT(BONE_UPDATE_MAX);
  44. }
  45. void XRHandModifier3D::set_hand_tracker(const StringName &p_tracker_name) {
  46. tracker_name = p_tracker_name;
  47. }
  48. StringName XRHandModifier3D::get_hand_tracker() const {
  49. return tracker_name;
  50. }
  51. void XRHandModifier3D::set_bone_update(BoneUpdate p_bone_update) {
  52. ERR_FAIL_INDEX(p_bone_update, BONE_UPDATE_MAX);
  53. bone_update = p_bone_update;
  54. }
  55. XRHandModifier3D::BoneUpdate XRHandModifier3D::get_bone_update() const {
  56. return bone_update;
  57. }
  58. void XRHandModifier3D::_get_joint_data() {
  59. if (!is_inside_tree()) {
  60. return;
  61. }
  62. if (has_stored_previous_transforms) {
  63. previous_relative_transforms.clear();
  64. has_stored_previous_transforms = false;
  65. }
  66. // Table of bone names for different rig types.
  67. static const String bone_names[XRHandTracker::HAND_JOINT_MAX] = {
  68. "Palm",
  69. "Hand",
  70. "ThumbMetacarpal",
  71. "ThumbProximal",
  72. "ThumbDistal",
  73. "ThumbTip",
  74. "IndexMetacarpal",
  75. "IndexProximal",
  76. "IndexIntermediate",
  77. "IndexDistal",
  78. "IndexTip",
  79. "MiddleMetacarpal",
  80. "MiddleProximal",
  81. "MiddleIntermediate",
  82. "MiddleDistal",
  83. "MiddleTip",
  84. "RingMetacarpal",
  85. "RingProximal",
  86. "RingIntermediate",
  87. "RingDistal",
  88. "RingTip",
  89. "LittleMetacarpal",
  90. "LittleProximal",
  91. "LittleIntermediate",
  92. "LittleDistal",
  93. "LittleTip",
  94. };
  95. static const String bone_name_format[2] = {
  96. "Left<bone>",
  97. "Right<bone>",
  98. };
  99. // reset JIC
  100. for (int i = 0; i < XRHandTracker::HAND_JOINT_MAX; i++) {
  101. joints[i].bone = -1;
  102. joints[i].parent_joint = -1;
  103. }
  104. const Skeleton3D *skeleton = get_skeleton();
  105. if (!skeleton) {
  106. return;
  107. }
  108. const XRServer *xr_server = XRServer::get_singleton();
  109. if (!xr_server) {
  110. return;
  111. }
  112. const Ref<XRHandTracker> tracker = xr_server->get_tracker(tracker_name);
  113. if (tracker.is_null()) {
  114. return;
  115. }
  116. // Verify we have a left or right hand tracker.
  117. const XRPositionalTracker::TrackerHand tracker_hand = tracker->get_tracker_hand();
  118. if (tracker_hand != XRPositionalTracker::TRACKER_HAND_LEFT &&
  119. tracker_hand != XRPositionalTracker::TRACKER_HAND_RIGHT) {
  120. return;
  121. }
  122. // Get the hand index (0 = left, 1 = right).
  123. const int hand = tracker_hand == XRPositionalTracker::TRACKER_HAND_LEFT ? 0 : 1;
  124. // Find the skeleton-bones associated with each joint.
  125. int bones[XRHandTracker::HAND_JOINT_MAX];
  126. for (int i = 0; i < XRHandTracker::HAND_JOINT_MAX; i++) {
  127. // Construct the expected bone name.
  128. String bone_name = bone_name_format[hand].replace("<bone>", bone_names[i]);
  129. // Find the skeleton bone.
  130. bones[i] = skeleton->find_bone(bone_name);
  131. if (bones[i] == -1) {
  132. WARN_PRINT(vformat("Couldn't obtain bone for %s", bone_name));
  133. }
  134. }
  135. // Assemble the joint relationship to the available skeleton bones.
  136. for (int i = 0; i < XRHandTracker::HAND_JOINT_MAX; i++) {
  137. // Get the skeleton bone (skip if not found).
  138. const int bone = bones[i];
  139. if (bone == -1) {
  140. continue;
  141. }
  142. // Find the parent skeleton-bone.
  143. const int parent_bone = skeleton->get_bone_parent(bone);
  144. if (parent_bone == -1) {
  145. // If no parent skeleton-bone exists then drive this relative to palm joint.
  146. joints[i].bone = bone;
  147. joints[i].parent_joint = XRHandTracker::HAND_JOINT_PALM;
  148. continue;
  149. }
  150. // Find the joint associated with the parent skeleton-bone.
  151. for (int j = 0; j < XRHandTracker::HAND_JOINT_MAX; ++j) {
  152. if (bones[j] == parent_bone) {
  153. // If a parent joint is found then drive this bone relative to it.
  154. joints[i].bone = bone;
  155. joints[i].parent_joint = j;
  156. break;
  157. }
  158. }
  159. }
  160. }
  161. void XRHandModifier3D::_process_modification() {
  162. Skeleton3D *skeleton = get_skeleton();
  163. if (!skeleton) {
  164. return;
  165. }
  166. const XRServer *xr_server = XRServer::get_singleton();
  167. if (!xr_server) {
  168. return;
  169. }
  170. const Ref<XRHandTracker> tracker = xr_server->get_tracker(tracker_name);
  171. if (tracker.is_null()) {
  172. return;
  173. }
  174. // Skip if no tracking data
  175. if (!tracker->get_has_tracking_data()) {
  176. if (!has_stored_previous_transforms) {
  177. return;
  178. }
  179. // Apply previous relative transforms if they are stored.
  180. for (int joint = 0; joint < XRHandTracker::HAND_JOINT_MAX; joint++) {
  181. const int bone = joints[joint].bone;
  182. if (bone == -1) {
  183. continue;
  184. }
  185. if (bone_update == BONE_UPDATE_FULL) {
  186. skeleton->set_bone_pose_position(joints[joint].bone, previous_relative_transforms[joint].origin);
  187. }
  188. skeleton->set_bone_pose_rotation(joints[joint].bone, Quaternion(previous_relative_transforms[joint].basis));
  189. }
  190. return;
  191. }
  192. // Get the world and skeleton scale.
  193. const float ss = skeleton->get_motion_scale();
  194. // We cache our transforms so we can quickly calculate local transforms.
  195. bool has_valid_data[XRHandTracker::HAND_JOINT_MAX];
  196. Transform3D transforms[XRHandTracker::HAND_JOINT_MAX];
  197. Transform3D inv_transforms[XRHandTracker::HAND_JOINT_MAX];
  198. for (int joint = 0; joint < XRHandTracker::HAND_JOINT_MAX; joint++) {
  199. BitField<XRHandTracker::HandJointFlags> flags = tracker->get_hand_joint_flags((XRHandTracker::HandJoint)joint);
  200. has_valid_data[joint] = flags.has_flag(XRHandTracker::HAND_JOINT_FLAG_ORIENTATION_VALID);
  201. if (has_valid_data[joint]) {
  202. transforms[joint] = tracker->get_hand_joint_transform((XRHandTracker::HandJoint)joint);
  203. transforms[joint].origin *= ss;
  204. inv_transforms[joint] = transforms[joint].inverse();
  205. }
  206. }
  207. // Skip if palm has no tracking data
  208. if (!has_valid_data[XRHandTracker::HAND_JOINT_PALM]) {
  209. return;
  210. }
  211. if (!has_stored_previous_transforms) {
  212. previous_relative_transforms.resize(XRHandTracker::HAND_JOINT_MAX);
  213. has_stored_previous_transforms = true;
  214. }
  215. Transform3D *previous_relative_transforms_ptr = previous_relative_transforms.ptrw();
  216. for (int joint = 0; joint < XRHandTracker::HAND_JOINT_MAX; joint++) {
  217. // Get the skeleton bone (skip if none).
  218. const int bone = joints[joint].bone;
  219. if (bone == -1) {
  220. continue;
  221. }
  222. // Calculate the relative relationship to the parent bone joint.
  223. const int parent_joint = joints[joint].parent_joint;
  224. const Transform3D relative_transform = inv_transforms[parent_joint] * transforms[joint];
  225. previous_relative_transforms_ptr[joint] = relative_transform;
  226. // Update the bone position if enabled by update mode.
  227. if (bone_update == BONE_UPDATE_FULL) {
  228. skeleton->set_bone_pose_position(joints[joint].bone, relative_transform.origin);
  229. }
  230. // Always update the bone rotation.
  231. skeleton->set_bone_pose_rotation(joints[joint].bone, Quaternion(relative_transform.basis));
  232. }
  233. }
  234. void XRHandModifier3D::_tracker_changed(StringName p_tracker_name, XRServer::TrackerType p_tracker_type) {
  235. if (tracker_name == p_tracker_name) {
  236. _get_joint_data();
  237. }
  238. }
  239. void XRHandModifier3D::_skeleton_changed(Skeleton3D *p_old, Skeleton3D *p_new) {
  240. _get_joint_data();
  241. }
  242. PackedStringArray XRHandModifier3D::get_configuration_warnings() const {
  243. PackedStringArray warnings = SkeletonModifier3D::get_configuration_warnings();
  244. // Detect OpenXR without the Hand Tracking extension.
  245. if (GLOBAL_GET("xr/openxr/enabled") && !GLOBAL_GET("xr/openxr/extensions/hand_tracking")) {
  246. warnings.push_back("XRHandModifier3D requires the OpenXR Hand Tracking extension to be enabled.");
  247. }
  248. return warnings;
  249. }
  250. void XRHandModifier3D::_notification(int p_what) {
  251. switch (p_what) {
  252. case NOTIFICATION_ENTER_TREE: {
  253. XRServer *xr_server = XRServer::get_singleton();
  254. if (xr_server) {
  255. xr_server->connect("tracker_added", callable_mp(this, &XRHandModifier3D::_tracker_changed));
  256. xr_server->connect("tracker_updated", callable_mp(this, &XRHandModifier3D::_tracker_changed));
  257. xr_server->connect("tracker_removed", callable_mp(this, &XRHandModifier3D::_tracker_changed));
  258. }
  259. _get_joint_data();
  260. } break;
  261. case NOTIFICATION_EXIT_TREE: {
  262. XRServer *xr_server = XRServer::get_singleton();
  263. if (xr_server) {
  264. xr_server->disconnect("tracker_added", callable_mp(this, &XRHandModifier3D::_tracker_changed));
  265. xr_server->disconnect("tracker_updated", callable_mp(this, &XRHandModifier3D::_tracker_changed));
  266. xr_server->disconnect("tracker_removed", callable_mp(this, &XRHandModifier3D::_tracker_changed));
  267. }
  268. for (int i = 0; i < XRHandTracker::HAND_JOINT_MAX; i++) {
  269. joints[i].bone = -1;
  270. joints[i].parent_joint = -1;
  271. }
  272. } break;
  273. default: {
  274. } break;
  275. }
  276. }