Skip to content

Commit

Permalink
Merge pull request microsoft#3187 from Microsoft/htk_development
Browse files Browse the repository at this point in the history
Move editor controller model fix from htk_dev to master for release
  • Loading branch information
keveleigh authored Nov 26, 2018
2 parents a25d437 + 5c00153 commit 049b6c2
Showing 1 changed file with 50 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public class MotionControllerVisualizer : Singleton<MotionControllerVisualizer>

public event Action<MotionControllerInfo> OnControllerModelLoaded;
public event Action<MotionControllerInfo> OnControllerModelUnloaded;

private bool leftModelIsAlternate = false;
private bool rightModelIsAlternate = false;
#endif

#if UNITY_EDITOR_WIN
Expand All @@ -74,14 +77,6 @@ protected override void Awake()
base.Awake();

#if UNITY_WSA && UNITY_2017_2_OR_NEWER
foreach (var sourceState in InteractionManager.GetCurrentReading())
{
if (sourceState.source.kind == InteractionSourceKind.Controller)
{
StartTrackingController(sourceState.source);
}
}

Application.onBeforeRender += Application_onBeforeRender;

if (GLTFMaterial == null)
Expand Down Expand Up @@ -116,23 +111,41 @@ protected override void OnDestroy()
InteractionManager.InteractionSourceDetected -= InteractionManager_InteractionSourceDetected;
InteractionManager.InteractionSourceLost -= InteractionManager_InteractionSourceLost;
Application.onBeforeRender -= Application_onBeforeRender;

foreach (MotionControllerInfo controllerInfo in controllerDictionary.Values)
{
Destroy(controllerInfo.ControllerParent);
}
#endif
}

private void Application_onBeforeRender()
{
// NOTE: This work is being done here to present the most correct rendered location of the controller each frame.
// Any app logic depending on the controller state should happen in Update() or using InteractionManager's events.
UpdateControllerState();
// We don't want to potentially start loading a new controller model in this call, since onBeforeRender shouldn't
// do much work for performance reasons.
UpdateControllerState(false);
}

private void UpdateControllerState()
private void UpdateControllerState(bool createNewControllerIfNeeded = true)
{
#if UNITY_WSA && UNITY_2017_2_OR_NEWER
foreach (var sourceState in InteractionManager.GetCurrentReading())
{
if (sourceState.source.kind != InteractionSourceKind.Controller)
{
continue;
}

string key = GenerateKey(sourceState.source);
if (createNewControllerIfNeeded && !controllerDictionary.ContainsKey(key) && !loadingControllers.Contains(key))
{
StartTrackingController(sourceState.source);
}

MotionControllerInfo currentController;
if (sourceState.source.kind == InteractionSourceKind.Controller && controllerDictionary.TryGetValue(GenerateKey(sourceState.source), out currentController))
if (controllerDictionary.TryGetValue(key, out currentController))
{
if (AnimateControllerModel)
{
Expand Down Expand Up @@ -236,7 +249,8 @@ private void InteractionManager_InteractionSourceLost(InteractionSourceLostEvent
if (source.kind == InteractionSourceKind.Controller)
{
MotionControllerInfo controllerInfo;
if (controllerDictionary != null && controllerDictionary.TryGetValue(GenerateKey(source), out controllerInfo))
string key = GenerateKey(source);
if (controllerDictionary != null && controllerDictionary.TryGetValue(key, out controllerInfo))
{
if (OnControllerModelUnloaded != null)
{
Expand All @@ -246,10 +260,22 @@ private void InteractionManager_InteractionSourceLost(InteractionSourceLostEvent
if (controllerInfo.Handedness == InteractionSourceHandedness.Left)
{
leftControllerModel = null;

if (!AlwaysUseAlternateLeftModel && leftModelIsAlternate)
{
controllerDictionary.Remove(key);
Destroy(controllerInfo.ControllerParent);
}
}
else if (controllerInfo.Handedness == InteractionSourceHandedness.Right)
{
rightControllerModel = null;

if (!AlwaysUseAlternateRightModel && rightModelIsAlternate)
{
controllerDictionary.Remove(key);
Destroy(controllerInfo.ControllerParent);
}
}

controllerInfo.ControllerParent.SetActive(false);
Expand Down Expand Up @@ -380,6 +406,15 @@ private IEnumerator LoadSourceControllerModel(InteractionSource source)

yield return sceneImporter.Load();

if (source.handedness == InteractionSourceHandedness.Left)
{
leftModelIsAlternate = false;
}
else if (source.handedness == InteractionSourceHandedness.Right)
{
rightModelIsAlternate = false;
}

FinishControllerSetup(controllerModelGameObject, source.handedness, GenerateKey(source));
}

Expand All @@ -389,10 +424,12 @@ private void LoadAlternateControllerModel(InteractionSource source)
if (source.handedness == InteractionSourceHandedness.Left && AlternateLeftController != null)
{
controllerModelGameObject = Instantiate(AlternateLeftController);
leftModelIsAlternate = true;
}
else if (source.handedness == InteractionSourceHandedness.Right && AlternateRightController != null)
{
controllerModelGameObject = Instantiate(AlternateRightController);
rightModelIsAlternate = true;
}
else
{
Expand All @@ -412,7 +449,7 @@ private void FinishControllerSetup(GameObject controllerModelGameObject, Interac
{
var parentGameObject = new GameObject
{
name = handedness + "Controller"
name = dictionaryKey + "Controller"
};

parentGameObject.transform.parent = transform;
Expand Down

0 comments on commit 049b6c2

Please sign in to comment.