BsEditorUtility.cpp 1.9 KB

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