Skip to content

Commit 82ed48a

Browse files
committed
Refactor out IWindowsMixedRealityUtilitiesProvider
for Unity backend-specific implementations
1 parent cd0098f commit 82ed48a

8 files changed

+91
-21
lines changed

Assets/MixedRealityToolkit.Providers/WindowsMixedReality/Definitions/HolographicFrameNativeData.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ public struct HolographicFrameNativeData
2424
public uint MaxNumberOfCameras;
2525

2626
/// <summary>
27-
/// The current native root <see href="https://docs.microsoft.com/uwp/api/windows.perception.spatial.spatialcoordinatesystem">ISpatialCoordinateSystem</see>).
27+
/// The current native root <see href="https://docs.microsoft.com/uwp/api/windows.perception.spatial.spatialcoordinatesystem">ISpatialCoordinateSystem</see>.
2828
/// </summary>
2929
public IntPtr ISpatialCoordinateSystemPtr;
3030

3131
/// <summary>
32-
/// The current native <see href="https://docs.microsoft.com/uwp/api/Windows.Graphics.Holographic.HolographicFrame">IHolographicFrame</see>).
32+
/// The current native <see href="https://docs.microsoft.com/uwp/api/Windows.Graphics.Holographic.HolographicFrame">IHolographicFrame</see>.
3333
/// </summary>
3434
public IntPtr IHolographicFramePtr;
3535

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
6+
namespace Microsoft.MixedReality.Toolkit.WindowsMixedReality
7+
{
8+
public interface IWindowsMixedRealityUtilitiesProvider
9+
{
10+
/// <summary>
11+
/// The current native root <see href="https://docs.microsoft.com/uwp/api/windows.perception.spatial.spatialcoordinatesystem">ISpatialCoordinateSystem</see>.
12+
/// </summary>
13+
IntPtr ISpatialCoordinateSystemPtr { get; }
14+
15+
/// <summary>
16+
/// The current native <see href="https://docs.microsoft.com/uwp/api/Windows.Graphics.Holographic.HolographicFrame">IHolographicFrame</see>.
17+
/// </summary>
18+
IntPtr IHolographicFramePtr { get; }
19+
}
20+
}

