| 1 | /**************************************************************************/ |
| 2 | /* node_3d.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 NODE_3D_H |
| 32 | #define NODE_3D_H |
| 33 | |
| 34 | #include "scene/main/node.h" |
| 35 | #include "scene/resources/world_3d.h" |
| 36 | |
| 37 | class Node3DGizmo : public RefCounted { |
| 38 | GDCLASS(Node3DGizmo, RefCounted); |
| 39 | |
| 40 | public: |
| 41 | virtual void create() = 0; |
| 42 | virtual void transform() = 0; |
| 43 | virtual void clear() = 0; |
| 44 | virtual void redraw() = 0; |
| 45 | virtual void free() = 0; |
| 46 | |
| 47 | Node3DGizmo(); |
| 48 | virtual ~Node3DGizmo() {} |
| 49 | }; |
| 50 | |
| 51 | class Node3D : public Node { |
| 52 | GDCLASS(Node3D, Node); |
| 53 | |
| 54 | public: |
| 55 | // Edit mode for the rotation. |
| 56 | // THIS MODE ONLY AFFECTS HOW DATA IS EDITED AND SAVED |
| 57 | // IT DOES _NOT_ AFFECT THE TRANSFORM LOGIC (see comment in TransformDirty). |
| 58 | enum RotationEditMode { |
| 59 | ROTATION_EDIT_MODE_EULER, |
| 60 | ROTATION_EDIT_MODE_QUATERNION, |
| 61 | ROTATION_EDIT_MODE_BASIS, |
| 62 | }; |
| 63 | |
| 64 | private: |
| 65 | // For the sake of ease of use, Node3D can operate with Transforms (Basis+Origin), Quaternion/Scale and Euler Rotation/Scale. |
| 66 | // Transform and Quaternion are stored in data.local_transform Basis (so quaternion is not really stored, but converted back/forth from 3x3 matrix on demand). |
| 67 | // Euler needs to be kept separate because converting to Basis and back may result in a different vector (which is troublesome for users |
| 68 | // editing in the inspector, not only because of the numerical precision loss but because they expect these rotations to be consistent, or support |
| 69 | // "redundant" rotations for animation interpolation, like going from 0 to 720 degrees). |
| 70 | // |
| 71 | // As such, the system works in a way where if the local transform is set (via transform/basis/quaternion), the EULER rotation and scale becomes dirty. |
| 72 | // It will remain dirty until reading back is attempted (for performance reasons). Likewise, if the Euler rotation scale are set, the local transform |
| 73 | // will become dirty (and again, will not become valid again until read). |
| 74 | // |
| 75 | // All this is transparent from outside the Node3D API, which allows everything to works by calling these functions in exchange. |
| 76 | // |
| 77 | // Additionally, setting either transform, quaternion, Euler rotation or scale makes the global transform dirty, which will be updated when read again. |
| 78 | // |
| 79 | // NOTE: Again, RotationEditMode is _independent_ of this mechanism, it is only meant to expose the right set of properties for editing (editor) and saving |
| 80 | // (to scene, in order to keep the same values and avoid data loss on conversions). It has zero influence in the logic described above. |
| 81 | enum TransformDirty { |
| 82 | DIRTY_NONE = 0, |
| 83 | DIRTY_EULER_ROTATION_AND_SCALE = 1, |
| 84 | DIRTY_LOCAL_TRANSFORM = 2, |
| 85 | DIRTY_GLOBAL_TRANSFORM = 4 |
| 86 | }; |
| 87 | |
| 88 | mutable SelfList<Node> xform_change; |
| 89 | |
| 90 | // This Data struct is to avoid namespace pollution in derived classes. |
| 91 | |
| 92 | struct Data { |
| 93 | mutable Transform3D global_transform; |
| 94 | mutable Transform3D local_transform; |
| 95 | mutable EulerOrder euler_rotation_order = EulerOrder::YXZ; |
| 96 | mutable Vector3 euler_rotation; |
| 97 | mutable Vector3 scale = Vector3(1, 1, 1); |
| 98 | mutable RotationEditMode rotation_edit_mode = ROTATION_EDIT_MODE_EULER; |
| 99 | |
| 100 | mutable MTNumeric<uint32_t> dirty; |
| 101 | |
| 102 | Viewport *viewport = nullptr; |
| 103 | |
| 104 | bool top_level = false; |
| 105 | bool inside_world = false; |
| 106 | |
| 107 | RID visibility_parent; |
| 108 | |
| 109 | Node3D *parent = nullptr; |
| 110 | List<Node3D *> children; |
| 111 | List<Node3D *>::Element *C = nullptr; |
| 112 | |
| 113 | bool ignore_notification = false; |
| 114 | bool notify_local_transform = false; |
| 115 | bool notify_transform = false; |
| 116 | |
| 117 | bool visible = true; |
| 118 | bool disable_scale = false; |
| 119 | |
| 120 | #ifdef TOOLS_ENABLED |
| 121 | Vector<Ref<Node3DGizmo>> gizmos; |
| 122 | bool gizmos_disabled = false; |
| 123 | bool gizmos_dirty = false; |
| 124 | bool transform_gizmo_visible = true; |
| 125 | #endif |
| 126 | |
| 127 | } data; |
| 128 | |
| 129 | NodePath visibility_parent_path; |
| 130 | |
| 131 | _FORCE_INLINE_ uint32_t _read_dirty_mask() const { return is_group_processing() ? data.dirty.mt.get() : data.dirty.st; } |
| 132 | _FORCE_INLINE_ bool _test_dirty_bits(uint32_t p_bits) const { return is_group_processing() ? data.dirty.mt.bit_and(p_bits) : (data.dirty.st & p_bits); } |
| 133 | void _replace_dirty_mask(uint32_t p_mask) const; |
| 134 | void _set_dirty_bits(uint32_t p_bits) const; |
| 135 | void _clear_dirty_bits(uint32_t p_bits) const; |
| 136 | |
| 137 | void _update_gizmos(); |
| 138 | void _notify_dirty(); |
| 139 | void _propagate_transform_changed(Node3D *p_origin); |
| 140 | |
| 141 | void _propagate_visibility_changed(); |
| 142 | |
| 143 | void _propagate_visibility_parent(); |
| 144 | void _update_visibility_parent(bool p_update_root); |
| 145 | void _propagate_transform_changed_deferred(); |
| 146 | |
| 147 | protected: |
| 148 | _FORCE_INLINE_ void set_ignore_transform_notification(bool p_ignore) { data.ignore_notification = p_ignore; } |
| 149 | |
| 150 | _FORCE_INLINE_ void _update_local_transform() const; |
| 151 | _FORCE_INLINE_ void _update_rotation_and_scale() const; |
| 152 | |
| 153 | void _notification(int p_what); |
| 154 | static void _bind_methods(); |
| 155 | |
| 156 | void _validate_property(PropertyInfo &p_property) const; |
| 157 | |
| 158 | bool _property_can_revert(const StringName &p_name) const; |
| 159 | bool _property_get_revert(const StringName &p_name, Variant &r_property) const; |
| 160 | |
| 161 | public: |
| 162 | enum { |
| 163 | NOTIFICATION_TRANSFORM_CHANGED = SceneTree::NOTIFICATION_TRANSFORM_CHANGED, |
| 164 | NOTIFICATION_ENTER_WORLD = 41, |
| 165 | NOTIFICATION_EXIT_WORLD = 42, |
| 166 | NOTIFICATION_VISIBILITY_CHANGED = 43, |
| 167 | NOTIFICATION_LOCAL_TRANSFORM_CHANGED = 44, |
| 168 | }; |
| 169 | |
| 170 | Node3D *get_parent_node_3d() const; |
| 171 | |
| 172 | Ref<World3D> get_world_3d() const; |
| 173 | |
| 174 | void set_position(const Vector3 &p_position); |
| 175 | |
| 176 | void set_rotation_edit_mode(RotationEditMode p_mode); |
| 177 | RotationEditMode get_rotation_edit_mode() const; |
| 178 | |
| 179 | void set_rotation_order(EulerOrder p_order); |
| 180 | void set_rotation(const Vector3 &p_euler_rad); |
| 181 | void set_rotation_degrees(const Vector3 &p_euler_degrees); |
| 182 | void set_scale(const Vector3 &p_scale); |
| 183 | |
| 184 | void set_global_position(const Vector3 &p_position); |
| 185 | void set_global_basis(const Basis &p_basis); |
| 186 | void set_global_rotation(const Vector3 &p_euler_rad); |
| 187 | void set_global_rotation_degrees(const Vector3 &p_euler_degrees); |
| 188 | |
| 189 | Vector3 get_position() const; |
| 190 | |
| 191 | EulerOrder get_rotation_order() const; |
| 192 | Vector3 get_rotation() const; |
| 193 | Vector3 get_rotation_degrees() const; |
| 194 | Vector3 get_scale() const; |
| 195 | |
| 196 | Vector3 get_global_position() const; |
| 197 | Basis get_global_basis() const; |
| 198 | Vector3 get_global_rotation() const; |
| 199 | Vector3 get_global_rotation_degrees() const; |
| 200 | |
| 201 | void set_transform(const Transform3D &p_transform); |
| 202 | void set_basis(const Basis &p_basis); |
| 203 | void set_quaternion(const Quaternion &p_quaternion); |
| 204 | void set_global_transform(const Transform3D &p_transform); |
| 205 | |
| 206 | Transform3D get_transform() const; |
| 207 | Basis get_basis() const; |
| 208 | Quaternion get_quaternion() const; |
| 209 | Transform3D get_global_transform() const; |
| 210 | |
| 211 | #ifdef TOOLS_ENABLED |
| 212 | virtual Transform3D get_global_gizmo_transform() const; |
| 213 | virtual Transform3D get_local_gizmo_transform() const; |
| 214 | virtual void set_transform_gizmo_visible(bool p_enabled) { data.transform_gizmo_visible = p_enabled; }; |
| 215 | virtual bool is_transform_gizmo_visible() const { return data.transform_gizmo_visible; }; |
| 216 | #endif |
| 217 | virtual void reparent(Node *p_parent, bool p_keep_global_transform = true) override; |
| 218 | |
| 219 | void set_disable_gizmos(bool p_enabled); |
| 220 | void update_gizmos(); |
| 221 | void set_subgizmo_selection(Ref<Node3DGizmo> p_gizmo, int p_id, Transform3D p_transform = Transform3D()); |
| 222 | void clear_subgizmo_selection(); |
| 223 | Vector<Ref<Node3DGizmo>> get_gizmos() const; |
| 224 | TypedArray<Node3DGizmo> get_gizmos_bind() const; |
| 225 | void add_gizmo(Ref<Node3DGizmo> p_gizmo); |
| 226 | void remove_gizmo(Ref<Node3DGizmo> p_gizmo); |
| 227 | void clear_gizmos(); |
| 228 | |
| 229 | void set_as_top_level(bool p_enabled); |
| 230 | bool is_set_as_top_level() const; |
| 231 | |
| 232 | void set_disable_scale(bool p_enabled); |
| 233 | bool is_scale_disabled() const; |
| 234 | |
| 235 | _FORCE_INLINE_ bool is_inside_world() const { return data.inside_world; } |
| 236 | |
| 237 | Transform3D get_relative_transform(const Node *p_parent) const; |
| 238 | |
| 239 | void rotate(const Vector3 &p_axis, real_t p_angle); |
| 240 | void rotate_x(real_t p_angle); |
| 241 | void rotate_y(real_t p_angle); |
| 242 | void rotate_z(real_t p_angle); |
| 243 | void translate(const Vector3 &p_offset); |
| 244 | void scale(const Vector3 &p_ratio); |
| 245 | |
| 246 | void rotate_object_local(const Vector3 &p_axis, real_t p_angle); |
| 247 | void scale_object_local(const Vector3 &p_scale); |
| 248 | void translate_object_local(const Vector3 &p_offset); |
| 249 | |
| 250 | void global_rotate(const Vector3 &p_axis, real_t p_angle); |
| 251 | void global_scale(const Vector3 &p_scale); |
| 252 | void global_translate(const Vector3 &p_offset); |
| 253 | |
| 254 | void look_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0), bool p_use_model_front = false); |
| 255 | void look_at_from_position(const Vector3 &p_pos, const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0), bool p_use_model_front = false); |
| 256 | |
| 257 | Vector3 to_local(Vector3 p_global) const; |
| 258 | Vector3 to_global(Vector3 p_local) const; |
| 259 | |
| 260 | void set_notify_transform(bool p_enabled); |
| 261 | bool is_transform_notification_enabled() const; |
| 262 | |
| 263 | void set_notify_local_transform(bool p_enabled); |
| 264 | bool is_local_transform_notification_enabled() const; |
| 265 | |
| 266 | void orthonormalize(); |
| 267 | void set_identity(); |
| 268 | |
| 269 | void set_visible(bool p_visible); |
| 270 | void show(); |
| 271 | void hide(); |
| 272 | bool is_visible() const; |
| 273 | bool is_visible_in_tree() const; |
| 274 | |
| 275 | void force_update_transform(); |
| 276 | |
| 277 | void set_visibility_parent(const NodePath &p_path); |
| 278 | NodePath get_visibility_parent() const; |
| 279 | |
| 280 | Node3D(); |
| 281 | }; |
| 282 | |
| 283 | VARIANT_ENUM_CAST(Node3D::RotationEditMode) |
| 284 | |
| 285 | #endif // NODE_3D_H |
| 286 | |