XRHumanBodySubsystem.cs
15 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
using System;
using Unity.Collections;
#if UNITY_2020_2_OR_NEWER
using UnityEngine.SubsystemsImplementation;
#endif
namespace UnityEngine.XR.ARSubsystems
{
/// <summary>
/// Defines an interface for interacting with human body functionality.
/// </summary>
#if UNITY_2020_2_OR_NEWER
public class XRHumanBodySubsystem
: TrackingSubsystem<XRHumanBody, XRHumanBodySubsystem, XRHumanBodySubsystemDescriptor, XRHumanBodySubsystem.Provider>
#else
public abstract class XRHumanBodySubsystem
: TrackingSubsystem<XRHumanBody, XRHumanBodySubsystemDescriptor>
#endif
{
#if !UNITY_2020_2_OR_NEWER
/// <summary>
/// The implementation specific provider of human body functionality.
/// </summary>
/// <value>
/// The implementation specific provider of human body functionality.
/// </value>
protected Provider provider { get; }
#endif
/// <summary>
/// Whether 2D human body pose estimation is requested.
/// </summary>
/// <value>
/// <c>true</c> if 2D human body pose estimation is requested. Otherwise, <c>false</c>.
/// </value>
public bool pose2DRequested
{
get => provider.pose2DRequested;
set => provider.pose2DRequested = value;
}
/// <summary>
/// Whether 2D human body pose estimation is enabled.
/// </summary>
/// <value>
/// <c>true</c> if 2D human body pose estimation is enabled. Otherwise, <c>false</c>.
/// </value>
public bool pose2DEnabled => provider.pose2DEnabled;
/// <summary>
/// Whether 3D human body pose estimation is requested.
/// </summary>
/// <value>
/// <c>true</c> if 3D human body pose estimation is requested. Otherwise, <c>false</c>.
/// </value>
public bool pose3DRequested
{
get => provider.pose3DRequested;
set => provider.pose3DRequested = value;
}
/// <summary>
/// Whether 3D human body pose estimation is enabled.
/// </summary>
/// <value>
/// <c>true</c> if 3D human body pose estimation is enabled. Otherwise, <c>false</c>.
/// </value>
public bool pose3DEnabled => provider.pose3DEnabled;
/// <summary>
/// Whether 3D human body scale estimation is requested.
/// </summary>
/// <value>
/// <c>true</c> if 3D human body scale estimation is requested. Otherwise, <c>false</c>.
/// </value>
public bool pose3DScaleEstimationRequested
{
get => provider.pose3DScaleEstimationRequested;
set => provider.pose3DScaleEstimationRequested = value;
}
/// <summary>
/// Whether 3D human body scale estimation is enabled.
/// </summary>
/// <value>
/// <c>true</c> if 3D human body scale estimation is enabled. Otherwise, <c>false</c>.
/// </value>
public bool pose3DScaleEstimationEnabled => provider.pose3DScaleEstimationEnabled;
/// <summary>
/// Construct the subsystem by creating the functionality provider.
/// </summary>
public XRHumanBodySubsystem()
{
#if !UNITY_2020_2_OR_NEWER
provider = CreateProvider();
#endif
}
#if !UNITY_2020_2_OR_NEWER
/// <summary>
/// Start the subsystem by starting the provider.
/// </summary>
protected sealed override void OnStart() => provider.Start();
/// <summary>
/// Stop the subsystem by stopping the provider.
/// </summary>
protected sealed override void OnStop() => provider.Stop();
/// <summary>
/// Destroy the subsystem by destroying the provider.
/// </summary>
protected sealed override void OnDestroyed() => provider.Destroy();
#endif
/// <summary>
/// Query the provider for the trackable changes.
/// </summary>
/// <param name="allocator">The memory allocator to use for allocating the arrays.</param>
/// <returns>
/// The trackable human body changes.
/// </returns>
public override TrackableChanges<XRHumanBody> GetChanges(Allocator allocator)
=> provider.GetChanges(XRHumanBody.defaultValue, allocator);
/// <summary>
/// Query the provider for the skeleton joints for the requested trackable identifier.
/// </summary>
/// <param name="trackableId">The human body trackable identifier for which to query.</param>
/// <param name="allocator">The memory allocator to use for the returned arrays.</param>
/// <param name="skeleton">The array of skeleton joints to update and returns.</param>
public void GetSkeleton(TrackableId trackableId, Allocator allocator, ref NativeArray<XRHumanBodyJoint> skeleton)
=> provider.GetSkeleton(trackableId, allocator, ref skeleton);
/// <summary>
/// Gets the human body pose 2D joints for the current frame.
/// </summary>
/// <param name="allocator">The allocator to use for the returned array memory.</param>
/// <returns>
/// The array of body pose 2D joints.
/// </returns>
/// <remarks>
/// The returned array may be empty if the system is not enabled for human body pose 2D or if the system
/// does not detect a human in the camera image.
/// </remarks>
/// <exception cref="System.NotSupportedException">Thrown if the implementation does not support human body
/// pose 2D.</exception>
public NativeArray<XRHumanBodyPose2DJoint> GetHumanBodyPose2DJoints(Allocator allocator)
=> provider.GetHumanBodyPose2DJoints(default(XRHumanBodyPose2DJoint), Screen.width, Screen.height,
Screen.orientation, allocator);
#if !UNITY_2020_2_OR_NEWER
/// <summary>
/// Create the implementation specific functionality provider.
/// </summary>
/// <returns>
/// The implementation specific functionality provider.
/// </returns>
protected abstract Provider CreateProvider();
#endif
/// <summary>
/// Register the descriptor for the human body subsystem implementation.
/// </summary>
/// <param name="humanBodySubsystemCinfo">The human body subsystem implementation construction information.
/// </param>
/// <returns>
/// <c>true</c> if the descriptor was registered. Otherwise, <c>false</c>.
/// </returns>
public static bool Register(XRHumanBodySubsystemCinfo humanBodySubsystemCinfo)
{
XRHumanBodySubsystemDescriptor humanBodySubsystemDescriptor = XRHumanBodySubsystemDescriptor.Create(humanBodySubsystemCinfo);
#if UNITY_2020_2_OR_NEWER
SubsystemDescriptorStore.RegisterDescriptor(humanBodySubsystemDescriptor);
return true;
#else
return SubsystemRegistration.CreateDescriptor(humanBodySubsystemDescriptor);
#endif
}
/// <summary>
/// The provider which will service the <see cref="XRHumanBodySubsystem"/>.
/// </summary>
public abstract class Provider
#if UNITY_2020_2_OR_NEWER
: SubsystemProvider<XRHumanBodySubsystem>
#endif
{
#if !UNITY_2020_2_OR_NEWER
/// <summary>
/// Method to be implemented by the provider to start the functionality.
/// </summary>
public virtual void Start() { }
/// <summary>
/// Method to be implemented by the provider to stop the functionality.
/// </summary>
public virtual void Stop() { }
/// <summary>
/// Method to be implemented by the provider to destroy itself and release any resources.
/// </summary>
public virtual void Destroy() { }
#endif
/// <summary>
/// Property to be implemented by the provider to sets whether human body pose 2D estimation is requested.
/// </summary>
/// <returns>
/// <c>true</c> if human body pose 2D estimation has been requested. Otherwise, <c>false</c>.
/// </returns>
/// <exception cref="System.NotSupportedException">Thrown when setting the human body pose 2D estimation to
/// <c>true</c> if the implementation does not support human body pose 2D estimation.</exception>
public virtual bool pose2DRequested
{
get => false;
set
{
if (value)
{
throw new NotSupportedException("Setting human body pose 2D estimation to enabled is not "
+ "supported by this implementation");
}
}
}
/// <summary>
/// Property to be implemented by the provider to get whether human body pose 2D estimation is enabled.
/// </summary>
public virtual bool pose2DEnabled => false;
/// <summary>
/// Property to be implemented by the provider to sets whether human body pose 3D estimation is requested.
/// </summary>
/// <returns>
/// <c>true</c> if the human body pose 3D estimation has been requested. Otherwise, <c>false</c>.
/// </returns>
/// <exception cref="System.NotSupportedException">Thrown when setting the human body pose 3D estimation to
/// <c>true</c> if the implementation does not support human body pose 3D estimation.</exception>
public virtual bool pose3DRequested
{
get => false;
set
{
if (value)
{
throw new NotSupportedException("Setting human body pose 3D estimation to enabled is not "
+ "supported by this implementation");
}
}
}
/// <summary>
/// Method to be implemented by the provider to get whether human body pose 3D estimation is enabled.
/// </summary>
public virtual bool pose3DEnabled => false;
/// <summary>
/// Property to be implemented by the provider to get or set whether 3D human body scale estimation is requested.
/// </summary>
/// <returns>
/// <c>true</c> if the 3D human body scale estimation is set to the given value. Otherwise, <c>false</c>.
/// </returns>
/// <exception cref="System.NotSupportedException">Thrown when setting the 3D human body scale estimation to
/// <c>true</c> if the implementation does not support 3D human body scale estimation.</exception>
public virtual bool pose3DScaleEstimationRequested
{
get => false;
set
{
if (value)
{
throw new NotSupportedException("Setting 3D human body scale estimation to enabled is not "
+ "supported by this implementation");
}
}
}
/// <summary>
/// Property to be implemented by the provider to get whether 3D human body scale estimation is enabled.
/// </summary>
public virtual bool pose3DScaleEstimationEnabled => false;
/// <summary>
/// Method to be implemented by the provider to query for the set of human body changes.
/// </summary>
/// <param name="defaultHumanBody">The default human body.</param>
/// <param name="allocator">The memory allocator to use for the returns trackable changes.</param>
/// <returns>
/// The set of human body changes.
/// </returns>
/// <exception cref="System.NotSupportedException">Thrown for platforms not supporting human body pose
/// estimation.</exception>
public abstract TrackableChanges<XRHumanBody> GetChanges(XRHumanBody defaultHumanBody, Allocator allocator);
/// <summary>
/// Method to be implemented by the provider to get the skeleton joints for the requested trackable identifier.
/// </summary>
/// <param name="trackableId">The human body trackable identifier for which to query.</param>
/// <param name="allocator">The memory allocator to use for the returned arrays.</param>
/// <param name="skeleton">The array of skeleton joints to update and return.</param>
/// <exception cref="System.NotSupportedException">Thrown for platforms not supporting human body pose 3D.
/// </exception>
public virtual void GetSkeleton(TrackableId trackableId, Allocator allocator, ref NativeArray<XRHumanBodyJoint> skeleton)
=> throw new NotSupportedException("Skeletons are not supported by this implementation.");
/// <summary>
/// Method to be implemented by the provider to get the human body pose 2D joints for the current frame.
/// </summary>
/// <param name="defaultHumanBodyPose2DJoint">The default value for the body pose 2D joint.</param>
/// <param name="screenWidth">The width of the screen, in pixels.</param>
/// <param name="screenHeight">The height of the screen, in pixels.</param>
/// <param name="screenOrientation">The orientation of the device so that the joint positions may be
/// adjusted as required.</param>
/// <param name="allocator">The allocator to use for the returned array memory.</param>
/// <returns>
/// The array of body pose 2D joints.
/// </returns>
/// <remarks>
/// The returned array may be empty if the system is not enabled for human body pose 2D or if the system
/// does not detect a human in the camera image.
/// </remarks>
/// <exception cref="System.NotSupportedException">Thrown if the implementation does not support human body
/// pose 2D.</exception>
public virtual NativeArray<XRHumanBodyPose2DJoint> GetHumanBodyPose2DJoints(XRHumanBodyPose2DJoint defaultHumanBodyPose2DJoint,
int screenWidth,
int screenHeight,
ScreenOrientation screenOrientation,
Allocator allocator)
=> throw new NotSupportedException("human body pose 2D is not supported by this implementation");
}
}
}