-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathARSerializableObjects.cs
379 lines (331 loc) · 11.1 KB
/
ARSerializableObjects.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.XR.iOS.Utils;
namespace UnityARInterface
{
/// <summary>
/// Since unity doesn't flag the Vector4 as serializable, we
/// need to create our own version. This one will automatically convert
/// between Vector4 and SerializableVector4
/// </summary>
[Serializable]
public class SerializableVector4
{
public float x;
public float y;
public float z;
public float w;
public SerializableVector4(float x, float y, float z, float w)
{
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public override string ToString()
{
return string.Format("[{0}, {1}, {2}, {3}]", x, y, z, w);
}
public static implicit operator Quaternion(SerializableVector4 serializedVector)
{
return new Quaternion(
serializedVector.x,
serializedVector.y,
serializedVector.z,
serializedVector.w);
}
public static implicit operator Vector4(SerializableVector4 serializedVector)
{
return new Vector4(
serializedVector.x,
serializedVector.y,
serializedVector.z,
serializedVector.w);
}
public static implicit operator SerializableVector4(Quaternion quaternion)
{
return new SerializableVector4(quaternion.x, quaternion.y, quaternion.z, quaternion.w);
}
public static implicit operator SerializableVector4(Vector4 vector)
{
return new SerializableVector4(vector.x, vector.y, vector.z, vector.w);
}
}
[Serializable]
public class SerializableVector3
{
public float x;
public float y;
public float z;
public SerializableVector3(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
public override string ToString()
{
return string.Format("[{0}, {1}, {2}]", x, y, z);
}
public static implicit operator Vector3(SerializableVector3 serializedVector)
{
return new Vector3(serializedVector.x, serializedVector.y, serializedVector.z);
}
public static implicit operator SerializableVector3(Vector3 vector)
{
return new SerializableVector3(vector.x, vector.y, vector.z);
}
}
[Serializable]
public class SerializableVector2
{
public float x;
public float y;
public SerializableVector2(float x, float y)
{
this.x = x;
this.y = y;
}
public override string ToString()
{
return string.Format("[{0}, {1}]", x, y);
}
public static implicit operator Vector2(SerializableVector2 serializedVector)
{
return new Vector2(serializedVector.x, serializedVector.y);
}
public static implicit operator SerializableVector2(Vector2 vector)
{
return new SerializableVector2(vector.x, vector.y);
}
}
[Serializable]
public class SerializableBoundedPlane
{
public byte[] identifier;
public SerializableVector3 center;
public SerializableVector4 rotation;
public SerializableVector2 extents;
public SerializableBoundedPlane(
byte[] identifier,
SerializableVector3 center,
SerializableVector4 rotation,
SerializableVector2 extents)
{
this.identifier = identifier;
this.center = center;
this.rotation = rotation;
this.extents = extents;
}
public static implicit operator SerializableBoundedPlane(BoundedPlane plane)
{
return new SerializableBoundedPlane(
Encoding.UTF8.GetBytes(plane.id),
plane.center,
plane.rotation,
plane.extents);
}
public static implicit operator BoundedPlane(SerializableBoundedPlane serializedPlane)
{
return new BoundedPlane()
{
id = Encoding.UTF8.GetString(serializedPlane.identifier),
center = serializedPlane.center,
extents = serializedPlane.extents,
rotation = serializedPlane.rotation
};
}
}
[Serializable]
public class SerializableSubMessage
{
public Guid subMessageId;
public byte[] data { get; private set; }
public SerializableSubMessage(Guid subMessageId, object data)
{
this.subMessageId = subMessageId;
if (data != null)
this.data = data.SerializeToByteArray();
}
public T GetDataAs<T>() where T : class
{
return data.Deserialize<T>();
}
}
[Serializable]
public class SerializableEnableVideo
{
public bool enableVideo;
public SerializableEnableVideo(bool enableVideo)
{
this.enableVideo = enableVideo;
}
}
[Serializable]
public class SerializableBackgroundRendering
{
public bool backgroundRendering;
public SerializableBackgroundRendering(bool backgroundRendering)
{
this.backgroundRendering = backgroundRendering;
}
}
[Serializable]
public class SerializableARSettings
{
public bool enablePointCloud;
public bool enablePlaneDetection;
public bool enableLightEstimation;
public SerializableARSettings(bool pointCloud, bool planeDetection, bool lightEstimation)
{
enablePointCloud = pointCloud;
enablePlaneDetection = planeDetection;
enableLightEstimation = lightEstimation;
}
public static implicit operator SerializableARSettings(ARInterface.Settings settings)
{
return new SerializableARSettings(
settings.enablePointCloud,
settings.enablePlaneDetection,
settings.enableLightEstimation);
}
public static implicit operator ARInterface.Settings(SerializableARSettings serializedSettings)
{
return new ARInterface.Settings()
{
enableLightEstimation = serializedSettings.enableLightEstimation,
enablePlaneDetection = serializedSettings.enablePlaneDetection,
enablePointCloud = serializedSettings.enablePointCloud
};
}
}
[Serializable]
public class SerializableMatrix4x4
{
public SerializableVector4 column0;
public SerializableVector4 column1;
public SerializableVector4 column2;
public SerializableVector4 column3;
public SerializableMatrix4x4(
SerializableVector4 column0,
SerializableVector4 column1,
SerializableVector4 column2,
SerializableVector4 column3)
{
this.column0 = column0;
this.column1 = column1;
this.column2 = column2;
this.column3 = column3;
}
public static implicit operator SerializableMatrix4x4(Matrix4x4 matrix)
{
return new SerializableMatrix4x4(
matrix.GetColumn(0),
matrix.GetColumn(1),
matrix.GetColumn(2),
matrix.GetColumn(3));
}
public static implicit operator Matrix4x4(SerializableMatrix4x4 serializedMatrix)
{
return new Matrix4x4(
serializedMatrix.column0,
serializedMatrix.column1,
serializedMatrix.column2,
serializedMatrix.column3);
}
}
[Serializable]
public class SerializableFrame
{
public SerializableMatrix4x4 projectionMatrix;
public SerializableVector3 cameraPosition;
public SerializableVector4 cameraRotation;
public SerializableMatrix4x4 displayTransform;
public SerializableFrame(
SerializableMatrix4x4 projectionMatrix,
SerializableVector3 cameraPosition,
SerializableVector4 cameraRotation,
SerializableMatrix4x4 displayTransform)
{
this.projectionMatrix = projectionMatrix;
this.cameraPosition = cameraPosition;
this.cameraRotation = cameraRotation;
this.displayTransform = displayTransform;
}
}
[Serializable]
public class SerializableScreenCaptureParams
{
public int width;
public int height;
public int format;
public SerializableScreenCaptureParams(int width, int height, int format)
{
this.width = width;
this.height = height;
this.format = format;
}
}
[Serializable]
public class SerializablePointCloud
{
public SerializableVector3[] points;
public SerializablePointCloud(ARInterface.PointCloud pointCloud)
{
points = new SerializableVector3[pointCloud.points.Count];
for (int i = 0; i < points.Length; ++i)
{
points[i] = pointCloud.points[i];
}
}
public SerializablePointCloud(Vector3[] points)
{
this.points = new SerializableVector3[points.Length];
for (int i = 0; i < points.Length; ++i)
{
this.points[i] = points[i];
}
}
public IEnumerable<Vector3> asEnumerable
{
get
{
for (int i = 0; i < points.Length; ++i)
{
yield return points[i];
}
}
}
}
[Serializable]
public class SerializableLightEstimate
{
public int capabilities;
public float ambientIntensity;
public float ambientColorTemperature;
public SerializableLightEstimate(ARInterface.LightEstimateCapabilities capabilities, float ambientIntensity, float ambientColorTemperature)
{
this.capabilities = (int)capabilities;
this.ambientIntensity = ambientIntensity;
this.ambientColorTemperature = ambientColorTemperature;
}
public static implicit operator SerializableLightEstimate(ARInterface.LightEstimate lightEstimate)
{
return new SerializableLightEstimate(
lightEstimate.capabilities,
lightEstimate.ambientIntensity,
lightEstimate.ambientColorTemperature);
}
public static implicit operator ARInterface.LightEstimate(SerializableLightEstimate serializedLightEstimate)
{
return new ARInterface.LightEstimate()
{
capabilities = (ARInterface.LightEstimateCapabilities)serializedLightEstimate.capabilities,
ambientIntensity = serializedLightEstimate.ambientIntensity,
ambientColorTemperature = serializedLightEstimate.ambientColorTemperature,
};
}
}
}