Skip to content

Commit

Permalink
Improvements to bounding box generation code. Should prevent the gian…
Browse files Browse the repository at this point in the history
…t bounding box issue.
  • Loading branch information
BobDoleOwndU committed Sep 24, 2020
1 parent 3c0e374 commit 42c1a4b
Showing 1 changed file with 41 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public static void GenerateBoundingBoxes(Transform transform)
break;
} //if
} //foreach

InitializeBoundingBoxes(root);

GetMeshes(transform, meshes);
SetBoundingBoxByMeshes(meshes);
VerifyBoundingBoxes(root);
Expand Down Expand Up @@ -58,21 +57,6 @@ public static void GenerateBoundingBoxes(Transform transform)
Debug.Log("Bounding boxes generated!");
} //GenerateBoundingBoxes

private static void InitializeBoundingBoxes(Transform transform)
{
foreach (Transform t in transform)
{
BoxCollider collider = t.gameObject.AddComponent<BoxCollider>();
Bounds bounds = new Bounds();
bounds.SetMinMax(new Vector3(-9999f, -9999f, -9999f), new Vector3(9999f, 9999f, 9999f));

collider.center = t.InverseTransformPoint(bounds.center);
collider.size = bounds.size;

InitializeBoundingBoxes(t);
} //foreach
} //InitializeBoundingBoxes

private static void GetMeshes(Transform transform, List<SkinnedMeshRenderer> meshes)
{
foreach (Transform t in transform)
Expand Down Expand Up @@ -109,22 +93,35 @@ private static void SetBoundingBoxByMeshes(List<SkinnedMeshRenderer> meshes)

private static void SetBoundingBoxByVertex(Transform bone, Vector3 vertex)
{
BoxCollider collider = bone.gameObject.GetComponent<BoxCollider>();
Vector3 max = collider.bounds.max;
Vector3 min = collider.bounds.min;

if (vertex.x > max.x || max.x == 9999f || float.IsNaN(max.x))
max.x = vertex.x;
if (vertex.y > max.y || max.y == 9999f || float.IsNaN(max.y))
max.y = vertex.y;
if (vertex.z > max.z || max.z == 9999f || float.IsNaN(max.z))
max.z = vertex.z;
if (vertex.x < min.x || min.x == -9999f || float.IsNaN(min.x))
min.x = vertex.x;
if (vertex.y < min.y || min.y == -9999f || float.IsNaN(min.y))
min.y = vertex.y;
if (vertex.z < min.z || min.z == -9999f || float.IsNaN(min.z))
min.z = vertex.z;
BoxCollider collider;
Vector3 max;
Vector3 min;

if (bone.gameObject.GetComponent<BoxCollider>())
{
collider = bone.gameObject.GetComponent<BoxCollider>();
max = collider.bounds.max;
min = collider.bounds.min;

if (vertex.x > max.x || float.IsNaN(max.x))
max.x = vertex.x;
if (vertex.y > max.y || float.IsNaN(max.y))
max.y = vertex.y;
if (vertex.z > max.z || float.IsNaN(max.z))
max.z = vertex.z;
if (vertex.x < min.x || float.IsNaN(min.x))
min.x = vertex.x;
if (vertex.y < min.y || float.IsNaN(min.y))
min.y = vertex.y;
if (vertex.z < min.z || float.IsNaN(min.z))
min.z = vertex.z;
} //if
else
{
collider = bone.gameObject.AddComponent<BoxCollider>();
max = new Vector3(vertex.x, vertex.y, vertex.z);
min = new Vector3(vertex.x, vertex.y, vertex.z);
} // else

Bounds bounds = new Bounds();
bounds.SetMinMax(min, max);
Expand All @@ -137,39 +134,17 @@ private static void VerifyBoundingBoxes(Transform transform)
{
foreach(Transform t in transform)
{
BoxCollider collider = t.GetComponent<BoxCollider>();
Bounds bounds = new Bounds();
Vector3 min = new Vector3();
Vector3 max = new Vector3();

if (collider.bounds.max.x == 9999f)
max.x = 0;
else
max.x = collider.bounds.max.x;
if (collider.bounds.max.y == 9999f)
max.y = 0;
else
max.y = collider.bounds.max.y;
if (collider.bounds.max.z == 9999f)
max.z = 0;
else
max.z = collider.bounds.max.z;
if (collider.bounds.min.x == -9999f)
min.x = 0;
else
min.x = collider.bounds.min.x;
if (collider.bounds.min.y == -9999f)
min.y = 0;
else
min.y = collider.bounds.min.y;
if (collider.bounds.min.z == -9999f)
min.z = 0;
else
min.z = collider.bounds.min.z;

bounds.SetMinMax(min, max);
collider.center = t.InverseTransformPoint(bounds.center);
collider.size = bounds.size;
if (!t.GetComponent<BoxCollider>())
{
BoxCollider collider = t.gameObject.AddComponent<BoxCollider>();
Bounds bounds = new Bounds();
Vector3 min = new Vector3(-0.01f, -0.01f, -0.01f);
Vector3 max = new Vector3(0.01f, 0.01f, 0.01f);

bounds.SetMinMax(min, max);
collider.center = t.InverseTransformPoint(bounds.center);
collider.size = bounds.size;
} //if

VerifyBoundingBoxes(t);
} //foreach
Expand Down

0 comments on commit 42c1a4b

Please sign in to comment.