Spatial.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Scene/Octree.h>
  7. #include <AnKi/Scene/Components/SceneComponent.h>
  8. #include <AnKi/Collision.h>
  9. namespace anki {
  10. /// @addtogroup scene
  11. /// @{
  12. /// A class that assists with visibility testing.
  13. class Spatial
  14. {
  15. public:
  16. Spatial(SceneComponent* owner)
  17. : m_owner(owner)
  18. , m_sceneComponentType(owner->getType())
  19. {
  20. ANKI_ASSERT(owner);
  21. m_octreeInfo.m_userData = this;
  22. }
  23. Spatial(const Spatial&) = delete;
  24. ~Spatial()
  25. {
  26. ANKI_ASSERT(!m_placed && "Forgot to call removeFromOctree");
  27. }
  28. Spatial& operator=(const Spatial&) = delete;
  29. const SceneComponent& getSceneComponent() const
  30. {
  31. return *m_owner;
  32. }
  33. SceneComponent& getSceneComponent()
  34. {
  35. return *m_owner;
  36. }
  37. SceneComponentType getSceneComponentClassId() const
  38. {
  39. return m_sceneComponentType;
  40. }
  41. const Aabb& getAabbWorldSpace() const
  42. {
  43. ANKI_ASSERT(!m_alwaysVisible || !m_dirty);
  44. return m_aabb;
  45. }
  46. /// Update the "actual scene bounds" of the octree or not.
  47. void setUpdatesOctreeBounds(Bool update)
  48. {
  49. m_updatesOctreeBounds = update;
  50. }
  51. /// Make it or not always visible.
  52. void setAlwaysVisible(Bool alwaysVisible)
  53. {
  54. m_alwaysVisible = alwaysVisible;
  55. }
  56. /// Check if it's always visible or not.
  57. Bool getAlwaysVisible() const
  58. {
  59. return m_alwaysVisible;
  60. }
  61. template<typename TCollisionShape>
  62. void setBoundingShape(const TCollisionShape& shape)
  63. {
  64. m_aabb = computeAabb(shape);
  65. m_dirty = true;
  66. }
  67. template<typename TVec>
  68. void setBoundingShape(ConstWeakArray<TVec> points)
  69. {
  70. ANKI_ASSERT(points.getSize() > 0);
  71. TVec min(kMaxF32), max(kMinF32);
  72. for(const TVec& point : points)
  73. {
  74. min = min.min(point);
  75. max = max.max(point);
  76. }
  77. m_aabb.setMin(min.xyz());
  78. m_aabb.setMax(max.xyz());
  79. m_dirty = true;
  80. }
  81. /// Should be called each frame.
  82. /// @return True if updated.
  83. Bool update(Octree& octree)
  84. {
  85. const Bool updated = m_dirty;
  86. if(m_dirty)
  87. {
  88. if(!m_alwaysVisible) [[likely]]
  89. {
  90. octree.place(m_aabb, &m_octreeInfo, m_updatesOctreeBounds);
  91. }
  92. else
  93. {
  94. octree.placeAlwaysVisible(&m_octreeInfo);
  95. }
  96. m_placed = true;
  97. m_dirty = false;
  98. }
  99. ANKI_ASSERT(m_placed);
  100. m_octreeInfo.reset();
  101. return updated;
  102. }
  103. void removeFromOctree(Octree& octree)
  104. {
  105. if(m_placed)
  106. {
  107. octree.remove(m_octreeInfo);
  108. m_placed = false;
  109. m_dirty = true;
  110. }
  111. }
  112. private:
  113. Aabb m_aabb = Aabb(Vec3(-1.0f), Vec3(1.0f)); ///< A faster shape.
  114. OctreePlaceable m_octreeInfo;
  115. SceneComponent* m_owner;
  116. SceneComponentType m_sceneComponentType; ///< Cache it.
  117. Bool m_placed : 1 = false;
  118. Bool m_updatesOctreeBounds : 1 = true;
  119. Bool m_alwaysVisible : 1 = false;
  120. Bool m_dirty : 1 = true;
  121. };
  122. template<>
  123. inline void Spatial::setBoundingShape<Aabb>(const Aabb& shape)
  124. {
  125. m_aabb = shape;
  126. m_dirty = true;
  127. }
  128. /// @}
  129. } // end namespace anki