BsEditorUtility.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. bool foundOne = false;
  37. const Vector<HComponent>& components = object->getComponents();
  38. for (auto& component : components)
  39. {
  40. Bounds curBounds;
  41. if (component->calculateBounds(curBounds))
  42. {
  43. if (!foundOne)
  44. {
  45. bounds = curBounds.getBox();
  46. foundOne = true;
  47. }
  48. else
  49. bounds.merge(curBounds.getBox());
  50. }
  51. else
  52. {
  53. if (!foundOne)
  54. bounds = curBounds.getBox();
  55. }
  56. }
  57. return foundOne;
  58. }
  59. }