SpatialComponent.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Scene/Components/SpatialComponent.h>
  6. #include <AnKi/Scene/SceneNode.h>
  7. #include <AnKi/Scene/SceneGraph.h>
  8. namespace anki {
  9. ANKI_SCENE_COMPONENT_STATICS(SpatialComponent)
  10. SpatialComponent::SpatialComponent(SceneNode* node)
  11. : SceneComponent(node, getStaticClassId())
  12. , m_node(node)
  13. , m_markedForUpdate(true)
  14. , m_placed(false)
  15. , m_updateOctreeBounds(true)
  16. , m_alwaysVisible(false)
  17. {
  18. ANKI_ASSERT(node);
  19. m_octreeInfo.m_userData = this;
  20. setAabbWorldSpace(Aabb(Vec3(-1.0f), Vec3(1.0f)));
  21. }
  22. SpatialComponent::~SpatialComponent()
  23. {
  24. if(m_placed)
  25. {
  26. m_node->getSceneGraph().getOctree().remove(m_octreeInfo);
  27. }
  28. m_convexHullPoints.destroy(m_node->getAllocator());
  29. }
  30. void SpatialComponent::setConvexHullWorldSpace(const ConvexHullShape& hull)
  31. {
  32. ANKI_ASSERT(hull.getPoints().getSize() > 0);
  33. if(m_convexHullPoints.getSize() != hull.getPoints().getSize())
  34. {
  35. m_convexHullPoints.resize(m_node->getAllocator(), hull.getPoints().getSize());
  36. }
  37. memcpy(&m_convexHullPoints[0], &hull.getPoints()[0], hull.getPoints().getSizeInBytes());
  38. m_hull = ConvexHullShape(&m_convexHullPoints[0], m_convexHullPoints.getSize());
  39. if(!hull.isTransformIdentity())
  40. {
  41. m_hull.setTransform(hull.getTransform());
  42. }
  43. m_collisionObjectType = hull.kClassType;
  44. m_markedForUpdate = true;
  45. }
  46. Error SpatialComponent::update([[maybe_unused]] SceneComponentUpdateInfo& info, Bool& updated)
  47. {
  48. ANKI_ASSERT(info.m_node == m_node);
  49. updated = m_markedForUpdate;
  50. if(updated)
  51. {
  52. if(!m_alwaysVisible)
  53. {
  54. // Compute the AABB
  55. switch(m_collisionObjectType)
  56. {
  57. case CollisionShapeType::kAABB:
  58. m_derivedAabb = m_aabb;
  59. break;
  60. case CollisionShapeType::kOBB:
  61. m_derivedAabb = computeAabb(m_obb);
  62. break;
  63. case CollisionShapeType::kSphere:
  64. m_derivedAabb = computeAabb(m_sphere);
  65. break;
  66. case CollisionShapeType::kConvexHull:
  67. m_derivedAabb = computeAabb(m_hull);
  68. break;
  69. default:
  70. ANKI_ASSERT(0);
  71. }
  72. m_node->getSceneGraph().getOctree().place(m_derivedAabb, &m_octreeInfo, m_updateOctreeBounds);
  73. }
  74. else
  75. {
  76. m_node->getSceneGraph().getOctree().placeAlwaysVisible(&m_octreeInfo);
  77. }
  78. m_markedForUpdate = false;
  79. m_placed = true;
  80. }
  81. m_octreeInfo.reset();
  82. return Error::kNone;
  83. }
  84. } // end namespace anki