Skip to content

Commit 07ad30f

Browse files
committed
Refactor out MixedRealityControllerModelHelpers
1 parent 2925bd0 commit 07ad30f

File tree

6 files changed

+123
-132
lines changed

6 files changed

+123
-132
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.MixedReality.Toolkit.Utilities;
5+
using System;
6+
using UnityEngine;
7+
8+
namespace Microsoft.MixedReality.Toolkit.Input
9+
{
10+
/// <summary>
11+
/// Provides helpers for setting up the controller model with a visualization script.
12+
/// </summary>
13+
public static class MixedRealityControllerModelHelpers
14+
{
15+
private static MixedRealityControllerVisualizationProfile visualizationProfile = null;
16+
17+
/// <summary>
18+
/// Tries to read the controller visualization profile to apply a visualization script to the passed-in controller model.
19+
/// </summary>
20+
/// <remarks>Automatically disables DestroyOnSourceLost to encourage controller model creators to manage their life-cycle themselves.</remarks>
21+
/// <param name="controllerModel">The GameObject to modify.</param>
22+
/// <param name="controllerType">The type of controller this model represents.</param>
23+
/// <param name="handedness">The handedness of this controller.</param>
24+
/// <returns>True if a visualization script could be loaded and applied.</returns>
25+
public static bool TryAddVisualizationScript(GameObject controllerModel, Type controllerType, Handedness handedness)
26+
{
27+
if (controllerModel != null)
28+
{
29+
if (visualizationProfile == null && CoreServices.InputSystem?.InputSystemProfile != null)
30+
{
31+
visualizationProfile = CoreServices.InputSystem.InputSystemProfile.ControllerVisualizationProfile;
32+
}
33+
34+
if (visualizationProfile != null)
35+
{
36+
var visualizationType = visualizationProfile.GetControllerVisualizationTypeOverride(controllerType, handedness);
37+
if (visualizationType != null)
38+
{
39+
// Set the platform controller model to not be destroyed when the source is lost. It'll be disabled instead,
40+
// and re-enabled when the same controller is re-detected.
41+
if (controllerModel.EnsureComponent(visualizationType.Type) is IMixedRealityControllerPoseSynchronizer visualizer)
42+
{
43+
visualizer.DestroyOnSourceLost = false;
44+
}
45+
46+
return true;
47+
}
48+
else
49+
{
50+
Debug.LogError("Controller visualization type not defined for controller visualization profile");
51+
}
52+
}
53+
else
54+
{
55+
Debug.LogError("Failed to obtain a controller visualization profile");
56+
}
57+
}
58+
59+
return false;
60+
}
61+
}
62+
}

Assets/MRTK/Core/Definitions/Devices/MixedRealityControllerModelHelpers.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.

Assets/MRTK/Providers/OpenXR/Scripts/HPReverbG2Controller.cs

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ public class HPReverbG2Controller : GenericXRSDKController
2020
/// </summary>
2121
public HPReverbG2Controller(TrackingState trackingState, Handedness controllerHandedness, IMixedRealityInputSource inputSource = null, MixedRealityInteractionMapping[] interactions = null)
2222
: base(trackingState, controllerHandedness, inputSource, interactions, new HPMotionControllerDefinition(controllerHandedness))
23-
{
24-
}
23+
{ }
2524

2625
private Vector3 currentPointerPosition = Vector3.zero;
2726
private Quaternion currentPointerRotation = Quaternion.identity;
@@ -95,42 +94,22 @@ private async void TryRenderControllerModelWithModelProvider()
9594

9695
GameObject controllerModel = await controllerModelProvider.TryGenerateControllerModelFromPlatformSDK();
9796

