spatial.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /**************************************************************************/
  2. /* spatial.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. #ifndef SPATIAL_H
  31. #define SPATIAL_H
  32. #include "scene/main/node.h"
  33. #include "scene/main/scene_tree.h"
  34. class SpatialGizmo : public Reference {
  35. GDCLASS(SpatialGizmo, Reference);
  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. SpatialGizmo();
  43. virtual ~SpatialGizmo() {}
  44. };
  45. class Spatial : public Node {
  46. GDCLASS(Spatial, Node);
  47. OBJ_CATEGORY("3D");
  48. // optionally stored if we need to do interpolation
  49. // client side (i.e. not in VisualServer) so interpolated transforms
  50. // can be read back with get_global_transform_interpolated()
  51. struct ClientPhysicsInterpolationData {
  52. Transform global_xform_curr;
  53. Transform global_xform_prev;
  54. uint64_t current_physics_tick = 0;
  55. uint64_t timeout_physics_tick = 0;
  56. };
  57. enum TransformDirty {
  58. DIRTY_NONE = 0,
  59. DIRTY_VECTORS = 1,
  60. DIRTY_LOCAL = 2,
  61. DIRTY_GLOBAL = 4
  62. };
  63. mutable SelfList<Node> xform_change;
  64. SelfList<Spatial> _client_physics_interpolation_spatials_list;
  65. struct Data {
  66. mutable Transform global_transform;
  67. mutable Transform local_transform;
  68. mutable Vector3 rotation;
  69. mutable Vector3 scale;
  70. mutable int dirty;
  71. Viewport *viewport;
  72. bool toplevel_active : 1;
  73. bool toplevel : 1;
  74. bool inside_world : 1;
  75. // this is cached, and only currently kept up to date in visual instances
  76. // this is set if a visual instance is
  77. // (a) in the tree AND (b) visible via is_visible_in_tree() call
  78. bool vi_visible : 1;
  79. bool ignore_notification : 1;
  80. bool notify_local_transform : 1;
  81. bool notify_transform : 1;
  82. bool visible : 1;
  83. bool disable_scale : 1;
  84. int children_lock;
  85. Spatial *parent;
  86. List<Spatial *> children;
  87. List<Spatial *>::Element *C;
  88. ClientPhysicsInterpolationData *client_physics_interpolation_data;
  89. #ifdef TOOLS_ENABLED
  90. Ref<SpatialGizmo> gizmo;
  91. bool gizmo_disabled : 1;
  92. bool gizmo_dirty : 1;
  93. #endif
  94. } data;
  95. void _update_gizmo();
  96. void _notify_dirty();
  97. void _propagate_transform_changed(Spatial *p_origin);
  98. void _propagate_visibility_changed();
  99. protected:
  100. _FORCE_INLINE_ void set_ignore_transform_notification(bool p_ignore) { data.ignore_notification = p_ignore; }
  101. _FORCE_INLINE_ void _update_local_transform() const;
  102. void _set_vi_visible(bool p_visible);
  103. bool _is_vi_visible() const { return data.vi_visible; }
  104. Transform _get_global_transform_interpolated(real_t p_interpolation_fraction);
  105. void _disable_client_physics_interpolation();
  106. void _notification(int p_what);
  107. static void _bind_methods();
  108. public:
  109. enum {
  110. NOTIFICATION_TRANSFORM_CHANGED = SceneTree::NOTIFICATION_TRANSFORM_CHANGED,
  111. NOTIFICATION_ENTER_WORLD = 41,
  112. NOTIFICATION_EXIT_WORLD = 42,
  113. NOTIFICATION_VISIBILITY_CHANGED = 43,
  114. NOTIFICATION_LOCAL_TRANSFORM_CHANGED = 44,
  115. NOTIFICATION_ENTER_GAMEPLAY = 45,
  116. NOTIFICATION_EXIT_GAMEPLAY = 46,
  117. };
  118. virtual void notification_callback(int p_message_type);
  119. Spatial *get_parent_spatial() const;
  120. Ref<World> get_world() const;
  121. void set_translation(const Vector3 &p_translation);
  122. void set_rotation(const Vector3 &p_euler_rad);
  123. void set_rotation_degrees(const Vector3 &p_euler_deg);
  124. void set_scale(const Vector3 &p_scale);
  125. void set_global_translation(const Vector3 &p_translation);
  126. void set_global_rotation(const Vector3 &p_euler_rad);
  127. Vector3 get_translation() const;
  128. Vector3 get_rotation() const;
  129. Vector3 get_rotation_degrees() const;
  130. Vector3 get_scale() const;
  131. Vector3 get_global_translation() const;
  132. Vector3 get_global_rotation() const;
  133. void set_transform(const Transform &p_transform);
  134. void set_global_transform(const Transform &p_transform);
  135. Transform get_transform() const;
  136. Transform get_global_transform() const;
  137. Transform get_global_transform_interpolated();
  138. bool update_client_physics_interpolation_data();
  139. #ifdef TOOLS_ENABLED
  140. virtual Transform get_global_gizmo_transform() const;
  141. virtual Transform get_local_gizmo_transform() const;
  142. virtual AABB get_fallback_gizmo_aabb() const;
  143. #endif
  144. void set_as_toplevel(bool p_enabled);
  145. bool is_set_as_toplevel() const;
  146. void set_disable_scale(bool p_enabled);
  147. bool is_scale_disabled() const;
  148. void set_disable_gizmo(bool p_enabled);
  149. void update_gizmo();
  150. void set_gizmo(const Ref<SpatialGizmo> &p_gizmo);
  151. Ref<SpatialGizmo> get_gizmo() const;
  152. _FORCE_INLINE_ bool is_inside_world() const { return data.inside_world; }
  153. Transform get_relative_transform(const Node *p_parent) const;
  154. void rotate(const Vector3 &p_axis, float p_angle);
  155. void rotate_x(float p_angle);
  156. void rotate_y(float p_angle);
  157. void rotate_z(float p_angle);
  158. void translate(const Vector3 &p_offset);
  159. void scale(const Vector3 &p_ratio);
  160. void rotate_object_local(const Vector3 &p_axis, float p_angle);
  161. void scale_object_local(const Vector3 &p_scale);
  162. void translate_object_local(const Vector3 &p_offset);
  163. void global_rotate(const Vector3 &p_axis, float p_angle);
  164. void global_scale(const Vector3 &p_scale);
  165. void global_translate(const Vector3 &p_offset);
  166. void look_at(const Vector3 &p_target, const Vector3 &p_up);
  167. void look_at_from_position(const Vector3 &p_pos, const Vector3 &p_target, const Vector3 &p_up);
  168. Vector3 to_local(Vector3 p_global) const;
  169. Vector3 to_global(Vector3 p_local) const;
  170. void set_notify_transform(bool p_enable);
  171. bool is_transform_notification_enabled() const;
  172. void set_notify_local_transform(bool p_enable);
  173. bool is_local_transform_notification_enabled() const;
  174. void orthonormalize();
  175. void set_identity();
  176. void set_visible(bool p_visible);
  177. bool is_visible() const;
  178. void show();
  179. void hide();
  180. bool is_visible_in_tree() const;
  181. void force_update_transform();
  182. Spatial();
  183. ~Spatial();
  184. };
  185. #endif // SPATIAL_H