visibility_notifier.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**************************************************************************/
  2. /* visibility_notifier.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 VISIBILITY_NOTIFIER_H
  31. #define VISIBILITY_NOTIFIER_H
  32. #include "scene/3d/cull_instance.h"
  33. class World;
  34. class Camera;
  35. class VisibilityNotifier : public CullInstance {
  36. GDCLASS(VisibilityNotifier, CullInstance);
  37. Ref<World> world;
  38. Set<Camera *> cameras;
  39. AABB aabb;
  40. Vector3 _world_aabb_center;
  41. // if using rooms and portals
  42. RID _cull_instance_rid;
  43. bool _in_gameplay;
  44. bool _max_distance_active;
  45. real_t _max_distance;
  46. real_t _max_distance_squared;
  47. // This is a first number of frames where distance objects
  48. // are forced seen as visible, to make sure their animations
  49. // and physics positions etc are something reasonable.
  50. uint32_t _max_distance_leadin_counter;
  51. protected:
  52. virtual void _screen_enter() {}
  53. virtual void _screen_exit() {}
  54. virtual void _refresh_portal_mode();
  55. void _notification(int p_what);
  56. static void _bind_methods();
  57. friend struct SpatialIndexer;
  58. void _enter_camera(Camera *p_camera);
  59. void _exit_camera(Camera *p_camera);
  60. public:
  61. void set_aabb(const AABB &p_aabb);
  62. AABB get_aabb() const;
  63. bool is_on_screen() const;
  64. // This is only currently kept up to date if max_distance is active
  65. const Vector3 &get_world_aabb_center() const { return _world_aabb_center; }
  66. void set_max_distance(real_t p_max_distance);
  67. real_t get_max_distance() const { return _max_distance; }
  68. real_t get_max_distance_squared() const { return _max_distance_squared; }
  69. bool is_max_distance_active() const { return _max_distance_active; }
  70. bool inside_max_distance_leadin() {
  71. if (!_max_distance_leadin_counter) {
  72. return false;
  73. }
  74. _max_distance_leadin_counter--;
  75. return true;
  76. }
  77. VisibilityNotifier();
  78. ~VisibilityNotifier();
  79. };
  80. class VisibilityEnabler : public VisibilityNotifier {
  81. GDCLASS(VisibilityEnabler, VisibilityNotifier);
  82. public:
  83. enum Enabler {
  84. ENABLER_PAUSE_ANIMATIONS,
  85. ENABLER_FREEZE_BODIES,
  86. ENABLER_MAX
  87. };
  88. protected:
  89. virtual void _screen_enter();
  90. virtual void _screen_exit();
  91. bool visible;
  92. void _find_nodes(Node *p_node);
  93. Map<Node *, Variant> nodes;
  94. void _node_removed(Node *p_node);
  95. bool enabler[ENABLER_MAX];
  96. void _change_node_state(Node *p_node, bool p_enabled);
  97. void _notification(int p_what);
  98. static void _bind_methods();
  99. public:
  100. void set_enabler(Enabler p_enabler, bool p_enable);
  101. bool is_enabler_enabled(Enabler p_enabler) const;
  102. VisibilityEnabler();
  103. };
  104. VARIANT_ENUM_CAST(VisibilityEnabler::Enabler);
  105. #endif // VISIBILITY_NOTIFIER_H