diff options
author | Malcolm Nixon <Malcolm.nixon@gmail.com> | 2024-02-24 19:20:11 -0500 |
---|---|---|
committer | Malcolm Nixon <Malcolm.nixon@gmail.com> | 2024-02-27 19:51:59 -0500 |
commit | 57aa7345627075fa4cf3f1de33bcddf67baafe80 (patch) | |
tree | 65197395e798f2db30789ae4f0e65c0cf6e2b8f1 /servers | |
parent | bb6b06c81343073f10cbbd2af515cf0dac1e6549 (diff) | |
download | redot-engine-57aa7345627075fa4cf3f1de33bcddf67baafe80.tar.gz |
Add body tracking support.
Diffstat (limited to 'servers')
-rw-r--r-- | servers/register_server_types.cpp | 2 | ||||
-rw-r--r-- | servers/xr/xr_body_tracker.cpp | 171 | ||||
-rw-r--r-- | servers/xr/xr_body_tracker.h | 170 | ||||
-rw-r--r-- | servers/xr_server.cpp | 48 | ||||
-rw-r--r-- | servers/xr_server.h | 10 |
5 files changed, 401 insertions, 0 deletions
diff --git a/servers/register_server_types.cpp b/servers/register_server_types.cpp index 380500769c..ee2a65ad91 100644 --- a/servers/register_server_types.cpp +++ b/servers/register_server_types.cpp @@ -76,6 +76,7 @@ #include "text/text_server_dummy.h" #include "text/text_server_extension.h" #include "text_server.h" +#include "xr/xr_body_tracker.h" #include "xr/xr_face_tracker.h" #include "xr/xr_hand_tracker.h" #include "xr/xr_interface.h" @@ -173,6 +174,7 @@ void register_server_types() { GDREGISTER_ABSTRACT_CLASS(RenderingDevice); + GDREGISTER_CLASS(XRBodyTracker); GDREGISTER_ABSTRACT_CLASS(XRInterface); GDREGISTER_CLASS(XRHandTracker); GDREGISTER_CLASS(XRInterfaceExtension); // can't register this as virtual because we need a creation function for our extensions. diff --git a/servers/xr/xr_body_tracker.cpp b/servers/xr/xr_body_tracker.cpp new file mode 100644 index 0000000000..cd58c14348 --- /dev/null +++ b/servers/xr/xr_body_tracker.cpp @@ -0,0 +1,171 @@ +/**************************************************************************/ +/* xr_body_tracker.cpp */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#include "xr_body_tracker.h" + +void XRBodyTracker::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_has_tracking_data", "has_data"), &XRBodyTracker::set_has_tracking_data); + ClassDB::bind_method(D_METHOD("get_has_tracking_data"), &XRBodyTracker::get_has_tracking_data); + + ClassDB::bind_method(D_METHOD("set_body_flags", "flags"), &XRBodyTracker::set_body_flags); + ClassDB::bind_method(D_METHOD("get_body_flags"), &XRBodyTracker::get_body_flags); + + ClassDB::bind_method(D_METHOD("set_joint_flags", "joint", "flags"), &XRBodyTracker::set_joint_flags); + ClassDB::bind_method(D_METHOD("get_joint_flags", "joint"), &XRBodyTracker::get_joint_flags); + + ClassDB::bind_method(D_METHOD("set_joint_transform", "joint", "transform"), &XRBodyTracker::set_joint_transform); + ClassDB::bind_method(D_METHOD("get_joint_transform", "joint"), &XRBodyTracker::get_joint_transform); + + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "has_tracking_data", PROPERTY_HINT_NONE), "set_has_tracking_data", "get_has_tracking_data"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "body_flags", PROPERTY_HINT_FLAGS, "Upper Body,Lower Body,Hands"), "set_body_flags", "get_body_flags"); + + BIND_BITFIELD_FLAG(BODY_FLAG_UPPER_BODY_SUPPORTED); + BIND_BITFIELD_FLAG(BODY_FLAG_LOWER_BODY_SUPPORTED); + BIND_BITFIELD_FLAG(BODY_FLAG_HANDS_SUPPORTED); + + BIND_ENUM_CONSTANT(JOINT_ROOT); + BIND_ENUM_CONSTANT(JOINT_HIPS); + BIND_ENUM_CONSTANT(JOINT_SPINE); + BIND_ENUM_CONSTANT(JOINT_CHEST); + BIND_ENUM_CONSTANT(JOINT_UPPER_CHEST); + BIND_ENUM_CONSTANT(JOINT_NECK); + BIND_ENUM_CONSTANT(JOINT_HEAD); + BIND_ENUM_CONSTANT(JOINT_HEAD_TIP); + BIND_ENUM_CONSTANT(JOINT_LEFT_SHOULDER); + BIND_ENUM_CONSTANT(JOINT_LEFT_UPPER_ARM); + BIND_ENUM_CONSTANT(JOINT_LEFT_LOWER_ARM); + BIND_ENUM_CONSTANT(JOINT_RIGHT_SHOULDER); + BIND_ENUM_CONSTANT(JOINT_RIGHT_UPPER_ARM); + BIND_ENUM_CONSTANT(JOINT_RIGHT_LOWER_ARM); + BIND_ENUM_CONSTANT(JOINT_LEFT_UPPER_LEG); + BIND_ENUM_CONSTANT(JOINT_LEFT_LOWER_LEG); + BIND_ENUM_CONSTANT(JOINT_LEFT_FOOT); + BIND_ENUM_CONSTANT(JOINT_LEFT_TOES); + BIND_ENUM_CONSTANT(JOINT_RIGHT_UPPER_LEG); + BIND_ENUM_CONSTANT(JOINT_RIGHT_LOWER_LEG); + BIND_ENUM_CONSTANT(JOINT_RIGHT_FOOT); + BIND_ENUM_CONSTANT(JOINT_RIGHT_TOES); + BIND_ENUM_CONSTANT(JOINT_LEFT_HAND); + BIND_ENUM_CONSTANT(JOINT_LEFT_PALM); + BIND_ENUM_CONSTANT(JOINT_LEFT_WRIST); + BIND_ENUM_CONSTANT(JOINT_LEFT_THUMB_METACARPAL); + BIND_ENUM_CONSTANT(JOINT_LEFT_THUMB_PHALANX_PROXIMAL); + BIND_ENUM_CONSTANT(JOINT_LEFT_THUMB_PHALANX_DISTAL); + BIND_ENUM_CONSTANT(JOINT_LEFT_THUMB_TIP); + BIND_ENUM_CONSTANT(JOINT_LEFT_INDEX_FINGER_METACARPAL); + BIND_ENUM_CONSTANT(JOINT_LEFT_INDEX_FINGER_PHALANX_PROXIMAL); + BIND_ENUM_CONSTANT(JOINT_LEFT_INDEX_FINGER_PHALANX_INTERMEDIATE); + BIND_ENUM_CONSTANT(JOINT_LEFT_INDEX_FINGER_PHALANX_DISTAL); + BIND_ENUM_CONSTANT(JOINT_LEFT_INDEX_FINGER_TIP); + BIND_ENUM_CONSTANT(JOINT_LEFT_MIDDLE_FINGER_METACARPAL); + BIND_ENUM_CONSTANT(JOINT_LEFT_MIDDLE_FINGER_PHALANX_PROXIMAL); + BIND_ENUM_CONSTANT(JOINT_LEFT_MIDDLE_FINGER_PHALANX_INTERMEDIATE); + BIND_ENUM_CONSTANT(JOINT_LEFT_MIDDLE_FINGER_PHALANX_DISTAL); + BIND_ENUM_CONSTANT(JOINT_LEFT_MIDDLE_FINGER_TIP); + BIND_ENUM_CONSTANT(JOINT_LEFT_RING_FINGER_METACARPAL); + BIND_ENUM_CONSTANT(JOINT_LEFT_RING_FINGER_PHALANX_PROXIMAL); + BIND_ENUM_CONSTANT(JOINT_LEFT_RING_FINGER_PHALANX_INTERMEDIATE); + BIND_ENUM_CONSTANT(JOINT_LEFT_RING_FINGER_PHALANX_DISTAL); + BIND_ENUM_CONSTANT(JOINT_LEFT_RING_FINGER_TIP); + BIND_ENUM_CONSTANT(JOINT_LEFT_PINKY_FINGER_METACARPAL); + BIND_ENUM_CONSTANT(JOINT_LEFT_PINKY_FINGER_PHALANX_PROXIMAL); + BIND_ENUM_CONSTANT(JOINT_LEFT_PINKY_FINGER_PHALANX_INTERMEDIATE); + BIND_ENUM_CONSTANT(JOINT_LEFT_PINKY_FINGER_PHALANX_DISTAL); + BIND_ENUM_CONSTANT(JOINT_LEFT_PINKY_FINGER_TIP); + BIND_ENUM_CONSTANT(JOINT_RIGHT_HAND); + BIND_ENUM_CONSTANT(JOINT_RIGHT_PALM); + BIND_ENUM_CONSTANT(JOINT_RIGHT_WRIST); + BIND_ENUM_CONSTANT(JOINT_RIGHT_THUMB_METACARPAL); + BIND_ENUM_CONSTANT(JOINT_RIGHT_THUMB_PHALANX_PROXIMAL); + BIND_ENUM_CONSTANT(JOINT_RIGHT_THUMB_PHALANX_DISTAL); + BIND_ENUM_CONSTANT(JOINT_RIGHT_THUMB_TIP); + BIND_ENUM_CONSTANT(JOINT_RIGHT_INDEX_FINGER_METACARPAL); + BIND_ENUM_CONSTANT(JOINT_RIGHT_INDEX_FINGER_PHALANX_PROXIMAL); + BIND_ENUM_CONSTANT(JOINT_RIGHT_INDEX_FINGER_PHALANX_INTERMEDIATE); + BIND_ENUM_CONSTANT(JOINT_RIGHT_INDEX_FINGER_PHALANX_DISTAL); + BIND_ENUM_CONSTANT(JOINT_RIGHT_INDEX_FINGER_TIP); + BIND_ENUM_CONSTANT(JOINT_RIGHT_MIDDLE_FINGER_METACARPAL); + BIND_ENUM_CONSTANT(JOINT_RIGHT_MIDDLE_FINGER_PHALANX_PROXIMAL); + BIND_ENUM_CONSTANT(JOINT_RIGHT_MIDDLE_FINGER_PHALANX_INTERMEDIATE); + BIND_ENUM_CONSTANT(JOINT_RIGHT_MIDDLE_FINGER_PHALANX_DISTAL); + BIND_ENUM_CONSTANT(JOINT_RIGHT_MIDDLE_FINGER_TIP); + BIND_ENUM_CONSTANT(JOINT_RIGHT_RING_FINGER_METACARPAL); + BIND_ENUM_CONSTANT(JOINT_RIGHT_RING_FINGER_PHALANX_PROXIMAL); + BIND_ENUM_CONSTANT(JOINT_RIGHT_RING_FINGER_PHALANX_INTERMEDIATE); + BIND_ENUM_CONSTANT(JOINT_RIGHT_RING_FINGER_PHALANX_DISTAL); + BIND_ENUM_CONSTANT(JOINT_RIGHT_RING_FINGER_TIP); + BIND_ENUM_CONSTANT(JOINT_RIGHT_PINKY_FINGER_METACARPAL); + BIND_ENUM_CONSTANT(JOINT_RIGHT_PINKY_FINGER_PHALANX_PROXIMAL); + BIND_ENUM_CONSTANT(JOINT_RIGHT_PINKY_FINGER_PHALANX_INTERMEDIATE); + BIND_ENUM_CONSTANT(JOINT_RIGHT_PINKY_FINGER_PHALANX_DISTAL); + BIND_ENUM_CONSTANT(JOINT_RIGHT_PINKY_FINGER_TIP); + BIND_ENUM_CONSTANT(JOINT_MAX); + + BIND_BITFIELD_FLAG(JOINT_FLAG_ORIENTATION_VALID); + BIND_BITFIELD_FLAG(JOINT_FLAG_ORIENTATION_TRACKED); + BIND_BITFIELD_FLAG(JOINT_FLAG_POSITION_VALID); + BIND_BITFIELD_FLAG(JOINT_FLAG_POSITION_TRACKED); +} + +void XRBodyTracker::set_has_tracking_data(bool p_has_tracking_data) { + has_tracking_data = p_has_tracking_data; +} + +bool XRBodyTracker::get_has_tracking_data() const { + return has_tracking_data; +} + +void XRBodyTracker::set_body_flags(BitField<BodyFlags> p_body_flags) { + body_flags = p_body_flags; +} + +BitField<XRBodyTracker::BodyFlags> XRBodyTracker::get_body_flags() const { + return body_flags; +} + +void XRBodyTracker::set_joint_flags(Joint p_joint, BitField<JointFlags> p_flags) { + ERR_FAIL_INDEX(p_joint, JOINT_MAX); + joint_flags[p_joint] = p_flags; +} + +BitField<XRBodyTracker::JointFlags> XRBodyTracker::get_joint_flags(Joint p_joint) const { + ERR_FAIL_INDEX_V(p_joint, JOINT_MAX, BitField<JointFlags>()); + return joint_flags[p_joint]; +} + +void XRBodyTracker::set_joint_transform(Joint p_joint, const Transform3D &p_transform) { + ERR_FAIL_INDEX(p_joint, JOINT_MAX); + joint_transforms[p_joint] = p_transform; +} + +Transform3D XRBodyTracker::get_joint_transform(Joint p_joint) const { + ERR_FAIL_INDEX_V(p_joint, JOINT_MAX, Transform3D()); + return joint_transforms[p_joint]; +} diff --git a/servers/xr/xr_body_tracker.h b/servers/xr/xr_body_tracker.h new file mode 100644 index 0000000000..659aa39df1 --- /dev/null +++ b/servers/xr/xr_body_tracker.h @@ -0,0 +1,170 @@ +/**************************************************************************/ +/* xr_body_tracker.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#ifndef XR_BODY_TRACKER_H +#define XR_BODY_TRACKER_H + +#include "core/object/ref_counted.h" + +class XRBodyTracker : public RefCounted { + GDCLASS(XRBodyTracker, RefCounted); + _THREAD_SAFE_CLASS_ + +public: + enum BodyFlags { + BODY_FLAG_UPPER_BODY_SUPPORTED = 1, + BODY_FLAG_LOWER_BODY_SUPPORTED = 2, + BODY_FLAG_HANDS_SUPPORTED = 4, + }; + + enum Joint { + JOINT_ROOT, + + // Upper Body Joints + JOINT_HIPS, + JOINT_SPINE, + JOINT_CHEST, + JOINT_UPPER_CHEST, + JOINT_NECK, + JOINT_HEAD, + JOINT_HEAD_TIP, + JOINT_LEFT_SHOULDER, + JOINT_LEFT_UPPER_ARM, + JOINT_LEFT_LOWER_ARM, + JOINT_RIGHT_SHOULDER, + JOINT_RIGHT_UPPER_ARM, + JOINT_RIGHT_LOWER_ARM, + + // Lower Body Joints + JOINT_LEFT_UPPER_LEG, + JOINT_LEFT_LOWER_LEG, + JOINT_LEFT_FOOT, + JOINT_LEFT_TOES, + JOINT_RIGHT_UPPER_LEG, + JOINT_RIGHT_LOWER_LEG, + JOINT_RIGHT_FOOT, + JOINT_RIGHT_TOES, + + // Left Hand Joints + JOINT_LEFT_HAND, + JOINT_LEFT_PALM, + JOINT_LEFT_WRIST, + JOINT_LEFT_THUMB_METACARPAL, + JOINT_LEFT_THUMB_PHALANX_PROXIMAL, + JOINT_LEFT_THUMB_PHALANX_DISTAL, + JOINT_LEFT_THUMB_TIP, + JOINT_LEFT_INDEX_FINGER_METACARPAL, + JOINT_LEFT_INDEX_FINGER_PHALANX_PROXIMAL, + JOINT_LEFT_INDEX_FINGER_PHALANX_INTERMEDIATE, + JOINT_LEFT_INDEX_FINGER_PHALANX_DISTAL, + JOINT_LEFT_INDEX_FINGER_TIP, + JOINT_LEFT_MIDDLE_FINGER_METACARPAL, + JOINT_LEFT_MIDDLE_FINGER_PHALANX_PROXIMAL, + JOINT_LEFT_MIDDLE_FINGER_PHALANX_INTERMEDIATE, + JOINT_LEFT_MIDDLE_FINGER_PHALANX_DISTAL, + JOINT_LEFT_MIDDLE_FINGER_TIP, + JOINT_LEFT_RING_FINGER_METACARPAL, + JOINT_LEFT_RING_FINGER_PHALANX_PROXIMAL, + JOINT_LEFT_RING_FINGER_PHALANX_INTERMEDIATE, + JOINT_LEFT_RING_FINGER_PHALANX_DISTAL, + JOINT_LEFT_RING_FINGER_TIP, + JOINT_LEFT_PINKY_FINGER_METACARPAL, + JOINT_LEFT_PINKY_FINGER_PHALANX_PROXIMAL, + JOINT_LEFT_PINKY_FINGER_PHALANX_INTERMEDIATE, + JOINT_LEFT_PINKY_FINGER_PHALANX_DISTAL, + JOINT_LEFT_PINKY_FINGER_TIP, + + // Right Hand Joints + JOINT_RIGHT_HAND, + JOINT_RIGHT_PALM, + JOINT_RIGHT_WRIST, + JOINT_RIGHT_THUMB_METACARPAL, + JOINT_RIGHT_THUMB_PHALANX_PROXIMAL, + JOINT_RIGHT_THUMB_PHALANX_DISTAL, + JOINT_RIGHT_THUMB_TIP, + JOINT_RIGHT_INDEX_FINGER_METACARPAL, + JOINT_RIGHT_INDEX_FINGER_PHALANX_PROXIMAL, + JOINT_RIGHT_INDEX_FINGER_PHALANX_INTERMEDIATE, + JOINT_RIGHT_INDEX_FINGER_PHALANX_DISTAL, + JOINT_RIGHT_INDEX_FINGER_TIP, + JOINT_RIGHT_MIDDLE_FINGER_METACARPAL, + JOINT_RIGHT_MIDDLE_FINGER_PHALANX_PROXIMAL, + JOINT_RIGHT_MIDDLE_FINGER_PHALANX_INTERMEDIATE, + JOINT_RIGHT_MIDDLE_FINGER_PHALANX_DISTAL, + JOINT_RIGHT_MIDDLE_FINGER_TIP, + JOINT_RIGHT_RING_FINGER_METACARPAL, + JOINT_RIGHT_RING_FINGER_PHALANX_PROXIMAL, + JOINT_RIGHT_RING_FINGER_PHALANX_INTERMEDIATE, + JOINT_RIGHT_RING_FINGER_PHALANX_DISTAL, + JOINT_RIGHT_RING_FINGER_TIP, + JOINT_RIGHT_PINKY_FINGER_METACARPAL, + JOINT_RIGHT_PINKY_FINGER_PHALANX_PROXIMAL, + JOINT_RIGHT_PINKY_FINGER_PHALANX_INTERMEDIATE, + JOINT_RIGHT_PINKY_FINGER_PHALANX_DISTAL, + JOINT_RIGHT_PINKY_FINGER_TIP, + + JOINT_MAX, + }; + + enum JointFlags { + JOINT_FLAG_ORIENTATION_VALID = 1, + JOINT_FLAG_ORIENTATION_TRACKED = 2, + JOINT_FLAG_POSITION_VALID = 4, + JOINT_FLAG_POSITION_TRACKED = 8, + }; + + void set_has_tracking_data(bool p_has_tracking_data); + bool get_has_tracking_data() const; + + void set_body_flags(BitField<BodyFlags> p_body_flags); + BitField<BodyFlags> get_body_flags() const; + + void set_joint_flags(Joint p_joint, BitField<JointFlags> p_flags); + BitField<JointFlags> get_joint_flags(Joint p_joint) const; + + void set_joint_transform(Joint p_joint, const Transform3D &p_transform); + Transform3D get_joint_transform(Joint p_joint) const; + +protected: + static void _bind_methods(); + +private: + bool has_tracking_data = false; + BitField<BodyFlags> body_flags; + + BitField<JointFlags> joint_flags[JOINT_MAX]; + Transform3D joint_transforms[JOINT_MAX]; +}; + +VARIANT_BITFIELD_CAST(XRBodyTracker::BodyFlags) +VARIANT_ENUM_CAST(XRBodyTracker::Joint) +VARIANT_BITFIELD_CAST(XRBodyTracker::JointFlags) + +#endif // XR_BODY_TRACKER_H diff --git a/servers/xr_server.cpp b/servers/xr_server.cpp index 1e497e22c3..af14ba4a00 100644 --- a/servers/xr_server.cpp +++ b/servers/xr_server.cpp @@ -30,6 +30,7 @@ #include "xr_server.h" #include "core/config/project_settings.h" +#include "xr/xr_body_tracker.h" #include "xr/xr_face_tracker.h" #include "xr/xr_hand_tracker.h" #include "xr/xr_interface.h" @@ -86,6 +87,11 @@ void XRServer::_bind_methods() { ClassDB::bind_method(D_METHOD("get_face_trackers"), &XRServer::get_face_trackers); ClassDB::bind_method(D_METHOD("get_face_tracker", "tracker_name"), &XRServer::get_face_tracker); + ClassDB::bind_method(D_METHOD("add_body_tracker", "tracker_name", "body_tracker"), &XRServer::add_body_tracker); + ClassDB::bind_method(D_METHOD("remove_body_tracker", "tracker_name"), &XRServer::remove_body_tracker); + ClassDB::bind_method(D_METHOD("get_body_trackers"), &XRServer::get_body_trackers); + ClassDB::bind_method(D_METHOD("get_body_tracker", "tracker_name"), &XRServer::get_body_tracker); + ClassDB::bind_method(D_METHOD("get_primary_interface"), &XRServer::get_primary_interface); ClassDB::bind_method(D_METHOD("set_primary_interface", "interface"), &XRServer::set_primary_interface); @@ -117,6 +123,10 @@ void XRServer::_bind_methods() { ADD_SIGNAL(MethodInfo("face_tracker_added", PropertyInfo(Variant::STRING_NAME, "tracker_name"), PropertyInfo(Variant::OBJECT, "face_tracker", PROPERTY_HINT_RESOURCE_TYPE, "XRFaceTracker"))); ADD_SIGNAL(MethodInfo("face_tracker_updated", PropertyInfo(Variant::STRING_NAME, "tracker_name"), PropertyInfo(Variant::OBJECT, "face_tracker", PROPERTY_HINT_RESOURCE_TYPE, "XRFaceTracker"))); ADD_SIGNAL(MethodInfo("face_tracker_removed", PropertyInfo(Variant::STRING_NAME, "tracker_name"))); + + ADD_SIGNAL(MethodInfo("body_tracker_added", PropertyInfo(Variant::STRING_NAME, "tracker_name"), PropertyInfo(Variant::OBJECT, "body_tracker", PROPERTY_HINT_RESOURCE_TYPE, "XRBodyTracker"))); + ADD_SIGNAL(MethodInfo("body_tracker_updated", PropertyInfo(Variant::STRING_NAME, "tracker_name"), PropertyInfo(Variant::OBJECT, "body_tracker", PROPERTY_HINT_RESOURCE_TYPE, "XRBodyTracker"))); + ADD_SIGNAL(MethodInfo("body_tracker_removed", PropertyInfo(Variant::STRING_NAME, "tracker_name"))); }; double XRServer::get_world_scale() const { @@ -448,6 +458,44 @@ Ref<XRFaceTracker> XRServer::get_face_tracker(const StringName &p_tracker_name) return face_trackers[p_tracker_name]; } +void XRServer::add_body_tracker(const StringName &p_tracker_name, Ref<XRBodyTracker> p_body_tracker) { + ERR_FAIL_COND(p_body_tracker.is_null()); + + if (!body_trackers.has(p_tracker_name)) { + // We don't have a tracker with this name, we're going to add it. + body_trackers[p_tracker_name] = p_body_tracker; + emit_signal(SNAME("body_tracker_added"), p_tracker_name, p_body_tracker); + } else if (body_trackers[p_tracker_name] != p_body_tracker) { + // We already have a tracker with this name, we're going to replace it. + body_trackers[p_tracker_name] = p_body_tracker; + emit_signal(SNAME("body_tracker_updated"), p_tracker_name, p_body_tracker); + } +} + +void XRServer::remove_body_tracker(const StringName &p_tracker_name) { + // Skip if no face tracker is found. + if (!body_trackers.has(p_tracker_name)) { + return; + } + + // Send the removed signal, then remove the face tracker. + emit_signal(SNAME("body_tracker_removed"), p_tracker_name); + body_trackers.erase(p_tracker_name); +} + +Dictionary XRServer::get_body_trackers() const { + return body_trackers; +} + +Ref<XRBodyTracker> XRServer::get_body_tracker(const StringName &p_tracker_name) const { + // Skip if no tracker is found. + if (!body_trackers.has(p_tracker_name)) { + return Ref<XRBodyTracker>(); + } + + return body_trackers[p_tracker_name]; +} + void XRServer::_process() { // called from our main game loop before we handle physics and game logic // note that we can have multiple interfaces active if we have interfaces that purely handle tracking diff --git a/servers/xr_server.h b/servers/xr_server.h index 3e45bbb76c..6aaa34b21d 100644 --- a/servers/xr_server.h +++ b/servers/xr_server.h @@ -41,6 +41,7 @@ class XRInterface; class XRPositionalTracker; class XRHandTracker; class XRFaceTracker; +class XRBodyTracker; /** The XR server is a singleton object that gives access to the various @@ -89,6 +90,7 @@ private: Dictionary trackers; Dictionary hand_trackers; Dictionary face_trackers; + Dictionary body_trackers; Ref<XRInterface> primary_interface; /* we'll identify one interface as primary, this will be used by our viewports */ @@ -203,6 +205,14 @@ public: Dictionary get_face_trackers() const; Ref<XRFaceTracker> get_face_tracker(const StringName &p_tracker_name) const; + /* + Body trackers are objects that expose the tracked joints of a body. + */ + void add_body_tracker(const StringName &p_tracker_name, Ref<XRBodyTracker> p_face_tracker); + void remove_body_tracker(const StringName &p_tracker_name); + Dictionary get_body_trackers() const; + Ref<XRBodyTracker> get_body_tracker(const StringName &p_tracker_name) const; + // Process is called before we handle our physics process and game process. This is where our interfaces will update controller data and such. void _process(); |