ARCoreOcclusionSubsystem.cs
11.8 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
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Unity.Collections;
using UnityEngine.Scripting;
using UnityEngine.XR.ARSubsystems;
namespace UnityEngine.XR.ARCore
{
/// <summary>
/// The ARCore implementation of the
/// [XROcclusionSubsystem](xref:UnityEngine.XR.ARSubsystems.XROcclusionSubsystem).
/// Do not create this directly. Use the
/// [SubsystemManager](xref:UnityEngine.SubsystemManager)
/// instead.
/// </summary>
[Preserve]
class ARCoreOcclusionSubsystem : XROcclusionSubsystem
{
/// <summary>
/// Register the ARCore occlusion subsystem if iOS and not the editor.
/// </summary>
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void Register()
{
if (!Api.platformAndroid || !Api.loaderPresent)
return;
const string k_SubsystemId = "ARCore-Occlusion";
XROcclusionSubsystemCinfo occlusionSubsystemCinfo = new XROcclusionSubsystemCinfo()
{
id = k_SubsystemId,
#if UNITY_2020_2_OR_NEWER
providerType = typeof(ARCoreOcclusionSubsystem.ARCoreProvider),
subsystemTypeOverride = typeof(ARCoreOcclusionSubsystem),
#else // UNITY_2020_2_OR_NEWER
implementationType = typeof(ARCoreOcclusionSubsystem),
#endif // UNITY_2020_2_OR_NEWER
supportsHumanSegmentationStencilImage = false,
supportsHumanSegmentationDepthImage = false,
queryForSupportsEnvironmentDepthImage = NativeApi.UnityARCore_OcclusionProvider_DoesSupportEnvironmentDepth,
};
if (!XROcclusionSubsystem.Register(occlusionSubsystemCinfo))
{
Debug.Log($"Cannot register the {k_SubsystemId} subsystem");
}
}
#if !UNITY_2020_2_OR_NEWER
/// <summary>
/// Create the implementation provider.
/// </summary>
/// <returns>
/// The implementation provider.
/// </returns>
protected override Provider CreateProvider() => new ARCoreProvider();
#endif // !UNITY_2020_2_OR_NEWER
/// <summary>
/// The implementation provider class.
/// </summary>
class ARCoreProvider : Provider
{
/// <summary>
/// The shader property name for the environment depth texture.
/// </summary>
/// <value>
/// The shader property name for the environment depth texture.
/// </value>
const string k_TextureEnvironmentDepthPropertyName = "_EnvironmentDepth";
/// <summary>
/// The shader keyword for enabling environment depth rendering.
/// </summary>
/// <value>
/// The shader keyword for enabling environment depth rendering.
/// </value>
const string k_EnvironmentDepthEnabledMaterialKeyword = "ARCORE_ENVIRONMENT_DEPTH_ENABLED";
/// <summary>
/// The shader property name identifier for the environment depth texture.
/// </summary>
/// <value>
/// The shader property name identifier for the environment depth texture.
/// </value>
static readonly int k_TextureEnvironmentDepthPropertyId = Shader.PropertyToID(k_TextureEnvironmentDepthPropertyName);
/// <summary>
/// The shader keywords for enabling environment depth rendering.
/// </summary>
/// <value>
/// The shader keywords for enabling environment depth rendering.
/// </value>
static readonly List<string> m_EnvironmentDepthEnabledMaterialKeywords = new List<string>() {k_EnvironmentDepthEnabledMaterialKeyword};
/// <summary>
/// Construct the implementation provider.
/// </summary>
public ARCoreProvider()
{
bool supportsR16 = SystemInfo.SupportsTextureFormat(TextureFormat.R16);
bool supportsRHalf = SystemInfo.SupportsTextureFormat(TextureFormat.RHalf);
bool supportsRenderTextureRHalf = SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.RHalf);
bool useAdvancedRendering = supportsR16 && supportsRHalf && supportsRenderTextureRHalf;
NativeApi.UnityARCore_OcclusionProvider_Construct(k_TextureEnvironmentDepthPropertyId, useAdvancedRendering);
}
/// <summary>
/// Start the provider.
/// </summary>
public override void Start() => NativeApi.UnityARCore_OcclusionProvider_Start();
/// <summary>
/// Stop the provider.
/// </summary>
public override void Stop() => NativeApi.UnityARCore_OcclusionProvider_Stop();
/// <summary>
/// Destroy the provider.
/// </summary>
public override void Destroy() => NativeApi.UnityARCore_OcclusionProvider_Destruct();
/// <summary>
/// Property to get/set the requested environment depth mode.
/// </summary>
/// <value>
/// The requested environment depth mode.
/// </value>
public override EnvironmentDepthMode requestedEnvironmentDepthMode
{
get => NativeApi.UnityARCore_OcclusionProvider_GetRequestedEnvironmentDepthMode();
set
{
NativeApi.UnityARCore_OcclusionProvider_SetRequestedEnvironmentDepthMode(value);
Api.SetFeatureRequested(Feature.EnvironmentDepth, value.Enabled());
}
}
/// <summary>
/// Property to get the current environment depth mode.
/// </summary>
public override EnvironmentDepthMode currentEnvironmentDepthMode
=> NativeApi.UnityARCore_OcclusionProvider_GetCurrentEnvironmentDepthMode();
/// <summary>
/// Get the environment texture descriptor.
/// </summary>
/// <param name="environmentDepthDescriptor">The environment depth texture descriptor to be populated, if
/// available.</param>
/// <returns>
/// <c>true</c> if the environment depth texture descriptor is available and is returned. Otherwise,
/// <c>false</c>.
/// </returns>
public override bool TryGetEnvironmentDepth(out XRTextureDescriptor environmentDepthDescriptor)
=> NativeApi.UnityARCore_OcclusionProvider_TryGetEnvironmentDepth(out environmentDepthDescriptor);
/// <summary>
/// Gets the CPU construction information for a environment depth image.
/// </summary>
/// <param name="cinfo">The CPU image construction information, on success.</param>
/// <returns>
/// <c>true</c> if the environment depth texture is available and its CPU image construction information is
/// returned. Otherwise, <c>false</c>.
/// </returns>
public override bool TryAcquireEnvironmentDepthCpuImage(out XRCpuImage.Cinfo cinfo)
=> ARCoreCpuImageApi.TryAcquireLatestImage(ARCoreCpuImageApi.ImageType.EnvironmentDepth, out cinfo);
/// <summary>
/// The CPU image API for interacting with the environment depth image.
/// </summary>
public override XRCpuImage.Api environmentDepthCpuImageApi => ARCoreCpuImageApi.instance;
/// <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.UnityARCore_OcclusionProvider_AcquireTextureDescriptors(out int length,
out int elementSize);
try
{
return NativeCopyUtility.PtrToNativeArrayWithDefault(defaultDescriptor, textureDescriptors,
elementSize, length, allocator);
}
finally
{
NativeApi.UnityARCore_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)
{
bool isEnvDepthEnabled = NativeApi.UnityARCore_OcclusionProvider_IsEnvironmentDepthEnabled();
if (isEnvDepthEnabled)
{
enabledKeywords = m_EnvironmentDepthEnabledMaterialKeywords;
disabledKeywords = null;
}
else
{
enabledKeywords = null;
disabledKeywords = m_EnvironmentDepthEnabledMaterialKeywords;
}
}
}
/// <summary>
/// Container to wrap the native ARCore human body APIs.
/// </summary>
static class NativeApi
{
[DllImport("UnityARCore")]
public static extern bool UnityARCore_OcclusionProvider_DoesSupportEnvironmentDepth();
[DllImport("UnityARCore")]
public static extern void UnityARCore_OcclusionProvider_Construct(int textureEnvDepthPropertyId, bool useAdvancedRendering);
[DllImport("UnityARCore")]
public static extern void UnityARCore_OcclusionProvider_Start();
[DllImport("UnityARCore")]
public static extern void UnityARCore_OcclusionProvider_Stop();
[DllImport("UnityARCore")]
public static extern void UnityARCore_OcclusionProvider_Destruct();
[DllImport("UnityARCore")]
public static extern EnvironmentDepthMode UnityARCore_OcclusionProvider_GetRequestedEnvironmentDepthMode();
[DllImport("UnityARCore")]
public static extern void UnityARCore_OcclusionProvider_SetRequestedEnvironmentDepthMode(EnvironmentDepthMode environmentDepthMode);
[DllImport("UnityARCore")]
public static extern EnvironmentDepthMode UnityARCore_OcclusionProvider_GetCurrentEnvironmentDepthMode();
[DllImport("UnityARCore")]
public static unsafe extern bool UnityARCore_OcclusionProvider_TryGetEnvironmentDepth(out XRTextureDescriptor envDepthDescriptor);
[DllImport("UnityARCore")]
public static unsafe extern void* UnityARCore_OcclusionProvider_AcquireTextureDescriptors(out int length, out int elementSize);
[DllImport("UnityARCore")]
public static extern unsafe void UnityARCore_OcclusionProvider_ReleaseTextureDescriptors(void* descriptors);
[DllImport("UnityARCore")]
public static extern bool UnityARCore_OcclusionProvider_IsEnvironmentDepthEnabled();
}
}
}