spatial.h 8.3 KB

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