BsEditorUtility.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "BsEditorUtility.h"
  2. #include "BsSceneObject.h"
  3. #include "BsRenderable.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. if (object->hasComponent<Renderable>())
  36. {
  37. HRenderable renderable = object->getComponent<Renderable>();
  38. bounds = renderable->getBounds().getBox();
  39. return true;
  40. }
  41. return false;
  42. }
  43. }