diff --git a/Assets/MRTK/Extensions/LostTrackingService/LostTrackingService.cs b/Assets/MRTK/Extensions/LostTrackingService/LostTrackingService.cs index 4e27644f1a2..80fb87c5ffe 100644 --- a/Assets/MRTK/Extensions/LostTrackingService/LostTrackingService.cs +++ b/Assets/MRTK/Extensions/LostTrackingService/LostTrackingService.cs @@ -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 { @@ -72,20 +77,55 @@ public LostTrackingService( /// 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"); + /// - 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 + /// + public void EditorSetTrackingLost(bool trackingLost) => SetTrackingLost(trackingLost); +#endif // UNITY_EDITOR private static readonly ProfilerMarker DisableTrackingLostVisualPerfMarker = new ProfilerMarker("[MRTK] LostTrackingService.DisableTrackingLostVisual"); @@ -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) @@ -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 XRSessionSubsystems = new List(); + + /// + /// The XR SDK display subsystem for the currently loaded XR plug-in. + /// + 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 } -} \ No newline at end of file +} diff --git a/Assets/MRTK/Extensions/LostTrackingService/Microsoft.MixedReality.Toolkit.Extensions.Tracking.asmdef b/Assets/MRTK/Extensions/LostTrackingService/Microsoft.MixedReality.Toolkit.Extensions.Tracking.asmdef index 19022c2fffc..d9790fc6db8 100644 --- a/Assets/MRTK/Extensions/LostTrackingService/Microsoft.MixedReality.Toolkit.Extensions.Tracking.asmdef +++ b/Assets/MRTK/Extensions/LostTrackingService/Microsoft.MixedReality.Toolkit.Extensions.Tracking.asmdef @@ -1,7 +1,8 @@ { "name": "Microsoft.MixedReality.Toolkit.Extensions.Tracking", "references": [ - "Microsoft.MixedReality.Toolkit" + "Microsoft.MixedReality.Toolkit", + "Unity.XR.ARSubsystems" ], "optionalUnityReferences": [], "includePlatforms": [], diff --git a/Assets/MRTK/Providers/UnityAR/Editor/UnityARConfigurationChecker.cs b/Assets/MRTK/Providers/UnityAR/Editor/UnityARConfigurationChecker.cs index 5439f12e511..1efcdaf922e 100644 --- a/Assets/MRTK/Providers/UnityAR/Editor/UnityARConfigurationChecker.cs +++ b/Assets/MRTK/Providers/UnityAR/Editor/UnityARConfigurationChecker.cs @@ -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; } }