BsEditorUtility.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. bounds.merge(object->getWorldPosition());
  30. }
  31. return bounds;
  32. }
  33. bool EditorUtility::calculateMeshBounds(const HSceneObject& object, AABox& bounds)
  34. {
  35. bounds = AABox(Vector3::ZERO, Vector3::ZERO);
  36. if (object.isDestroyed())
  37. return false;
  38. bool foundOne = false;
  39. const Vector<HComponent>& components = object->getComponents();
  40. for (auto& component : components)
  41. {
  42. Bounds curBounds;
  43. if (component->calculateBounds(curBounds))
  44. {
  45. if (!foundOne)
  46. {
  47. bounds = curBounds.getBox();
  48. foundOne = true;
  49. }
  50. else
  51. bounds.merge(curBounds.getBox());
  52. }
  53. else
  54. {
  55. if (!foundOne)
  56. bounds = curBounds.getBox();
  57. }
  58. }
  59. return foundOne;
  60. }
  61. }