Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update LostTrackingService for Unity 2020 and XRSDK/WMR #8282

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 85 additions & 12 deletions Assets/MRTK/Extensions/LostTrackingService/LostTrackingService.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
// 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;

#if UNITY_2018
using UnityEngine.Experimental;
#endif // UNITY_2018
#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 +81,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 +227,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 +246,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