Skip to content

Commit fdb04cd

Browse files
committed
Test for checking exact fit and alignment of handle colliders + visuals
1 parent ced23c3 commit fdb04cd

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

Assets/MRTK/Tests/PlayModeTests/BoundsControlTests.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,6 +2083,91 @@ public IEnumerator PerAxisHandlePrefabTest([ValueSource("perAxisHandleTestData")
20832083
yield return null;
20842084
}
20852085

2086+
/// <summary>
2087+
/// Test for verifying automatically generated handle colliders are properly aligned to the handle visual
2088+
/// </summary>
2089+
[UnityTest]
2090+
public IEnumerator PerAxisHandleAlignmentTest([ValueSource("perAxisHandleTestData")] PerAxisHandleTestData testData)
2091+
{
2092+
var boundsControl = InstantiateSceneAndDefaultBoundsControl();
2093+
yield return VerifyInitialBoundsCorrect(boundsControl);
2094+
2095+
// Create an oblong-shaped handle
2096+
// (cylinder primitive will do, as it is longer than it is wide!)
2097+
var cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
2098+
GameObject.Destroy(cylinder.GetComponent<CapsuleCollider>());
2099+
2100+
// Wait for Destroy() to do its thing
2101+
yield return null;
2102+
2103+
// Set the rotation handles to be cylinders!
2104+
System.Reflection.PropertyInfo propName = boundsControl.GetType().GetProperty(testData.configPropertyName);
2105+
PerAxisHandlesConfiguration config = (PerAxisHandlesConfiguration)propName.GetValue(boundsControl);
2106+
config.HandlePrefab = cylinder;
2107+
2108+
// Reflection voodoo to retrieve the ColliderPadding value regardless of which
2109+
// handle configuration subclass we're currently using
2110+
System.Type configType = config.GetType();
2111+
var paddingProperty = configType.GetProperty("ColliderPadding");
2112+
Vector3 padding = (Vector3)paddingProperty.GetValue(config);
2113+
2114+
// Wait for BoundsControl to update to new handle prefab/rebuild rig
2115+
yield return null;
2116+
yield return new WaitForFixedUpdate();
2117+
2118+
// Iterate over all handle transforms
2119+
foreach(Transform handle in boundsControl.transform.Find("rigRoot"))
2120+
{
2121+
// Is this the handle type we're currently looking for?
2122+
if(handle.name.StartsWith(testData.handleName))
2123+
{
2124+
BoxCollider handleCollider = handle.GetComponent<BoxCollider>();
2125+
2126+
Vector3[] colliderPoints = new Vector3[8];
2127+
Vector3[] globalColliderPoints = new Vector3[8];
2128+
2129+
// Strip the padding off the collider bounds, so that these bounds will match up
2130+
// correctly with the visual bounds, if the alignment was properly done.
2131+
VisualUtils.GetCornerPositionsFromBounds(new Bounds(handleCollider.center, handleCollider.size - padding), ref colliderPoints);
2132+
2133+
// Perform a local-global transformation on all corners of the local collider bounds.
2134+
for(int i = 0; i < colliderPoints.Length; ++i)
2135+
{
2136+
globalColliderPoints[i] = handle.TransformPoint(colliderPoints[i]);
2137+
}
2138+
2139+
Transform visual = handle.GetChild(0);
2140+
Bounds handleBounds = VisualUtils.GetMaxBounds(visual.gameObject);
2141+
2142+
Vector3[] visualPoints = new Vector3[8];
2143+
Vector3[] globalVisualPoints = new Vector3[8];
2144+
2145+
VisualUtils.GetCornerPositionsFromBounds(handleBounds, ref visualPoints);
2146+
2147+
// Perform a local-global transformation on all corners of the local visual handle bounds.
2148+
for(int i = 0; i < visualPoints.Length; ++i)
2149+
{
2150+
globalVisualPoints[i] = visual.TransformPoint(visualPoints[i]);
2151+
}
2152+
2153+
// Make sure all corners/vertices of the bounds are coherent after realignment, in global space
2154+
bool flag = true;
2155+
for(int i = 0; i < globalColliderPoints.Length; ++i)
2156+
{
2157+
if(globalColliderPoints[i] != globalVisualPoints[i])
2158+
{
2159+
flag = false;
2160+
Debug.LogError($"Bounds mismatch, collider point: {globalColliderPoints[i].ToString("F3")}, visual point: {globalVisualPoints[i].ToString("F3")}");
2161+
}
2162+
}
2163+
2164+
Assert.IsTrue(flag, "Handle collider does not match visual bounds, likely incorrect realignment of handle/visual transforms");
2165+
}
2166+
}
2167+
2168+
yield return null;
2169+
}
2170+
20862171
/// <summary>
20872172
/// Test for verifying changing the handle prefabs during runtime
20882173
/// in regular and flatten mode and making sure the entire rig won't be recreated

0 commit comments

Comments
 (0)