using System;
using System.Runtime.InteropServices;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine.Scripting;
using UnityEngine.XR.ARSubsystems;
namespace UnityEngine.XR.ARKit
{
///
/// ARKit extension methods to the [XRMeshSubsystem](https://docs.unity3d.com/ScriptReference/XR.XRMeshSubsystem.html).
///
public static class ARKitMeshSubsystemExtensions
{
static class NativeApi
{
#if UNITY_XR_ARKIT_LOADER_ENABLED
[DllImport("__Internal")]
public static extern unsafe void* UnityARKit_MeshProvider_AcquireClassifications(TrackableId trackableId, out int numClassifications);
[DllImport("__Internal")]
public static extern unsafe void UnityARKit_MeshProvider_ReleaseClassifications(void* classifications);
[DllImport("__Internal")]
public static extern unsafe bool UnityARKit_MeshProvider_IsClassificationEnabled();
[DllImport("__Internal")]
public static extern unsafe void UnityARKit_MeshProvider_SetClassificationEnabled(bool enabled);
#else
static readonly string k_ExceptionMsg = "Apple ARKit XR Plug-in Provider not enabled in project settings.";
public static unsafe void* UnityARKit_MeshProvider_AcquireClassifications(TrackableId trackableId, out int numClassifications)
{
throw new System.NotImplementedException(k_ExceptionMsg);
}
public static unsafe void UnityARKit_MeshProvider_ReleaseClassifications(void* classifications)
{
throw new System.NotImplementedException(k_ExceptionMsg);
}
public static unsafe bool UnityARKit_MeshProvider_IsClassificationEnabled()
{
throw new System.NotImplementedException(k_ExceptionMsg);
}
public static unsafe void UnityARKit_MeshProvider_SetClassificationEnabled(bool enabled)
{
throw new System.NotImplementedException(k_ExceptionMsg);
}
#endif
}
///
/// Get the face classifications for the given mesh ID.
///
/// The meshing subsystem.
/// The trackable ID representing the mesh.
/// The memory allocator type to use in allocating the native array memory.
///
/// An array of mesh classifications, one for each face in the mesh.
///
public static unsafe NativeArray GetFaceClassifications(this XRMeshSubsystem subsystem, TrackableId meshId, Allocator allocator)
{
void* classifications = NativeApi.UnityARKit_MeshProvider_AcquireClassifications(meshId, out int numClassifications);
try
{
if (classifications == null)
{
numClassifications = 0;
}
var meshClassifications = new NativeArray(numClassifications, allocator);
if (classifications != null)
{
NativeArray tmp = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray(classifications, numClassifications, Allocator.None);
meshClassifications.CopyFrom(tmp);
}
return meshClassifications;
}
finally
{
NativeApi.UnityARKit_MeshProvider_ReleaseClassifications(classifications);
}
}
///
/// Whether mesh classification is enabled.
///
/// The meshing subsystem.
///
/// true if the mesh classification is enabled. Otherwise, false.
///
public static bool GetClassificationEnabled(this XRMeshSubsystem subsystem)
{
return NativeApi.UnityARKit_MeshProvider_IsClassificationEnabled();
}
///
/// Sets whether mesh classification should be enabled.
///
/// The meshing subsystem.
/// Whether the mesh classification should be enabled.
public static void SetClassificationEnabled(this XRMeshSubsystem subsystem, bool enabled)
{
NativeApi.UnityARKit_MeshProvider_SetClassificationEnabled(enabled);
}
}
}