using System;
using Unity.Collections;
namespace UnityEngine.XR.ARSubsystems
{
///
/// Base class for plane subsystems.
///
///
/// This subsystem surfaces information regarding plane (i.e., flat surface) detection in the physical environment.
/// Implementations are typically found in other provider or platform-specific packages.
///
public abstract class XRPlaneSubsystem : TrackingSubsystem
{
///
/// Constructs a plane subsystem. Do not invoked directly; call Create on the instead.
///
public XRPlaneSubsystem() => m_Provider = CreateProvider();
///
/// Start the plane subsystem, i.e., start tracking planes.
///
protected sealed override void OnStart() => m_Provider.Start();
///
/// Destroy the plane subsystem.
///
protected sealed override void OnDestroyed() => m_Provider.Destroy();
///
/// Stop the subsystem, i.e., stop tracking planes.
///
protected sealed override void OnStop() => m_Provider.Stop();
///
/// Set , e.g., to enable different modes of detection.
///
public PlaneDetectionMode planeDetectionMode
{
set => m_Provider.planeDetectionMode = value;
}
///
/// Get the changes (added, updated, and removed) planes since the last call to .
///
/// An Allocator to use when allocating the returned NativeArrays.
///
/// describing the planes that have been added, updated, and removed
/// since the last call to . The caller owns the memory allocated with Allocator.
///
public override TrackableChanges GetChanges(Allocator allocator)
{
var changes = m_Provider.GetChanges(BoundedPlane.defaultValue, allocator);
#if DEVELOPMENT_BUILD || UNITY_EDITOR
m_ValidationUtility.ValidateAndDisposeIfThrown(changes);
#endif
return changes;
}
///
/// Gets the boundary polygon describing the plane.
///
/// The associated with the plane of which to retrieve the boundary.
/// An Allocator to use if needs to be created. Allocator.Temp is not supported; use Allocator.TempJob if you need temporary memory.
/// The boundary will be stored here. If boundary is the same length as the new boundary,
/// it is simply overwritten with the new data. Otherwise, it is disposed and recreated with the corect length.
public void GetBoundary(
TrackableId trackableId,
Allocator allocator,
ref NativeArray boundary)
{
if (allocator == Allocator.Temp)
throw new InvalidOperationException("Allocator.Temp is not supported. Use Allocator.TempJob if you wish to use a temporary allocator.");
if (allocator == Allocator.None)
throw new InvalidOperationException("Allocator.None is not a valid allocator.");
m_Provider.GetBoundary(trackableId, allocator, ref boundary);
}
///
/// Concrete classes must implement this to provide the provider-specific implementation.
///
///
protected abstract Provider CreateProvider();
///
/// Creates or resizes the if necessary. If
/// has been allocated and its length is equal to , then this method
/// does nothing. If its length is different, then it is first disposed before being assigned
/// to a new NativeArray.
///
/// The length that will have after this method returns.
/// If allocation is necessary, this allocator will be used to create the new NativeArray.
/// The array to create or resize.
protected static void CreateOrResizeNativeArrayIfNecessary(
int length,
Allocator allocator,
ref NativeArray array) where T : struct
{
if (array.IsCreated)
{
if (array.Length != length)
{
array.Dispose();
array = new NativeArray(length, allocator);
}
}
else
{
array = new NativeArray(length, allocator);
}
}
///
/// The API that derived classes must implement.
///
protected abstract class Provider
{
///
/// Start the plane subsystem, i.e., start tracking planes.
///
public virtual void Start() { }
///
/// Stop the subsystem, i.e., stop tracking planes.
///
public virtual void Stop() { }
///
/// Destroy the plane subsystem. is always called first.
///
public virtual void Destroy() { }
///
/// Retrieves the boundary points of the plane with .
///
/// The id of the plane.
/// An Allocator to use for the returned NativeArray.
/// An existing NativeArray to update or recreate if necessary.
/// See .
/// A NativeArray of 2D plane-space (relative to ) positions describing
/// the plane's boundary. NativeArray should be allocated using .
public virtual void GetBoundary(
TrackableId trackableId,
Allocator allocator,
ref NativeArray boundary)
{
throw new NotSupportedException("Boundary vertices are not supported.");
}
///
/// Get the changes (added, updated, and removed) planes since the last call to .
///
///
/// The default plane. This should be used to initialize the returned NativeArrays for backwards compatibility.
/// See .
///
/// An Allocator to use when allocating the returned NativeArrays.
///
/// describing the planes that have been added, updated, and removed
/// since the last call to . The changes should be allocated using
/// .
///
public abstract TrackableChanges GetChanges(BoundedPlane defaultPlane, Allocator allocator);
///
/// Set the .
///
public virtual PlaneDetectionMode planeDetectionMode
{
set { }
}
}
Provider m_Provider;
#if DEVELOPMENT_BUILD || UNITY_EDITOR
ValidationUtility m_ValidationUtility =
new ValidationUtility();
#endif
}
}