// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.MixedReality.Toolkit.Utilities;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
using UnityBoundary = UnityEngine.Experimental.XR.Boundary;
namespace Microsoft.MixedReality.Toolkit.Boundary
{
///
/// The Boundary system controls the presentation and display of the users boundary in a scene.
///
[HelpURL("https://docs.microsoft.com/windows/mixed-reality/mrtk-unity/features/boundary/boundary-system-getting-started")]
public class MixedRealityBoundarySystem : BaseBoundarySystem
{
///
/// Constructor.
///
/// The configuration profile for the service.
/// The application's configured .
public MixedRealityBoundarySystem(
MixedRealityBoundaryVisualizationProfile profile,
ExperienceScale scale) : base(profile, scale) { }
#region IMixedRealityService Implementation
///
public override string Name { get; protected set; } = "Mixed Reality Boundary System";
#endregion IMixedRealityService Implementation
///
protected override List GetBoundaryGeometry()
{
// Boundaries are supported for Room Scale experiences only.
if (XRDevice.GetTrackingSpaceType() != TrackingSpaceType.RoomScale)
{
return null;
}
// Get the boundary geometry.
var boundaryGeometry = new List(0);
if (!UnityBoundary.TryGetGeometry(boundaryGeometry, UnityBoundary.Type.TrackedArea) || boundaryGeometry.Count == 0)
{
return null;
}
return boundaryGeometry;
}
///
/// Updates the TrackingSpaceType on the XR device.
///
protected override void SetTrackingSpace()
{
if (Application.isPlaying)
{
TrackingSpaceType trackingSpace;
// In current versions of Unity, there are two types of tracking spaces. For boundaries, if the scale
// is not Room or Standing, it currently maps to TrackingSpaceType.Stationary.
switch (Scale)
{
case ExperienceScale.Standing:
case ExperienceScale.Room:
trackingSpace = TrackingSpaceType.RoomScale;
break;
case ExperienceScale.OrientationOnly:
case ExperienceScale.Seated:
case ExperienceScale.World:
trackingSpace = TrackingSpaceType.Stationary;
break;
default:
trackingSpace = TrackingSpaceType.Stationary;
Debug.LogWarning("Unknown / unsupported ExperienceScale. Defaulting to Stationary tracking space.");
break;
}
InputTracking.disablePositionalTracking = Scale == ExperienceScale.OrientationOnly;
if (!XRDevice.SetTrackingSpaceType(trackingSpace))
{
Debug.LogWarning($"MRTK was unable to set Tracking Space to {trackingSpace}");
}
}
}
#region Obsolete
///
/// Constructor.
///
/// The instance that loaded the service.
/// The configuration profile for the service.
/// The application's configured .
[System.Obsolete("This constructor is obsolete (registrar parameter is no longer required) and will be removed in a future version of the Microsoft Mixed Reality Toolkit.")]
public MixedRealityBoundarySystem(
IMixedRealityServiceRegistrar registrar,
MixedRealityBoundaryVisualizationProfile profile,
ExperienceScale scale) : this(profile, scale)
{
Registrar = registrar;
}
#endregion
}
}