Skip to content

Commit

Permalink
Update LostTrackingService to use XRSessionSubsystem
Browse files Browse the repository at this point in the history
  • Loading branch information
keveleigh committed Aug 12, 2020
1 parent 7d35a31 commit f578874
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 13 deletions.
93 changes: 81 additions & 12 deletions Assets/MRTK/Extensions/LostTrackingService/LostTrackingService.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.MixedReality.Toolkit.Utilities;
using System;
using Unity.Profiling;
using UnityEngine;
using Microsoft.MixedReality.Toolkit.Utilities;

#if UNITY_WSA
#if ARFOUNDATION_PRESENT
using System.Collections.Generic;
using UnityEngine.XR.ARSubsystems;
#endif // ARFOUNDATION_PRESENT

#if UNITY_WSA && !UNITY_2020_1_OR_NEWER
using UnityEngine.XR.WSA;
#endif
#endif // UNITY_WSA && !UNITY_2020_1_OR_NEWER

namespace Microsoft.MixedReality.Toolkit.Extensions.Tracking
{
Expand Down Expand Up @@ -72,20 +77,55 @@ public LostTrackingService(
/// <inheritdoc />
public override void Initialize()
{
#if UNITY_WSA
#if UNITY_WSA && !UNITY_2020_1_OR_NEWER
WorldManager.OnPositionalLocatorStateChanged += OnPositionalLocatorStateChanged;
#else
#elif !ARFOUNDATION_PRESENT
Debug.LogWarning("This service is not supported on this platform.");
#endif
}

#if UNITY_EDITOR
#if ARFOUNDATION_PRESENT
private UnityEngine.XR.ARSubsystems.TrackingState lastTrackingState = UnityEngine.XR.ARSubsystems.TrackingState.None;
private NotTrackingReason lastNotTrackingReason = NotTrackingReason.None;

private static readonly ProfilerMarker UpdatePerfMarker = new ProfilerMarker("[MRTK] LostTrackingService.Update");

/// <inheritdoc />
public void EditorSetTrackingLost(bool trackingLost)
public override void Update()
{
SetTrackingLost(trackingLost);
using (UpdatePerfMarker.Auto())
{
XRSessionSubsystem sessionSubsystem = SessionSubsystem;
if (sessionSubsystem == null)
{
return;
}

if (sessionSubsystem.trackingState == lastTrackingState && sessionSubsystem.notTrackingReason == lastNotTrackingReason)
{
return;
}

// This combination of states is from the Windows XR Plugin docs, describing the combination when positional tracking is inhibited.
if (sessionSubsystem.trackingState == UnityEngine.XR.ARSubsystems.TrackingState.None && sessionSubsystem.notTrackingReason == NotTrackingReason.Relocalizing)
{
SetTrackingLost(true);
}
else
{
SetTrackingLost(false);
}

lastTrackingState = sessionSubsystem.trackingState;
lastNotTrackingReason = sessionSubsystem.notTrackingReason;
}
}
#endif
#endif // ARFOUNDATION_PRESENT

#if UNITY_EDITOR
/// <inheritdoc />
public void EditorSetTrackingLost(bool trackingLost) => SetTrackingLost(trackingLost);
#endif // UNITY_EDITOR

private static readonly ProfilerMarker DisableTrackingLostVisualPerfMarker = new ProfilerMarker("[MRTK] LostTrackingService.DisableTrackingLostVisual");

Expand Down Expand Up @@ -183,7 +223,7 @@ private void SetTrackingLost(bool trackingLost)
}
}

#if UNITY_WSA
#if UNITY_WSA && !UNITY_2020_1_OR_NEWER
private static readonly ProfilerMarker OnPositionLocatorStateChangedPerfMarker = new ProfilerMarker("[MRTK] LostTrackingService.OnPositionalLocatorStateChanged");

private void OnPositionalLocatorStateChanged(PositionalLocatorState oldState, PositionalLocatorState newState)
Expand All @@ -202,6 +242,35 @@ private void OnPositionalLocatorStateChanged(PositionalLocatorState oldState, Po
}
}
}
#endif
#endif // UNITY_WSA && !UNITY_2020_1_OR_NEWER

#if ARFOUNDATION_PRESENT
private static XRSessionSubsystem sessionSubsystem = null;
private static readonly List<XRSessionSubsystem> XRSessionSubsystems = new List<XRSessionSubsystem>();

/// <summary>
/// The XR SDK display subsystem for the currently loaded XR plug-in.
/// </summary>
private static XRSessionSubsystem SessionSubsystem
{
get
{
if (sessionSubsystem == null || !sessionSubsystem.running)
{
sessionSubsystem = null;
SubsystemManager.GetInstances(XRSessionSubsystems);
foreach (XRSessionSubsystem xrSessionSubsystem in XRSessionSubsystems)
{
if (xrSessionSubsystem.running)
{
sessionSubsystem = xrSessionSubsystem;
break;
}
}
}
return sessionSubsystem;
}
}
#endif // ARFOUNDATION_PRESENT
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "Microsoft.MixedReality.Toolkit.Extensions.Tracking",
"references": [
"Microsoft.MixedReality.Toolkit"
"Microsoft.MixedReality.Toolkit",
"Unity.XR.ARSubsystems"
],
"optionalUnityReferences": [],
"includePlatforms": [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ private static bool ReconcileArFoundationDefine()
{
ScriptUtilities.AppendScriptingDefinitions(BuildTargetGroup.Android, definitions);
ScriptUtilities.AppendScriptingDefinitions(BuildTargetGroup.iOS, definitions);
ScriptUtilities.AppendScriptingDefinitions(BuildTargetGroup.WSA, definitions);
return true;
}
else
{
ScriptUtilities.RemoveScriptingDefinitions(BuildTargetGroup.Android, definitions);
ScriptUtilities.RemoveScriptingDefinitions(BuildTargetGroup.iOS, definitions);
ScriptUtilities.RemoveScriptingDefinitions(BuildTargetGroup.WSA, definitions);
return false;
}
}
Expand Down

0 comments on commit f578874

Please sign in to comment.