98-
if (controllerModel != null)
97+
if (this != null)
9998
{
100-
if (this != null)
99+
if (controllerModel != null
100+
&& MixedRealityControllerModelHelpers.TryAddVisualizationScript(controllerModel, GetType(), ControllerHandedness)
101+
&& TryAddControllerModelToSceneHierarchy(controllerModel))
101102
{
102-
var visualizationProfile = GetControllerVisualizationProfile();
103-
if (visualizationProfile != null)
104-
{
105-
var visualizationType = visualizationProfile.GetControllerVisualizationTypeOverride(GetType(), ControllerHandedness);
106-
if (visualizationType != null)
107-
{
108-
// Set the platform controller model to not be destroyed when the source is lost. It'll be disabled instead,
109-
// and re-enabled when the same controller is re-detected.
110-
if (controllerModel.EnsureComponent(visualizationType.Type) is IMixedRealityControllerPoseSynchronizer visualizer)
111-
{
112-
visualizer.DestroyOnSourceLost = false;
113-
}
114-
115-
if (TryAddControllerModelToSceneHierarchy(controllerModel))
116-
{
117-
return;
118-
}
119-
}
120-
else
121-
{
122-
Debug.LogError("Controller visualization type not defined for controller visualization profile");
123-
}
124-
}
125-
else
126-
{
127-
Debug.LogError("Failed to obtain a controller visualization profile");
128-
}
129-
130-
Debug.LogWarning("Failed to create controller model from driver; defaulting to BaseController behavior.");
131-
base.TryRenderControllerModel(GetType(), InputSource.SourceType);
103+
controllerModel.SetActive(true);
104+
return;
132105
}
133106

107+
Debug.LogWarning("Failed to create controller model from driver; defaulting to BaseController behavior.");
108+
base.TryRenderControllerModel(GetType(), InputSource.SourceType);
109+
}
110+
111+
if (controllerModel != null)
112+
{
134113
// If we didn't successfully set up the model and add it to the hierarchy (which returns early), set it inactive.
135114
controllerModel.SetActive(false);
136115
}

Assets/MRTK/Providers/OpenXR/Scripts/MicrosoftMotionController.cs

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ public class MicrosoftMotionController : GenericXRSDKController
2424
/// </summary>
2525
public MicrosoftMotionController(TrackingState trackingState, Handedness controllerHandedness, IMixedRealityInputSource inputSource = null, MixedRealityInteractionMapping[] interactions = null)
2626
: base(trackingState, controllerHandedness, inputSource, interactions, new WindowsMixedRealityControllerDefinition(controllerHandedness))
27-
{
28-
}
27+
{ }
2928

3029
private Vector3 currentPointerPosition = Vector3.zero;
3130
private Quaternion currentPointerRotation = Quaternion.identity;
@@ -99,42 +98,22 @@ private async void TryRenderControllerModelWithModelProvider()
9998

10099
GameObject controllerModel = await controllerModelProvider.TryGenerateControllerModelFromPlatformSDK();
101100

102-
if (controllerModel != null)
101+
if (this != null)
103102
{
104-
if (this != null)
103+
if (controllerModel != null
104+
&& MixedRealityControllerModelHelpers.TryAddVisualizationScript(controllerModel, GetType(), ControllerHandedness)
105+
&& TryAddControllerModelToSceneHierarchy(controllerModel))
105106
{
106-
var visualizationProfile = GetControllerVisualizationProfile();
107-
if (visualizationProfile != null)
108-
{
109-
var visualizationType = visualizationProfile.GetControllerVisualizationTypeOverride(GetType(), ControllerHandedness);
110-
if (visualizationType != null)
111-
{
112-
// Set the platform controller model to not be destroyed when the source is lost. It'll be disabled instead,
113-
// and re-enabled when the same controller is re-detected.
114-
if (controllerModel.EnsureComponent(visualizationType.Type) is IMixedRealityControllerPoseSynchronizer visualizer)
115-
{
116-
visualizer.DestroyOnSourceLost = false;
117-
}
118-
119-
if (TryAddControllerModelToSceneHierarchy(controllerModel))
120-
{
121-
return;
122-
}
123-
}
124-
else
125-
{
126-
Debug.LogError("Controller visualization type not defined for controller visualization profile");
127-
}
128-
}
129-
else
130-
{
131-
Debug.LogError("Failed to obtain a controller visualization profile");
132-
}
133-
134-
Debug.LogWarning("Failed to create controller model from driver; defaulting to BaseController behavior.");
135-
base.TryRenderControllerModel(GetType(), InputSource.SourceType);
107+
controllerModel.SetActive(true);
108+
return;
136109
}
137110

111+
Debug.LogWarning("Failed to create controller model from driver; defaulting to BaseController behavior.");
112+
base.TryRenderControllerModel(GetType(), InputSource.SourceType);
113+
}
114+
115+
if (controllerModel != null)
116+
{
138117
// If we didn't successfully set up the model and add it to the hierarchy (which returns early), set it inactive.
139118
controllerModel.SetActive(false);
140119
}

Assets/MRTK/Providers/WindowsMixedReality/XR2018/Controllers/WindowsMixedRealityController.cs

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -259,42 +259,22 @@ private async void TryRenderControllerModelWithModelProvider()
259259

260260
UnityEngine.GameObject controllerModel = await controllerModelProvider.TryGenerateControllerModelFromPlatformSDK();
261261

