node_3d.h 14 KB

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