Skip to content

Commit aea84fb

Browse files
Railboyradicalad
authored andcommitted
Small Performance Tweaks Round 1 (#3479)
* Disabled auto-sync transforms https://docs.unity3d.com/ScriptReference/Physics-autoSyncTransforms.html * Improved physical parabola speed * Improved bounding box performances Cached collider bounds (biggest cost) Stored transforms instead of game objects for visual objects * Set line step count for default pointers to reasonable settings, added perf warning tooltip to line pointer * Removed allocation in GazeHandHelper * Performance improvements to line data provider, line renderer, distorter Tiny gains but they add up when getting a lot of points. * Added warning to line pointer inspector * Small perf imrpovements in line renderer * Swapped out Unity Vector3 math for custom math Gains are minor but they add up when you've got a lot of ray steps. * Set transform mode to matrix in controller prefabs, updated range / max recommended for line steps * Reverting cached collider bounds change
1 parent 398a1b2 commit aea84fb

File tree

16 files changed

+857
-828
lines changed

16 files changed

+857
-828
lines changed

Assets/MixedRealityToolkit.SDK/Features/UX/Prefabs/Pointers/DefaultControllerPointer.prefab

Lines changed: 259 additions & 272 deletions
Large diffs are not rendered by default.

Assets/MixedRealityToolkit.SDK/Features/UX/Prefabs/Pointers/ParabolicPointer.prefab

Lines changed: 282 additions & 472 deletions
Large diffs are not rendered by default.

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

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace Microsoft.MixedReality.Toolkit.SDK.UX
1515
{
16-
public class BoundingBox : BaseFocusHandler,
16+
public class BoundingBox : BaseFocusHandler,
1717
IMixedRealityInputHandler,
1818
IMixedRealityInputHandler<MixedRealityPose>,
1919
IMixedRealityPointerHandler,
@@ -250,7 +250,7 @@ public bool IsActive
250250
if (value)
251251
{
252252
CreateRig();
253-
rigRoot.SetActive(true);
253+
rigRoot.gameObject.SetActive(true);
254254
}
255255
else
256256
{
@@ -265,15 +265,15 @@ public bool IsActive
265265
private IMixedRealityInputSource currentInputSource;
266266
private Vector3 initialGazePoint = Vector3.zero;
267267
private GameObject targetObject;
268-
private GameObject rigRoot;
268+
private Transform rigRoot;
269269
private BoxCollider cachedTargetCollider;
270270
private Vector3[] boundsCorners;
271271
private Vector3 currentBoundsSize;
272272
private BoundsCalculationMethod boundsMethod;
273273
private HandleMoveType handleMoveType = HandleMoveType.Point;
274-
private List<GameObject> links;
275-
private List<GameObject> corners;
276-
private List<GameObject> balls;
274+
private List<Transform> links;
275+
private List<Transform> corners;
276+
private List<Transform> balls;
277277
private List<Renderer> cornerRenderers;
278278
private List<Renderer> ballRenderers;
279279
private List<Renderer> linkRenderers;
@@ -304,7 +304,7 @@ private void Start()
304304

305305
if (MixedRealityToolkit.IsInitialized && MixedRealityToolkit.InputSystem != null)
306306
{
307-
MixedRealityToolkit.InputSystem.Register(targetObject);
307+
MixedRealityToolkit.InputSystem.Register(targetObject);
308308
}
309309

310310
if (activateOnStart == true)
@@ -344,7 +344,7 @@ private void CreateRig()
344344
UpdateRigHandles();
345345
Flatten();
346346
ResetHandleVisibility();
347-
rigRoot.SetActive(false);
347+
rigRoot.gameObject.SetActive(false);
348348
}
349349

350350
private void DestroyRig()
@@ -530,7 +530,7 @@ private void AddCorners()
530530
var cubeRenderer = cube.GetComponent<Renderer>();
531531
cornerRenderers.Add(cubeRenderer);
532532
cornerColliders.Add(cube.GetComponent<Collider>());
533-
corners.Add(cube);
533+
corners.Add(cube.transform);
534534

535535
if (handleMaterial != null)
536536
{
@@ -556,7 +556,7 @@ private void AddLinks()
556556
var ballRenderer = ball.GetComponent<Renderer>();
557557
ballRenderers.Add(ballRenderer);
558558
ballColliders.Add(ball.GetComponent<Collider>());
559-
balls.Add(ball);
559+
balls.Add(ball.transform);
560560

561561
if (handleMaterial != null)
562562
{
@@ -615,7 +615,7 @@ private void AddLinks()
615615
linkRenderer.material = wireframeMaterial;
616616
}
617617

618-
links.Add(link);
618+
links.Add(link.transform);
619619
}
620620
}
621621

@@ -628,6 +628,7 @@ private void SetBoundingBoxCollider()
628628
if (boxColliderToUse != null)
629629
{
630630
cachedTargetCollider = boxColliderToUse;
631+
cachedTargetCollider.transform.hasChanged = true;
631632
}
632633
else
633634
{
@@ -695,9 +696,10 @@ private Bounds GetTargetBounds()
695696

696697
for (int i = 0; i < colliders.Length; ++i)
697698
{
698-
if (colliders[i].bounds.size != Vector3.zero)
699+
Bounds colliderBounds = colliders[i].bounds;
700+
if (colliderBounds.size != Vector3.zero)
699701
{
700-
bounds.Encapsulate(colliders[i].bounds);
702+
bounds.Encapsulate(colliderBounds);
701703
}
702704
}
703705

@@ -828,18 +830,18 @@ private void SetMaterials()
828830

829831
private void InitializeDataStructures()
830832
{
831-
rigRoot = new GameObject("rigRoot");
833+
rigRoot = new GameObject("rigRoot").transform;
832834
rigRoot.hideFlags = HideFlags.HideInHierarchy | HideFlags.HideInInspector;
833835

834836
boundsCorners = new Vector3[8];
835837

836-
corners = new List<GameObject>();
838+
corners = new List<Transform>();
837839
cornerColliders = new List<Collider>();
838840
cornerRenderers = new List<Renderer>();
839-
balls = new List<GameObject>();
841+
balls = new List<Transform>();
840842
ballRenderers = new List<Renderer>();
841843
ballColliders = new List<Collider>();
842-
links = new List<GameObject>();
844+
links = new List<Transform>();
843845
linkRenderers = new List<Renderer>();
844846
}
845847

@@ -965,8 +967,9 @@ private void UpdateBounds()
965967

966968
if (cachedTargetCollider != null)
967969
{
968-
boundsSize = cachedTargetCollider.bounds.extents;
969-
centroid = cachedTargetCollider.bounds.center;
970+
Bounds colliderBounds = cachedTargetCollider.bounds;
971+
boundsSize = colliderBounds.extents;
972+
centroid = colliderBounds.center;
970973
}
971974

972975
//after bounds are computed, restore rotation...
@@ -1005,38 +1008,38 @@ private void UpdateRigHandles()
10051008
{
10061009
if (rigRoot != null && targetObject != null)
10071010
{
1008-
rigRoot.transform.rotation = Quaternion.identity;
1009-
rigRoot.transform.position = Vector3.zero;
1011+
rigRoot.rotation = Quaternion.identity;
1012+
rigRoot.position = Vector3.zero;
10101013

10111014
for (int i = 0; i < corners.Count; ++i)
10121015
{
1013-
corners[i].transform.position = boundsCorners[i];
1016+
corners[i].position = boundsCorners[i];
10141017
}
10151018

10161019
Vector3 linkDimensions = GetLinkDimensions();
10171020

10181021
for (int i = 0; i < edgeCenters.Length; ++i)
10191022
{
1020-
balls[i].transform.position = edgeCenters[i];
1021-
links[i].transform.position = edgeCenters[i];
1023+
balls[i].position = edgeCenters[i];
1024+
links[i].position = edgeCenters[i];
10221025

10231026
if (edgeAxes[i] == CardinalAxisType.X)
10241027
{
1025-
links[i].transform.localScale = new Vector3(linkRadius, linkDimensions.x, linkRadius);
1028+
links[i].localScale = new Vector3(linkRadius, linkDimensions.x, linkRadius);
10261029
}
10271030
else if (edgeAxes[i] == CardinalAxisType.Y)
10281031
{
1029-
links[i].transform.localScale = new Vector3(linkRadius, linkDimensions.y, linkRadius);
1032+
links[i].localScale = new Vector3(linkRadius, linkDimensions.y, linkRadius);
10301033
}
10311034
else
10321035
{
1033-
links[i].transform.localScale = new Vector3(linkRadius, linkDimensions.z, linkRadius);
1036+
links[i].localScale = new Vector3(linkRadius, linkDimensions.z, linkRadius);
10341037
}
10351038
}
10361039

10371040
//move rig into position and rotation
1038-
rigRoot.transform.position = cachedTargetCollider.bounds.center;
1039-
rigRoot.transform.rotation = targetObject.transform.rotation;
1041+
rigRoot.position = cachedTargetCollider.bounds.center;
1042+
rigRoot.rotation = targetObject.transform.rotation;
10401043
}
10411044
}
10421045

@@ -1261,7 +1264,7 @@ public void OnPointerDown(MixedRealityPointerEventData eventData) { }
12611264
public void OnPointerUp(MixedRealityPointerEventData eventData) { }
12621265
public void OnPointerClicked(MixedRealityPointerEventData eventData) { }
12631266
public void OnInputPressed(InputEventData<float> eventData) { }
1264-
public void OnPositionInputChanged(InputEventData<Vector2> eventData){}
1267+
public void OnPositionInputChanged(InputEventData<Vector2> eventData) { }
12651268
public void OnPositionChanged(InputEventData<Vector3> eventData) { }
12661269
public void OnRotationChanged(InputEventData<Quaternion> eventData) { }
12671270
public void OnSourceDetected(SourceStateEventData eventData) { }

Assets/MixedRealityToolkit.SDK/Features/UX/Scripts/Pointers/LinePointer.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ namespace Microsoft.MixedReality.Toolkit.SDK.UX.Pointers
1515
[RequireComponent(typeof(DistorterGravity))]
1616
public class LinePointer : BaseControllerPointer
1717
{
18+
[Range(1, 50)]
19+
[SerializeField]
20+
[Tooltip("This setting has a high performance cost. Values above 20 are not recommended.")]
21+
protected int LineCastResolution = 10;
22+
1823
[SerializeField]
1924
protected Gradient LineColorSelected = new Gradient();
2025

@@ -30,10 +35,6 @@ public class LinePointer : BaseControllerPointer
3035
[SerializeField]
3136
protected Gradient LineColorLockFocus = new Gradient();
3237

33-
[Range(2, 100)]
34-
[SerializeField]
35-
protected int LineCastResolution = 25;
36-
3738
[SerializeField]
3839
private BaseMixedRealityLineDataProvider lineBase;
3940

Assets/MixedRealityToolkit.SDK/Features/UX/Scripts/Utilities/GazeHandHelper.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.MixedReality.Toolkit.Core.EventDatum.Input;
77
using Microsoft.MixedReality.Toolkit.Core.Definitions.Utilities;
88
using UnityEngine;
9+
using System;
910

1011
namespace Microsoft.MixedReality.Toolkit.SDK.UX.Utilities
1112
{
@@ -141,6 +142,7 @@ public Vector3 GetHandsCentroid()
141142
/// This function gets an array of all active hand positions
142143
/// </summary>
143144
/// <returns>array of Vector3</returns>
145+
[Obsolete]
144146
public Vector3[] GetHandPositions()
145147
{
146148
List<Vector3> positions = new List<Vector3>();
@@ -156,14 +158,32 @@ public Vector3[] GetHandPositions()
156158
return positions.ToArray();
157159
}
158160

161+
/// <summary>
162+
/// This function gets an array of all active hand positions
163+
/// </summary>
164+
/// <returns>enumerable of Vector3</returns>
165+
public IEnumerable<Vector3> GetAllHandPositions()
166+
{
167+
foreach (uint key in positionAvailableMap.Keys)
168+
{
169+
if (positionAvailableMap[key] == true)
170+
{
171+
yield return handPositionMap[key];
172+
}
173+
}
174+
yield break;
175+
}
176+
159177
/// <summary>
160178
/// This function retrieves the position of the first active hand.
161179
/// </summary>
162180
/// <returns>Vector3 representing position</returns>
163181
public Vector3 GetFirstHand()
164182
{
165-
Vector3[] hands = GetHandPositions();
166-
return hands.Length > 0 ? hands[0] : Vector3.zero;
183+
foreach (Vector3 hand in GetAllHandPositions())
184+
return hand;
185+
186+
return Vector3.zero;
167187
}
168188

169189
/// <summary>

Assets/MixedRealityToolkit.SDK/Inspectors/UX/Pointers/LinePointerInspector.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class LinePointerInspector : BaseControllerPointerInspector
1818
private SerializedProperty lineRenderers;
1919

2020
private bool linePointerFoldout = true;
21+
private const int maxRecommendedLinecastResolution = 20;
2122

2223
protected override void OnEnable()
2324
{
@@ -42,12 +43,19 @@ public override void OnInspectorGUI()
4243
if (linePointerFoldout)
4344
{
4445
EditorGUI.indentLevel++;
46+
47+
int lineCastResolutionValue = lineCastResolution.intValue;
48+
if (lineCastResolutionValue > maxRecommendedLinecastResolution)
49+
{
50+
EditorGUILayout.LabelField("Note: values above " + maxRecommendedLinecastResolution + " should only be used when your line is expected to be highly non-uniform.", EditorStyles.miniLabel);
51+
}
52+
53+
EditorGUILayout.PropertyField(lineCastResolution);
4554
EditorGUILayout.PropertyField(lineColorSelected);
4655
EditorGUILayout.PropertyField(lineColorValid);
4756
EditorGUILayout.PropertyField(lineColorInvalid);
4857
EditorGUILayout.PropertyField(lineColorNoTarget);
4958
EditorGUILayout.PropertyField(lineColorLockFocus);
50-
EditorGUILayout.PropertyField(lineCastResolution);
5159
EditorGUILayout.PropertyField(lineRenderers, true);
5260
EditorGUI.indentLevel--;
5361
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
namespace Microsoft.MixedReality.Toolkit.Core.Definitions.Lines
5+
{
6+
/// <summary>
7+
/// Defines how a base line data provider will transform its points
8+
/// </summary>
9+
public enum LinePointTransformMode
10+
{
11+
/// <summary>
12+
/// Use the local line transform. More reliable but with a performance cost.
13+
/// </summary>
14+
UseTransform,
15+
/// <summary>
16+
/// Use a matrix. Lines that are not active and enabled will not update point positions.
17+
/// </summary>
18+
UseMatrix,
19+
}
20+
}

Assets/MixedRealityToolkit/Definitions/Lines/LinePointTransformMode.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)