diff --git a/FMDL-Studio-v2/Assets/Fmdl Studio/Scripts/Static/BoundingBoxGenerator.cs b/FMDL-Studio-v2/Assets/Fmdl Studio/Scripts/Static/BoundingBoxGenerator.cs index c90a2dd..f239f2f 100644 --- a/FMDL-Studio-v2/Assets/Fmdl Studio/Scripts/Static/BoundingBoxGenerator.cs +++ b/FMDL-Studio-v2/Assets/Fmdl Studio/Scripts/Static/BoundingBoxGenerator.cs @@ -21,8 +21,7 @@ public static void GenerateBoundingBoxes(Transform transform) break; } //if } //foreach - - InitializeBoundingBoxes(root); + GetMeshes(transform, meshes); SetBoundingBoxByMeshes(meshes); VerifyBoundingBoxes(root); @@ -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(); - 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 meshes) { foreach (Transform t in transform) @@ -109,22 +93,35 @@ private static void SetBoundingBoxByMeshes(List meshes) private static void SetBoundingBoxByVertex(Transform bone, Vector3 vertex) { - BoxCollider collider = bone.gameObject.GetComponent(); - 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()) + { + collider = bone.gameObject.GetComponent(); + 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(); + 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); @@ -137,39 +134,17 @@ private static void VerifyBoundingBoxes(Transform transform) { foreach(Transform t in transform) { - BoxCollider collider = t.GetComponent(); - 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 collider = t.gameObject.AddComponent(); + 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