BsEditorUtility.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. Vector3 EditorUtility::calculateCenter(const Vector<HSceneObject>& objects)
  43. {
  44. if (objects.size() == 0)
  45. return Vector3::ZERO;
  46. Vector3 center = Vector3::ZERO;
  47. bool gotOneMesh = false;
  48. UINT32 count = 0;
  49. for (auto& object : objects)
  50. {
  51. AABox meshBounds;
  52. if (calculateMeshBounds(object, meshBounds))
  53. {
  54. count++;
  55. if (meshBounds.getSize() == Vector3::INF)
  56. center += object->getWorldPosition();
  57. else
  58. center += meshBounds.getCenter();
  59. gotOneMesh = true;
  60. }
  61. }
  62. if (!gotOneMesh)
  63. {
  64. for (auto& object : objects)
  65. {
  66. if (object.isDestroyed())
  67. continue;
  68. center += object->getWorldPosition();
  69. count++;
  70. gotOneMesh = true;
  71. }
  72. }
  73. if (gotOneMesh)
  74. return center / count;
  75. return Vector3::ZERO;
  76. }
  77. bool EditorUtility::calculateMeshBounds(const HSceneObject& object, AABox& bounds)
  78. {
  79. bounds = AABox(Vector3::ZERO, Vector3::ZERO);
  80. if (object.isDestroyed())
  81. return false;
  82. bool foundOne = false;
  83. const Vector<HComponent>& components = object->getComponents();
  84. for (auto& component : components)
  85. {
  86. Bounds curBounds;
  87. if (component->calculateBounds(curBounds))
  88. {
  89. if (!foundOne)
  90. {
  91. bounds = curBounds.getBox();
  92. foundOne = true;
  93. }
  94. else
  95. bounds.merge(curBounds.getBox());
  96. }
  97. else
  98. {
  99. if (!foundOne)
  100. bounds = curBounds.getBox();
  101. }
  102. }
  103. return foundOne;
  104. }
  105. }