Skip to content

Commit 8974a4c

Browse files
author
David Kline
authored
Merge pull request #3550 from keveleigh/bounding-box-fix
Fix bounding box creation issues
2 parents 05840c4 + 2003e66 commit 8974a4c

File tree

1 file changed

+49
-82
lines changed
  • Assets/MixedRealityToolkit.SDK/Features/UX/Scripts/BoundingBox

1 file changed

+49
-82
lines changed

Assets/MixedRealityToolkit.SDK/Features/UX/Scripts/BoundingBox/BoundingBox.cs

Lines changed: 49 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -220,18 +220,6 @@ public bool ShowRotateHandles
220220
private float cornerRadius = 0.03f;
221221
#endregion Serialized Fields
222222

223-
#region Constants
224-
private const int LeftTopBack = 0;
225-
private const int LeftTopFront = 1;
226-
private const int LeftBottomFront = 2;
227-
private const int LeftBottomBack = 3;
228-
private const int RightTopBack = 4;
229-
private const int RightTopFront = 5;
230-
private const int RightBottonFront = 6;
231-
private const int RightBottomBack = 7;
232-
private const int CORNER_COUNT = 8;
233-
#endregion Constants
234-
235223
#region Private Properties
236224
private bool isActive = false;
237225
/// <summary>
@@ -268,7 +256,7 @@ public bool IsActive
268256
private Transform rigRoot;
269257
private BoxCollider cachedTargetCollider;
270258
private Vector3[] boundsCorners;
271-
private Vector3 currentBoundsSize;
259+
private Vector3 currentBoundsExtents;
272260
private BoundsCalculationMethod boundsMethod;
273261
private HandleMoveType handleMoveType = HandleMoveType.Point;
274262
private List<Transform> links;
@@ -710,12 +698,11 @@ private Bounds GetTargetBounds()
710698
}
711699
}
712700

