| 1 | /**************************************************************************/ |
| 2 | /* xr_nodes.h */ |
| 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 | |
| 31 | #ifndef XR_NODES_H |
| 32 | #define XR_NODES_H |
| 33 | |
| 34 | #include "scene/3d/camera_3d.h" |
| 35 | #include "servers/xr/xr_positional_tracker.h" |
| 36 | |
| 37 | /* |
| 38 | XRCamera is a subclass of camera which will register itself with its parent XROrigin and as a result is automatically positioned |
| 39 | */ |
| 40 | |
| 41 | class XRCamera3D : public Camera3D { |
| 42 | GDCLASS(XRCamera3D, Camera3D); |
| 43 | |
| 44 | protected: |
| 45 | // The name and pose for our HMD tracker is currently the only hardcoded bit. |
| 46 | // If we ever are able to support multiple HMDs we may need to make this settable. |
| 47 | StringName tracker_name = "head" ; |
| 48 | StringName pose_name = "default" ; |
| 49 | Ref<XRPositionalTracker> tracker; |
| 50 | |
| 51 | void _bind_tracker(); |
| 52 | void _unbind_tracker(); |
| 53 | void _changed_tracker(const StringName p_tracker_name, int p_tracker_type); |
| 54 | void _removed_tracker(const StringName p_tracker_name, int p_tracker_type); |
| 55 | void _pose_changed(const Ref<XRPose> &p_pose); |
| 56 | |
| 57 | public: |
| 58 | PackedStringArray get_configuration_warnings() const override; |
| 59 | |
| 60 | virtual Vector3 project_local_ray_normal(const Point2 &p_pos) const override; |
| 61 | virtual Point2 unproject_position(const Vector3 &p_pos) const override; |
| 62 | virtual Vector3 project_position(const Point2 &p_point, real_t p_z_depth) const override; |
| 63 | virtual Vector<Plane> get_frustum() const override; |
| 64 | |
| 65 | XRCamera3D(); |
| 66 | ~XRCamera3D(); |
| 67 | }; |
| 68 | |
| 69 | /* |
| 70 | XRNode3D is a helper node that implements binding to a tracker. |
| 71 | |
| 72 | It must be a child node of our XROrigin node |
| 73 | */ |
| 74 | |
| 75 | class XRNode3D : public Node3D { |
| 76 | GDCLASS(XRNode3D, Node3D); |
| 77 | |
| 78 | private: |
| 79 | StringName tracker_name; |
| 80 | StringName pose_name = "default" ; |
| 81 | bool has_tracking_data = false; |
| 82 | |
| 83 | protected: |
| 84 | Ref<XRPositionalTracker> tracker; |
| 85 | |
| 86 | static void _bind_methods(); |
| 87 | |
| 88 | virtual void _bind_tracker(); |
| 89 | virtual void _unbind_tracker(); |
| 90 | void _changed_tracker(const StringName p_tracker_name, int p_tracker_type); |
| 91 | void _removed_tracker(const StringName p_tracker_name, int p_tracker_type); |
| 92 | |
| 93 | void _pose_changed(const Ref<XRPose> &p_pose); |
| 94 | void _pose_lost_tracking(const Ref<XRPose> &p_pose); |
| 95 | void _set_has_tracking_data(bool p_has_tracking_data); |
| 96 | |
| 97 | public: |
| 98 | void _validate_property(PropertyInfo &p_property) const; |
| 99 | void set_tracker(const StringName p_tracker_name); |
| 100 | StringName get_tracker() const; |
| 101 | |
| 102 | void set_pose_name(const StringName p_pose); |
| 103 | StringName get_pose_name() const; |
| 104 | |
| 105 | bool get_is_active() const; |
| 106 | bool get_has_tracking_data() const; |
| 107 | |
| 108 | void trigger_haptic_pulse(const String &p_action_name, double p_frequency, double p_amplitude, double p_duration_sec, double p_delay_sec = 0); |
| 109 | |
| 110 | Ref<XRPose> get_pose(); |
| 111 | |
| 112 | PackedStringArray get_configuration_warnings() const override; |
| 113 | |
| 114 | XRNode3D(); |
| 115 | ~XRNode3D(); |
| 116 | }; |
| 117 | |
| 118 | /* |
| 119 | XRController3D is a helper node that automatically updates its position based on tracker data. |
| 120 | |
| 121 | It must be a child node of our XROrigin node |
| 122 | */ |
| 123 | |
| 124 | class XRController3D : public XRNode3D { |
| 125 | GDCLASS(XRController3D, XRNode3D); |
| 126 | |
| 127 | private: |
| 128 | protected: |
| 129 | static void _bind_methods(); |
| 130 | |
| 131 | virtual void _bind_tracker() override; |
| 132 | virtual void _unbind_tracker() override; |
| 133 | |
| 134 | void _button_pressed(const String &p_name); |
| 135 | void _button_released(const String &p_name); |
| 136 | void _input_float_changed(const String &p_name, float p_value); |
| 137 | void _input_vector2_changed(const String &p_name, Vector2 p_value); |
| 138 | |
| 139 | public: |
| 140 | bool is_button_pressed(const StringName &p_name) const; |
| 141 | Variant get_input(const StringName &p_name) const; |
| 142 | float get_float(const StringName &p_name) const; |
| 143 | Vector2 get_vector2(const StringName &p_name) const; |
| 144 | |
| 145 | XRPositionalTracker::TrackerHand get_tracker_hand() const; |
| 146 | |
| 147 | XRController3D() {} |
| 148 | ~XRController3D() {} |
| 149 | }; |
| 150 | |
| 151 | /* |
| 152 | XRAnchor3D is a helper node that automatically updates its position based on anchor data, it represents a real world location. |
| 153 | It must be a child node of our XROrigin3D node |
| 154 | */ |
| 155 | |
| 156 | class XRAnchor3D : public XRNode3D { |
| 157 | GDCLASS(XRAnchor3D, XRNode3D); |
| 158 | |
| 159 | private: |
| 160 | Vector3 size; |
| 161 | |
| 162 | protected: |
| 163 | static void _bind_methods(); |
| 164 | |
| 165 | public: |
| 166 | Vector3 get_size() const; |
| 167 | Plane get_plane() const; |
| 168 | |
| 169 | XRAnchor3D() {} |
| 170 | ~XRAnchor3D() {} |
| 171 | }; |
| 172 | |
| 173 | /* |
| 174 | XROrigin3D is special spatial node that acts as our origin point mapping our real world center of our tracking volume into our virtual world. |
| 175 | |
| 176 | It is this point that you will move around the world as the player 'moves while standing still', i.e. the player moves through teleporting or controller inputs as opposed to physically moving. |
| 177 | |
| 178 | Our camera and controllers will always be child nodes and thus place relative to this origin point. |
| 179 | This node will automatically locate any camera child nodes and update its position while our XRController3D node will handle tracked controllers. |
| 180 | */ |
| 181 | |
| 182 | class XROrigin3D : public Node3D { |
| 183 | GDCLASS(XROrigin3D, Node3D); |
| 184 | |
| 185 | private: |
| 186 | bool current = false; |
| 187 | static Vector<XROrigin3D *> origin_nodes; // all origin nodes in tree |
| 188 | |
| 189 | void _set_current(bool p_enabled, bool p_update_others); |
| 190 | |
| 191 | protected: |
| 192 | void _notification(int p_what); |
| 193 | static void _bind_methods(); |
| 194 | |
| 195 | public: |
| 196 | PackedStringArray get_configuration_warnings() const override; |
| 197 | |
| 198 | real_t get_world_scale() const; |
| 199 | void set_world_scale(real_t p_world_scale); |
| 200 | |
| 201 | void set_current(bool p_enabled); |
| 202 | bool is_current() const; |
| 203 | |
| 204 | XROrigin3D() {} |
| 205 | ~XROrigin3D() {} |
| 206 | }; |
| 207 | |
| 208 | #endif // XR_NODES_H |
| 209 | |