ARKitXRDepthSubsystem.cs
5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Runtime.InteropServices;
using AOT;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
using UnityEngine.Scripting;
using UnityEngine.XR.ARSubsystems;
namespace UnityEngine.XR.ARKit
{
/// <summary>
/// The ARKit implementation of the <c>XRDepthSubsystem</c>. Do not create this directly. Use the <c>SubsystemManager</c> instead.
/// </summary>
[Preserve]
public sealed class ARKitXRDepthSubsystem : XRDepthSubsystem
{
class ARKitProvider : Provider
{
[DllImport("__Internal")]
static extern void UnityARKit_depth_start();
[DllImport("__Internal")]
static extern void UnityARKit_depth_stop();
[DllImport("__Internal")]
static extern void UnityARKit_depth_destroy();
[DllImport("__Internal")]
static extern unsafe void* UnityARKit_depth_acquireChanges(
out void* addedPtr, out int addedLength,
out void* updatedPtr, out int updatedLength,
out void* removedPtr, out int removedLength,
out int elementSize);
[DllImport("__Internal")]
static extern unsafe void UnityARKit_depth_releaseChanges(
void* changes);
[DllImport("__Internal")]
static extern unsafe void* UnityARKit_depth_acquirePointCloud(
TrackableId trackableId,
out void* positionsPtr, out void* identifiersPtr, out int numPoints);
[DllImport("__Internal")]
static extern unsafe void UnityARKit_depth_releasePointCloud(
void* pointCloud);
public override unsafe TrackableChanges<XRPointCloud> GetChanges(
XRPointCloud defaultPointCloud,
Allocator allocator)
{
var context = UnityARKit_depth_acquireChanges(
out var addedPtr, out var addedLength,
out var updatedPtr, out var updatedLength,
out var removedPtr, out var removedLength,
out var elementSize);
try
{
return new TrackableChanges<XRPointCloud>(
addedPtr, addedLength,
updatedPtr, updatedLength,
removedPtr, removedLength,
defaultPointCloud, elementSize,
allocator);
}
finally
{
UnityARKit_depth_releaseChanges(context);
}
}
public override void Start() => UnityARKit_depth_start();
public override void Stop() => UnityARKit_depth_stop();
public override void Destroy() => UnityARKit_depth_destroy();
public override unsafe XRPointCloudData GetPointCloudData(
TrackableId trackableId,
Allocator allocator)
{
var pointCloud = UnityARKit_depth_acquirePointCloud(
trackableId,
out var positionsPtr, out var identifiersPtr, out var numPoints);
try
{
var positions = new NativeArray<Vector3>(numPoints, allocator);
var positionsHandle = new TransformPositionsJob
{
positionsIn = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<Quaternion>(positionsPtr, numPoints, Allocator.None),
positionsOut = positions
}.Schedule(numPoints, 32);
var identifiers = new NativeArray<ulong>(numPoints, allocator);
identifiers.CopyFrom(NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<ulong>(identifiersPtr, numPoints, Allocator.None));
positionsHandle.Complete();
return new XRPointCloudData
{
positions = positions,
identifiers = identifiers
};
}
finally
{
UnityARKit_depth_releasePointCloud(pointCloud);
}
}
}
struct TransformPositionsJob : IJobParallelFor
{
[ReadOnly]
public NativeArray<Quaternion> positionsIn;
[WriteOnly]
public NativeArray<Vector3> positionsOut;
public void Execute(int index)
{
positionsOut[index] = new Vector3(
positionsIn[index].x,
positionsIn[index].y,
-positionsIn[index].z);
}
}
#if !UNITY_2020_2_OR_NEWER
/// <summary>
/// Creates the ARKit-specific implementation which will service the `XRDepthSubsystem`.
/// </summary>
/// <returns>A new instance of the `Provider` specific to ARKit.</returns>
protected override Provider CreateProvider() => new ARKitProvider();
#endif
//this method is run on startup of the app to register this provider with XR Subsystem Manager
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void RegisterDescriptor()
{
#if UNITY_IOS && !UNITY_EDITOR
var descriptorParams = new XRDepthSubsystemDescriptor.Cinfo
{
id = "ARKit-Depth",
#if UNITY_2020_2_OR_NEWER
providerType = typeof(ARKitXRDepthSubsystem.ARKitProvider),
subsystemTypeOverride = typeof(ARKitXRDepthSubsystem),
#else
implementationType = typeof(ARKitXRDepthSubsystem),
#endif
supportsFeaturePoints = true,
supportsConfidence = false,
supportsUniqueIds = true
};
XRDepthSubsystemDescriptor.RegisterDescriptor(descriptorParams);
#endif
}
}
}