262-
if (controllerModel != null)
262+
if (this != null)
263263
{
264-
if (this != null)
264+
if (controllerModel != null
265+
&& MixedRealityControllerModelHelpers.TryAddVisualizationScript(controllerModel, GetType(), ControllerHandedness)
266+
&& TryAddControllerModelToSceneHierarchy(controllerModel))
265267
{
266-
var visualizationProfile = GetControllerVisualizationProfile();
267-
if (visualizationProfile != null)
268-
{
269-
var visualizationType = visualizationProfile.GetControllerVisualizationTypeOverride(GetType(), ControllerHandedness);
270-
if (visualizationType != null)
271-
{
272-
// Set the platform controller model to not be destroyed when the source is lost. It'll be disabled instead,
273-
// and re-enabled when the same controller is re-detected.
274-
if (controllerModel.EnsureComponent(visualizationType.Type) is IMixedRealityControllerPoseSynchronizer visualizer)
275-
{
276-
visualizer.DestroyOnSourceLost = false;
277-
}
278-
279-
if (TryAddControllerModelToSceneHierarchy(controllerModel))
280-
{
281-
return;
282-
}
283-
}
284-
else
285-
{
286-
UnityEngine.Debug.LogError("Controller visualization type not defined for controller visualization profile");
287-
}
288-
}
289-
else
290-
{
291-
UnityEngine.Debug.LogError("Failed to obtain a controller visualization profile");
292-
}
293-
294-
UnityEngine.Debug.LogWarning("Failed to create controller model from driver; defaulting to BaseController behavior.");
295-
base.TryRenderControllerModel(GetType(), InputSource.SourceType);
268+
controllerModel.SetActive(true);
269+
return;
296270
}
297271

272+
UnityEngine.Debug.LogWarning("Failed to create controller model from driver; defaulting to BaseController behavior.");
273+
base.TryRenderControllerModel(GetType(), InputSource.SourceType);
274+
}
275+
276+
if (controllerModel != null)
277+
{
298278
// If we didn't successfully set up the model and add it to the hierarchy (which returns early), set it inactive.
299279
controllerModel.SetActive(false);
300280
}

Assets/MRTK/Providers/WindowsMixedReality/XRSDK/Controllers/WindowsMixedRealityXRSDKMotionController.cs

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -161,42 +161,22 @@ private async void TryRenderControllerModelWithModelProvider()
161161

162162
GameObject controllerModel = await controllerModelProvider.TryGenerateControllerModelFromPlatformSDK();
163163

164-
if (controllerModel != null)
164+
if (this != null)
165165
{
166-
if (this != null)
166+
if (controllerModel != null
167+
&& MixedRealityControllerModelHelpers.TryAddVisualizationScript(controllerModel, GetType(), ControllerHandedness)
168+
&& TryAddControllerModelToSceneHierarchy(controllerModel))
167169
{
168-
var visualizationProfile = GetControllerVisualizationProfile();
169-
if (visualizationProfile != null)
170-
{
171-
var visualizationType = visualizationProfile.GetControllerVisualizationTypeOverride(GetType(), ControllerHandedness);
172-
if (visualizationType != null)
173-
{
174-
// Set the platform controller model to not be destroyed when the source is lost. It'll be disabled instead,
175-
// and re-enabled when the same controller is re-detected.
176-
if (controllerModel.EnsureComponent(visualizationType.Type) is IMixedRealityControllerPoseSynchronizer visualizer)
177-
{
178-
visualizer.DestroyOnSourceLost = false;
179-
}
180-
181-
if (TryAddControllerModelToSceneHierarchy(controllerModel))
182-
{
183-
return;
184-
}
185-
}
186-
else
187-
{
188-
Debug.LogError("Controller visualization type not defined for controller visualization profile");
189-
}
190-
}
191-
else
192-
{
193-
Debug.LogError("Failed to obtain a controller visualization profile");
194-
}
195-
196-
Debug.LogWarning("Failed to create controller model from driver; defaulting to BaseController behavior.");
197-
base.TryRenderControllerModel(GetType(), InputSource.SourceType);
170+
controllerModel.SetActive(true);
171+
return;
198172
}
199173

174+
Debug.LogWarning("Failed to create controller model from driver; defaulting to BaseController behavior.");
175+
base.TryRenderControllerModel(GetType(), InputSource.SourceType);
176+
}
177+
178+
if (controllerModel != null)
179+
{
200180
// If we didn't successfully set up the model and add it to the hierarchy (which returns early), set it inactive.
201181
controllerModel.SetActive(false);
202182
}

0 commit comments

Comments
 (0)