Debian Codegraph @

scene/3d/node_3d.h

raw symbols SCIP
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 #pragma once
32
33 #include "scene/main/node.h"
34 #include "scene/resources/3d/world_3d.h"
35
36 class Node3DGizmo : public RefCounted {
37 GDCLASS(Node3DGizmo, RefCounted);
38
39 public:
40 virtual void create() = 0;
41 virtual void transform() = 0;
42 virtual void clear() = 0;
43 virtual void redraw() = 0;
44 virtual void free() = 0;
45
46 Node3DGizmo();
47 virtual ~Node3DGizmo() {}
48 };
49
50 class Node3D : public Node {
51 GDCLASS(Node3D, Node);
52
53 friend class SceneTreeFTI;
54 friend class SceneTreeFTITests;
55
56 public:
57 static constexpr AncestralClass static_ancestral_class = AncestralClass::NODE_3D;
58
59 // Edit mode for the rotation.
60 // THIS MODE ONLY AFFECTS HOW DATA IS EDITED AND SAVED
61 // IT DOES _NOT_ AFFECT THE TRANSFORM LOGIC (see comment in TransformDirty).
62 enum RotationEditMode {
63 ROTATION_EDIT_MODE_EULER,
64 ROTATION_EDIT_MODE_QUATERNION,
65 ROTATION_EDIT_MODE_BASIS,
66 };
67
68 private:
69 // For the sake of ease of use, Node3D can operate with Transforms (Basis+Origin), Quaternion/Scale and Euler Rotation/Scale.
70 // 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).
71 // Euler needs to be kept separate because converting to Basis and back may result in a different vector (which is troublesome for users
72 // editing in the inspector, not only because of the numerical precision loss but because they expect these rotations to be consistent, or support
73 // "redundant" rotations for animation interpolation, like going from 0 to 720 degrees).
74 //
75 // 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.
76 // It will remain dirty until reading back is attempted (for performance reasons). Likewise, if the Euler rotation scale are set, the local transform
77 // will become dirty (and again, will not become valid again until read).
78 //
79 // All this is transparent from outside the Node3D API, which allows everything to works by calling these functions in exchange.
80 //
81 // Additionally, setting either transform, quaternion, Euler rotation or scale makes the global transform dirty, which will be updated when read again.
82 //
83 // NOTE: Again, RotationEditMode is _independent_ of this mechanism, it is only meant to expose the right set of properties for editing (editor) and saving
84 // (to scene, in order to keep the same values and avoid data loss on conversions). It has zero influence in the logic described above.
85 enum TransformDirty {
86 DIRTY_NONE = 0,
87 DIRTY_EULER_ROTATION_AND_SCALE = 1,
88 DIRTY_LOCAL_TRANSFORM = 2,
89 DIRTY_GLOBAL_TRANSFORM = 4,
90 DIRTY_GLOBAL_INTERPOLATED_TRANSFORM = 8,
91 };
92
93 struct ClientPhysicsInterpolationData {
94 Transform3D global_xform_curr;
95 Transform3D global_xform_prev;
96 uint64_t current_physics_tick = 0;
97 uint64_t timeout_physics_tick = 0;
98 };
99
100 mutable SelfList<Node> xform_change;
101 SelfList<Node3D> _client_physics_interpolation_node_3d_list;
102
103 // This Data struct is to avoid namespace pollution in derived classes.
104
105 struct Data {
106 // Interpolated global transform - correct on the frame only.
107 // Only used with FTI.
108 Transform3D global_transform_interpolated;
109
110 // Current xforms are either
111 // * Used for everything (when not using FTI)
112 // * Correct on the physics tick (when using FTI)
113 mutable Transform3D global_transform;
114 mutable Transform3D local_transform;
115
116 // Only used with FTI.
117 Transform3D local_transform_prev;
118
119 mutable EulerOrder euler_rotation_order = EulerOrder::YXZ;
120 mutable Vector3 euler_rotation;
121 mutable Vector3 scale = Vector3(1, 1, 1);
122 mutable RotationEditMode rotation_edit_mode = ROTATION_EDIT_MODE_EULER;
123
124 mutable MTNumeric<uint32_t> dirty;
125
126 Viewport *viewport = nullptr;
127
128 bool top_level : 1;
129 bool inside_world : 1;
130
131 // This is cached, and only currently kept up to date in visual instances.
132 // This is set if a visual instance is (a) in the tree AND (b) visible via is_visible_in_tree() call.
133 bool vi_visible : 1;
134
135 bool ignore_notification : 1;
136 bool notify_local_transform : 1;
137 bool notify_transform : 1;
138
139 bool visible : 1;
140 bool disable_scale : 1;
141
142 // Scene tree interpolation.
143 bool fti_on_frame_xform_list : 1;
144 bool fti_on_frame_property_list : 1;
145 bool fti_on_tick_xform_list : 1;
146 bool fti_on_tick_property_list : 1;
147 bool fti_global_xform_interp_set : 1;
148 bool fti_frame_xform_force_update : 1;
149 bool fti_is_identity_xform : 1;
150 bool fti_processed : 1;
151
152 RID visibility_parent;
153
154 Node3D *parent = nullptr;
155
156 // An unordered vector of `Spatial` children only.
157 // This is a subset of the `Node::children`, purely
158 // an optimization for faster traversal.
159 LocalVector<Node3D *> node3d_children;
160 uint32_t index_in_parent = UINT32_MAX;
161
162 ClientPhysicsInterpolationData *client_physics_interpolation_data = nullptr;
163
164 #ifdef TOOLS_ENABLED
165 Vector<Ref<Node3DGizmo>> gizmos;
166 bool gizmos_requested : 1;
167 bool gizmos_disabled : 1;
168 bool gizmos_dirty : 1;
169 bool transform_gizmo_visible : 1;
170 #endif
171
172 } data;
173
174 NodePath visibility_parent_path;
175
176 _FORCE_INLINE_ uint32_t _read_dirty_mask() const { return is_group_processing() ? data.dirty.mt.get() : data.dirty.st; }
177 _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); }
178 void _replace_dirty_mask(uint32_t p_mask) const;
179 void _set_dirty_bits(uint32_t p_bits) const;
180 void _clear_dirty_bits(uint32_t p_bits) const;
181
182 void _update_gizmos();
183 void _notify_dirty();
184 void _propagate_transform_changed(Node3D *p_origin);
185
186 void _propagate_visibility_changed();
187
188 void _propagate_visibility_parent();
189 void _update_visibility_parent(bool p_update_root);
190 void _propagate_transform_changed_deferred();
191
192 protected:
193 _FORCE_INLINE_ void set_ignore_transform_notification(bool p_ignore) { data.ignore_notification = p_ignore; }
194
195 _FORCE_INLINE_ void _update_local_transform() const;
196 _FORCE_INLINE_ void _update_rotation_and_scale() const;
197
198 void _set_vi_visible(bool p_visible) { data.vi_visible = p_visible; }
199 bool _is_vi_visible() const { return data.vi_visible; }
200 Transform3D _get_global_transform_interpolated(real_t p_interpolation_fraction);
201 const Transform3D &_get_cached_global_transform_interpolated() const { return data.global_transform_interpolated; }
202 void _disable_client_physics_interpolation();
203
204 // Calling this announces to the FTI system that a node has been moved,
205 // or requires an update in terms of interpolation
206 // (e.g. changing Camera zoom even if position hasn't changed).
207 void fti_notify_node_changed(bool p_transform_changed = true);
208
209 // Opportunity after FTI to update the servers
210 // with global_transform_interpolated,
211 // and any custom interpolated data in derived classes.
212 // Make sure to call the parent class fti_update_servers(),
213 // so all data is updated to the servers.
214 virtual void fti_update_servers_xform() {}
215 virtual void fti_update_servers_property() {}
216
217 // Pump the FTI data, also gives a chance for inherited classes
218 // to pump custom data, but they *must* call the base class here too.
219 // This is the opportunity for classes to move current values for
220 // transforms and properties to stored previous values,
221 // and this should take place both on ticks, and during resets.
222 virtual void fti_pump_xform();
223 virtual void fti_pump_property() {}
224
225 void _notification(int p_what);
226 static void _bind_methods();
227
228 void _validate_property(PropertyInfo &p_property) const;
229
230 bool _property_can_revert(const StringName &p_name) const;
231 bool _property_get_revert(const StringName &p_name, Variant &r_property) const;
232
233 public:
234 enum {
235 NOTIFICATION_TRANSFORM_CHANGED = SceneTree::NOTIFICATION_TRANSFORM_CHANGED,
236 NOTIFICATION_ENTER_WORLD = 41,
237 NOTIFICATION_EXIT_WORLD = 42,
238 NOTIFICATION_VISIBILITY_CHANGED = 43,
239 NOTIFICATION_LOCAL_TRANSFORM_CHANGED = 44,
240 };
241
242 Node3D *get_parent_node_3d() const;
243
244 Ref<World3D> get_world_3d() const;
245
246 void set_position(const Vector3 &p_position);
247
248 void set_rotation_edit_mode(RotationEditMode p_mode);
249 RotationEditMode get_rotation_edit_mode() const;
250
251 void set_rotation_order(EulerOrder p_order);
252 void set_rotation(const Vector3 &p_euler_rad);
253 void set_rotation_degrees(const Vector3 &p_euler_degrees);
254 void set_scale(const Vector3 &p_scale);
255
256 void set_global_position(const Vector3 &p_position);
257 void set_global_basis(const Basis &p_basis);
258 void set_global_rotation(const Vector3 &p_euler_rad);
259 void set_global_rotation_degrees(const Vector3 &p_euler_degrees);
260
261 Vector3 get_position() const;
262
263 EulerOrder get_rotation_order() const;
264 Vector3 get_rotation() const;
265 Vector3 get_rotation_degrees() const;
266 Vector3 get_scale() const;
267
268 Vector3 get_global_position() const;
269 Basis get_global_basis() const;
270 Vector3 get_global_rotation() const;
271 Vector3 get_global_rotation_degrees() const;
272
273 void set_transform(const Transform3D &p_transform);
274 void set_basis(const Basis &p_basis);
275 void set_quaternion(const Quaternion &p_quaternion);
276 void set_global_transform(const Transform3D &p_transform);
277
278 Transform3D get_transform() const;
279 Basis get_basis() const;
280 Quaternion get_quaternion() const;
281 Transform3D get_global_transform() const;
282
283 Transform3D get_global_transform_interpolated();
284 bool update_client_physics_interpolation_data();
285
286 #ifdef TOOLS_ENABLED
287 virtual Transform3D get_global_gizmo_transform() const;
288 virtual Transform3D get_local_gizmo_transform() const;
289 virtual void set_transform_gizmo_visible(bool p_enabled) { data.transform_gizmo_visible = p_enabled; }
290 virtual bool is_transform_gizmo_visible() const { return data.transform_gizmo_visible; }
291 #endif
292 virtual void reparent(RequiredParam<Node> p_parent, bool p_keep_global_transform = true) override;
293
294 void set_disable_gizmos(bool p_enabled);
295 void update_gizmos();
296 void set_subgizmo_selection(Ref<Node3DGizmo> p_gizmo, int p_id, Transform3D p_transform = Transform3D());
297 void clear_subgizmo_selection();
298 Vector<Ref<Node3DGizmo>> get_gizmos() const;
299 TypedArray<Node3DGizmo> get_gizmos_bind() const;
300 void add_gizmo(Ref<Node3DGizmo> p_gizmo);
301 void remove_gizmo(Ref<Node3DGizmo> p_gizmo);
302 void clear_gizmos();
303
304 void set_as_top_level(bool p_enabled);
305 void set_as_top_level_keep_local(bool p_enabled);
306 bool is_set_as_top_level() const;
307
308 void set_disable_scale(bool p_enabled);
309 bool is_scale_disabled() const;
310
311 _FORCE_INLINE_ bool is_inside_world() const { return data.inside_world; }
312
313 Transform3D get_relative_transform(const Node *p_parent) const;
314
315 void rotate(const Vector3 &p_axis, real_t p_angle);
316 void rotate_x(real_t p_angle);
317 void rotate_y(real_t p_angle);
318 void rotate_z(real_t p_angle);
319 void translate(const Vector3 &p_offset);
320 void scale(const Vector3 &p_ratio);
321
322 void rotate_object_local(const Vector3 &p_axis, real_t p_angle);
323 void scale_object_local(const Vector3 &p_scale);
324 void translate_object_local(const Vector3 &p_offset);
325
326 void global_rotate(const Vector3 &p_axis, real_t p_angle);
327 void global_scale(const Vector3 &p_scale);
328 void global_translate(const Vector3 &p_offset);
329
330 void look_at(const Vector3 &p_target, const Vector3 &p_up = Vector3::UP, bool p_use_model_front = false);
331 void look_at_from_position(const Vector3 &p_pos, const Vector3 &p_target, const Vector3 &p_up = Vector3::UP, bool p_use_model_front = false);
332
333 Vector3 to_local(Vector3 p_global) const;
334 Vector3 to_global(Vector3 p_local) const;
335
336 void set_notify_transform(bool p_enabled);
337 bool is_transform_notification_enabled() const;
338
339 void set_notify_local_transform(bool p_enabled);
340 bool is_local_transform_notification_enabled() const;
341
342 void orthonormalize();
343 void set_identity();
344
345 void set_visible(bool p_visible);
346 void show();
347 void hide();
348 bool is_visible() const;
349 bool is_visible_in_tree() const;
350
351 void force_update_transform();
352
353 void set_visibility_parent(const NodePath &p_path);
354 NodePath get_visibility_parent() const;
355
356 Node3D();
357 ~Node3D();
358 };
359
360 VARIANT_ENUM_CAST(Node3D::RotationEditMode)