713-
//Renderer bounds is local. Requires transform to global coord system.
701+
//Renderer bounds is local. Requires transform to global coordinate system.
714702
Renderer[] childRenderers = targetObject.GetComponentsInChildren<Renderer>();
715703
if (childRenderers.Length > 0)
716704
{
717705
bounds = childRenderers[0].bounds;
718-
var _corners = new Vector3[CORNER_COUNT];
719706

720707
for (int i = 0; i < childRenderers.Length; ++i)
721708
{
@@ -724,7 +711,7 @@ private Bounds GetTargetBounds()
724711

725712
GetCornerPositionsFromBounds(bounds, ref boundsCorners);
726713

727-
for (int cornerIndex = 0; cornerIndex < _corners.Length; ++cornerIndex)
714+
for (int cornerIndex = 0; cornerIndex < boundsCorners.Length; ++cornerIndex)
728715
{
729716
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
730717
cube.name = cornerIndex.ToString();
@@ -850,14 +837,14 @@ private void CalculateEdgeCenters()
850837
if (boundsCorners != null && edgeCenters != null)
851838
{
852839
edgeCenters[0] = (boundsCorners[0] + boundsCorners[1]) * 0.5f;
853-
edgeCenters[1] = (boundsCorners[1] + boundsCorners[2]) * 0.5f;
854-
edgeCenters[2] = (boundsCorners[2] + boundsCorners[3]) * 0.5f;
855-
edgeCenters[3] = (boundsCorners[3] + boundsCorners[0]) * 0.5f;
840+
edgeCenters[1] = (boundsCorners[0] + boundsCorners[2]) * 0.5f;
841+
edgeCenters[2] = (boundsCorners[3] + boundsCorners[2]) * 0.5f;
842+
edgeCenters[3] = (boundsCorners[3] + boundsCorners[1]) * 0.5f;
856843

857844
edgeCenters[4] = (boundsCorners[4] + boundsCorners[5]) * 0.5f;
858-
edgeCenters[5] = (boundsCorners[5] + boundsCorners[6]) * 0.5f;
859-
edgeCenters[6] = (boundsCorners[6] + boundsCorners[7]) * 0.5f;
860-
edgeCenters[7] = (boundsCorners[7] + boundsCorners[4]) * 0.5f;
845+
edgeCenters[5] = (boundsCorners[4] + boundsCorners[6]) * 0.5f;
846+
edgeCenters[6] = (boundsCorners[7] + boundsCorners[6]) * 0.5f;
847+
edgeCenters[7] = (boundsCorners[7] + boundsCorners[5]) * 0.5f;
861848

862849
edgeCenters[8] = (boundsCorners[0] + boundsCorners[4]) * 0.5f;
863850
edgeCenters[9] = (boundsCorners[1] + boundsCorners[5]) * 0.5f;
@@ -892,7 +879,7 @@ private Vector3 ClampScale(Vector3 scale, out bool clamped)
892879
private Vector3 GetLinkDimensions()
893880
{
894881
float linkLengthAdjustor = wireframeShape == WireframeType.Cubic ? 2.0f : 1.0f - (6.0f * linkRadius);
895-
return (currentBoundsSize * linkLengthAdjustor) + new Vector3(linkRadius, linkRadius, linkRadius);
882+
return (currentBoundsExtents * linkLengthAdjustor) + new Vector3(linkRadius, linkRadius, linkRadius);
896883
}
897884

898885
private void ResetHandleVisibility()
@@ -957,50 +944,36 @@ private void ShowOneHandle(Transform handle)
957944

958945
private void UpdateBounds()
959946
{
960-
Vector3 boundsSize = Vector3.zero;
961-
Vector3 centroid = Vector3.zero;
962-
963-
//store current rotation then zero out the rotation so that the bounds
964-
//are computed when the object is in its 'axis aligned orientation'.
965-
Quaternion currentRotation = targetObject.transform.rotation;
966-
targetObject.transform.rotation = Quaternion.identity;
967-
968947
if (cachedTargetCollider != null)
969948
{
970-
Bounds colliderBounds = cachedTargetCollider.bounds;
971-
boundsSize = colliderBounds.extents;
972-
centroid = colliderBounds.center;
973-
}
974-
975-
//after bounds are computed, restore rotation...
976-
targetObject.transform.rotation = currentRotation;
977-
978-
if (boundsSize != Vector3.zero)
979-
{
980-
if (flattenAxis == FlattenModeType.FlattenAuto)
981-
{
982-
float min = Mathf.Min(boundsSize.x, Mathf.Min(boundsSize.y, boundsSize.z));
983-
flattenAxis = min.Equals(boundsSize.x) ? FlattenModeType.FlattenX : (min.Equals(boundsSize.y) ? FlattenModeType.FlattenY : FlattenModeType.FlattenZ);
984-
}
949+
// Store current rotation then zero out the rotation so that the bounds
950+
// are computed when the object is in its 'axis aligned orientation'.
951+
Quaternion currentRotation = targetObject.transform.rotation;
952+
targetObject.transform.rotation = Quaternion.identity;
953+
Physics.SyncTransforms(); // Update collider bounds
985954

986-
boundsSize.x = flattenAxis == FlattenModeType.FlattenX ? 0.0f : boundsSize.x;
987-
boundsSize.y = flattenAxis == FlattenModeType.FlattenY ? 0.0f : boundsSize.y;
988-
boundsSize.z = flattenAxis == FlattenModeType.FlattenZ ? 0.0f : boundsSize.z;
955+
Vector3 boundsExtents = cachedTargetCollider.bounds.extents;
989956

990-
currentBoundsSize = boundsSize;
991-
boundsCentroid = centroid;
957+
// After bounds are computed, restore rotation...
958+
targetObject.transform.rotation = currentRotation;
959+
Physics.SyncTransforms();
992960

993-
boundsCorners[0] = centroid - new Vector3(centroid.x - currentBoundsSize.x, centroid.y - currentBoundsSize.y, centroid.z - currentBoundsSize.z);
994-
boundsCorners[1] = centroid - new Vector3(centroid.x + currentBoundsSize.x, centroid.y - currentBoundsSize.y, centroid.z - currentBoundsSize.z);
995-
boundsCorners[2] = centroid - new Vector3(centroid.x + currentBoundsSize.x, centroid.y + currentBoundsSize.y, centroid.z - currentBoundsSize.z);
996-
boundsCorners[3] = centroid - new Vector3(centroid.x - currentBoundsSize.x, centroid.y + currentBoundsSize.y, centroid.z - currentBoundsSize.z);
961+
if (boundsExtents != Vector3.zero)
962+
{
963+
if (flattenAxis == FlattenModeType.FlattenAuto)
964+
{
965+
float min = Mathf.Min(boundsExtents.x, Mathf.Min(boundsExtents.y, boundsExtents.z));
966+
flattenAxis = min.Equals(boundsExtents.x) ? FlattenModeType.FlattenX : (min.Equals(boundsExtents.y) ? FlattenModeType.FlattenY : FlattenModeType.FlattenZ);
967+
}
997968

998-
boundsCorners[4] = centroid - new Vector3(centroid.x - currentBoundsSize.x, centroid.y - currentBoundsSize.y, centroid.z + currentBoundsSize.z);
999-
boundsCorners[5] = centroid - new Vector3(centroid.x + currentBoundsSize.x, centroid.y - currentBoundsSize.y, centroid.z + currentBoundsSize.z);
1000-
boundsCorners[6] = centroid - new Vector3(centroid.x + currentBoundsSize.x, centroid.y + currentBoundsSize.y, centroid.z + currentBoundsSize.z);
1001-
boundsCorners[7] = centroid - new Vector3(centroid.x - currentBoundsSize.x, centroid.y + currentBoundsSize.y, centroid.z + currentBoundsSize.z);
969+
boundsExtents.x = flattenAxis == FlattenModeType.FlattenX ? 0.0f : boundsExtents.x;
970+
boundsExtents.y = flattenAxis == FlattenModeType.FlattenY ? 0.0f : boundsExtents.y;
971+
boundsExtents.z = flattenAxis == FlattenModeType.FlattenZ ? 0.0f : boundsExtents.z;
972+
currentBoundsExtents = boundsExtents;
1002973

1003-
CalculateEdgeCenters();
974+
GetCornerPositionsFromBounds(new Bounds(Vector3.zero, boundsExtents * 2.0f), ref boundsCorners);
975+
CalculateEdgeCenters();
976+
}
1004977
}
1005978
}
1006979

@@ -1148,28 +1121,22 @@ private void SetHiddenHandles()
11481121

11491122
private void GetCornerPositionsFromBounds(Bounds bounds, ref Vector3[] positions)
11501123
{
1151-
Vector3 center = bounds.center;
1152-
Vector3 extents = bounds.extents;
1153-
float leftEdge = center.x - extents.x;
1154-
float rightEdge = center.x + extents.x;
1155-
float bottomEdge = center.y - extents.y;
1156-
float topEdge = center.y + extents.y;
1157-
float frontEdge = center.z - extents.z;
1158-
float backEdge = center.z + extents.z;
1159-
1160-
if (positions == null || positions.Length != CORNER_COUNT)
1161-
{
1162-
positions = new Vector3[CORNER_COUNT];
1163-
}
1164-
1165-
positions[LeftBottomFront] = new Vector3(leftEdge, bottomEdge, frontEdge);
1166-
positions[LeftBottomBack] = new Vector3(leftEdge, bottomEdge, backEdge);
1167-
positions[LeftTopFront] = new Vector3(leftEdge, topEdge, frontEdge);
1168-
positions[LeftTopBack] = new Vector3(leftEdge, topEdge, backEdge);
1169-
positions[RightBottonFront] = new Vector3(rightEdge, bottomEdge, frontEdge);
1170-
positions[RightBottomBack] = new Vector3(rightEdge, bottomEdge, backEdge);
1171-
positions[RightTopFront] = new Vector3(rightEdge, topEdge, frontEdge);
1172-
positions[RightTopBack] = new Vector3(rightEdge, topEdge, backEdge);
1124+
int numCorners = 1 << 3;
1125+
if (positions == null || positions.Length != numCorners)
1126+
{
1127+
positions = new Vector3[numCorners];
1128+
}
1129+
1130+
// Permutate all axes using minCorner and maxCorner.
1131+
Vector3 minCorner = bounds.center - bounds.extents;
1132+
Vector3 maxCorner = bounds.center + bounds.extents;
1133+
for (int c = 0; c < numCorners; c++)
1134+
{
1135+
positions[c] = new Vector3(
1136+
(c & (1 << 0)) == 0 ? minCorner[0] : maxCorner[0],
1137+
(c & (1 << 1)) == 0 ? minCorner[1] : maxCorner[1],
1138+
(c & (1 << 2)) == 0 ? minCorner[2] : maxCorner[2]);
1139+
}
11731140
}
11741141

11751142
private static Vector3 PointToRay(Vector3 origin, Vector3 end, Vector3 closestPoint)

0 commit comments

Comments
 (0)