ARKitXROcclusionSubsystem.cs
14.5 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Unity.Collections;
using UnityEngine.Scripting;
using UnityEngine.XR.ARSubsystems;
namespace UnityEngine.XR.ARKit
{
/// <summary>
/// This subsystem provides implementing functionality for the <c>XROcclusionSubsystem</c> class.
/// </summary>
[Preserve]
class ARKitOcclusionSubsystem : XROcclusionSubsystem
{
#if !UNITY_2020_2_OR_NEWER
/// <summary>
/// Create the implementation provider.
/// </summary>
/// <returns>
/// The implementation provider.
/// </returns>
protected override Provider CreateProvider() => new ARKitProvider();
#endif
/// <summary>
/// Register the ARKit occlusion subsystem if iOS and not the editor.
/// </summary>
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void Register()
{
if (!Api.AtLeast13_0())
return;
const string k_SubsystemId = "ARKit-Occlusion";
bool supportsHumanSegmentationStencilImage = NativeApi.UnityARKit_OcclusionProvider_DoesSupportBodySegmentationStencil();
bool supportsHumanSegmentationDepthImage = NativeApi.UnityARKit_OcclusionProvider_DoesSupportBodySegmentationDepth();
if (supportsHumanSegmentationStencilImage || supportsHumanSegmentationDepthImage)
{
XROcclusionSubsystemCinfo occlusionSubsystemCinfo = new XROcclusionSubsystemCinfo()
{
id = k_SubsystemId,
#if UNITY_2020_2_OR_NEWER
providerType = typeof(ARKitOcclusionSubsystem.ARKitProvider),
subsystemTypeOverride = typeof(ARKitOcclusionSubsystem),
#else
implementationType = typeof(ARKitOcclusionSubsystem),
#endif
supportsHumanSegmentationStencilImage = supportsHumanSegmentationStencilImage,
supportsHumanSegmentationDepthImage = supportsHumanSegmentationDepthImage,
};
XROcclusionSubsystem.Register(occlusionSubsystemCinfo);
}
}
/// <summary>
/// The implementation provider class.
/// </summary>
class ARKitProvider : XROcclusionSubsystem.Provider
{
/// <summary>
/// The shader property name for the human segmentation stencil texture.
/// </summary>
/// <value>
/// The shader property name for the human segmentation stencil texture.
/// </value>
const string k_TextureHumanStencilPropertyName = "_HumanStencil";
/// <summary>
/// The shader property name for the human segmentation depth texture.
/// </summary>
/// <value>
/// The shader property name for the human segmentation depth texture.
/// </value>
const string k_TextureHumanDepthPropertyName = "_HumanDepth";
/// <summary>
/// The shader keyword for enabling human segmentation rendering.
/// </summary>
/// <value>
/// The shader keyword for enabling human segmentation rendering.
/// </value>
const string k_HumanEnabledMaterialKeyword = "ARKIT_HUMAN_SEGMENTATION_ENABLED";
/// <summary>
/// The shader property name identifier for the human segmentation stencil texture.
/// </summary>
/// <value>
/// The shader property name identifier for the human segmentation stencil texture.
/// </value>
static readonly int k_TextureHumanStencilPropertyId = Shader.PropertyToID(k_TextureHumanStencilPropertyName);
/// <summary>
/// The shader property name identifier for the human segmentation depth texture.
/// </summary>
/// <value>
/// The shader property name identifier for the human segmentation depth texture.
/// </value>
static readonly int k_TextureHumanDepthPropertyId = Shader.PropertyToID(k_TextureHumanDepthPropertyName);
/// <summary>
/// The shader keywords for enabling human segmentation rendering.
/// </summary>
/// <value>
/// The shader keywords for enabling human segmentation rendering.
/// </value>
static readonly List<string> m_HumanEnabledMaterialKeywords = new List<string>() {k_HumanEnabledMaterialKeyword};
/// <summary>
/// Construct the implementation provider.
/// </summary>
public ARKitProvider() => NativeApi.UnityARKit_OcclusionProvider_Construct(k_TextureHumanStencilPropertyId,
k_TextureHumanDepthPropertyId);
/// <summary>
/// Start the provider.
/// </summary>
public override void Start() => NativeApi.UnityARKit_OcclusionProvider_Start();
/// <summary>
/// Stop the provider.
/// </summary>
public override void Stop() => NativeApi.UnityARKit_OcclusionProvider_Stop();
/// <summary>
/// Destroy the provider.
/// </summary>
public override void Destroy() => NativeApi.UnityARKit_OcclusionProvider_Destruct();
/// <summary>
/// Property to be implemented by the provider to get/set the human segmentation stencil mode.
/// </summary>
/// <value>
/// The human segmentation stencil mode.
/// </value>
/// <exception cref="System.NotSupportedException">Thrown when setting the human segmentation stencil mode
/// to enabled if the implementation does not support human segmentation.</exception>
public override HumanSegmentationStencilMode requestedHumanStencilMode
{
get => NativeApi.UnityARKit_OcclusionProvider_GetRequestedSegmentationStencilMode();
set
{
NativeApi.UnityARKit_OcclusionProvider_SetRequestedSegmentationStencilMode(value);
Api.SetFeatureRequested(Feature.PeopleOcclusionStencil, value.Enabled());
}
}
public override HumanSegmentationStencilMode currentHumanStencilMode
=> NativeApi.UnityARKit_OcclusionProvider_GetCurrentSegmentationStencilMode();
/// <summary>
/// Property to be implemented by the provider to get/set the human segmentation depth mode.
/// </summary>
/// <value>
/// The human segmentation depth mode.
/// </value>
/// <exception cref="System.NotSupportedException">Thrown when setting the human segmentation depth mode
/// to enabled if the implementation does not support human segmentation.</exception>
public override HumanSegmentationDepthMode requestedHumanDepthMode
{
get => NativeApi.UnityARKit_OcclusionProvider_GetRequestedSegmentationDepthMode();
set
{
NativeApi.UnityARKit_OcclusionProvider_SetRequestedSegmentationDepthMode(value);
Api.SetFeatureRequested(Feature.PeopleOcclusionDepth, value.Enabled());
}
}
public override HumanSegmentationDepthMode currentHumanDepthMode
=> NativeApi.UnityARKit_OcclusionProvider_GetCurrentSegmentationDepthMode();
/// <summary>
/// Gets the human stencil texture descriptor.
/// </summary>
/// <param name="humanStencilDescriptor">The human stencil texture descriptor to be populated, if
/// available.</param>
/// <returns>
/// <c>true</c> if the human stencil texture descriptor is available and is returned. Otherwise,
/// <c>false</c>.
/// </returns>
public override bool TryGetHumanStencil(out XRTextureDescriptor humanStencilDescriptor)
=> NativeApi.UnityARKit_OcclusionProvider_TryGetHumanStencil(out humanStencilDescriptor);
public override XRCpuImage.Api humanStencilCpuImageApi => ARKitCpuImageApi.instance;
public override bool TryAcquireHumanStencilCpuImage(out XRCpuImage.Cinfo cinfo)
=> ARKitCpuImageApi.TryAcquireLatestImage(ARKitCpuImageApi.ImageType.HumanStencil, out cinfo);
/// <summary>
/// Get the human depth texture descriptor.
/// </summary>
/// <param name="humanDepthDescriptor">The human depth texture descriptor to be populated, if available
/// </param>
/// <returns>
/// <c>true</c> if the human depth texture descriptor is available and is returned. Otherwise,
/// <c>false</c>.
/// </returns>
public override bool TryGetHumanDepth(out XRTextureDescriptor humanDepthDescriptor)
=> NativeApi.UnityARKit_OcclusionProvider_TryGetHumanDepth(out humanDepthDescriptor);
public override XRCpuImage.Api humanDepthCpuImageApi => ARKitCpuImageApi.instance;
public override bool TryAcquireHumanDepthCpuImage(out XRCpuImage.Cinfo cinfo)
=> ARKitCpuImageApi.TryAcquireLatestImage(ARKitCpuImageApi.ImageType.HumanDepth, out cinfo);
/// <summary>
/// Gets the occlusion texture descriptors associated with the current AR frame.
/// </summary>
/// <param name="defaultDescriptor">The default descriptor value.</param>
/// <param name="allocator">The allocator to use when creating the returned <c>NativeArray</c>.</param>
/// <returns>The occlusion texture descriptors.</returns>
public override unsafe NativeArray<XRTextureDescriptor> GetTextureDescriptors(XRTextureDescriptor defaultDescriptor,
Allocator allocator)
{
var textureDescriptors = NativeApi.UnityARKit_OcclusionProvider_AcquireTextureDescriptors(out int length,
out int elementSize);
try
{
return NativeCopyUtility.PtrToNativeArrayWithDefault(defaultDescriptor, textureDescriptors,
elementSize, length, allocator);
}
finally
{
NativeApi.UnityARKit_OcclusionProvider_ReleaseTextureDescriptors(textureDescriptors);
}
}
/// <summary>
/// Get the enabled and disabled shader keywords for the material.
/// </summary>
/// <param name="enabledKeywords">The keywords to enable for the material.</param>
/// <param name="disabledKeywords">The keywords to disable for the material.</param>
public override void GetMaterialKeywords(out List<string> enabledKeywords, out List<string> disabledKeywords)
{
if (NativeApi.UnityARKit_OcclusionProvider_IsHumanEnabled())
{
enabledKeywords = m_HumanEnabledMaterialKeywords;
disabledKeywords = null;
}
else
{
enabledKeywords = null;
disabledKeywords = m_HumanEnabledMaterialKeywords;
}
}
}
/// <summary>
/// Container to wrap the native ARKit human body APIs.
/// </summary>
static class NativeApi
{
[DllImport("__Internal")]
public static extern void UnityARKit_OcclusionProvider_Construct(int textureHumanStencilPropertyId,
int textureHumanDepthPropertyId);
[DllImport("__Internal")]
public static extern void UnityARKit_OcclusionProvider_Start();
[DllImport("__Internal")]
public static extern void UnityARKit_OcclusionProvider_Stop();
[DllImport("__Internal")]
public static extern void UnityARKit_OcclusionProvider_Destruct();
[DllImport("__Internal")]
public static extern HumanSegmentationStencilMode UnityARKit_OcclusionProvider_GetRequestedSegmentationStencilMode();
[DllImport("__Internal")]
public static extern void UnityARKit_OcclusionProvider_SetRequestedSegmentationStencilMode(HumanSegmentationStencilMode humanSegmentationStencilMode);
[DllImport("__Internal")]
public static extern HumanSegmentationStencilMode UnityARKit_OcclusionProvider_GetCurrentSegmentationStencilMode();
[DllImport("__Internal")]
public static extern HumanSegmentationDepthMode UnityARKit_OcclusionProvider_GetRequestedSegmentationDepthMode();
[DllImport("__Internal")]
public static extern void UnityARKit_OcclusionProvider_SetRequestedSegmentationDepthMode(HumanSegmentationDepthMode humanSegmentationDepthMode);
[DllImport("__Internal")]
public static extern HumanSegmentationDepthMode UnityARKit_OcclusionProvider_GetCurrentSegmentationDepthMode();
[DllImport("__Internal")]
public static extern unsafe bool UnityARKit_OcclusionProvider_TryGetHumanStencil(out XRTextureDescriptor humanStencilDescriptor);
[DllImport("__Internal")]
public static extern unsafe bool UnityARKit_OcclusionProvider_TryGetHumanDepth(out XRTextureDescriptor humanDepthDescriptor);
[DllImport("__Internal")]
public static extern unsafe void* UnityARKit_OcclusionProvider_AcquireTextureDescriptors(out int length, out int elementSize);
[DllImport("__Internal")]
public static extern unsafe void UnityARKit_OcclusionProvider_ReleaseTextureDescriptors(void* descriptors);
[DllImport("__Internal")]
public static extern bool UnityARKit_OcclusionProvider_IsHumanEnabled();
[DllImport("__Internal")]
public static extern bool UnityARKit_OcclusionProvider_DoesSupportBodySegmentationStencil();
[DllImport("__Internal")]
public static extern bool UnityARKit_OcclusionProvider_DoesSupportBodySegmentationDepth();
}
}
}