SerializableObjects.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.XR.iOS;
  4. using System.Text;
  5. namespace Utils
  6. {
  7. /// <summary>
  8. /// Since unity doesn't flag the Vector4 as serializable, we
  9. /// need to create our own version. This one will automatically convert
  10. /// between Vector4 and SerializableVector4
  11. /// </summary>
  12. [Serializable]
  13. public class SerializableVector4
  14. {
  15. /// <summary>
  16. /// x component
  17. /// </summary>
  18. public float x;
  19. /// <summary>
  20. /// y component
  21. /// </summary>
  22. public float y;
  23. /// <summary>
  24. /// z component
  25. /// </summary>
  26. public float z;
  27. /// <summary>
  28. /// w component
  29. /// </summary>
  30. public float w;
  31. /// <summary>
  32. /// Constructor
  33. /// </summary>
  34. /// <param name="rX"></param>
  35. /// <param name="rY"></param>
  36. /// <param name="rZ"></param>
  37. /// <param name="rW"></param>
  38. public SerializableVector4(float rX, float rY, float rZ, float rW)
  39. {
  40. x = rX;
  41. y = rY;
  42. z = rZ;
  43. w = rW;
  44. }
  45. /// <summary>
  46. /// Returns a string representation of the object
  47. /// </summary>
  48. /// <returns></returns>
  49. public override string ToString()
  50. {
  51. return String.Format("[{0}, {1}, {2}, {3}]", x, y, z, w);
  52. }
  53. /// <summary>
  54. /// Automatic conversion from SerializableVector4 to Vector4
  55. /// </summary>
  56. /// <param name="rValue"></param>
  57. /// <returns></returns>
  58. public static implicit operator Vector4(SerializableVector4 rValue)
  59. {
  60. return new Vector4(rValue.x, rValue.y, rValue.z, rValue.w);
  61. }
  62. /// <summary>
  63. /// Automatic conversion from Vector4 to SerializableVector4
  64. /// </summary>
  65. /// <param name="rValue"></param>
  66. /// <returns></returns>
  67. public static implicit operator SerializableVector4(Vector4 rValue)
  68. {
  69. return new SerializableVector4(rValue.x, rValue.y, rValue.z, rValue.w);
  70. }
  71. }
  72. [Serializable]
  73. public class serializableUnityARMatrix4x4
  74. {
  75. public SerializableVector4 column0;
  76. public SerializableVector4 column1;
  77. public SerializableVector4 column2;
  78. public SerializableVector4 column3;
  79. public serializableUnityARMatrix4x4(SerializableVector4 v0, SerializableVector4 v1, SerializableVector4 v2, SerializableVector4 v3)
  80. {
  81. column0 = v0;
  82. column1 = v1;
  83. column2 = v2;
  84. column3 = v3;
  85. }
  86. /// <summary>
  87. /// Automatic conversion from UnityARMatrix4x4 to serializableUnityARMatrix4x4
  88. /// </summary>
  89. /// <param name="rValue"></param>
  90. /// <returns></returns>
  91. public static implicit operator serializableUnityARMatrix4x4(UnityARMatrix4x4 rValue)
  92. {
  93. return new serializableUnityARMatrix4x4(rValue.column0, rValue.column1, rValue.column2, rValue.column3);
  94. }
  95. /// <summary>
  96. /// Automatic conversion from serializableUnityARMatrix4x4 to UnityARMatrix4x4
  97. /// </summary>
  98. /// <param name="rValue"></param>
  99. /// <returns></returns>
  100. public static implicit operator UnityARMatrix4x4(serializableUnityARMatrix4x4 rValue)
  101. {
  102. return new UnityARMatrix4x4(rValue.column0, rValue.column1, rValue.column2, rValue.column3);
  103. }
  104. public static implicit operator serializableUnityARMatrix4x4(Matrix4x4 rValue)
  105. {
  106. return new serializableUnityARMatrix4x4(rValue.GetColumn(0), rValue.GetColumn(1), rValue.GetColumn(2), rValue.GetColumn(3));
  107. }
  108. public static implicit operator Matrix4x4(serializableUnityARMatrix4x4 rValue)
  109. {
  110. #if UNITY_2017_1_OR_NEWER
  111. return new Matrix4x4(rValue.column0, rValue.column1, rValue.column2, rValue.column3);
  112. #else
  113. Matrix4x4 mRet = new Matrix4x4 ();
  114. mRet.SetColumn (0, rValue.column0);
  115. mRet.SetColumn (1, rValue.column1);
  116. mRet.SetColumn (2, rValue.column2);
  117. mRet.SetColumn (3, rValue.column3);
  118. return mRet;
  119. #endif
  120. }
  121. };
  122. [Serializable]
  123. public class serializableSHC
  124. {
  125. public byte [] shcData;
  126. public serializableSHC(byte [] inputSHCData)
  127. {
  128. shcData = inputSHCData;
  129. }
  130. public static implicit operator serializableSHC(float [] floatsSHC)
  131. {
  132. if (floatsSHC != null)
  133. {
  134. byte [] createBuf = new byte[floatsSHC.Length * sizeof(float)];
  135. for(int i = 0; i < floatsSHC.Length; i++)
  136. {
  137. Buffer.BlockCopy( BitConverter.GetBytes( floatsSHC[i] ), 0, createBuf, (i)*sizeof(float), sizeof(float) );
  138. }
  139. return new serializableSHC (createBuf);
  140. }
  141. else
  142. {
  143. return new serializableSHC(null);
  144. }
  145. }
  146. public static implicit operator float [] (serializableSHC spc)
  147. {
  148. if (spc.shcData != null)
  149. {
  150. int numFloats = spc.shcData.Length / (sizeof(float));
  151. float [] shcFloats = new float[numFloats];
  152. for (int i = 0; i < numFloats; i++)
  153. {
  154. shcFloats [i] = BitConverter.ToSingle (spc.shcData, i * sizeof(float));
  155. }
  156. return shcFloats;
  157. }
  158. else
  159. {
  160. return null;
  161. }
  162. }
  163. };
  164. [Serializable]
  165. public class serializableUnityARLightData
  166. {
  167. public LightDataType whichLight;
  168. public serializableSHC lightSHC;
  169. public SerializableVector4 primaryLightDirAndIntensity;
  170. public float ambientIntensity;
  171. public float ambientColorTemperature;
  172. serializableUnityARLightData(UnityARLightData lightData)
  173. {
  174. whichLight = lightData.arLightingType;
  175. if (whichLight == LightDataType.DirectionalLightEstimate) {
  176. lightSHC = lightData.arDirectonalLightEstimate.sphericalHarmonicsCoefficients;
  177. Vector3 lightDir = lightData.arDirectonalLightEstimate.primaryLightDirection;
  178. float lightIntensity = lightData.arDirectonalLightEstimate.primaryLightIntensity;
  179. primaryLightDirAndIntensity = new SerializableVector4 (lightDir.x, lightDir.y, lightDir.z, lightIntensity);
  180. } else {
  181. ambientIntensity = lightData.arLightEstimate.ambientIntensity;
  182. ambientColorTemperature = lightData.arLightEstimate.ambientColorTemperature;
  183. }
  184. }
  185. public static implicit operator serializableUnityARLightData(UnityARLightData rValue)
  186. {
  187. return new serializableUnityARLightData(rValue);
  188. }
  189. public static implicit operator UnityARLightData(serializableUnityARLightData rValue)
  190. {
  191. UnityARDirectionalLightEstimate udle = null;
  192. UnityARLightEstimate ule = new UnityARLightEstimate (rValue.ambientIntensity, rValue.ambientColorTemperature);
  193. if (rValue.whichLight == LightDataType.DirectionalLightEstimate) {
  194. Vector3 lightDir = new Vector3 (rValue.primaryLightDirAndIntensity.x, rValue.primaryLightDirAndIntensity.y, rValue.primaryLightDirAndIntensity.z);
  195. udle = new UnityARDirectionalLightEstimate (rValue.lightSHC, lightDir, rValue.primaryLightDirAndIntensity.w);
  196. }
  197. return new UnityARLightData(rValue.whichLight, ule, udle);
  198. }
  199. }
  200. [Serializable]
  201. public class serializableUnityARCamera
  202. {
  203. public serializableUnityARMatrix4x4 worldTransform;
  204. public serializableUnityARMatrix4x4 projectionMatrix;
  205. public ARTrackingState trackingState;
  206. public ARTrackingStateReason trackingReason;
  207. public UnityVideoParams videoParams;
  208. public serializableUnityARLightData lightData;
  209. public serializablePointCloud pointCloud;
  210. public serializableUnityARMatrix4x4 displayTransform;
  211. public serializableUnityARCamera( serializableUnityARMatrix4x4 wt, serializableUnityARMatrix4x4 pm, ARTrackingState ats, ARTrackingStateReason atsr, UnityVideoParams uvp, UnityARLightData lightDat, serializableUnityARMatrix4x4 dt, serializablePointCloud spc)
  212. {
  213. worldTransform = wt;
  214. projectionMatrix = pm;
  215. trackingState = ats;
  216. trackingReason = atsr;
  217. videoParams = uvp;
  218. lightData = lightDat;
  219. displayTransform = dt;
  220. pointCloud = spc;
  221. }
  222. public static implicit operator serializableUnityARCamera(UnityARCamera rValue)
  223. {
  224. return new serializableUnityARCamera(rValue.worldTransform, rValue.projectionMatrix, rValue.trackingState, rValue.trackingReason, rValue.videoParams, rValue.lightData, rValue.displayTransform, rValue.pointCloudData);
  225. }
  226. public static implicit operator UnityARCamera(serializableUnityARCamera rValue)
  227. {
  228. return new UnityARCamera (rValue.worldTransform, rValue.projectionMatrix, rValue.trackingState, rValue.trackingReason, rValue.videoParams, rValue.lightData, rValue.displayTransform, rValue.pointCloud);
  229. }
  230. };
  231. [Serializable]
  232. public class serializableUnityARPlaneAnchor
  233. {
  234. public serializableUnityARMatrix4x4 worldTransform;
  235. public SerializableVector4 center;
  236. public SerializableVector4 extent;
  237. public ARPlaneAnchorAlignment planeAlignment;
  238. public byte[] identifierStr;
  239. public serializableUnityARPlaneAnchor( serializableUnityARMatrix4x4 wt, SerializableVector4 ctr, SerializableVector4 ext, ARPlaneAnchorAlignment apaa,
  240. byte [] idstr)
  241. {
  242. worldTransform = wt;
  243. center = ctr;
  244. extent = ext;
  245. planeAlignment = apaa;
  246. identifierStr = idstr;
  247. }
  248. public static implicit operator serializableUnityARPlaneAnchor(ARPlaneAnchor rValue)
  249. {
  250. serializableUnityARMatrix4x4 wt = rValue.transform;
  251. SerializableVector4 ctr = new SerializableVector4 (rValue.center.x, rValue.center.y, rValue.center.z, 1.0f);
  252. SerializableVector4 ext = new SerializableVector4 (rValue.extent.x, rValue.extent.y, rValue.extent.z, 1.0f);
  253. byte[] idstr = Encoding.UTF8.GetBytes (rValue.identifier);
  254. return new serializableUnityARPlaneAnchor(wt, ctr, ext, rValue.alignment, idstr);
  255. }
  256. public static implicit operator ARPlaneAnchor(serializableUnityARPlaneAnchor rValue)
  257. {
  258. ARPlaneAnchor retValue;
  259. retValue.identifier = Encoding.UTF8.GetString (rValue.identifierStr);
  260. retValue.center = new Vector3 (rValue.center.x, rValue.center.y, rValue.center.z);
  261. retValue.extent = new Vector3 (rValue.extent.x, rValue.extent.y, rValue.extent.z);
  262. retValue.alignment = rValue.planeAlignment;
  263. retValue.transform = rValue.worldTransform;
  264. return retValue;
  265. }
  266. };
  267. [Serializable]
  268. public class serializablePointCloud
  269. {
  270. public byte [] pointCloudData;
  271. public serializablePointCloud(byte [] inputPoints)
  272. {
  273. pointCloudData = inputPoints;
  274. }
  275. public static implicit operator serializablePointCloud(Vector3 [] vecPointCloud)
  276. {
  277. if (vecPointCloud != null)
  278. {
  279. byte [] createBuf = new byte[vecPointCloud.Length * sizeof(float) * 3];
  280. for(int i = 0; i < vecPointCloud.Length; i++)
  281. {
  282. int bufferStart = i * 3;
  283. Buffer.BlockCopy( BitConverter.GetBytes( vecPointCloud[i].x ), 0, createBuf, (bufferStart)*sizeof(float), sizeof(float) );
  284. Buffer.BlockCopy( BitConverter.GetBytes( vecPointCloud[i].y ), 0, createBuf, (bufferStart+1)*sizeof(float), sizeof(float) );
  285. Buffer.BlockCopy( BitConverter.GetBytes( vecPointCloud[i].z ), 0, createBuf, (bufferStart+2)*sizeof(float), sizeof(float) );
  286. }
  287. return new serializablePointCloud (createBuf);
  288. }
  289. else
  290. {
  291. return new serializablePointCloud(null);
  292. }
  293. }
  294. public static implicit operator Vector3 [] (serializablePointCloud spc)
  295. {
  296. if (spc.pointCloudData != null)
  297. {
  298. int numVectors = spc.pointCloudData.Length / (3 * sizeof(float));
  299. Vector3 [] pointCloudVec = new Vector3[numVectors];
  300. for (int i = 0; i < numVectors; i++)
  301. {
  302. int bufferStart = i * 3;
  303. pointCloudVec [i].x = BitConverter.ToSingle (spc.pointCloudData, (bufferStart) * sizeof(float));
  304. pointCloudVec [i].y = BitConverter.ToSingle (spc.pointCloudData, (bufferStart+1) * sizeof(float));
  305. pointCloudVec [i].z = BitConverter.ToSingle (spc.pointCloudData, (bufferStart+2) * sizeof(float));
  306. }
  307. return pointCloudVec;
  308. }
  309. else
  310. {
  311. return null;
  312. }
  313. }
  314. };
  315. [Serializable]
  316. public class serializableARSessionConfiguration
  317. {
  318. public UnityARAlignment alignment;
  319. public UnityARPlaneDetection planeDetection;
  320. public bool getPointCloudData;
  321. public bool enableLightEstimation;
  322. public serializableARSessionConfiguration(UnityARAlignment align, UnityARPlaneDetection planeDet, bool getPtCloud, bool enableLightEst)
  323. {
  324. alignment = align;
  325. planeDetection = planeDet;
  326. getPointCloudData = getPtCloud;
  327. enableLightEstimation = enableLightEst;
  328. }
  329. public static implicit operator serializableARSessionConfiguration(ARKitWorldTrackingSessionConfiguration awtsc)
  330. {
  331. return new serializableARSessionConfiguration (awtsc.alignment, awtsc.planeDetection, awtsc.getPointCloudData, awtsc.enableLightEstimation);
  332. }
  333. public static implicit operator ARKitWorldTrackingSessionConfiguration (serializableARSessionConfiguration sasc)
  334. {
  335. return new ARKitWorldTrackingSessionConfiguration (sasc.alignment, sasc.planeDetection, sasc.getPointCloudData, sasc.enableLightEstimation);
  336. }
  337. };
  338. [Serializable]
  339. public class serializableARKitInit
  340. {
  341. public serializableARSessionConfiguration config;
  342. public UnityARSessionRunOption runOption;
  343. public serializableARKitInit(serializableARSessionConfiguration cfg, UnityARSessionRunOption option)
  344. {
  345. config = cfg;
  346. runOption = option;
  347. }
  348. };
  349. [Serializable]
  350. public class serializableFromEditorMessage
  351. {
  352. public Guid subMessageId;
  353. public serializableARKitInit arkitConfigMsg;
  354. };
  355. }