node_3d.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*************************************************************************/
  2. /* node_3d.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #ifndef NODE_3D_H
  31. #define NODE_3D_H
  32. #include "scene/main/node.h"
  33. #include "scene/resources/world_3d.h"
  34. class Node3DGizmo : public RefCounted {
  35. GDCLASS(Node3DGizmo, RefCounted);
  36. public:
  37. virtual void create() = 0;
  38. virtual void transform() = 0;
  39. virtual void clear() = 0;
  40. virtual void redraw() = 0;
  41. virtual void free() = 0;
  42. Node3DGizmo();
  43. virtual ~Node3DGizmo() {}
  44. };
  45. class Node3D : public Node {
  46. GDCLASS(Node3D, Node);
  47. public:
  48. // Edit mode for the rotation.
  49. // THIS MODE ONLY AFFECTS HOW DATA IS EDITED AND SAVED
  50. // IT DOES _NOT_ AFFECT THE TRANSFORM LOGIC (see comment in TransformDirty).
  51. enum RotationEditMode {
  52. ROTATION_EDIT_MODE_EULER,
  53. ROTATION_EDIT_MODE_QUATERNION,
  54. ROTATION_EDIT_MODE_BASIS,
  55. };
  56. enum RotationOrder {
  57. ROTATION_ORDER_XYZ,
  58. ROTATION_ORDER_XZY,
  59. ROTATION_ORDER_YXZ,
  60. ROTATION_ORDER_YZX,
  61. ROTATION_ORDER_ZXY,
  62. ROTATION_ORDER_ZYX
  63. };
  64. private:
  65. // For the sake of ease of use, Node3D can operate with Transforms (Basis+Origin), Quaterinon/Scale and Euler Rotation/Scale.
  66. // Transform and Quaterinon 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. mutable SelfList<Node> xform_change;
  88. struct Data {
  89. mutable Transform3D global_transform;
  90. mutable Transform3D local_transform;
  91. mutable Basis::EulerOrder euler_rotation_order = Basis::EULER_ORDER_YXZ;
  92. mutable Vector3 euler_rotation;
  93. mutable Vector3 scale = Vector3(1, 1, 1);
  94. mutable RotationEditMode rotation_edit_mode = ROTATION_EDIT_MODE_EULER;
  95. mutable int dirty = DIRTY_NONE;
  96. Viewport *viewport = nullptr;
  97. bool top_level_active = false;
  98. bool top_level = false;
  99. bool inside_world = false;
  100. RID visibility_parent;
  101. int children_lock = 0;
  102. Node3D *parent = nullptr;
  103. List<Node3D *> children;
  104. List<Node3D *>::Element *C = nullptr;
  105. bool ignore_notification = false;
  106. bool notify_local_transform = false;
  107. bool notify_transform = false;
  108. bool visible = true;
  109. bool disable_scale = false;
  110. #ifdef TOOLS_ENABLED
  111. Vector<Ref<Node3DGizmo>> gizmos;
  112. bool gizmos_disabled = false;
  113. bool gizmos_dirty = false;
  114. bool transform_gizmo_visible = true;
  115. #endif
  116. } data;
  117. NodePath visibility_parent_path;
  118. void _update_gizmos();
  119. void _notify_dirty();
  120. void _propagate_transform_changed(Node3D *p_origin);
  121. void _propagate_visibility_changed();
  122. void _propagate_visibility_parent();
  123. void _update_visibility_parent(bool p_update_root);
  124. protected:
  125. _FORCE_INLINE_ void set_ignore_transform_notification(bool p_ignore) { data.ignore_notification = p_ignore; }
  126. _FORCE_INLINE_ void _update_local_transform() const;
  127. _FORCE_INLINE_ void _update_rotation_and_scale() const;
  128. void _notification(int p_what);
  129. static void _bind_methods();
  130. void _validate_property(PropertyInfo &p_property) const;
  131. bool _property_can_revert(const StringName &p_name) const;
  132. bool _property_get_revert(const StringName &p_name, Variant &r_property) const;
  133. public:
  134. enum {
  135. NOTIFICATION_TRANSFORM_CHANGED = SceneTree::NOTIFICATION_TRANSFORM_CHANGED,
  136. NOTIFICATION_ENTER_WORLD = 41,
  137. NOTIFICATION_EXIT_WORLD = 42,
  138. NOTIFICATION_VISIBILITY_CHANGED = 43,
  139. NOTIFICATION_LOCAL_TRANSFORM_CHANGED = 44,
  140. };
  141. Node3D *get_parent_node_3d() const;
  142. Ref<World3D> get_world_3d() const;
  143. void set_position(const Vector3 &p_position);
  144. void set_rotation_edit_mode(RotationEditMode p_mode);
  145. RotationEditMode get_rotation_edit_mode() const;
  146. void set_rotation_order(RotationOrder p_order);
  147. void set_rotation(const Vector3 &p_euler_rad);
  148. void set_scale(const Vector3 &p_scale);
  149. void set_global_position(const Vector3 &p_position);
  150. void set_global_rotation(const Vector3 &p_euler_rad);
  151. Vector3 get_position() const;
  152. RotationOrder get_rotation_order() const;
  153. Vector3 get_rotation() const;
  154. Vector3 get_scale() const;
  155. Vector3 get_global_position() const;
  156. Vector3 get_global_rotation() const;
  157. void set_transform(const Transform3D &p_transform);
  158. void set_basis(const Basis &p_basis);
  159. void set_quaternion(const Quaternion &p_quaternion);
  160. void set_global_transform(const Transform3D &p_transform);
  161. Transform3D get_transform() const;
  162. Basis get_basis() const;
  163. Quaternion get_quaternion() const;
  164. Transform3D get_global_transform() const;
  165. #ifdef TOOLS_ENABLED
  166. virtual Transform3D get_global_gizmo_transform() const;
  167. virtual Transform3D get_local_gizmo_transform() const;
  168. virtual void set_transform_gizmo_visible(bool p_enabled) { data.transform_gizmo_visible = p_enabled; };
  169. virtual bool is_transform_gizmo_visible() const { return data.transform_gizmo_visible; };
  170. #endif
  171. void set_disable_gizmos(bool p_enabled);
  172. void update_gizmos();
  173. void set_subgizmo_selection(Ref<Node3DGizmo> p_gizmo, int p_id, Transform3D p_transform = Transform3D());
  174. void clear_subgizmo_selection();
  175. Vector<Ref<Node3DGizmo>> get_gizmos() const;
  176. TypedArray<Node3DGizmo> get_gizmos_bind() const;
  177. void add_gizmo(Ref<Node3DGizmo> p_gizmo);
  178. void remove_gizmo(Ref<Node3DGizmo> p_gizmo);
  179. void clear_gizmos();
  180. void set_as_top_level(bool p_enabled);
  181. bool is_set_as_top_level() const;
  182. void set_disable_scale(bool p_enabled);
  183. bool is_scale_disabled() const;
  184. _FORCE_INLINE_ bool is_inside_world() const { return data.inside_world; }
  185. Transform3D get_relative_transform(const Node *p_parent) const;
  186. void rotate(const Vector3 &p_axis, real_t p_angle);
  187. void rotate_x(real_t p_angle);
  188. void rotate_y(real_t p_angle);
  189. void rotate_z(real_t p_angle);
  190. void translate(const Vector3 &p_offset);
  191. void scale(const Vector3 &p_ratio);
  192. void rotate_object_local(const Vector3 &p_axis, real_t p_angle);
  193. void scale_object_local(const Vector3 &p_scale);
  194. void translate_object_local(const Vector3 &p_offset);
  195. void global_rotate(const Vector3 &p_axis, real_t p_angle);
  196. void global_scale(const Vector3 &p_scale);
  197. void global_translate(const Vector3 &p_offset);
  198. void look_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0));
  199. void look_at_from_position(const Vector3 &p_pos, const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0));
  200. Vector3 to_local(Vector3 p_global) const;
  201. Vector3 to_global(Vector3 p_local) const;
  202. void set_notify_transform(bool p_enabled);
  203. bool is_transform_notification_enabled() const;
  204. void set_notify_local_transform(bool p_enabled);
  205. bool is_local_transform_notification_enabled() const;
  206. void orthonormalize();
  207. void set_identity();
  208. void set_visible(bool p_visible);
  209. void show();
  210. void hide();
  211. bool is_visible() const;
  212. bool is_visible_in_tree() const;
  213. void force_update_transform();
  214. void set_visibility_parent(const NodePath &p_path);
  215. NodePath get_visibility_parent() const;
  216. Node3D();
  217. };
  218. VARIANT_ENUM_CAST(Node3D::RotationEditMode)
  219. VARIANT_ENUM_CAST(Node3D::RotationOrder)
  220. #endif // NODE_3D_H