From 3189e7adfef61290521335f03c5c9997b57c1007 Mon Sep 17 00:00:00 2001 From: Kurtis Eveleigh Date: Tue, 11 Aug 2020 17:26:46 -0700 Subject: [PATCH 1/3] Update LostTrackingService to use XRSessionSubsystem --- .../LostTrackingService.cs | 93 ++++++++++++++++--- ...Reality.Toolkit.Extensions.Tracking.asmdef | 3 +- .../Editor/UnityARConfigurationChecker.cs | 2 + 3 files changed, 85 insertions(+), 13 deletions(-) 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; } } From 5c10fe6c73c1fc97c0ca1957beb631953c1410b0 Mon Sep 17 00:00:00 2001 From: Kurtis Eveleigh Date: Tue, 11 Aug 2020 17:42:22 -0700 Subject: [PATCH 2/3] Fix Unity 2018 namespaces --- .../Extensions/LostTrackingService/LostTrackingService.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Assets/MRTK/Extensions/LostTrackingService/LostTrackingService.cs b/Assets/MRTK/Extensions/LostTrackingService/LostTrackingService.cs index 80fb87c5ffe..1feaf3faa85 100644 --- a/Assets/MRTK/Extensions/LostTrackingService/LostTrackingService.cs +++ b/Assets/MRTK/Extensions/LostTrackingService/LostTrackingService.cs @@ -9,6 +9,10 @@ #if ARFOUNDATION_PRESENT using System.Collections.Generic; using UnityEngine.XR.ARSubsystems; + +#if UNITY_2018 +using UnityEngine.Experimental; +#endif // UNITY_2018 #endif // ARFOUNDATION_PRESENT #if UNITY_WSA && !UNITY_2020_1_OR_NEWER From 475704aab368d2eabd027dc7604edd06b969bae2 Mon Sep 17 00:00:00 2001 From: Kurtis Eveleigh Date: Tue, 18 Aug 2020 13:23:30 -0700 Subject: [PATCH 3/3] ARFOUNDATION_PRESENT -> ARSUBSYSTEMS_ENABLED --- .../LostTrackingService/LostTrackingService.cs | 14 +++++++------- ...MixedReality.Toolkit.Extensions.Tracking.asmdef | 11 +++++++++-- .../UnityAR/Editor/UnityARConfigurationChecker.cs | 4 +--- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Assets/MRTK/Extensions/LostTrackingService/LostTrackingService.cs b/Assets/MRTK/Extensions/LostTrackingService/LostTrackingService.cs index 1feaf3faa85..a8705cedb33 100644 --- a/Assets/MRTK/Extensions/LostTrackingService/LostTrackingService.cs +++ b/Assets/MRTK/Extensions/LostTrackingService/LostTrackingService.cs @@ -6,14 +6,14 @@ using Unity.Profiling; using UnityEngine; -#if ARFOUNDATION_PRESENT +#if ARSUBSYSTEMS_ENABLED using System.Collections.Generic; using UnityEngine.XR.ARSubsystems; #if UNITY_2018 using UnityEngine.Experimental; #endif // UNITY_2018 -#endif // ARFOUNDATION_PRESENT +#endif // ARSUBSYSTEMS_ENABLED #if UNITY_WSA && !UNITY_2020_1_OR_NEWER using UnityEngine.XR.WSA; @@ -83,12 +83,12 @@ public override void Initialize() { #if UNITY_WSA && !UNITY_2020_1_OR_NEWER WorldManager.OnPositionalLocatorStateChanged += OnPositionalLocatorStateChanged; -#elif !ARFOUNDATION_PRESENT +#elif !ARSUBSYSTEMS_ENABLED Debug.LogWarning("This service is not supported on this platform."); #endif } -#if ARFOUNDATION_PRESENT +#if ARSUBSYSTEMS_ENABLED private UnityEngine.XR.ARSubsystems.TrackingState lastTrackingState = UnityEngine.XR.ARSubsystems.TrackingState.None; private NotTrackingReason lastNotTrackingReason = NotTrackingReason.None; @@ -124,7 +124,7 @@ public override void Update() lastNotTrackingReason = sessionSubsystem.notTrackingReason; } } -#endif // ARFOUNDATION_PRESENT +#endif // ARSUBSYSTEMS_ENABLED #if UNITY_EDITOR /// @@ -248,7 +248,7 @@ private void OnPositionalLocatorStateChanged(PositionalLocatorState oldState, Po } #endif // UNITY_WSA && !UNITY_2020_1_OR_NEWER -#if ARFOUNDATION_PRESENT +#if ARSUBSYSTEMS_ENABLED private static XRSessionSubsystem sessionSubsystem = null; private static readonly List XRSessionSubsystems = new List(); @@ -275,6 +275,6 @@ private static XRSessionSubsystem SessionSubsystem return sessionSubsystem; } } -#endif // ARFOUNDATION_PRESENT +#endif // ARSUBSYSTEMS_ENABLED } } 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 d9790fc6db8..6a30018967b 100644 --- a/Assets/MRTK/Extensions/LostTrackingService/Microsoft.MixedReality.Toolkit.Extensions.Tracking.asmdef +++ b/Assets/MRTK/Extensions/LostTrackingService/Microsoft.MixedReality.Toolkit.Extensions.Tracking.asmdef @@ -4,12 +4,19 @@ "Microsoft.MixedReality.Toolkit", "Unity.XR.ARSubsystems" ], - "optionalUnityReferences": [], "includePlatforms": [], "excludePlatforms": [], "allowUnsafeCode": false, "overrideReferences": false, "precompiledReferences": [], "autoReferenced": true, - "defineConstraints": [] + "defineConstraints": [], + "versionDefines": [ + { + "name": "com.unity.xr.arsubsystems", + "expression": "", + "define": "ARSUBSYSTEMS_ENABLED" + } + ], + "noEngineReferences": false } \ No newline at end of file diff --git a/Assets/MRTK/Providers/UnityAR/Editor/UnityARConfigurationChecker.cs b/Assets/MRTK/Providers/UnityAR/Editor/UnityARConfigurationChecker.cs index 1efcdaf922e..f87126a400f 100644 --- a/Assets/MRTK/Providers/UnityAR/Editor/UnityARConfigurationChecker.cs +++ b/Assets/MRTK/Providers/UnityAR/Editor/UnityARConfigurationChecker.cs @@ -41,14 +41,12 @@ 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; } } @@ -104,7 +102,7 @@ private static void UpdateAsmDef(bool arFoundationPresent) if (arFoundationPresent) { -#if UNITY_2018 || UNITY_2019_1_OR_NEWER +#if UNITY_2018_1_OR_NEWER if (!references.Contains(arFoundationReference)) { // Add a reference to the ARFoundation assembly