Skip to content

Commit 5c26eb5

Browse files
Railboyradicalad
authored andcommitted
Small Performance Tweaks Round 2 (#3503)
* ToolTips only refresh content when changed * Update VectorRollingStatistics.cs * Improved ButtonBorder, removed allocations from Interactable Easing * Update Easing.cs * Updated Interactable themes to cache components, added Reset to InteractableThemePropertyValue
1 parent 234996a commit 5c26eb5

19 files changed

+300
-83
lines changed

Assets/MixedRealityToolkit.SDK/Features/UX/Interactable/Scripts/Layout/ButtonBorder.cs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,29 @@ public class ButtonBorder : MonoBehaviour
8080
[SerializeField]
8181
private bool OnlyInEditMode = true;
8282

83+
private Vector3 scale;
84+
private Vector3 startPosition;
85+
private Vector3 weighDirection;
86+
private Vector3 localPosition;
87+
private Vector3 anchorTransformLocalPosition;
88+
private Vector3 anchorTransformLocalScale;
89+
8390
/// <summary>
8491
/// Set the size and position
8592
/// </summary>
8693
private void UpdateSize()
8794
{
88-
Vector3 weighDireciton = new Vector3(Mathf.Abs(Alignment.x), Mathf.Abs(Alignment.y), Mathf.Abs(Alignment.z));
89-
Vector3 scale = weighDireciton * (Weight / BasePixelScale);// Vector3.Scale(Alignment, Scale) + Offset / BasePixelScale;
95+
// Vector3 weighDireciton = new Vector3(Mathf.Abs(Alignment.x), Mathf.Abs(Alignment.y), Mathf.Abs(Alignment.z));
96+
// Vector3 scale = weighDireciton * (Weight / BasePixelScale);// Vector3.Scale(Alignment, Scale) + Offset / BasePixelScale;
97+
98+
weighDirection.x = Mathf.Abs(Alignment.x);
99+
weighDirection.y = Mathf.Abs(Alignment.y);
100+
weighDirection.z = Mathf.Abs(Alignment.z);
101+
102+
scale.x = weighDirection.x * (Weight / BasePixelScale);
103+
scale.y = weighDirection.y * (Weight / BasePixelScale);
104+
scale.z = weighDirection.z * (Weight / BasePixelScale);
105+
90106
float size = ((Weight * 2) / BasePixelScale);
91107
if (scale.x > scale.y)
92108
{
@@ -100,14 +116,24 @@ private void UpdateSize()
100116

101117
transform.localScale = scale;
102118

103-
Vector3 startPosition = AnchorTransform.localPosition;
119+
anchorTransformLocalPosition = AnchorTransform.localPosition;
120+
anchorTransformLocalScale = AnchorTransform.localScale;
121+
122+
startPosition = anchorTransformLocalPosition;
104123

105124
if (AnchorTransform != this.transform)
106125
{
107-
startPosition = AnchorTransform.localPosition + (Vector3.Scale(AnchorTransform.localScale * 0.5f, Alignment));
126+
// startPosition = AnchorTransform.localPosition + (Vector3.Scale(AnchorTransform.localScale * 0.5f, Alignment));
127+
startPosition.x = anchorTransformLocalPosition.x + ((anchorTransformLocalScale.x * 0.5f) * Alignment.x);
128+
startPosition.y = anchorTransformLocalPosition.y + ((anchorTransformLocalScale.y * 0.5f) * Alignment.y);
129+
startPosition.z = anchorTransformLocalPosition.z + ((anchorTransformLocalScale.z * 0.5f) * Alignment.z);
108130
}
109131

110-
transform.localPosition = startPosition + (Alignment * Weight * 0.5f / BasePixelScale) + (PositionOffset / BasePixelScale);
132+
// transform.localPosition = startPosition + (Alignment * Weight * 0.5f / BasePixelScale) + (PositionOffset / BasePixelScale);
133+
localPosition.x = startPosition.x + (Alignment.x * Weight * 0.5f / BasePixelScale) + (PositionOffset.x / BasePixelScale);
134+
localPosition.y = startPosition.y + (Alignment.y * Weight * 0.5f / BasePixelScale) + (PositionOffset.y / BasePixelScale);
135+
localPosition.z = startPosition.z + (Alignment.z * Weight * 0.5f / BasePixelScale) + (PositionOffset.z / BasePixelScale);
136+
transform.localPosition = localPosition;
111137
}
112138

113139
// Update is called once per frame

Assets/MixedRealityToolkit.SDK/Features/UX/Interactable/Scripts/Themes/InteractableAnimatorTheme.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace Microsoft.MixedReality.Toolkit.SDK.UX.Interactable.Themes
1414
public class InteractableAnimatorTheme : InteractableThemeBase
1515
{
1616
private int lastIndex = 0;
17+
private Animator controller;
1718

1819
public InteractableAnimatorTheme()
1920
{
@@ -29,6 +30,12 @@ public InteractableAnimatorTheme()
2930
});
3031
}
3132

33+
public override void Init(GameObject host, InteractableThemePropertySettings settings)
34+
{
35+
base.Init(host, settings);
36+
controller = Host.GetComponent<Animator>();
37+
}
38+
3239
public override InteractableThemePropertyValue GetProperty(InteractableThemeProperty property)
3340
{
3441
InteractableThemePropertyValue start = new InteractableThemePropertyValue();
@@ -40,7 +47,6 @@ public override void SetValue(InteractableThemeProperty property, int index, flo
4047
{
4148
if(lastIndex != index)
4249
{
43-
Animator controller = Host.GetComponent<Animator>();
4450
if(controller != null)
4551
{
4652
controller.SetTrigger(property.Values[index].String);

Assets/MixedRealityToolkit.SDK/Features/UX/Interactable/Scripts/Themes/InteractableAudioTheme.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace Microsoft.MixedReality.Toolkit.SDK.UX.Interactable.Themes
1010
{
1111
public class InteractableAudioTheme : InteractableThemeBase
1212
{
13+
private AudioSource audioSource;
1314

1415
public InteractableAudioTheme()
1516
{
@@ -26,6 +27,12 @@ public InteractableAudioTheme()
2627
});
2728
}
2829

30+
public override void Init(GameObject host, InteractableThemePropertySettings settings)
31+
{
32+
base.Init(host, settings);
33+
audioSource = Host.GetComponentInChildren<AudioSource>();
34+
}
35+
2936
public override InteractableThemePropertyValue GetProperty(InteractableThemeProperty property)
3037
{
3138
InteractableThemePropertyValue start = new InteractableThemePropertyValue();
@@ -39,7 +46,6 @@ public override InteractableThemePropertyValue GetProperty(InteractableThemeProp
3946

4047
public override void SetValue(InteractableThemeProperty property, int index, float percentage)
4148
{
42-
AudioSource audioSource = Host.GetComponentInChildren<AudioSource>();
4349
if (audioSource == null)
4450
{
4551
audioSource = Host.AddComponent<AudioSource>();

Assets/MixedRealityToolkit.SDK/Features/UX/Interactable/Scripts/Themes/InteractableColorTheme.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ namespace Microsoft.MixedReality.Toolkit.SDK.UX.Interactable.Themes
1111
{
1212
public class InteractableColorTheme : InteractableShaderTheme
1313
{
14+
private TextMesh mesh;
15+
private Text text;
16+
1417
public InteractableColorTheme()
1518
{
1619
Types = new Type[] { typeof(Renderer), typeof(TextMesh), typeof(Text) };
@@ -26,17 +29,23 @@ public InteractableColorTheme()
2629
});
2730
}
2831

32+
public override void Init(GameObject host, InteractableThemePropertySettings settings)
33+
{
34+
base.Init(host, settings);
35+
mesh = Host.GetComponent<TextMesh>();
36+
text = Host.GetComponent<Text>();
37+
}
38+
2939
public override InteractableThemePropertyValue GetProperty(InteractableThemeProperty property)
3040
{
3141
InteractableThemePropertyValue color = new InteractableThemePropertyValue();
32-
TextMesh mesh = Host.GetComponent<TextMesh>();
42+
3343
if (mesh != null)
3444
{
3545
color.Color = mesh.color;
3646
return color;
3747
}
3848

39-
Text text = Host.GetComponent<Text>();
4049
if (text != null)
4150
{
4251
color.Color = text.color;
@@ -49,14 +58,13 @@ public override InteractableThemePropertyValue GetProperty(InteractableThemeProp
4958
public override void SetValue(InteractableThemeProperty property, int index, float percentage)
5059
{
5160
Color color = Color.Lerp(property.StartValue.Color, property.Values[index].Color, percentage);
52-
TextMesh mesh = Host.GetComponent<TextMesh>();
61+
5362
if (mesh != null)
5463
{
5564
mesh.color = color;
5665
return;
5766
}
5867

59-
Text text = Host.GetComponent<Text>();
6068
if (text != null)
6169
{
6270
text.color = color;

Assets/MixedRealityToolkit.SDK/Features/UX/Interactable/Scripts/Themes/InteractableMaterialTheme.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ namespace Microsoft.MixedReality.Toolkit.SDK.UX.Interactable.Themes
1111
public class InteractableMaterialTheme : InteractableThemeBase
1212
{
1313
private Material material = null;
14+
private Renderer renderer;
15+
16+
public override void Init(GameObject host, InteractableThemePropertySettings settings)
17+
{
18+
base.Init(host, settings);
19+
20+
renderer = Host.GetComponent<Renderer>();
21+
}
1422

1523
public InteractableMaterialTheme()
1624
{
@@ -30,7 +38,7 @@ public InteractableMaterialTheme()
3038
public override InteractableThemePropertyValue GetProperty(InteractableThemeProperty property)
3139
{
3240
InteractableThemePropertyValue start = new InteractableThemePropertyValue();
33-
Renderer renderer = Host.GetComponent<Renderer>();
41+
3442
material = renderer.material;
3543
start.Material = material;
3644
return start;
@@ -40,7 +48,6 @@ public override void SetValue(InteractableThemeProperty property, int index, flo
4048
{
4149
Host.SetActive(property.Values[index].Bool);
4250

43-
Renderer renderer = Host.GetComponent<Renderer>();
4451
material = property.Values[index].Material;
4552
renderer.material = material;
4653
}

Assets/MixedRealityToolkit.SDK/Features/UX/Interactable/Scripts/Themes/InteractableOffsetTheme.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace Microsoft.MixedReality.Toolkit.SDK.UX.Interactable.Themes
1111
public class InteractableOffsetTheme : InteractableThemeBase
1212
{
1313
private Vector3 startPosition;
14+
private Transform hostTransform;
1415

1516
public InteractableOffsetTheme()
1617
{
@@ -29,19 +30,20 @@ public InteractableOffsetTheme()
2930
public override void Init(GameObject host, InteractableThemePropertySettings settings)
3031
{
3132
base.Init(host, settings);
32-
startPosition = Host.transform.localPosition;
33+
hostTransform = Host.transform;
34+
startPosition = hostTransform.localPosition;
3335
}
3436

3537
public override InteractableThemePropertyValue GetProperty(InteractableThemeProperty property)
3638
{
3739
InteractableThemePropertyValue start = new InteractableThemePropertyValue();
38-
start.Vector3 = Host.transform.localPosition;
40+
start.Vector3 = hostTransform.localPosition;
3941
return start;
4042
}
4143

4244
public override void SetValue(InteractableThemeProperty property, int index, float percentage)
4345
{
44-
Host.transform.localPosition = Vector3.Lerp(property.StartValue.Vector3, startPosition + property.Values[index].Vector3, percentage);
46+
hostTransform.localPosition = Vector3.Lerp(property.StartValue.Vector3, startPosition + property.Values[index].Vector3, percentage);
4547
}
4648
}
4749
}

Assets/MixedRealityToolkit.SDK/Features/UX/Interactable/Scripts/Themes/InteractableRotationTheme.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace Microsoft.MixedReality.Toolkit.SDK.UX.Interactable.Themes
1010
{
1111
public class InteractableRotationTheme : InteractableThemeBase
1212
{
13+
private Transform hostTransform;
1314

1415
public InteractableRotationTheme()
1516
{
@@ -25,16 +26,23 @@ public InteractableRotationTheme()
2526
});
2627
}
2728

29+
public override void Init(GameObject host, InteractableThemePropertySettings settings)
30+
{
31+
base.Init(host, settings);
32+
33+
hostTransform = Host.transform;
34+
}
35+
2836
public override InteractableThemePropertyValue GetProperty(InteractableThemeProperty property)
2937
{
3038
InteractableThemePropertyValue start = new InteractableThemePropertyValue();
31-
start.Vector3 = Host.transform.eulerAngles;
39+
start.Vector3 = hostTransform.eulerAngles;
3240
return start;
3341
}
3442

3543
public override void SetValue(InteractableThemeProperty property, int index, float percentage)
3644
{
37-
Host.transform.localRotation = Quaternion.Euler( Vector3.Lerp(property.StartValue.Vector3, property.Values[index].Vector3, percentage));
45+
hostTransform.localRotation = Quaternion.Euler( Vector3.Lerp(property.StartValue.Vector3, property.Values[index].Vector3, percentage));
3846
}
3947
}
4048
}

Assets/MixedRealityToolkit.SDK/Features/UX/Interactable/Scripts/Themes/InteractableScaleTheme.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ namespace Microsoft.MixedReality.Toolkit.SDK.UX.Interactable.Themes
1010
{
1111
public class InteractableScaleTheme : InteractableThemeBase
1212
{
13+
private Transform hostTransform;
14+
15+
public override void Init(GameObject host, InteractableThemePropertySettings settings)
16+
{
17+
base.Init(host, settings);
18+
19+
hostTransform = Host.transform;
20+
}
1321

1422
public InteractableScaleTheme()
1523
{
@@ -28,13 +36,13 @@ public InteractableScaleTheme()
2836
public override InteractableThemePropertyValue GetProperty(InteractableThemeProperty property)
2937
{
3038
InteractableThemePropertyValue start = new InteractableThemePropertyValue();
31-
start.Vector3 = Host.transform.localScale;
39+
start.Vector3 = hostTransform.localScale;
3240
return start;
3341
}
3442

3543
public override void SetValue(InteractableThemeProperty property, int index, float percentage)
3644
{
37-
Host.transform.localScale = Vector3.Lerp(property.StartValue.Vector3, property.Values[index].Vector3, percentage);
45+
hostTransform.localScale = Vector3.Lerp(property.StartValue.Vector3, property.Values[index].Vector3, percentage);
3846
}
3947
}
4048
}

Assets/MixedRealityToolkit.SDK/Features/UX/Interactable/Scripts/Themes/InteractableShaderTheme.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ namespace Microsoft.MixedReality.Toolkit.SDK.UX.Interactable.Themes
1111
{
1212
public class InteractableShaderTheme : InteractableThemeBase
1313
{
14+
private static InteractableThemePropertyValue emptyValue = new InteractableThemePropertyValue();
15+
1416
protected MaterialPropertyBlock propertyBlock;
1517
protected List<ShaderProperties> shaderProperties;
18+
protected Renderer renderer;
19+
20+
private InteractableThemePropertyValue startValue = new InteractableThemePropertyValue();
1621

1722
public InteractableShaderTheme()
1823
{
@@ -43,6 +48,8 @@ public override void Init(GameObject host, InteractableThemePropertySettings set
4348
}
4449

4550
propertyBlock = InteractableThemeShaderUtils.GetMaterialPropertyBlock(host, shaderProperties.ToArray());
51+
52+
renderer = Host.GetComponent<Renderer>();
4653
}
4754

4855
public override void SetValue(InteractableThemeProperty property, int index, float percentage)
@@ -70,32 +77,33 @@ public override void SetValue(InteractableThemeProperty property, int index, flo
7077
break;
7178
}
7279

73-
SetPropertyBlock(Host, propertyBlock);
80+
renderer.SetPropertyBlock(propertyBlock);
7481
}
7582

7683
public override InteractableThemePropertyValue GetProperty(InteractableThemeProperty property)
7784
{
7885
if (Host == null)
79-
return new InteractableThemePropertyValue();
86+
return emptyValue;
87+
88+
startValue.Reset();
8089

81-
InteractableThemePropertyValue start = new InteractableThemePropertyValue();
8290
string propId = property.GetShaderPropId();
8391
switch (property.Type)
8492
{
8593
case InteractableThemePropertyValueTypes.Color:
86-
start.Color = propertyBlock.GetVector(propId);
94+
startValue.Color = propertyBlock.GetVector(propId);
8795
break;
8896
case InteractableThemePropertyValueTypes.ShaderFloat:
89-
start.Float = propertyBlock.GetFloat(propId);
97+
startValue.Float = propertyBlock.GetFloat(propId);
9098
break;
9199
case InteractableThemePropertyValueTypes.shaderRange:
92-
start.Float = propertyBlock.GetFloat(propId);
100+
startValue.Float = propertyBlock.GetFloat(propId);
93101
break;
94102
default:
95103
break;
96104
}
97105

98-
return start;
106+
return startValue;
99107
}
100108

101109
public static float GetFloat(GameObject host, string propId)

0 commit comments

Comments
 (0)