SpatialComponent.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. getExternalSubsystems(*node).m_gpuSceneMemoryPool->allocate(sizeof(Vec3) * 2, alignof(F32), m_gpuSceneAabb);
  22. }
  23. SpatialComponent::~SpatialComponent()
  24. {
  25. if(m_placed)
  26. {
  27. m_node->getSceneGraph().getOctree().remove(m_octreeInfo);
  28. }
  29. m_convexHullPoints.destroy(m_node->getMemoryPool());
  30. getExternalSubsystems(*m_node).m_gpuSceneMemoryPool->free(m_gpuSceneAabb);
  31. }
  32. void SpatialComponent::setConvexHullWorldSpace(const ConvexHullShape& hull)
  33. {
  34. ANKI_ASSERT(hull.getPoints().getSize() > 0);
  35. if(m_convexHullPoints.getSize() != hull.getPoints().getSize())
  36. {
  37. m_convexHullPoints.resize(m_node->getMemoryPool(), hull.getPoints().getSize());
  38. }
  39. memcpy(&m_convexHullPoints[0], &hull.getPoints()[0], hull.getPoints().getSizeInBytes());
  40. m_hull = ConvexHullShape(&m_convexHullPoints[0], m_convexHullPoints.getSize());
  41. if(!hull.isTransformIdentity())
  42. {
  43. m_hull.setTransform(hull.getTransform());
  44. }
  45. m_collisionObjectType = hull.kClassType;
  46. m_markedForUpdate = true;
  47. }
  48. Error SpatialComponent::update([[maybe_unused]] SceneComponentUpdateInfo& info, Bool& updated)
  49. {
  50. ANKI_ASSERT(info.m_node == m_node);
  51. updated = m_markedForUpdate;
  52. if(updated)
  53. {
  54. if(!m_alwaysVisible)
  55. {
  56. // Compute the AABB
  57. switch(m_collisionObjectType)
  58. {
  59. case CollisionShapeType::kAABB:
  60. m_derivedAabb = m_aabb;
  61. break;
  62. case CollisionShapeType::kOBB:
  63. m_derivedAabb = computeAabb(m_obb);
  64. break;
  65. case CollisionShapeType::kSphere:
  66. m_derivedAabb = computeAabb(m_sphere);
  67. break;
  68. case CollisionShapeType::kConvexHull:
  69. m_derivedAabb = computeAabb(m_hull);
  70. break;
  71. default:
  72. ANKI_ASSERT(0);
  73. }
  74. m_node->getSceneGraph().getOctree().place(m_derivedAabb, &m_octreeInfo, m_updateOctreeBounds);
  75. }
  76. else
  77. {
  78. m_node->getSceneGraph().getOctree().placeAlwaysVisible(&m_octreeInfo);
  79. }
  80. m_markedForUpdate = false;
  81. m_placed = true;
  82. }
  83. m_octreeInfo.reset();
  84. return Error::kNone;
  85. }
  86. } // end namespace anki