AROcclusionManager.cs
14.1 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
using System;
using System.Collections.Generic;
using Unity.Collections;
using UnityEngine.Serialization;
using UnityEngine.XR.ARSubsystems;
using UnityEngine.Rendering;
namespace UnityEngine.XR.ARFoundation
{
/// <summary>
/// The manager for the occlusion subsystem.
/// </summary>
[DisallowMultipleComponent]
[DefaultExecutionOrder(ARUpdateOrder.k_OcclusionManager)]
[HelpURL(HelpUrls.ApiWithNamespace + nameof(AROcclusionManager) + ".html")]
public sealed class AROcclusionManager :
#if UNITY_2020_2_OR_NEWER
SubsystemLifecycleManager<XROcclusionSubsystem, XROcclusionSubsystemDescriptor, XROcclusionSubsystem.Provider>
#else
SubsystemLifecycleManager<XROcclusionSubsystem, XROcclusionSubsystemDescriptor>
#endif
{
/// <summary>
/// The list of occlusion texture infos.
/// </summary>
/// <value>
/// The list of occlusion texture infos.
/// </value>
readonly List<ARTextureInfo> m_TextureInfos = new List<ARTextureInfo>();
/// <summary>
/// The list of occlusion textures.
/// </summary>
/// <value>
/// The list of occlusion textures.
/// </value>
readonly List<Texture2D> m_Textures = new List<Texture2D>();
/// <summary>
/// The list of occlusion texture property IDs.
/// </summary>
/// <value>
/// The list of occlusion texture property IDs.
/// </value>
readonly List<int> m_TexturePropertyIds = new List<int>();
/// <summary>
/// The human stencil texture info.
/// </summary>
/// <value>
/// The human stencil texture info.
/// </value>
ARTextureInfo m_HumanStencilTextureInfo;
/// <summary>
/// The human depth texture info.
/// </summary>
/// <value>
/// The human depth texture info.
/// </value>
ARTextureInfo m_HumanDepthTextureInfo;
/// <summary>
/// An event which fires each time an occlusion camera frame is received.
/// </summary>
public event Action<AROcclusionFrameEventArgs> frameReceived;
/// <summary>
/// The mode for generating the human segmentation stencil texture.
/// This method is obsolete.
/// Use <see cref="requestedHumanStencilMode"/>
/// or <see cref="currentHumanStencilMode"/> instead.
/// </summary>
[Obsolete("Use requestedSegmentationStencilMode or currentSegmentationStencilMode instead. (2020-01-14)")]
public HumanSegmentationStencilMode humanSegmentationStencilMode
{
get => m_HumanSegmentationStencilMode;
set => requestedHumanStencilMode = value;
}
/// <summary>
/// The requested mode for generating the human segmentation stencil texture.
/// </summary>
public HumanSegmentationStencilMode requestedHumanStencilMode
{
get => subsystem?.requestedHumanStencilMode ?? m_HumanSegmentationStencilMode;
set
{
m_HumanSegmentationStencilMode = value;
if (enabled && descriptor?.supportsHumanSegmentationStencilImage == true)
{
subsystem.requestedHumanStencilMode = value;
}
}
}
/// <summary>
/// Get the current mode in use for generating the human segmentation stencil mode.
/// </summary>
public HumanSegmentationStencilMode currentHumanStencilMode => subsystem?.currentHumanStencilMode ?? HumanSegmentationStencilMode.Disabled;
[SerializeField]
[Tooltip("The mode for generating human segmentation stencil texture.\n\n"
+ "Disabled -- No human stencil texture produced.\n"
+ "Fastest -- Minimal rendering quality. Minimal frame computation.\n"
+ "Medium -- Medium rendering quality. Medium frame computation.\n"
+ "Best -- Best rendering quality. Increased frame computation.")]
HumanSegmentationStencilMode m_HumanSegmentationStencilMode = HumanSegmentationStencilMode.Fastest;
/// <summary>
/// The mode for generating the human segmentation depth texture.
/// This method is obsolete.
/// Use <see cref="requestedHumanDepthMode"/>
/// or <see cref="currentHumanDepthMode"/> instead.
/// </summary>
[Obsolete("Use requestedSegmentationDepthMode or currentSegmentationDepthMode instead. (2020-01-15)")]
public HumanSegmentationDepthMode humanSegmentationDepthMode
{
get => m_HumanSegmentationDepthMode;
set => requestedHumanDepthMode = value;
}
/// <summary>
/// Get or set the requested human segmentation depth mode.
/// </summary>
public HumanSegmentationDepthMode requestedHumanDepthMode
{
get => subsystem?.requestedHumanDepthMode ?? m_HumanSegmentationDepthMode;
set
{
m_HumanSegmentationDepthMode = value;
if (enabled && descriptor?.supportsHumanSegmentationDepthImage == true)
{
subsystem.requestedHumanDepthMode = value;
}
}
}
/// <summary>
/// Get the current human segmentation depth mode in use by the subsystem.
/// </summary>
public HumanSegmentationDepthMode currentHumanDepthMode => subsystem?.currentHumanDepthMode ?? HumanSegmentationDepthMode.Disabled;
[SerializeField]
[Tooltip("The mode for generating human segmentation depth texture.\n\n"
+ "Disabled -- No human depth texture produced.\n"
+ "Fastest -- Minimal rendering quality. Minimal frame computation.\n"
+ "Best -- Best rendering quality. Increased frame computation.")]
HumanSegmentationDepthMode m_HumanSegmentationDepthMode = HumanSegmentationDepthMode.Fastest;
/// <summary>
/// The human segmentation stencil texture.
/// </summary>
/// <value>
/// The human segmentation stencil texture, if any. Otherwise, <c>null</c>.
/// </value>
public Texture2D humanStencilTexture
{
get
{
if ((subsystem != null) && subsystem.TryGetHumanStencil(out XRTextureDescriptor humanStencilDescriptor))
{
m_HumanStencilTextureInfo = ARTextureInfo.GetUpdatedTextureInfo(m_HumanStencilTextureInfo,
humanStencilDescriptor);
Debug.Assert(m_HumanStencilTextureInfo.descriptor.dimension == TextureDimension.Tex2D, $"Human Stencil Texture needs to be a Texture 2D, but instead is {m_HumanStencilTextureInfo.descriptor.dimension.ToString()}.");
return (Texture2D) m_HumanStencilTextureInfo.texture;
}
return null;
}
}
/// <summary>
/// Attempt to get the latest human stencil CPU image. This provides directly access to the raw pixel data.
/// </summary>
/// <remarks>
/// The `XRCpuImage` must be disposed to avoid resource leaks.
/// </remarks>
/// <param name="cpuImage">If this method returns `true`, an acquired `XRCpuImage`.</param>
/// <returns>Returns `true` if the CPU image was acquired. Returns `false` otherwise.</returns>
public bool TryAcquireHumanStencilCpuImage(out XRCpuImage cpuImage)
{
if (subsystem == null)
{
cpuImage = default;
return false;
}
return subsystem.TryAcquireHumanStencilCpuImage(out cpuImage);
}
/// <summary>
/// The human segmentation depth texture.
/// </summary>
/// <value>
/// The human segmentation depth texture, if any. Otherwise, <c>null</c>.
/// </value>
public Texture2D humanDepthTexture
{
get
{
if ((subsystem != null) && subsystem.TryGetHumanDepth(out XRTextureDescriptor humanDepthDescriptor))
{
m_HumanDepthTextureInfo = ARTextureInfo.GetUpdatedTextureInfo(m_HumanDepthTextureInfo,
humanDepthDescriptor);
Debug.Assert(m_HumanDepthTextureInfo.descriptor.dimension == TextureDimension.Tex2D, $"Human Depth Texture needs to be a Texture 2D, but instead is {m_HumanDepthTextureInfo.descriptor.dimension.ToString()}.");
return (Texture2D) m_HumanDepthTextureInfo.texture;
}
return null;
}
}
/// <summary>
/// Attempt to get the latest human depth CPU image. This provides directly access to the raw pixel data.
/// </summary>
/// <remarks>
/// The `XRCpuImage` must be disposed to avoid resource leaks.
/// </remarks>
/// <param name="cpuImage">If this method returns `true`, an acquired `XRCpuImage`.</param>
/// <returns>Returns `true` if the CPU image was acquired. Returns `false` otherwise.</returns>
public bool TryAcquireHumanDepthCpuImage(out XRCpuImage cpuImage)
{
if (subsystem == null)
{
cpuImage = default;
return false;
}
return subsystem.TryAcquireHumanDepthCpuImage(out cpuImage);
}
/// <summary>
/// Callback before the subsystem is started (but after it is created).
/// </summary>
protected override void OnBeforeStart()
{
if (descriptor.supportsHumanSegmentationStencilImage)
{
subsystem.requestedHumanStencilMode = m_HumanSegmentationStencilMode;
}
if (descriptor.supportsHumanSegmentationDepthImage)
{
subsystem.requestedHumanDepthMode = m_HumanSegmentationDepthMode;
}
m_HumanStencilTextureInfo.Reset();
m_HumanDepthTextureInfo.Reset();
}
/// <summary>
/// Callback when the manager is being disabled.
/// </summary>
protected override void OnDisable()
{
base.OnDisable();
m_HumanStencilTextureInfo.Reset();
m_HumanDepthTextureInfo.Reset();
}
/// <summary>
/// Callback as the manager is being updated.
/// </summary>
public void Update()
{
if (subsystem != null)
{
UpdateTexturesInfos();
InvokeFrameReceived();
}
}
/// <summary>
/// Pull the texture descriptors from the occlusion subsystem, and update the texture information maintained by
/// this component.
/// </summary>
void UpdateTexturesInfos()
{
var textureDescriptors = subsystem.GetTextureDescriptors(Allocator.Temp);
try
{
int numUpdated = Math.Min(m_TextureInfos.Count, textureDescriptors.Length);
// Update the existing textures that are in common between the two arrays.
for (int i = 0; i < numUpdated; ++i)
{
m_TextureInfos[i] = ARTextureInfo.GetUpdatedTextureInfo(m_TextureInfos[i], textureDescriptors[i]);
}
// If there are fewer textures in the current frame than we had previously, destroy any remaining unneeded
// textures.
if (numUpdated < m_TextureInfos.Count)
{
for (int i = numUpdated; i < m_TextureInfos.Count; ++i)
{
m_TextureInfos[i].Reset();
}
m_TextureInfos.RemoveRange(numUpdated, (m_TextureInfos.Count - numUpdated));
}
// Else, if there are more textures in the current frame than we have previously, add new textures for any
// additional descriptors.
else if (textureDescriptors.Length > m_TextureInfos.Count)
{
for (int i = numUpdated; i < textureDescriptors.Length; ++i)
{
m_TextureInfos.Add(new ARTextureInfo(textureDescriptors[i]));
}
}
}
finally
{
if (textureDescriptors.IsCreated)
{
textureDescriptors.Dispose();
}
}
}
/// <summary>
/// Invoke the occlusion frame received event with the updated textures and texture property IDs.
/// </summary>
void InvokeFrameReceived()
{
if (frameReceived != null)
{
int numTextureInfos = m_TextureInfos.Count;
m_Textures.Clear();
m_TexturePropertyIds.Clear();
m_Textures.Capacity = numTextureInfos;
m_TexturePropertyIds.Capacity = numTextureInfos;
for (int i = 0; i < numTextureInfos; ++i)
{
Debug.Assert(m_TextureInfos[i].descriptor.dimension == TextureDimension.Tex2D, $"Texture needs to be a Texture 2D, but instead is {m_TextureInfos[i].descriptor.dimension.ToString()}.");
m_Textures.Add((Texture2D)m_TextureInfos[i].texture);
m_TexturePropertyIds.Add(m_TextureInfos[i].descriptor.propertyNameId);
}
subsystem.GetMaterialKeywords(out List<string> enabledMaterialKeywords, out List<string>disabledMaterialKeywords);
AROcclusionFrameEventArgs args = new AROcclusionFrameEventArgs();
args.textures = m_Textures;
args.propertyNameIds = m_TexturePropertyIds;
args.enabledMaterialKeywords = enabledMaterialKeywords;
args.disabledMaterialKeywords = disabledMaterialKeywords;
frameReceived(args);
}
}
}
}