xr_body_modifier_3d.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /**************************************************************************/
  2. /* xr_body_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_body_modifier_3d.h"
  31. #include "scene/3d/skeleton_3d.h"
  32. #include "servers/xr/xr_pose.h"
  33. #include "servers/xr_server.h"
  34. void XRBodyModifier3D::_bind_methods() {
  35. ClassDB::bind_method(D_METHOD("set_body_tracker", "tracker_name"), &XRBodyModifier3D::set_body_tracker);
  36. ClassDB::bind_method(D_METHOD("get_body_tracker"), &XRBodyModifier3D::get_body_tracker);
  37. ClassDB::bind_method(D_METHOD("set_body_update", "body_update"), &XRBodyModifier3D::set_body_update);
  38. ClassDB::bind_method(D_METHOD("get_body_update"), &XRBodyModifier3D::get_body_update);
  39. ClassDB::bind_method(D_METHOD("set_bone_update", "bone_update"), &XRBodyModifier3D::set_bone_update);
  40. ClassDB::bind_method(D_METHOD("get_bone_update"), &XRBodyModifier3D::get_bone_update);
  41. ADD_PROPERTY(PropertyInfo(Variant::STRING, "body_tracker", PROPERTY_HINT_ENUM_SUGGESTION, "/user/body_tracker"), "set_body_tracker", "get_body_tracker");
  42. ADD_PROPERTY(PropertyInfo(Variant::INT, "body_update", PROPERTY_HINT_FLAGS, "Upper Body,Lower Body,Hands"), "set_body_update", "get_body_update");
  43. ADD_PROPERTY(PropertyInfo(Variant::INT, "bone_update", PROPERTY_HINT_ENUM, "Full,Rotation Only"), "set_bone_update", "get_bone_update");
  44. BIND_BITFIELD_FLAG(BODY_UPDATE_UPPER_BODY);
  45. BIND_BITFIELD_FLAG(BODY_UPDATE_LOWER_BODY);
  46. BIND_BITFIELD_FLAG(BODY_UPDATE_HANDS);
  47. BIND_ENUM_CONSTANT(BONE_UPDATE_FULL);
  48. BIND_ENUM_CONSTANT(BONE_UPDATE_ROTATION_ONLY);
  49. BIND_ENUM_CONSTANT(BONE_UPDATE_MAX);
  50. }
  51. void XRBodyModifier3D::set_body_tracker(const StringName &p_tracker_name) {
  52. tracker_name = p_tracker_name;
  53. }
  54. StringName XRBodyModifier3D::get_body_tracker() const {
  55. return tracker_name;
  56. }
  57. void XRBodyModifier3D::set_body_update(BitField<BodyUpdate> p_body_update) {
  58. body_update = p_body_update;
  59. }
  60. BitField<XRBodyModifier3D::BodyUpdate> XRBodyModifier3D::get_body_update() const {
  61. return body_update;
  62. }
  63. void XRBodyModifier3D::set_bone_update(BoneUpdate p_bone_update) {
  64. ERR_FAIL_INDEX(p_bone_update, BONE_UPDATE_MAX);
  65. bone_update = p_bone_update;
  66. }
  67. XRBodyModifier3D::BoneUpdate XRBodyModifier3D::get_bone_update() const {
  68. return bone_update;
  69. }
  70. void XRBodyModifier3D::_get_joint_data() {
  71. // Table of Godot Humanoid bone names.
  72. static const String bone_names[XRBodyTracker::JOINT_MAX] = {
  73. "Root", // XRBodyTracker::JOINT_ROOT
  74. // Upper Body Joints.
  75. "Hips", // XRBodyTracker::JOINT_HIPS
  76. "Spine", // XRBodyTracker::JOINT_SPINE
  77. "Chest", // XRBodyTracker::JOINT_CHEST
  78. "UpperChest", // XRBodyTracker::JOINT_UPPER_CHEST
  79. "Neck", // XRBodyTracker::JOINT_NECK"
  80. "Head", // XRBodyTracker::JOINT_HEAD"
  81. "HeadTip", // XRBodyTracker::JOINT_HEAD_TIP"
  82. "LeftShoulder", // XRBodyTracker::JOINT_LEFT_SHOULDER"
  83. "LeftUpperArm", // XRBodyTracker::JOINT_LEFT_UPPER_ARM"
  84. "LeftLowerArm", // XRBodyTracker::JOINT_LEFT_LOWER_ARM"
  85. "RightShoulder", // XRBodyTracker::JOINT_RIGHT_SHOULDER"
  86. "RightUpperArm", // XRBodyTracker::JOINT_RIGHT_UPPER_ARM"
  87. "RightLowerArm", // XRBodyTracker::JOINT_RIGHT_LOWER_ARM"
  88. // Lower Body Joints.
  89. "LeftUpperLeg", // XRBodyTracker::JOINT_LEFT_UPPER_LEG
  90. "LeftLowerLeg", // XRBodyTracker::JOINT_LEFT_LOWER_LEG
  91. "LeftFoot", // XRBodyTracker::JOINT_LEFT_FOOT
  92. "LeftToes", // XRBodyTracker::JOINT_LEFT_TOES
  93. "RightUpperLeg", // XRBodyTracker::JOINT_RIGHT_UPPER_LEG
  94. "RightLowerLeg", // XRBodyTracker::JOINT_RIGHT_LOWER_LEG
  95. "RightFoot", // XRBodyTracker::JOINT_RIGHT_FOOT
  96. "RightToes", // XRBodyTracker::JOINT_RIGHT_TOES
  97. // Left Hand Joints.
  98. "LeftHand", // XRBodyTracker::JOINT_LEFT_HAND
  99. "LeftPalm", // XRBodyTracker::JOINT_LEFT_PALM
  100. "LeftWrist", // XRBodyTracker::JOINT_LEFT_WRIST
  101. "LeftThumbMetacarpal", // XRBodyTracker::JOINT_LEFT_THUMB_METACARPAL
  102. "LeftThumbProximal", // XRBodyTracker::JOINT_LEFT_THUMB_PHALANX_PROXIMAL
  103. "LeftThumbDistal", // XRBodyTracker::JOINT_LEFT_THUMB_PHALANX_DISTAL
  104. "LeftThumbTip", // XRBodyTracker::JOINT_LEFT_THUMB_TIP
  105. "LeftIndexMetacarpal", // XRBodyTracker::JOINT_LEFT_INDEX_FINGER_METACARPAL
  106. "LeftIndexProximal", // XRBodyTracker::JOINT_LEFT_INDEX_FINGER_PHALANX_PROXIMAL
  107. "LeftIndexIntermediate", // XRBodyTracker::JOINT_LEFT_INDEX_FINGER_PHALANX_INTERMEDIATE
  108. "LeftIndexDistal", // XRBodyTracker::JOINT_LEFT_INDEX_FINGER_PHALANX_DISTAL
  109. "LeftIndexTip", // XRBodyTracker::JOINT_LEFT_INDEX_FINGER_TIP
  110. "LeftMiddleMetacarpal", // XRBodyTracker::JOINT_LEFT_MIDDLE_FINGER_METACARPAL
  111. "LeftMiddleProximal", // XRBodyTracker::JOINT_LEFT_MIDDLE_FINGER_PHALANX_PROXIMAL
  112. "LeftMiddleIntermediate", // XRBodyTracker::JOINT_LEFT_MIDDLE_FINGER_PHALANX_INTERMEDIATE
  113. "LeftMiddleDistal", // XRBodyTracker::JOINT_LEFT_MIDDLE_FINGER_PHALANX_DISTAL
  114. "LeftMiddleTip", // XRBodyTracker::JOINT_LEFT_MIDDLE_FINGER_TIP
  115. "LeftRingMetacarpal", // XRBodyTracker::JOINT_LEFT_RING_FINGER_METACARPAL
  116. "LeftRingProximal", // XRBodyTracker::JOINT_LEFT_RING_FINGER_PHALANX_PROXIMAL
  117. "LeftRingIntermediate", // XRBodyTracker::JOINT_LEFT_RING_FINGER_PHALANX_INTERMEDIATE
  118. "LeftRingDistal", // XRBodyTracker::JOINT_LEFT_RING_FINGER_PHALANX_DISTAL
  119. "LeftRingTip", // XRBodyTracker::JOINT_LEFT_RING_FINGER_TIP
  120. "LeftLittleMetacarpal", // XRBodyTracker::JOINT_LEFT_PINKY_FINGER_METACARPAL
  121. "LeftLittleProximal", // XRBodyTracker::JOINT_LEFT_PINKY_FINGER_PHALANX_PROXIMAL
  122. "LeftLittleIntermediate", // XRBodyTracker::JOINT_LEFT_PINKY_FINGER_PHALANX_INTERMEDIATE
  123. "LeftLittleDistal", // XRBodyTracker::JOINT_LEFT_PINKY_FINGER_PHALANX_DISTAL
  124. "LeftLittleTip", // XRBodyTracker::JOINT_LEFT_PINKY_FINGER_TIP
  125. // Right Hand Joints.
  126. "RightHand", // XRBodyTracker::JOINT_RIGHT_HAND
  127. "RightPalm", // XRBodyTracker::JOINT_RIGHT_PALM
  128. "RightWrist", // XRBodyTracker::JOINT_RIGHT_WRIST
  129. "RightThumbMetacarpal", // XRBodyTracker::JOINT_RIGHT_THUMB_METACARPAL
  130. "RightThumbProximal", // XRBodyTracker::JOINT_RIGHT_THUMB_PHALANX_PROXIMAL
  131. "RightThumbDistal", // XRBodyTracker::JOINT_RIGHT_THUMB_PHALANX_DISTAL
  132. "RightThumbTip", // XRBodyTracker::JOINT_RIGHT_THUMB_TIP
  133. "RightIndexMetacarpal", // XRBodyTracker::JOINT_RIGHT_INDEX_FINGER_METACARPAL
  134. "RightIndexProximal", // XRBodyTracker::JOINT_RIGHT_INDEX_FINGER_PHALANX_PROXIMAL
  135. "RightIndexIntermediate", // XRBodyTracker::JOINT_RIGHT_INDEX_FINGER_PHALANX_INTERMEDIATE
  136. "RightIndexDistal", // XRBodyTracker::JOINT_RIGHT_INDEX_FINGER_PHALANX_DISTAL
  137. "RightIndexTip", // XRBodyTracker::JOINT_RIGHT_INDEX_FINGER_TIP
  138. "RightMiddleMetacarpal", // XRBodyTracker::JOINT_RIGHT_MIDDLE_FINGER_METACARPAL
  139. "RightMiddleProximal", // XRBodyTracker::JOINT_RIGHT_MIDDLE_FINGER_PHALANX_PROXIMAL
  140. "RightMiddleIntermediate", // XRBodyTracker::JOINT_RIGHT_MIDDLE_FINGER_PHALANX_INTERMEDIATE
  141. "RightMiddleDistal", // XRBodyTracker::JOINT_RIGHT_MIDDLE_FINGER_PHALANX_DISTAL
  142. "RightMiddleTip", // XRBodyTracker::JOINT_RIGHT_MIDDLE_FINGER_TIP
  143. "RightRingMetacarpal", // XRBodyTracker::JOINT_RIGHT_RING_FINGER_METACARPAL
  144. "RightRingProximal", // XRBodyTracker::JOINT_RIGHT_RING_FINGER_PHALANX_PROXIMAL
  145. "RightRingIntermediate", // XRBodyTracker::JOINT_RIGHT_RING_FINGER_PHALANX_INTERMEDIATE
  146. "RightRingDistal", // XRBodyTracker::JOINT_RIGHT_RING_FINGER_PHALANX_DISTAL
  147. "RightRingTip", // XRBodyTracker::JOINT_RIGHT_RING_FINGER_TIP
  148. "RightLittleMetacarpal", // XRBodyTracker::JOINT_RIGHT_PINKY_FINGER_METACARPAL
  149. "RightLittleProximal", // XRBodyTracker::JOINT_RIGHT_PINKY_FINGER_PHALANX_PROXIMAL
  150. "RightLittleIntermediate", // XRBodyTracker::JOINT_RIGHT_PINKY_FINGER_PHALANX_INTERMEDIATE
  151. "RightLittleDistal", // XRBodyTracker::JOINT_RIGHT_PINKY_FINGER_PHALANX_DISTAL
  152. "RightLittleTip", // XRBodyTracker::JOINT_RIGHT_PINKY_FINGER_TIP
  153. };
  154. // reset JIC.
  155. for (int i = 0; i < XRBodyTracker::JOINT_MAX; i++) {
  156. joints[i].bone = -1;
  157. joints[i].parent_joint = -1;
  158. }
  159. const Skeleton3D *skeleton = get_skeleton();
  160. if (!skeleton) {
  161. return;
  162. }
  163. // Find the skeleton-bones associated with each joint.
  164. int bones[XRBodyTracker::JOINT_MAX];
  165. for (int i = 0; i < XRBodyTracker::JOINT_MAX; i++) {
  166. // Skip upper body joints if not enabled.
  167. if (!body_update.has_flag(BODY_UPDATE_UPPER_BODY) && i >= XRBodyTracker::JOINT_HIPS && i <= XRBodyTracker::JOINT_RIGHT_LOWER_ARM) {
  168. bones[i] = -1;
  169. continue;
  170. }
  171. // Skip lower body joints if not enabled.
  172. if (!body_update.has_flag(BODY_UPDATE_LOWER_BODY) && i >= XRBodyTracker::JOINT_LEFT_UPPER_LEG && i <= XRBodyTracker::JOINT_RIGHT_TOES) {
  173. bones[i] = -1;
  174. continue;
  175. }
  176. // Skip hand joints if not enabled.
  177. if (!body_update.has_flag(BODY_UPDATE_HANDS) && i >= XRBodyTracker::JOINT_LEFT_HAND && i <= XRBodyTracker::JOINT_RIGHT_PINKY_FINGER_TIP) {
  178. bones[i] = -1;
  179. continue;
  180. }
  181. // Find the skeleton bone.
  182. bones[i] = skeleton->find_bone(bone_names[i]);
  183. if (bones[i] == -1) {
  184. WARN_PRINT(vformat("Couldn't obtain bone for %s", bone_names[i]));
  185. }
  186. }
  187. // Assemble the joint relationship to the available skeleton bones.
  188. for (int i = 0; i < XRBodyTracker::JOINT_MAX; i++) {
  189. // Get the skeleton bone (skip if not found).
  190. const int bone = bones[i];
  191. if (bone == -1) {
  192. continue;
  193. }
  194. // Find the parent skeleton-bone.
  195. const int parent_bone = skeleton->get_bone_parent(bone);
  196. if (parent_bone == -1) {
  197. // If no parent skeleton-bone exists then drive this relative to the root joint.
  198. joints[i].bone = bone;
  199. joints[i].parent_joint = XRBodyTracker::JOINT_ROOT;
  200. continue;
  201. }
  202. // Find the joint associated with the parent skeleton-bone.
  203. for (int j = 0; j < XRBodyTracker::JOINT_MAX; ++j) {
  204. if (bones[j] == parent_bone) {
  205. // If a parent joint is found then drive this bone relative to it.
  206. joints[i].bone = bone;
  207. joints[i].parent_joint = j;
  208. break;
  209. }
  210. }
  211. }
  212. }
  213. void XRBodyModifier3D::_process_modification() {
  214. Skeleton3D *skeleton = get_skeleton();
  215. if (!skeleton) {
  216. return;
  217. }
  218. const XRServer *xr_server = XRServer::get_singleton();
  219. if (!xr_server) {
  220. return;
  221. }
  222. const Ref<XRBodyTracker> tracker = xr_server->get_tracker(tracker_name);
  223. if (!tracker.is_valid()) {
  224. return;
  225. }
  226. // Skip if no tracking data.
  227. if (!tracker->get_has_tracking_data()) {
  228. return;
  229. }
  230. // Get the world and skeleton scale.
  231. const float ss = skeleton->get_motion_scale();
  232. // Read the relevant tracking data. This applies the skeleton motion scale to
  233. // the joint transforms, allowing the tracking data to be scaled to the skeleton.
  234. bool has_valid_data[XRBodyTracker::JOINT_MAX];
  235. Transform3D transforms[XRBodyTracker::JOINT_MAX];
  236. Transform3D inv_transforms[XRBodyTracker::JOINT_MAX];
  237. for (int joint = 0; joint < XRBodyTracker::JOINT_MAX; joint++) {
  238. BitField<XRBodyTracker::JointFlags> flags = tracker->get_joint_flags(static_cast<XRBodyTracker::Joint>(joint));
  239. has_valid_data[joint] = flags.has_flag(XRBodyTracker::JOINT_FLAG_ORIENTATION_VALID) && flags.has_flag(XRBodyTracker::JOINT_FLAG_POSITION_VALID);
  240. if (has_valid_data[joint]) {
  241. transforms[joint] = tracker->get_joint_transform(static_cast<XRBodyTracker::Joint>(joint));
  242. transforms[joint].origin *= ss;
  243. inv_transforms[joint] = transforms[joint].inverse();
  244. }
  245. }
  246. // Skip if root joint not tracked.
  247. if (!has_valid_data[XRBodyTracker::JOINT_ROOT]) {
  248. return;
  249. }
  250. // Apply the joint information to the skeleton.
  251. for (int joint = 0; joint < XRBodyTracker::JOINT_MAX; joint++) {
  252. // Skip if no valid joint data
  253. if (!has_valid_data[joint]) {
  254. continue;
  255. }
  256. // Get the skeleton bone (skip if none).
  257. const int bone = joints[joint].bone;
  258. if (bone == -1) {
  259. continue;
  260. }
  261. // Calculate the relative relationship to the parent bone joint.
  262. const int parent_joint = joints[joint].parent_joint;
  263. const Transform3D relative_transform = inv_transforms[parent_joint] * transforms[joint];
  264. // Update the bone position if enabled by update mode, or if the joint is the hips, to allow
  265. // for climbing or crouching.
  266. if (bone_update == BONE_UPDATE_FULL || joint == XRBodyTracker::JOINT_HIPS) {
  267. skeleton->set_bone_pose_position(joints[joint].bone, relative_transform.origin);
  268. }
  269. // Always update the bone rotation.
  270. skeleton->set_bone_pose_rotation(joints[joint].bone, Quaternion(relative_transform.basis));
  271. }
  272. }
  273. void XRBodyModifier3D::_tracker_changed(const StringName &p_tracker_name, XRServer::TrackerType p_tracker_type) {
  274. if (tracker_name == p_tracker_name) {
  275. _get_joint_data();
  276. }
  277. }
  278. void XRBodyModifier3D::_skeleton_changed(Skeleton3D *p_old, Skeleton3D *p_new) {
  279. _get_joint_data();
  280. }
  281. void XRBodyModifier3D::_notification(int p_what) {
  282. switch (p_what) {
  283. case NOTIFICATION_ENTER_TREE: {
  284. XRServer *xr_server = XRServer::get_singleton();
  285. if (xr_server) {
  286. xr_server->connect("tracker_added", callable_mp(this, &XRBodyModifier3D::_tracker_changed));
  287. xr_server->connect("tracker_updated", callable_mp(this, &XRBodyModifier3D::_tracker_changed));
  288. xr_server->connect("tracker_removed", callable_mp(this, &XRBodyModifier3D::_tracker_changed));
  289. }
  290. _get_joint_data();
  291. } break;
  292. case NOTIFICATION_EXIT_TREE: {
  293. XRServer *xr_server = XRServer::get_singleton();
  294. if (xr_server) {
  295. xr_server->disconnect("tracker_added", callable_mp(this, &XRBodyModifier3D::_tracker_changed));
  296. xr_server->disconnect("tracker_updated", callable_mp(this, &XRBodyModifier3D::_tracker_changed));
  297. xr_server->disconnect("tracker_removed", callable_mp(this, &XRBodyModifier3D::_tracker_changed));
  298. }
  299. for (int i = 0; i < XRBodyTracker::JOINT_MAX; i++) {
  300. joints[i].bone = -1;
  301. joints[i].parent_joint = -1;
  302. }
  303. } break;
  304. default: {
  305. } break;
  306. }
  307. }