Skip to content

Commit a6cd488

Browse files
authored
Merge pull request #6940 from thalbern/user/bethalha/bb_target
added possibility to set target of bounds control / bounding box in code
2 parents 2334609 + 5a95fca commit a6cd488

File tree

4 files changed

+134
-4
lines changed

4 files changed

+134
-4
lines changed

Assets/MixedRealityToolkit.SDK/Experimental/Features/UX/BoundsControl/BoundsControl.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ public GameObject Target
4444

4545
return targetObject;
4646
}
47+
48+
set
49+
{
50+
if (targetObject != value)
51+
{
52+
targetObject = value;
53+
CreateRig();
54+
}
55+
}
4756
}
4857

4958
[Tooltip("For complex objects, automatic bounds calculation may not behave as expected. Use an existing Box Collider (even on a child object) to manually determine bounds of bounds control.")]
@@ -880,7 +889,7 @@ private void InitializeRigRoot()
880889
{
881890
var rigRootObj = new GameObject(rigRootName);
882891
rigRoot = rigRootObj.transform;
883-
rigRoot.parent = transform;
892+
rigRoot.parent = Target.transform;
884893

885894
var pH = rigRootObj.AddComponent<PointerHandler>();
886895
pH.OnPointerDown.AddListener(OnPointerDown);
@@ -1170,7 +1179,7 @@ private void UpdateVisuals()
11701179
// move rig into position and rotation
11711180
rigRoot.position = TargetBounds.bounds.center;
11721181
rigRoot.rotation = Target.transform.rotation;
1173-
rigRoot.parent = transform;
1182+
rigRoot.parent = Target.transform;
11741183
}
11751184
}
11761185

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ public GameObject Target
173173

174174
return targetObject;
175175
}
176+
177+
set
178+
{
179+
if (targetObject != value)
180+
{
181+
targetObject = value;
182+
CreateRig();
183+
}
184+
}
176185
}
177186

178187
[Tooltip("For complex objects, automatic bounds calculation may not behave as expected. Use an existing Box Collider (even on a child object) to manually determine bounds of Bounding Box.")]

Assets/MixedRealityToolkit.Tests/PlayModeTests/BoundingBoxTests.cs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,17 @@ public void ShutdownMrtk()
3737
PlayModeTestUtilities.TearDown();
3838
}
3939

40+
private readonly Vector3 boundingBoxStartCenter = Vector3.forward * 1.5f;
41+
private readonly Vector3 boundingBoxStartScale = Vector3.one * 0.5f;
42+
4043
/// <summary>
4144
/// Instantiates a bounding box at 0, 0, -1.5f
4245
/// box is at scale .5, .5, .5
4346
/// </summary>
4447
private BoundingBox InstantiateSceneAndDefaultBbox()
4548
{
4649
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
47-
cube.transform.position = Vector3.forward * 1.5f;
50+
cube.transform.position = boundingBoxStartCenter;
4851
BoundingBox bbox = cube.AddComponent<BoundingBox>();
4952

5053
MixedRealityPlayspace.PerformTransformation(
@@ -54,7 +57,7 @@ private BoundingBox InstantiateSceneAndDefaultBbox()
5457
p.LookAt(cube.transform.position);
5558
});
5659

57-
bbox.transform.localScale *= 0.5f;
60+
bbox.transform.localScale = boundingBoxStartScale;
5861
bbox.Active = true;
5962

6063
return bbox;
@@ -327,6 +330,61 @@ public IEnumerator DisableObject()
327330
Assert.AreEqual(afterTransformScale, bbox.transform.localScale);
328331
}
329332

