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 all 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 ARSUBSYSTEMS_ENABLED
using System.Collections.Generic;
using UnityEngine.XR.ARSubsystems;

#if UNITY_2018
using UnityEngine.Experimental;
#endif // UNITY_2018
#endif // ARSUBSYSTEMS_ENABLED

#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 !ARSUBSYSTEMS_ENABLED
Debug.LogWarning("This service is not supported on this platform.");
#endif
}

#if UNITY_EDITOR
#if ARSUBSYSTEMS_ENABLED
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 // ARSUBSYSTEMS_ENABLED

#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 ARSUBSYSTEMS_ENABLED
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 // ARSUBSYSTEMS_ENABLED
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
{
"name": "Microsoft.MixedReality.Toolkit.Extensions.Tracking",
"references": [
"Microsoft.MixedReality.Toolkit"
"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
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,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
Expand Down