-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathARKitDefines.h
438 lines (381 loc) · 11.3 KB
/
ARKitDefines.h
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// Unity Technologies Inc (c) 2017
// ARKitDefines.h
#import <ARKit/ARKit.h>
typedef struct
{
float x,y,z,w;
} UnityARVector4;
typedef struct
{
float x,y,z;
} UnityARVector3;
typedef struct
{
UnityARVector4 column0;
UnityARVector4 column1;
UnityARVector4 column2;
UnityARVector4 column3;
} UnityARMatrix4x4;
enum UnityARAlignment
{
UnityARAlignmentGravity,
UnityARAlignmentGravityAndHeading,
UnityARAlignmentCamera
};
enum UnityARPlaneDetection
{
UnityARPlaneDetectionNone = 0,
UnityARPlaneDetectionHorizontal = (1 << 0),
UnityARPlaneDetectionVertical = (1 << 1)
};
enum UnityAREnvironmentTexturing
{
UnityAREnvironmentTexturingNone,
UnityAREnvironmentTexturingManual,
UnityAREnvironmentTexturingAutomatic
};
typedef enum
{
UnityARWorldMappingStatusNotAvailable,
UnityARWorldMappingStatusLimited,
UnityARWorldMappingStatusExtending,
UnityARWorldMappingStatusMapped
}UnityARWorldMappingStatus;
typedef enum
{
kUnityARKitSupportedFeaturesNone = 0,
kUnityARKitSupportedFeaturesWorldMap = 1 << 0,
kUnityARKitSupportedFeaturesReferenceObject = 1 << 1,
} UnityARKitSupportedFeatures;
typedef struct
{
UnityARAlignment alignment;
uint32_t getPointCloudData;
uint32_t enableLightEstimation;
} ARKitSessionConfiguration;
typedef struct
{
UnityARAlignment alignment;
UnityARPlaneDetection planeDetection;
uint32_t getPointCloudData;
uint32_t enableLightEstimation;
uint32_t enableAutoFocus;
UnityAREnvironmentTexturing environmentTexturing;
int maximumNumberOfTrackedImages;
void *ptrVideoFormat;
char *referenceImagesResourceGroup;
char *referenceObjectsResourceGroup;
void *ptrDynamicReferenceObjects;
void *ptrWorldMap;
} ARKitWorldTrackingSessionConfiguration;
typedef struct
{
UnityARAlignment alignment;
uint32_t enableLightEstimation;
void *ptrVideoFormat;
} ARKitFaceTrackingConfiguration;
enum UnityARSessionRunOptions
{
UnityARSessionRunOptionsNone = 0,
UnityARSessionRunOptionResetTracking = (1 << 0),
UnityARSessionRunOptionRemoveExistingAnchors = (1 << 1)
};
typedef struct
{
NSUInteger vertexCount;
float *vertices;
NSUInteger textureCoordinateCount;
float *textureCoordinates;
NSUInteger triangleCount;
int *triangleIndices;
NSUInteger boundaryVertexCount;
float *boundaryVertices;
} UnityARPlaneGeometry;
typedef struct
{
void* identifier;
UnityARMatrix4x4 transform;
ARPlaneAnchorAlignment alignment;
UnityARVector4 center;
UnityARVector4 extent;
UnityARPlaneGeometry planeGeometry;
} UnityARAnchorData;
typedef struct
{
void* identifier;
UnityARMatrix4x4 transform;
} UnityARUserAnchorData;
typedef struct
{
NSUInteger vertexCount;
float *vertices;
NSUInteger textureCoordinateCount;
float *textureCoordinates;
NSUInteger triangleCount;
int *triangleIndices;
} UnityARFaceGeometry;
typedef struct
{
void *identifier;
UnityARMatrix4x4 transform;
UnityARFaceGeometry faceGeometry;
void *blendShapes; //NSDictionary<ARBlendShapeLocation, NSNumber*> *
UnityARMatrix4x4 leftEyeTransform;
UnityARMatrix4x4 rightEyeTransform;
UnityARVector3 lookAtPoint;
uint32_t isTracked;
} UnityARFaceAnchorData;
typedef struct
{
void* identifier;
UnityARMatrix4x4 transform;
void* referenceImageName;
float referenceImageSize;
int isTracked;
} UnityARImageAnchorData;
enum UnityARTrackingState
{
UnityARTrackingStateNotAvailable,
UnityARTrackingStateLimited,
UnityARTrackingStateNormal,
};
enum UnityARTrackingReason
{
UnityARTrackingStateReasonNone,
UnityARTrackingStateReasonInitializing,
UnityARTrackingStateReasonExcessiveMotion,
UnityARTrackingStateReasonInsufficientFeatures,
UnityARTrackingStateReasonRelocalizing,
};
typedef struct
{
uint32_t yWidth;
uint32_t yHeight;
uint32_t screenOrientation;
float texCoordScale;
void* cvPixelBufferPtr;
}UnityVideoParams;
typedef struct
{
float ambientIntensity;
float ambientColorTemperature;
}UnityARLightEstimation;
typedef struct
{
UnityARVector4 primaryLightDirectionAndIntensity;
float *sphericalHarmonicsCoefficients;
}UnityARDirectionalLightEstimate;
enum UnityLightDataType
{
LightEstimate,
DirectionalLightEstimate
};
typedef struct
{
UnityLightDataType arLightingType;
UnityARLightEstimation arLightEstimate;
UnityARDirectionalLightEstimate arDirectionalLightEstimate;
}UnityLightData;
typedef struct
{
UnityARMatrix4x4 worldTransform;
UnityARMatrix4x4 projectionMatrix;
UnityARTrackingState trackingState;
UnityARTrackingReason trackingReason;
UnityVideoParams videoParams;
UnityLightData lightData;
UnityARMatrix4x4 displayTransform;
void* ptrPointCloud;
uint32_t getLightEstimation;
UnityARWorldMappingStatus worldMappingStatus;
} UnityARCamera;
typedef struct
{
vector_float3* pointCloud;
NSUInteger pointCloudSize;
} UnityARPointCloudData;
typedef struct
{
void* pYPixelBytes;
void* pUVPixelBytes;
BOOL bEnable;
}UnityPixelBuffer;
typedef struct
{
void* ptrVideoFormat;
float imageResolutionWidth;
float imageResolutionHeight;
int framesPerSecond;
}UnityARVideoFormat;
typedef void (*UNITY_AR_FRAME_CALLBACK)(UnityARCamera camera);
typedef void (*UNITY_AR_ANCHOR_CALLBACK)(UnityARAnchorData anchorData);
typedef void (*UNITY_AR_USER_ANCHOR_CALLBACK)(UnityARUserAnchorData anchorData);
typedef void (*UNITY_AR_FACE_ANCHOR_CALLBACK)(UnityARFaceAnchorData anchorData);
typedef void (*UNITY_AR_IMAGE_ANCHOR_CALLBACK)(UnityARImageAnchorData anchorData);
typedef void (*UNITY_AR_SESSION_FAILED_CALLBACK)(const void* error);
typedef void (*UNITY_AR_SESSION_VOID_CALLBACK)(void);
typedef bool (*UNITY_AR_SESSION_RELOCALIZE_CALLBACK)(void);
typedef void (*UNITY_AR_SESSION_TRACKING_CHANGED)(UnityARCamera camera);
typedef void (*UNITY_AR_VIDEOFORMAT_CALLBACK)(UnityARVideoFormat format);
typedef void (*UNITY_AR_SESSION_WORLD_MAP_COMPLETION_CALLBACK)(const void*, void*);
typedef void (*UNITY_AR_SESSION_REF_OBJ_EXTRACT_COMPLETION_CALLBACK)(const void*, void*);
@protocol UnityARAnchorEventDispatcher
@required
-(void)sendAnchorAddedEvent:(ARAnchor*)anchor;
-(void)sendAnchorRemovedEvent:(ARAnchor*)anchor;
-(void)sendAnchorUpdatedEvent:(ARAnchor*)anchor;
@end
@interface UnityARSession : NSObject <ARSessionDelegate>
{
@public
ARSession* _session;
UNITY_AR_FRAME_CALLBACK _frameCallback;
UNITY_AR_SESSION_FAILED_CALLBACK _arSessionFailedCallback;
UNITY_AR_SESSION_VOID_CALLBACK _arSessionInterrupted;
UNITY_AR_SESSION_VOID_CALLBACK _arSessionInterruptionEnded;
UNITY_AR_SESSION_RELOCALIZE_CALLBACK _arSessionShouldRelocalize;
UNITY_AR_SESSION_TRACKING_CHANGED _arSessionTrackingChanged;
UNITY_AR_SESSION_WORLD_MAP_COMPLETION_CALLBACK _arSessionWorldMapCompletionHandler;
UNITY_AR_SESSION_REF_OBJ_EXTRACT_COMPLETION_CALLBACK _arSessionRefObjExtractCompletionHandler;
NSMutableDictionary* _classToCallbackMap;
id <MTLDevice> _device;
CVMetalTextureCacheRef _textureCache;
BOOL _getPointCloudData;
BOOL _getLightEstimation;
}
- (void) setupMetal;
@end
static inline bool UnityIsARKit_1_5_Supported()
{
if (@available(iOS 11.3, *))
{
return [ARImageAnchor class];
}
else
{
return false;
}
}
static inline bool UnityAreFeaturesSupported(int features)
{
bool featuresSupported = true;
if (features & kUnityARKitSupportedFeaturesWorldMap)
{
if (@available(iOS 12.0, *))
{
featuresSupported &= (bool)[ARWorldMap class];
}
else
{
featuresSupported = false;
}
}
if (features & kUnityARKitSupportedFeaturesReferenceObject)
{
if (@available(iOS 12.0, *))
{
featuresSupported &= (bool)[ARReferenceObject class];
}
else
{
featuresSupported = false;
}
}
return featuresSupported;
}
static inline bool UnityIsARKit_2_0_Supported()
{
if (@available(iOS 12.0, *))
{
if ([ARReferenceObject class])
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
inline void ARKitMatrixToUnityARMatrix4x4(const matrix_float4x4& matrixIn, UnityARMatrix4x4* matrixOut)
{
vector_float4 c0 = matrixIn.columns[0];
matrixOut->column0.x = c0.x;
matrixOut->column0.y = c0.y;
matrixOut->column0.z = c0.z;
matrixOut->column0.w = c0.w;
vector_float4 c1 = matrixIn.columns[1];
matrixOut->column1.x = c1.x;
matrixOut->column1.y = c1.y;
matrixOut->column1.z = c1.z;
matrixOut->column1.w = c1.w;
vector_float4 c2 = matrixIn.columns[2];
matrixOut->column2.x = c2.x;
matrixOut->column2.y = c2.y;
matrixOut->column2.z = c2.z;
matrixOut->column2.w = c2.w;
vector_float4 c3 = matrixIn.columns[3];
matrixOut->column3.x = c3.x;
matrixOut->column3.y = c3.y;
matrixOut->column3.z = c3.z;
matrixOut->column3.w = c3.w;
}
inline void UnityARMatrix4x4ToARKitMatrix(const UnityARMatrix4x4& matrixIn, matrix_float4x4* matrixOut)
{
matrixOut->columns[0].x = matrixIn.column0.x;
matrixOut->columns[0].y = matrixIn.column0.y;
matrixOut->columns[0].z = matrixIn.column0.z;
matrixOut->columns[0].w = matrixIn.column0.w;
matrixOut->columns[1].x = matrixIn.column1.x;
matrixOut->columns[1].y = matrixIn.column1.y;
matrixOut->columns[1].z = matrixIn.column1.z;
matrixOut->columns[1].w = matrixIn.column1.w;
matrixOut->columns[2].x = matrixIn.column2.x;
matrixOut->columns[2].y = matrixIn.column2.y;
matrixOut->columns[2].z = matrixIn.column2.z;
matrixOut->columns[2].w = matrixIn.column2.w;
matrixOut->columns[3].x = matrixIn.column3.x;
matrixOut->columns[3].y = matrixIn.column3.y;
matrixOut->columns[3].z = matrixIn.column3.z;
matrixOut->columns[3].w = matrixIn.column3.w;
}
inline ARSessionRunOptions GetARSessionRunOptionsFromUnityARSessionRunOptions(UnityARSessionRunOptions runOptions)
{
ARSessionRunOptions ret = 0;
if ((runOptions & UnityARSessionRunOptionResetTracking) != 0)
ret |= ARSessionRunOptionResetTracking;
if ((runOptions & UnityARSessionRunOptionRemoveExistingAnchors) != 0)
ret |= ARSessionRunOptionRemoveExistingAnchors;
return ret;
}
static inline ARWorldAlignment GetARWorldAlignmentFromUnityARAlignment(UnityARAlignment& unityAlignment)
{
switch (unityAlignment)
{
case UnityARAlignmentGravity:
return ARWorldAlignmentGravity;
case UnityARAlignmentGravityAndHeading:
return ARWorldAlignmentGravityAndHeading;
case UnityARAlignmentCamera:
return ARWorldAlignmentCamera;
}
}
static inline ARPlaneDetection GetARPlaneDetectionFromUnityARPlaneDetection(UnityARPlaneDetection planeDetection)
{
ARPlaneDetection ret = ARPlaneDetectionNone;
if ((planeDetection & UnityARPlaneDetectionNone) != 0)
ret |= ARPlaneDetectionNone;
if ((planeDetection & UnityARPlaneDetectionHorizontal) != 0)
ret |= ARPlaneDetectionHorizontal;
if ((planeDetection & UnityARPlaneDetectionVertical) != 0)
{
if (@available(iOS 11.3, *)) {
ret |= ARPlaneDetectionVertical;
}
}
return ret;
}