Skip to content

Commit 285df53

Browse files
committed
fix: bounding box negative space
1 parent 26e467e commit 285df53

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

Runtime/Scripts/Common/Utils.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ public static bool TryBoundBoxColliderSize(GameObject gameObject, out BoxCollide
114114
boxCollider = gameObject.GetComponent<BoxCollider>();
115115
if (boxCollider == null)
116116
{
117-
throw new NullReferenceException("BoxCollider is null");
117+
#if UNITY_EDITOR
118+
boxCollider = UnityEditor.Undo.AddComponent<BoxCollider>(gameObject);
119+
#endif
120+
if (boxCollider == null) throw new NullReferenceException("BoxCollider is null");
118121
}
119122
#if UNITY_EDITOR
120123
UnityEditor.Undo.RecordObject(boxCollider, "Set Box Collider Bound Size");
@@ -123,6 +126,8 @@ public static bool TryBoundBoxColliderSize(GameObject gameObject, out BoxCollide
123126
boxCollider.center = bounds.center;
124127
boxCollider.size = bounds.size;
125128

129+
Debug.Log($"BoxCollider size for {gameObject.name} is {boxCollider.size}", gameObject);
130+
126131
#if UNITY_EDITOR
127132

128133
UnityEditor.EditorUtility.SetDirty(gameObject);
@@ -145,15 +150,23 @@ public static bool TryBoundBoxColliderSize(GameObject gameObject, out BoxCollide
145150
public static Bounds GetLocalBoundsForChildrenMeshes(GameObject gameObject)
146151
{
147152
var transform = gameObject.transform;
148-
var localBounds = new Bounds(Vector3.zero, Vector3.zero);
149153
var filters = gameObject.GetComponentsInChildren<MeshFilter>();
154+
var localBounds = new List<Bounds>();
155+
150156
foreach (var meshFilter in filters)
151157
{
152158
var matrix = transform.localToWorldMatrix.inverse * meshFilter.transform.localToWorldMatrix;
153159
var axisAlignedBounds = GeometryUtility.CalculateBounds(meshFilter.sharedMesh.vertices, matrix);
154-
localBounds.Encapsulate(axisAlignedBounds);
160+
localBounds.Add(axisAlignedBounds);
161+
}
162+
163+
var result = localBounds[0];
164+
for (var i = 1; i < localBounds.Count; i++)
165+
{
166+
result.Encapsulate(localBounds[i]);
155167
}
156-
return localBounds;
168+
169+
return result;
157170
}
158171
}
159172
}

0 commit comments

Comments
 (0)