333+
334+
/// <summary>
335+
/// Tests setting a target in code that is a different gameobject than the gameobject the boundingbox component is attached to
336+
/// </summary>
337+
[UnityTest]
338+
public IEnumerator SetTarget()
339+
{
340+
// create cube without control
341+
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
342+
cube.transform.position = boundingBoxStartCenter;
343+
344+
MixedRealityPlayspace.PerformTransformation(
345+
p =>
346+
{
347+
p.position = Vector3.zero;
348+
p.LookAt(cube.transform.position);
349+
});
350+
351+
cube.transform.localScale = boundingBoxStartScale;
352+
353+
// create another gameobject with boundscontrol attached
354+
var emptyGameObject = new GameObject("empty");
355+
BoundingBox bbox = emptyGameObject.AddComponent<BoundingBox>();
356+
357+
// set target to cube
358+
bbox.Target = cube;
359+
bbox.Active = true;
360+
361+
// front right corner is corner 3
362+
var frontRightCornerPos = cube.transform.Find("rigRoot/corner_3").position;
363+
364+
// grab corner and scale object
365+
Vector3 initialHandPosition = new Vector3(0, 0, 0.5f);
366+
int numSteps = 30;
367+
var delta = new Vector3(0.1f, 0.1f, 0f);
368+
TestHand hand = new TestHand(Handedness.Right);
369+
yield return hand.Show(initialHandPosition);
370+
yield return hand.SetGesture(ArticulatedHandPose.GestureId.OpenSteadyGrabPoint);
371+
yield return hand.MoveTo(frontRightCornerPos, numSteps);
372+
yield return hand.SetGesture(ArticulatedHandPose.GestureId.Pinch);
373+
yield return hand.MoveTo(frontRightCornerPos + delta, numSteps);
374+
375+
var endBounds = cube.GetComponent<BoxCollider>().bounds;
376+
Vector3 expectedCenter = new Vector3(0.033f, 0.033f, 1.467f);
377+
Vector3 expectedSize = Vector3.one * .567f;
378+
TestUtilities.AssertAboutEqual(endBounds.center, expectedCenter, "endBounds incorrect center");
379+
TestUtilities.AssertAboutEqual(endBounds.size, expectedSize, "endBounds incorrect size");
380+
381+
Object.Destroy(emptyGameObject);
382+
Object.Destroy(cube);
383+
// Wait for a frame to give Unity a change to actually destroy the object
384+
yield return null;
385+
386+
}
387+
330388
/// <summary>
331389
/// Returns the AABB of the bounding box rig (corners, edges)
332390
/// that make up the bounding box by using the positions of the corners

Assets/MixedRealityToolkit.Tests/PlayModeTests/Experimental/BoundsControlTests.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,60 @@ public IEnumerator DisableObject()
333333
Assert.AreEqual(afterTransformScale, bbox.transform.localScale);
334334
}
335335

336+
/// <summary>
337+
/// Tests setting a target in code that is a different gameobject than the gameobject the bounds control component is attached to
338+
/// </summary>
339+
[UnityTest]
340+
public IEnumerator SetTarget()
341+
{
342+
// create cube without control
343+
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
344+
cube.transform.position = boundsControlStartCenter;
345+
346+
MixedRealityPlayspace.PerformTransformation(
347+
p =>
348+
{
349+
p.position = Vector3.zero;
350+
p.LookAt(cube.transform.position);
351+
});
352+
353+
cube.transform.localScale = boundsControlStartScale;
354+
355+
// create another gameobject with boundscontrol attached
356+
var emptyGameObject = new GameObject("empty");
357+
BoundsControl bbox = emptyGameObject.AddComponent<BoundsControl>();
358+
359+
// set target to cube
360+
bbox.Target = cube;
361+
bbox.Active = true;
362+
363+
// front right corner is corner 3
364+
var frontRightCornerPos = cube.transform.Find("rigRoot/corner_3").position;
365+
366+
// grab corner and scale object
367+
Vector3 initialHandPosition = new Vector3(0, 0, 0.5f);
368+
int numSteps = 30;
369+
var delta = new Vector3(0.1f, 0.1f, 0f);
370+
TestHand hand = new TestHand(Handedness.Right);
371+
yield return hand.Show(initialHandPosition);
372+
yield return hand.SetGesture(ArticulatedHandPose.GestureId.OpenSteadyGrabPoint);
373+
yield return hand.MoveTo(frontRightCornerPos, numSteps);
374+
yield return hand.SetGesture(ArticulatedHandPose.GestureId.Pinch);
375+
yield return hand.MoveTo(frontRightCornerPos + delta, numSteps);
376+
377+
var endBounds = cube.GetComponent<BoxCollider>().bounds;
378+
Vector3 expectedCenter = new Vector3(0.033f, 0.033f, 1.467f);
379+
Vector3 expectedSize = Vector3.one * .567f;
380+
TestUtilities.AssertAboutEqual(endBounds.center, expectedCenter, "endBounds incorrect center");
381+
TestUtilities.AssertAboutEqual(endBounds.size, expectedSize, "endBounds incorrect size");
382+
383+
Object.Destroy(emptyGameObject);
384+
Object.Destroy(cube);
385+
// Wait for a frame to give Unity a change to actually destroy the object
386+
yield return null;
387+
388+
}
389+
336390
/// <summary>
337391
/// Returns the AABB of the bounds control rig (corners, edges)
338392
/// that make up the bounds control by using the positions of the corners

0 commit comments

Comments
 (0)