-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathAREnvironmentProbe.mm
170 lines (146 loc) · 6.51 KB
/
AREnvironmentProbe.mm
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
// Unity Technologies Inc (c) 2017
// AREnvironmentProbe.mm
// Main implementation of ARKit plugin native AREnvironmentProbeAnchor
#include "ARKitDefines.h"
enum UnityAREnvironmentTextureFormat : long
{
// NOTE: Not a complete set, but an initial mapping that matches some internal texture readmap mappings.
UnityAREnvironmentTextureFormatR16,
UnityAREnvironmentTextureFormatRG16,
UnityAREnvironmentTextureFormatBGRA32,
UnityAREnvironmentTextureFormatRGBA32,
UnityAREnvironmentTextureFormatRGBAFloat,
UnityAREnvironmentTextureFormatRGBAHalf,
UnityAREnvironmentTextureFormatDefault = UnityAREnvironmentTextureFormatBGRA32
};
typedef struct
{
void* cubemapPtr;
UnityAREnvironmentTextureFormat textureFormat;
int width;
int height;
int mipmapCount;
} UnityAREnvironmentProbeCubemapData;
typedef struct
{
void* identifier;
UnityARMatrix4x4 transform;
UnityAREnvironmentProbeCubemapData cubemapData;
UnityARVector3 extent;
} UnityAREnvironmentProbeAnchorData;
inline UnityAREnvironmentTextureFormat GetUnityAREnvironmentTextureFormat (MTLPixelFormat mtlPixelFormat)
{
// This mapping is based on a Unity internal runtime method metal::UnityTextureFormat() in TextureFormatMetal.mm that maps a subset of the Metal pixel formats to the Unity texture format
switch (mtlPixelFormat)
{
case MTLPixelFormatRGBA16Float:
return UnityAREnvironmentTextureFormatRGBAHalf;
case MTLPixelFormatRGBA32Float:
return UnityAREnvironmentTextureFormatRGBAFloat;
case MTLPixelFormatRGBA8Unorm_sRGB:
case MTLPixelFormatRGBA8Unorm:
return UnityAREnvironmentTextureFormatRGBA32;
case MTLPixelFormatBGRA8Unorm_sRGB:
case MTLPixelFormatBGRA8Unorm:
return UnityAREnvironmentTextureFormatBGRA32;
case MTLPixelFormatR16Unorm:
return UnityAREnvironmentTextureFormatR16;
case MTLPixelFormatRG8Unorm:
return UnityAREnvironmentTextureFormatRG16;
default:
return UnityAREnvironmentTextureFormatDefault;
}
}
inline void UnityAREnvironmentProbeCubemapDataFromMTLTextureRef(UnityAREnvironmentProbeCubemapData& cubemapData, MTLTextureRef environmentTexture)
{
cubemapData.cubemapPtr = (__bridge void*)environmentTexture;
cubemapData.textureFormat = GetUnityAREnvironmentTextureFormat([environmentTexture pixelFormat]);
cubemapData.width = (int)[environmentTexture width];
cubemapData.height = (int)[environmentTexture height];
cubemapData.mipmapCount = (int)[environmentTexture mipmapLevelCount];
}
API_AVAILABLE(ios(12.0))
inline void UnityAREnvironmentProbeAnchorDataFromAREnvironmentProbeAnchorPtr(UnityAREnvironmentProbeAnchorData& anchorData, AREnvironmentProbeAnchor* nativeAnchor)
{
anchorData.identifier = (void*)[nativeAnchor.identifier.UUIDString UTF8String];
ARKitMatrixToUnityARMatrix4x4(nativeAnchor.transform, &anchorData.transform);
UnityAREnvironmentProbeCubemapDataFromMTLTextureRef(anchorData.cubemapData, [nativeAnchor environmentTexture]);
anchorData.extent = UnityARVector3 {
nativeAnchor.extent.x,
nativeAnchor.extent.y,
nativeAnchor.extent.z
};
}
typedef void (*UNITY_AR_ENVPROBE_ANCHOR_CALLBACK)(UnityAREnvironmentProbeAnchorData anchorData);
@interface UnityAREnvironmentProbeAnchorCallbackWrapper : NSObject <UnityARAnchorEventDispatcher>
{
@public
UNITY_AR_ENVPROBE_ANCHOR_CALLBACK _anchorAddedCallback;
UNITY_AR_ENVPROBE_ANCHOR_CALLBACK _anchorUpdatedCallback;
UNITY_AR_ENVPROBE_ANCHOR_CALLBACK _anchorRemovedCallback;
}
@end
@implementation UnityAREnvironmentProbeAnchorCallbackWrapper
-(void)sendAnchorAddedEvent:(ARAnchor*)anchor
{
if (@available(iOS 12.0, *))
{
UnityAREnvironmentProbeAnchorData data;
UnityAREnvironmentProbeAnchorDataFromAREnvironmentProbeAnchorPtr(data, (AREnvironmentProbeAnchor*)anchor);
_anchorAddedCallback(data);
}
}
-(void)sendAnchorRemovedEvent:(ARAnchor*)anchor
{
if (@available(iOS 12.0, *)) {
UnityAREnvironmentProbeAnchorData data;
UnityAREnvironmentProbeAnchorDataFromAREnvironmentProbeAnchorPtr(data, (AREnvironmentProbeAnchor*)anchor);
_anchorRemovedCallback(data);
}
}
-(void)sendAnchorUpdatedEvent:(ARAnchor*)anchor
{
if (@available(iOS 12.0, *))
{
UnityAREnvironmentProbeAnchorData data;
UnityAREnvironmentProbeAnchorDataFromAREnvironmentProbeAnchorPtr(data, (AREnvironmentProbeAnchor*)anchor);
_anchorUpdatedCallback(data);
}
}
@end
extern "C" UnityAREnvironmentProbeAnchorData SessionAddEnvironmentProbeAnchor(void* nativeSession, UnityAREnvironmentProbeAnchorData anchorData)
{
UnityAREnvironmentProbeAnchorData returnAnchorData;
if (UnityIsARKit_2_0_Supported())
{
// create a native AREnvironmentProbeAnchor and add it to the session
// then return the data back to the user that they will
// need in case they want to remove it
UnityARSession* session = (__bridge UnityARSession*)nativeSession;
matrix_float4x4 initMat;
UnityARMatrix4x4ToARKitMatrix(anchorData.transform, &initMat);
if (@available(iOS 12.0, *))
{
AREnvironmentProbeAnchor *newAnchor = [[AREnvironmentProbeAnchor alloc] initWithTransform:initMat];
[session->_session addAnchor:newAnchor];
UnityAREnvironmentProbeAnchorDataFromAREnvironmentProbeAnchorPtr(returnAnchorData, newAnchor);
}
}
return returnAnchorData;
}
extern "C" void session_SetEnvironmentProbeAnchorCallbacks(const void* session, UNITY_AR_ENVPROBE_ANCHOR_CALLBACK envProbeAnchorAddedCallback,
UNITY_AR_ENVPROBE_ANCHOR_CALLBACK envProbeAnchorUpdatedCallback,
UNITY_AR_ENVPROBE_ANCHOR_CALLBACK envProbeAnchorRemovedCallback)
{
if (UnityIsARKit_2_0_Supported())
{
UnityARSession* nativeSession = (__bridge UnityARSession*)session;
UnityAREnvironmentProbeAnchorCallbackWrapper* envProbeAnchorCallbacks = [[UnityAREnvironmentProbeAnchorCallbackWrapper alloc] init];
envProbeAnchorCallbacks->_anchorAddedCallback = envProbeAnchorAddedCallback;
envProbeAnchorCallbacks->_anchorUpdatedCallback = envProbeAnchorUpdatedCallback;
envProbeAnchorCallbacks->_anchorRemovedCallback = envProbeAnchorRemovedCallback;
if (@available(iOS 12.0, *)) {
[nativeSession->_classToCallbackMap setObject:envProbeAnchorCallbacks forKey:[AREnvironmentProbeAnchor class]];
}
}
}