// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using UnityEngine;
#if UNITY_WSA && DOTNETWINRT_PRESENT
using Microsoft.MixedReality.Toolkit.Windows.Utilities;
using System.Collections.Generic;
#endif // UNITY_WSA && DOTNETWINRT_PRESENT
namespace Microsoft.MixedReality.Toolkit.WindowsMixedReality
{
///
/// Script used to update the reprojection method for Windows Mixed Reality devices.
///
public class WindowsMixedRealityReprojectionUpdater : MonoBehaviour
{
///
/// Gets or sets the reprojection method used by Windows Mixed Reality.
///
public HolographicDepthReprojectionMethod ReprojectionMethod { get; set; }
#if UNITY_WSA && DOTNETWINRT_PRESENT
private readonly Dictionary cameraIdToSupportsAutoPlanar = new Dictionary();
private static readonly bool IsDepthReprojectionModeSupported = WindowsApiChecker.IsPropertyAvailable(
"Windows.Graphics.Holographic",
"HolographicCameraRenderingParameters",
"DepthReprojectionMethod");
private void OnPostRender()
{
// The reprojection method needs to be set each frame.
if (IsDepthReprojectionModeSupported &&
(ReprojectionMethod == HolographicDepthReprojectionMethod.AutoPlanar))
{
Microsoft.Windows.Graphics.Holographic.HolographicFrame frame = WindowsMixedRealityUtilities.CurrentHolographicFrame;
foreach (var cameraPose in frame?.CurrentPrediction.CameraPoses)
{
if (CameraSupportsAutoPlanar(cameraPose.HolographicCamera))
{
Microsoft.Windows.Graphics.Holographic.HolographicCameraRenderingParameters renderingParams = frame.GetRenderingParameters(cameraPose);
renderingParams.DepthReprojectionMethod = Microsoft.Windows.Graphics.Holographic.HolographicDepthReprojectionMethod.AutoPlanar;
}
}
}
}
///
/// Checks the Holographic camera to see if it supports auto-planar reprojection.
///
/// The camera to be queried.
///
/// True if the camera supports auto-planar reprojection, false otherwise.
///
private bool CameraSupportsAutoPlanar(Microsoft.Windows.Graphics.Holographic.HolographicCamera camera)
{
bool supportsAutoPlanar = false;
if (!cameraIdToSupportsAutoPlanar.TryGetValue(camera.Id, out supportsAutoPlanar))
{
foreach (var method in camera.ViewConfiguration.SupportedDepthReprojectionMethods)
{
if (method == Microsoft.Windows.Graphics.Holographic.HolographicDepthReprojectionMethod.AutoPlanar)
{
supportsAutoPlanar = true;
break;
}
}
cameraIdToSupportsAutoPlanar.Add(camera.Id, supportsAutoPlanar);
}
return supportsAutoPlanar;
}
#endif // UNITY_WSA && DOTNETWINRT_PRESENT
}
}