Assets/MixedRealityToolkit.Providers/WindowsMixedReality/Shared/Definitions/IWindowsMixedRealityUtilitiesProvider.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/MixedRealityToolkit.Providers/WindowsMixedReality/Shared/WindowsMixedRealityReprojectionUpdater.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private void OnPostRender()
3131
if (isDepthReprojectionModeSupported &&
3232
(ReprojectionMethod == HolographicDepthReprojectionMethod.AutoPlanar))
3333
{
34-
Microsoft.Windows.Graphics.Holographic.HolographicFrame frame = Input.WindowsMixedRealityUtilities.CurrentHolographicFrame;
34+
Microsoft.Windows.Graphics.Holographic.HolographicFrame frame = WindowsMixedRealityUtilities.CurrentHolographicFrame;
3535
foreach (var cameraPose in frame?.CurrentPrediction.CameraPoses)
3636
{
3737
if (CameraSupportsAutoPlanar(cameraPose.HolographicCamera))

Assets/MixedRealityToolkit.Providers/WindowsMixedReality/WindowsMixedRealityDeviceManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Microsoft.MixedReality.Toolkit.Windows.Utilities;
88
using UnityEngine;
99
using System;
10-
using Microsoft.MixedReality.Toolkit.Utilities.Editor;
1110

1211
#if UNITY_WSA
1312
using System.Collections.Generic;
@@ -321,6 +320,8 @@ public override void Enable()
321320

322321
if (InputSystemProfile == null) { return; }
323322

323+
WindowsMixedRealityUtilities.WmrUtilitiesProvider = new WindowsMixedRealityUtilitiesProvider();
324+
324325
if (InputSystemProfile.GesturesProfile != null)
325326
{
326327
var gestureProfile = InputSystemProfile.GesturesProfile;

Assets/MixedRealityToolkit.Providers/WindowsMixedReality/WindowsMixedRealityUtilities.cs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#if (UNITY_WSA && DOTNETWINRT_PRESENT) || WINDOWS_UWP
55
using System;
66
using System.Runtime.InteropServices;
7-
using UnityEngine.XR.WSA;
87
#if WINDOWS_UWP
98
using Windows.Perception.Spatial;
109
#if DOTNETWINRT_PRESENT
@@ -18,7 +17,7 @@
1817
#endif
1918
#endif // (UNITY_WSA && DOTNETWINRT_PRESENT) || WINDOWS_UWP
2019

21-
namespace Microsoft.MixedReality.Toolkit.WindowsMixedReality.Input
20+
namespace Microsoft.MixedReality.Toolkit.WindowsMixedReality
2221
{
2322
public static class WindowsMixedRealityUtilities
2423
{
@@ -51,6 +50,8 @@ private static SpatialCoordinateSystem GetSpatialCoordinateSystem(IntPtr nativeP
5150
}
5251
#endif //ENABLE_DOTNET
5352

53+
public static IWindowsMixedRealityUtilitiesProvider WmrUtilitiesProvider { get; set; } = null;
54+
5455
/// <summary>
5556
/// Access the underlying native spatial coordinate system.
5657
/// </summary>
@@ -62,18 +63,17 @@ public static SpatialCoordinateSystem SpatialCoordinateSystem
6263
{
6364
get
6465
{
66+
if (spatialCoordinateSystem == null && WmrUtilitiesProvider != null)
67+
{
6568
#if ENABLE_DOTNET
66-
return spatialCoordinateSystem ?? (spatialCoordinateSystem = GetSpatialCoordinateSystem(WorldManager.GetNativeISpatialCoordinateSystemPtr()));
69+
spatialCoordinateSystem = GetSpatialCoordinateSystem(WmrUtilitiesProvider.ISpatialCoordinateSystemPtr);
6770
#elif WINDOWS_UWP
68-
return spatialCoordinateSystem ?? (spatialCoordinateSystem = Marshal.GetObjectForIUnknown(WorldManager.GetNativeISpatialCoordinateSystemPtr()) as SpatialCoordinateSystem);
71+
spatialCoordinateSystem = Marshal.GetObjectForIUnknown(WmrUtilitiesProvider.ISpatialCoordinateSystemPtr) as SpatialCoordinateSystem;
6972
#elif DOTNETWINRT_PRESENT
70-
var spatialCoordinateSystemPtr = WorldManager.GetNativeISpatialCoordinateSystemPtr();
71-
if (spatialCoordinateSystem == null && spatialCoordinateSystemPtr != IntPtr.Zero)
72-
{
73-
spatialCoordinateSystem = SpatialCoordinateSystem.FromNativePtr(WorldManager.GetNativeISpatialCoordinateSystemPtr());
73+
spatialCoordinateSystem = SpatialCoordinateSystem.FromNativePtr(WmrUtilitiesProvider.ISpatialCoordinateSystemPtr);
74+
#endif
7475
}
7576
return spatialCoordinateSystem;
76-
#endif
7777
}
7878
}
7979

@@ -88,14 +88,15 @@ public static HolographicFrame CurrentHolographicFrame
8888
{
8989
get
9090
{
91+
if (WmrUtilitiesProvider == null)
92+
{
93+
return null;
94+
}
95+
9196
#if DOTNETWINRT_PRESENT
92-
IntPtr nativePtr = UnityEngine.XR.XRDevice.GetNativePtr();
93-
HolographicFrameNativeData hfd = Marshal.PtrToStructure<HolographicFrameNativeData>(nativePtr);
94-
return HolographicFrame.FromNativePtr(hfd.IHolographicFramePtr);
97+
return HolographicFrame.FromNativePtr(WmrUtilitiesProvider.IHolographicFramePtr);
9598
#elif WINDOWS_UWP
96-
IntPtr nativePtr = UnityEngine.XR.XRDevice.GetNativePtr();
97-
HolographicFrameNativeData hfd = Marshal.PtrToStructure<HolographicFrameNativeData>(nativePtr);
98-
return Marshal.GetObjectForIUnknown(hfd.IHolographicFramePtr) as HolographicFrame;
99+
return Marshal.GetObjectForIUnknown(WmrUtilitiesProvider.IHolographicFramePtr) as HolographicFrame;
99100
#else
100101
return null;
101102
#endif
@@ -105,13 +106,13 @@ public static HolographicFrame CurrentHolographicFrame
105106
private static SpatialCoordinateSystem spatialCoordinateSystem = null;
106107
#endif // (UNITY_WSA && DOTNETWINRT_PRESENT) || WINDOWS_UWP
107108

108-
[System.Obsolete("Use the System.Numerics.Vector3 extension method ToUnityVector3 instead.")]
109+
[Obsolete("Use the System.Numerics.Vector3 extension method ToUnityVector3 instead.")]
109110
public static UnityEngine.Vector3 SystemVector3ToUnity(System.Numerics.Vector3 vector)
110111
{
111112
return vector.ToUnityVector3();
112113
}
113114

114-
[System.Obsolete("Use the System.Numerics.Quaternion extension method ToUnityQuaternion instead.")]
115+
[Obsolete("Use the System.Numerics.Quaternion extension method ToUnityQuaternion instead.")]
115116
public static UnityEngine.Quaternion SystemQuaternionToUnity(System.Numerics.Quaternion quaternion)
116117
{
117118
return quaternion.ToUnityQuaternion();
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
using UnityEngine.XR.WSA;
7+
8+
namespace Microsoft.MixedReality.Toolkit.WindowsMixedReality
9+
{
10+
public class WindowsMixedRealityUtilitiesProvider : IWindowsMixedRealityUtilitiesProvider
11+
{
12+
/// <inheritdoc />
13+
IntPtr IWindowsMixedRealityUtilitiesProvider.ISpatialCoordinateSystemPtr => WorldManager.GetNativeISpatialCoordinateSystemPtr();
14+
15+
/// <inheritdoc />
16+
IntPtr IWindowsMixedRealityUtilitiesProvider.IHolographicFramePtr
17+
{
18+
get
19+
{
20+
IntPtr nativePtr = UnityEngine.XR.XRDevice.GetNativePtr();
21+
HolographicFrameNativeData hfd = Marshal.PtrToStructure<HolographicFrameNativeData>(nativePtr);
22+
return hfd.IHolographicFramePtr;
23+
}
24+
}
25+
}
26+
}

Assets/MixedRealityToolkit.Providers/WindowsMixedReality/WindowsMixedRealityUtilitiesProvider.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)