BsEditorUtility.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "BsEditorUtility.h"
  2. #include "BsSceneObject.h"
  3. #include "BsCRenderable.h"
  4. namespace BansheeEngine
  5. {
  6. AABox EditorUtility::calculateBounds(const HSceneObject& object)
  7. {
  8. Vector<HSceneObject> objects = { object };
  9. return calculateBounds(objects);
  10. }
  11. AABox EditorUtility::calculateBounds(const Vector<HSceneObject>& objects)
  12. {
  13. if (objects.size() == 0)
  14. return AABox(Vector3::ZERO, Vector3::ZERO);
  15. AABox bounds = AABox(Vector3::INF, -Vector3::INF);
  16. bool gotOneMesh = false;
  17. for (auto& object : objects)
  18. {
  19. AABox meshBounds;
  20. if (calculateMeshBounds(object, meshBounds))
  21. {
  22. bounds.merge(meshBounds);
  23. gotOneMesh = true;
  24. }
  25. }
  26. if (!gotOneMesh)
  27. {
  28. for (auto& object : objects)
  29. {
  30. if (object.isDestroyed())
  31. continue;
  32. bounds.merge(object->getWorldPosition());
  33. gotOneMesh = true;
  34. }
  35. }
  36. if(gotOneMesh)
  37. return bounds;
  38. return AABox(Vector3::ZERO, Vector3::ZERO);
  39. }
  40. bool EditorUtility::calculateMeshBounds(const HSceneObject& object, AABox& bounds)
  41. {
  42. bounds = AABox(Vector3::ZERO, Vector3::ZERO);
  43. if (object.isDestroyed())
  44. return false;
  45. bool foundOne = false;
  46. const Vector<HComponent>& components = object->getComponents();
  47. for (auto& component : components)
  48. {
  49. Bounds curBounds;
  50. if (component->calculateBounds(curBounds))
  51. {
  52. if (!foundOne)
  53. {
  54. bounds = curBounds.getBox();
  55. foundOne = true;
  56. }
  57. else
  58. bounds.merge(curBounds.getBox());
  59. }
  60. else
  61. {
  62. if (!foundOne)
  63. bounds = curBounds.getBox();
  64. }
  65. }
  66. return foundOne;
  67. }
  68. }