ARSerializableObjects.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. using Utils;
  6. namespace UnityARInterface
  7. {
  8. /// <summary>
  9. /// Since unity doesn't flag the Vector4 as serializable, we
  10. /// need to create our own version. This one will automatically convert
  11. /// between Vector4 and SerializableVector4
  12. /// </summary>
  13. [Serializable]
  14. public class SerializableVector4
  15. {
  16. public float x;
  17. public float y;
  18. public float z;
  19. public float w;
  20. public SerializableVector4(float x, float y, float z, float w)
  21. {
  22. this.x = x;
  23. this.y = y;
  24. this.z = z;
  25. this.w = w;
  26. }
  27. public override string ToString()
  28. {
  29. return string.Format("[{0}, {1}, {2}, {3}]", x, y, z, w);
  30. }
  31. public static implicit operator Quaternion(SerializableVector4 serializedVector)
  32. {
  33. return new Quaternion(
  34. serializedVector.x,
  35. serializedVector.y,
  36. serializedVector.z,
  37. serializedVector.w);
  38. }
  39. public static implicit operator Vector4(SerializableVector4 serializedVector)
  40. {
  41. return new Vector4(
  42. serializedVector.x,
  43. serializedVector.y,
  44. serializedVector.z,
  45. serializedVector.w);
  46. }
  47. public static implicit operator SerializableVector4(Quaternion quaternion)
  48. {
  49. return new SerializableVector4(quaternion.x, quaternion.y, quaternion.z, quaternion.w);
  50. }
  51. public static implicit operator SerializableVector4(Vector4 vector)
  52. {
  53. return new SerializableVector4(vector.x, vector.y, vector.z, vector.w);
  54. }
  55. }
  56. [Serializable]
  57. public class SerializableVector3
  58. {
  59. public float x;
  60. public float y;
  61. public float z;
  62. public SerializableVector3(float x, float y, float z)
  63. {
  64. this.x = x;
  65. this.y = y;
  66. this.z = z;
  67. }
  68. public override string ToString()
  69. {
  70. return string.Format("[{0}, {1}, {2}]", x, y, z);
  71. }
  72. public static implicit operator Vector3(SerializableVector3 serializedVector)
  73. {
  74. return new Vector3(serializedVector.x, serializedVector.y, serializedVector.z);
  75. }
  76. public static implicit operator SerializableVector3(Vector3 vector)
  77. {
  78. return new SerializableVector3(vector.x, vector.y, vector.z);
  79. }
  80. }
  81. [Serializable]
  82. public class SerializableVector2
  83. {
  84. public float x;
  85. public float y;
  86. public SerializableVector2(float x, float y)
  87. {
  88. this.x = x;
  89. this.y = y;
  90. }
  91. public override string ToString()
  92. {
  93. return string.Format("[{0}, {1}]", x, y);
  94. }
  95. public static implicit operator Vector2(SerializableVector2 serializedVector)
  96. {
  97. return new Vector2(serializedVector.x, serializedVector.y);
  98. }
  99. public static implicit operator SerializableVector2(Vector2 vector)
  100. {
  101. return new SerializableVector2(vector.x, vector.y);
  102. }
  103. }
  104. [Serializable]
  105. public class SerializableBoundedPlane
  106. {
  107. public byte[] identifier;
  108. public SerializableVector3 center;
  109. public SerializableVector4 rotation;
  110. public SerializableVector2 extents;
  111. public SerializableBoundedPlane(
  112. byte[] identifier,
  113. SerializableVector3 center,
  114. SerializableVector4 rotation,
  115. SerializableVector2 extents)
  116. {
  117. this.identifier = identifier;
  118. this.center = center;
  119. this.rotation = rotation;
  120. this.extents = extents;
  121. }
  122. public static implicit operator SerializableBoundedPlane(BoundedPlane plane)
  123. {
  124. return new SerializableBoundedPlane(
  125. Encoding.UTF8.GetBytes(plane.id),
  126. plane.center,
  127. plane.rotation,
  128. plane.extents);
  129. }
  130. public static implicit operator BoundedPlane(SerializableBoundedPlane serializedPlane)
  131. {
  132. return new BoundedPlane()
  133. {
  134. id = Encoding.UTF8.GetString(serializedPlane.identifier),
  135. center = serializedPlane.center,
  136. extents = serializedPlane.extents,
  137. rotation = serializedPlane.rotation
  138. };
  139. }
  140. }
  141. [Serializable]
  142. public class SerializableSubMessage
  143. {
  144. public Guid subMessageId;
  145. public byte[] data { get; private set; }
  146. public SerializableSubMessage(Guid subMessageId, object data)
  147. {
  148. this.subMessageId = subMessageId;
  149. if (data != null)
  150. this.data = data.SerializeToByteArray();
  151. }
  152. public T GetDataAs<T>() where T : class
  153. {
  154. return data.Deserialize<T>();
  155. }
  156. }
  157. [Serializable]
  158. public class SerializableEnableVideo
  159. {
  160. public bool enableVideo;
  161. public SerializableEnableVideo(bool enableVideo)
  162. {
  163. this.enableVideo = enableVideo;
  164. }
  165. }
  166. [Serializable]
  167. public class SerializableBackgroundRendering
  168. {
  169. public bool backgroundRendering;
  170. public SerializableBackgroundRendering(bool backgroundRendering)
  171. {
  172. this.backgroundRendering = backgroundRendering;
  173. }
  174. }
  175. [Serializable]
  176. public class SerializableARSettings
  177. {
  178. public bool enablePointCloud;
  179. public bool enablePlaneDetection;
  180. public bool enableLightEstimation;
  181. public SerializableARSettings(bool pointCloud, bool planeDetection, bool lightEstimation)
  182. {
  183. enablePointCloud = pointCloud;
  184. enablePlaneDetection = planeDetection;
  185. enableLightEstimation = lightEstimation;
  186. }
  187. public static implicit operator SerializableARSettings(ARInterface.Settings settings)
  188. {
  189. return new SerializableARSettings(
  190. settings.enablePointCloud,
  191. settings.enablePlaneDetection,
  192. settings.enableLightEstimation);
  193. }
  194. public static implicit operator ARInterface.Settings(SerializableARSettings serializedSettings)
  195. {
  196. return new ARInterface.Settings()
  197. {
  198. enableLightEstimation = serializedSettings.enableLightEstimation,
  199. enablePlaneDetection = serializedSettings.enablePlaneDetection,
  200. enablePointCloud = serializedSettings.enablePointCloud
  201. };
  202. }
  203. }
  204. [Serializable]
  205. public class SerializableMatrix4x4
  206. {
  207. public SerializableVector4 column0;
  208. public SerializableVector4 column1;
  209. public SerializableVector4 column2;
  210. public SerializableVector4 column3;
  211. public SerializableMatrix4x4(
  212. SerializableVector4 column0,
  213. SerializableVector4 column1,
  214. SerializableVector4 column2,
  215. SerializableVector4 column3)
  216. {
  217. this.column0 = column0;
  218. this.column1 = column1;
  219. this.column2 = column2;
  220. this.column3 = column3;
  221. }
  222. public static implicit operator SerializableMatrix4x4(Matrix4x4 matrix)
  223. {
  224. return new SerializableMatrix4x4(
  225. matrix.GetColumn(0),
  226. matrix.GetColumn(1),
  227. matrix.GetColumn(2),
  228. matrix.GetColumn(3));
  229. }
  230. public static implicit operator Matrix4x4(SerializableMatrix4x4 serializedMatrix)
  231. {
  232. return new Matrix4x4(
  233. serializedMatrix.column0,
  234. serializedMatrix.column1,
  235. serializedMatrix.column2,
  236. serializedMatrix.column3);
  237. }
  238. }
  239. [Serializable]
  240. public class SerializableFrame
  241. {
  242. public SerializableMatrix4x4 projectionMatrix;
  243. public SerializableVector3 cameraPosition;
  244. public SerializableVector4 cameraRotation;
  245. public SerializableMatrix4x4 displayTransform;
  246. public SerializableFrame(
  247. SerializableMatrix4x4 projectionMatrix,
  248. SerializableVector3 cameraPosition,
  249. SerializableVector4 cameraRotation,
  250. SerializableMatrix4x4 displayTransform)
  251. {
  252. this.projectionMatrix = projectionMatrix;
  253. this.cameraPosition = cameraPosition;
  254. this.cameraRotation = cameraRotation;
  255. this.displayTransform = displayTransform;
  256. }
  257. }
  258. [Serializable]
  259. public class SerializableScreenCaptureParams
  260. {
  261. public int width;
  262. public int height;
  263. public int format;
  264. public SerializableScreenCaptureParams(int width, int height, int format)
  265. {
  266. this.width = width;
  267. this.height = height;
  268. this.format = format;
  269. }
  270. }
  271. [Serializable]
  272. public class SerializablePointCloud
  273. {
  274. public SerializableVector3[] points;
  275. public SerializablePointCloud(ARInterface.PointCloud pointCloud)
  276. {
  277. points = new SerializableVector3[pointCloud.points.Count];
  278. for (int i = 0; i < points.Length; ++i)
  279. {
  280. points[i] = pointCloud.points[i];
  281. }
  282. }
  283. public SerializablePointCloud(Vector3[] points)
  284. {
  285. this.points = new SerializableVector3[points.Length];
  286. for (int i = 0; i < points.Length; ++i)
  287. {
  288. this.points[i] = points[i];
  289. }
  290. }
  291. public IEnumerable<Vector3> asEnumerable
  292. {
  293. get
  294. {
  295. for (int i = 0; i < points.Length; ++i)
  296. {
  297. yield return points[i];
  298. }
  299. }
  300. }
  301. }
  302. [Serializable]
  303. public class SerializableLightEstimate
  304. {
  305. public int capabilities;
  306. public float ambientIntensity;
  307. public float ambientColorTemperature;
  308. public SerializableLightEstimate(ARInterface.LightEstimateCapabilities capabilities, float ambientIntensity, float ambientColorTemperature)
  309. {
  310. this.capabilities = (int)capabilities;
  311. this.ambientIntensity = ambientIntensity;
  312. this.ambientColorTemperature = ambientColorTemperature;
  313. }
  314. public static implicit operator SerializableLightEstimate(ARInterface.LightEstimate lightEstimate)
  315. {
  316. return new SerializableLightEstimate(
  317. lightEstimate.capabilities,
  318. lightEstimate.ambientIntensity,
  319. lightEstimate.ambientColorTemperature);
  320. }
  321. public static implicit operator ARInterface.LightEstimate(SerializableLightEstimate serializedLightEstimate)
  322. {
  323. return new ARInterface.LightEstimate()
  324. {
  325. capabilities = (ARInterface.LightEstimateCapabilities)serializedLightEstimate.capabilities,
  326. ambientIntensity = serializedLightEstimate.ambientIntensity,
  327. ambientColorTemperature = serializedLightEstimate.ambientColorTemperature,
  328. };
  329. }
  330. }
  331. }