Skip to content

Commit 5ae2981

Browse files
committed
use bounds.encapsulate instead of averaging centers
1 parent f7dc37e commit 5ae2981

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -693,35 +693,29 @@ public static HashSet<GameObject> RemoveRedundantObjects(IEnumerable<UnityEngine
693693
}
694694

695695
/// <summary>
696-
/// Recursively go through the hierarchy, adding up the bounding box centers
697-
/// of all the children, return number of transforms recursed.
696+
/// Recursively go through the hierarchy, unioning the bounding box centers
697+
/// of all the children, to find the combined bounds.
698698
/// </summary>
699-
/// <returns>The number of GameObjects in hierarchy starting at t.</returns>
700699
/// <param name="t">Transform.</param>
701-
/// <param name="centerSum">Sum of all the bounding box centers in hierarchy.</param>
702-
private int GetBoundingBoxCentersSum(Transform t, ref Vector3 centerSum)
700+
/// <param name="boundsUnion">The Bounds that is the Union of all the bounds on this transform's hierarchy.</param>
701+
private void EncapsulateBounds(Transform t, ref Bounds boundsUnion)
703702
{
704703
var bounds = GetBounds (t);
705-
if (bounds.HasValue) {
706-
centerSum += bounds.Value.center;
707-
} else {
708-
centerSum += t.transform.position;
709-
}
704+
boundsUnion.Encapsulate (bounds);
710705

711-
int count = 1;
712706
foreach (Transform child in t) {
713-
count += GetBoundingBoxCentersSum (child, ref centerSum);
707+
EncapsulateBounds (child, ref boundsUnion);
714708
}
715-
return count;
716709
}
717710

718711
/// <summary>
719712
/// Gets the bounds of a transform.
720713
/// Looks first at the Renderer, then Mesh, then Collider.
714+
/// Default to a bounds with center transform.position and size zero.
721715
/// </summary>
722-
/// <returns>The bounds, or null if not found.</returns>
716+
/// <returns>The bounds.</returns>
723717
/// <param name="t">Transform.</param>
724-
private Bounds? GetBounds(Transform t)
718+
private Bounds GetBounds(Transform t)
725719
{
726720
var renderer = t.GetComponent<Renderer> ();
727721
if (renderer) {
@@ -735,7 +729,7 @@ private int GetBoundingBoxCentersSum(Transform t, ref Vector3 centerSum)
735729
if (collider) {
736730
return collider.bounds;
737731
}
738-
return null;
732+
return new Bounds(t.position, Vector3.zero);
739733
}
740734

741735
/// <summary>
@@ -745,12 +739,18 @@ private int GetBoundingBoxCentersSum(Transform t, ref Vector3 centerSum)
745739
/// <param name="gameObjects">Game objects.</param>
746740
private Vector3 FindCenter(IEnumerable<GameObject> gameObjects)
747741
{
748-
Vector3 average = Vector3.zero;
749-
int count = 0;
742+
Bounds bounds = new Bounds();
743+
// Assign the initial bounds to first GameObject's bounds
744+
// (if we initialize the bounds to 0, then 0 will be part of the bounds)
745+
foreach (var go in gameObjects) {
746+
var tempBounds = GetBounds (go.transform);
747+
bounds = new Bounds (tempBounds.center, tempBounds.size);
748+
break;
749+
}
750750
foreach (var go in gameObjects) {
751-
count += GetBoundingBoxCentersSum (go.transform, ref average);
751+
EncapsulateBounds (go.transform, ref bounds);
752752
}
753-
return average / count;
753+
return bounds.center;
754754
}
755755

756756
/// <summary>

0 commit comments

Comments
 (0)