Skip to content

Commit 3e9f980

Browse files
author
Finn Sinclair
committed
Fixing handle collider rotation/alignment + comment consistency
1 parent 78b8c7c commit 3e9f980

File tree

1 file changed

+31
-27
lines changed

1 file changed

+31
-27
lines changed

Assets/MRTK/SDK/Features/UX/Scripts/BoundsControl/Visuals/PerAxisHandles.cs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace Microsoft.MixedReality.Toolkit.UI.BoundsControl
1010
{
1111
/// <summary>
12-
/// Rotation handles for <see cref="BoundsControl"/> that are used for rotating the
12+
/// Handles for <see cref="BoundsControl"/> that are used for manipulating the
1313
/// Gameobject BoundsControl is attached to with near or far interaction
1414
/// </summary>
1515
public abstract class PerAxisHandles : HandlesBase
@@ -78,14 +78,14 @@ private void UpdateColliderType()
7878
if (oldBoxCollider != null && config.HandlePrefabColliderType == HandlePrefabCollider.Sphere)
7979
{
8080
shouldCreateNewCollider = true;
81-
Object.Destroy(oldBoxCollider);
81+
Object.DestroyImmediate(oldBoxCollider);
8282
}
8383

8484
var oldSphereCollider = handle.GetComponent<SphereCollider>();
8585
if (oldSphereCollider != null && config.HandlePrefabColliderType == HandlePrefabCollider.Box)
8686
{
8787
shouldCreateNewCollider = true;
88-
Object.Destroy(oldSphereCollider);
88+
Object.DestroyImmediate(oldSphereCollider);
8989
}
9090

9191
if (shouldCreateNewCollider)
@@ -185,10 +185,12 @@ private void CreateHandles(Transform parent)
185185
handle.transform.position = HandlePositions[i];
186186
handle.transform.parent = parent;
187187

188-
Bounds midpointBounds = CreateVisual(i, handle);
189-
float maxDim = VisualUtils.GetMaxComponent(midpointBounds.size);
188+
Bounds handleVisualBounds = CreateVisual(i, handle);
189+
float maxDim = VisualUtils.GetMaxComponent(handleVisualBounds.size);
190190
float invScale = maxDim == 0.0f ? 0.0f : config.HandleSize / maxDim;
191-
VisualUtils.AddComponentsToAffordance(handle, new Bounds(midpointBounds.center * invScale, midpointBounds.size * invScale),
191+
192+
// TODO: Some subclasses of PerAxisHandles shouldn't use CursorContextInfo.CursorAction.Rotate
193+
VisualUtils.AddComponentsToAffordance(handle, new Bounds(handleVisualBounds.center * invScale, handleVisualBounds.size * invScale),
192194
config.HandlePrefabColliderType, CursorContextInfo.CursorAction.Rotate, config.ColliderPadding, parent, config.DrawTetherWhenManipulating);
193195

194196
handles.Add(handle.transform);
@@ -209,11 +211,11 @@ protected override void RecreateVisuals()
209211
{
210212
// get old child and remove it
211213
obsoleteChild.parent = null;
212-
Object.Destroy(obsoleteChild.gameObject);
214+
Object.DestroyImmediate(obsoleteChild.gameObject);
213215
}
214216
else
215217
{
216-
Debug.LogError("couldn't find rotation visual on recreating visuals");
218+
Debug.LogError("Couldn't find handle visual on recreating visuals");
217219
}
218220

219221
// create new visual
@@ -247,40 +249,42 @@ protected override void UpdateColliderBounds(Transform handle, Vector3 visualSiz
247249

248250
private Bounds CreateVisual(int handleIndex, GameObject parent)
249251
{
250-
GameObject midpointVisual;
252+
GameObject handleVisual;
251253
GameObject prefabType = config.HandlePrefab;
252254
if (prefabType != null)
253255
{
254-
midpointVisual = Object.Instantiate(prefabType);
256+
handleVisual = Object.Instantiate(prefabType);
255257
}
256258
else
257259
{
258-
midpointVisual = GameObject.CreatePrimitive(PrimitiveType.Sphere);
259-
// deactivate collider on visuals and register for deletion - actual collider
260-
// of handle is attached to the handle gameobject, not the visual
261-
var collider = midpointVisual.GetComponent<SphereCollider>();
260+
handleVisual = GameObject.CreatePrimitive(PrimitiveType.Sphere);
261+
// We only want the Primitive sphere mesh, but CreatePrimitive will
262+
// give us a sphere collider too. Remove the sphere collider here
263+
// so we can manually add our own properly configured collider later.
264+
var collider = handleVisual.GetComponent<SphereCollider>();
262265
collider.enabled = false;
263-
Object.Destroy(collider);
264-
}
265-
266-
Quaternion realignment = GetRotationRealignment(handleIndex);
267-
midpointVisual.transform.localRotation = realignment * midpointVisual.transform.localRotation;
266+
Object.DestroyImmediate(collider);
267+
}
268268

269-
Bounds midpointBounds = VisualUtils.GetMaxBounds(midpointVisual);
270-
float maxDim = VisualUtils.GetMaxComponent(midpointBounds.size);
269+
// handleVisualBounds are returned in handleVisual-local space.
270+
Bounds handleVisualBounds = VisualUtils.GetMaxBounds(handleVisual);
271+
float maxDim = VisualUtils.GetMaxComponent(handleVisualBounds.size);
271272
float invScale = maxDim == 0.0f ? 0.0f : config.HandleSize / maxDim;
272273

273-
midpointVisual.name = visualsName;
274-
midpointVisual.transform.parent = parent.transform;
275-
midpointVisual.transform.localScale = new Vector3(invScale, invScale, invScale);
276-
midpointVisual.transform.localPosition = Vector3.zero;
274+
handleVisual.name = visualsName;
275+
handleVisual.transform.parent = parent.transform;
276+
handleVisual.transform.localScale = new Vector3(invScale, invScale, invScale);
277+
handleVisual.transform.localPosition = Vector3.zero;
278+
279+
Quaternion realignment = GetRotationRealignment(handleIndex);
280+
parent.transform.localRotation = realignment;
277281

278282
if (config.HandleMaterial != null)
279283
{
280-
VisualUtils.ApplyMaterialToAllRenderers(midpointVisual, config.HandleMaterial);
284+
VisualUtils.ApplyMaterialToAllRenderers(handleVisual, config.HandleMaterial);
281285
}
282286

283-
return midpointBounds;
287+
return handleVisualBounds;
284288
}
285289

286290
#region BoundsControlHandlerBase

0 commit comments

Comments
 (0)