ObjectTrackingProfile.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. using System.IO;
  6. using System;
  7. using Object = UnityEngine.Object;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace Ximmerse.XR
  11. {
  12. /// <summary>
  13. /// Internal object : represents a json file that defines the calibration data path and marker reference.
  14. /// </summary>
  15. [System.Serializable]
  16. internal class TrackedObjectJson
  17. {
  18. [System.Serializable]
  19. public class CardGroupJson
  20. {
  21. public string CalibFile;
  22. public string ModeType;
  23. public int GroupID;
  24. public int[] Markers;
  25. public float[] MarkersSize;
  26. }
  27. [System.Serializable]
  28. public class SingleCardJson
  29. {
  30. public string CalibFile;
  31. public int[] Markers;
  32. public float[] MarkersSize;
  33. }
  34. [SerializeField]
  35. public CardGroupJson CARD_GROUP;
  36. [SerializeField]
  37. public SingleCardJson CARD_SINGLE;
  38. public bool IsCardGroup
  39. {
  40. get
  41. {
  42. return CARD_GROUP != null && !string.IsNullOrEmpty(CARD_GROUP.CalibFile) && CARD_GROUP.GroupID >= 0;
  43. }
  44. }
  45. #region Ext Methods
  46. /// <summary>
  47. /// Fills the trackable json's content to a dictionary map where key = markerID, value = marker's config info.
  48. /// </summary>
  49. /// <param name="configurationMap">Configuration map.</param>
  50. public void FillDictionary(Dictionary<int, MarkerConfigInfo> configurationMap)
  51. {
  52. var jsonObj = this;
  53. var map = configurationMap;
  54. if (jsonObj.CARD_SINGLE.Markers != null && jsonObj.CARD_SINGLE.MarkersSize != null && !string.IsNullOrEmpty(jsonObj.CARD_SINGLE.CalibFile) &&
  55. jsonObj.CARD_SINGLE.Markers.Length == jsonObj.CARD_SINGLE.MarkersSize.Length && jsonObj.CARD_SINGLE.Markers.Length > 0)
  56. {
  57. for (int j = 0, jsonObjCARD_SINGLEMarkersLength = jsonObj.CARD_SINGLE.Markers.Length; j < jsonObjCARD_SINGLEMarkersLength; j++)
  58. {
  59. int markerID = jsonObj.CARD_SINGLE.Markers[j];
  60. float markerSize = jsonObj.CARD_SINGLE.MarkersSize[j];
  61. var markerConfigInfo = new MarkerConfigInfo()
  62. {
  63. MarkerID = markerID,
  64. MarkerConfigSize = markerSize,
  65. markerType = ConfigMarkerType.SingleMarker,
  66. GroupType = string.Empty,
  67. };
  68. if (!map.ContainsKey(markerID))
  69. {
  70. map.Add(markerID, markerConfigInfo);
  71. }
  72. else
  73. {
  74. map[markerID] = markerConfigInfo;
  75. }
  76. }
  77. }
  78. //For card_group:
  79. else if (jsonObj.CARD_GROUP.Markers != null && jsonObj.CARD_GROUP.MarkersSize != null && !string.IsNullOrEmpty(jsonObj.CARD_GROUP.CalibFile) &&
  80. jsonObj.CARD_GROUP.Markers.Length == jsonObj.CARD_GROUP.MarkersSize.Length && jsonObj.CARD_GROUP.Markers.Length > 0)
  81. {
  82. //add marke group:
  83. {
  84. int groupID = jsonObj.CARD_GROUP.GroupID;
  85. float markerSize = jsonObj.CARD_GROUP.MarkersSize[0];
  86. var markerConfigInfo = new MarkerConfigInfo()
  87. {
  88. MarkerID = groupID,
  89. MarkerConfigSize = markerSize,
  90. markerType = ConfigMarkerType.GroupNode,
  91. GroupType = jsonObj.CARD_GROUP.ModeType.ToUpper(),
  92. };
  93. if (!map.ContainsKey(groupID))
  94. {
  95. map.Add(groupID, markerConfigInfo);
  96. }
  97. else
  98. {
  99. map[groupID] = markerConfigInfo;
  100. }
  101. }
  102. //add sub-marker:
  103. {
  104. for (int j = 0, jsonObjCARD_GROUPMarkersLength = jsonObj.CARD_GROUP.Markers.Length; j < jsonObjCARD_GROUPMarkersLength; j++)
  105. {
  106. int markerID = jsonObj.CARD_GROUP.Markers[j];
  107. float markerSize = jsonObj.CARD_GROUP.MarkersSize[j];
  108. var markerConfigInfo = new MarkerConfigInfo()
  109. {
  110. MarkerID = markerID,
  111. MarkerConfigSize = markerSize,
  112. markerType = ConfigMarkerType.MarkerGroup_Submarker,
  113. GroupType = string.Empty,
  114. };
  115. if (!map.ContainsKey(markerID))
  116. {
  117. map.Add(markerID, markerConfigInfo);
  118. }
  119. else
  120. {
  121. map[markerID] = markerConfigInfo;
  122. }
  123. }
  124. }
  125. }
  126. }
  127. #endregion
  128. }
  129. /// <summary>
  130. /// Tracking item.
  131. /// </summary>
  132. [System.Serializable]
  133. public class TrackingItem
  134. {
  135. /// <summary>
  136. /// Editor only
  137. /// </summary>
  138. public UnityEngine.Object JSONConfig = null;
  139. /// <summary>
  140. /// The name of the json file.
  141. /// </summary>
  142. public string jsonName;
  143. /// <summary>
  144. /// The content of the json.
  145. /// </summary>
  146. public string jsonContent;
  147. [SerializeField]
  148. internal int[] m_MarkerIDs;
  149. /// <summary>
  150. /// The marker IDs inside the Json file
  151. /// </summary>
  152. public int[] MarkerIDs
  153. {
  154. get => m_MarkerIDs;
  155. }
  156. /// <summary>
  157. /// The datas
  158. /// </summary>
  159. [SerializeField]
  160. [HideInInspector]
  161. internal byte[] m_Data;
  162. /// <summary>
  163. /// Marker size
  164. /// </summary>
  165. [SerializeField]
  166. internal float[] m_MarkerSizes;
  167. /// <summary>
  168. /// The marker size.
  169. /// </summary>
  170. public float[] MarkerSizes
  171. {
  172. get => m_MarkerSizes;
  173. }
  174. [SerializeField]
  175. internal bool m_TrackedAsGroup;
  176. /// <summary>
  177. /// If true, the markers are taken in a group when being tracked, as reference group id.
  178. /// If false, the markers are tracked seperately as single piece.
  179. /// </summary>
  180. public bool TrackedAsGroup
  181. {
  182. get => m_TrackedAsGroup;
  183. }
  184. [System.NonSerialized]
  185. internal TrackedObjectJson trackableJson;
  186. }
  187. /// <summary>
  188. /// Marker config info : a single marker object's config info from JSON.
  189. /// </summary>
  190. internal struct MarkerConfigInfo
  191. {
  192. /// <summary>
  193. /// The marker identifier.
  194. /// </summary>
  195. public int MarkerID;
  196. /// <summary>
  197. /// The size of the marker config.
  198. /// </summary>
  199. public float MarkerConfigSize;
  200. /// <summary>
  201. /// 是 Marker 组还是 单个Marker ?
  202. /// </summary>
  203. public ConfigMarkerType markerType;
  204. /// <summary>
  205. /// The type of the group : controller | cube | map
  206. /// </summary>
  207. public string GroupType;
  208. }
  209. /// <summary>
  210. /// Config marker type.
  211. /// </summary>
  212. internal enum ConfigMarkerType
  213. {
  214. /// <summary>
  215. /// The marker is config as a single card
  216. /// </summary>
  217. SingleMarker = 0,
  218. /// <summary>
  219. /// The marker is config as a sub-card of the group
  220. /// </summary>
  221. MarkerGroup_Submarker = 1,
  222. /// <summary>
  223. /// This is a top-group.
  224. /// </summary>
  225. GroupNode = 2,
  226. }
  227. [CreateAssetMenu(menuName = "Ximmerse/Tracking Profile", fileName = "TrackingProfile")]
  228. public sealed class ObjectTrackingProfile : ScriptableObject
  229. {
  230. [System.NonSerialized]
  231. bool m_IsLoaded;
  232. /// <summary>
  233. /// Has the tracking profile loaded by tracking system ?
  234. /// </summary>
  235. /// <value><c>true</c> if is loaded; otherwise, <c>false</c>.</value>
  236. public bool IsLoaded
  237. {
  238. get
  239. {
  240. return m_IsLoaded;
  241. }
  242. internal set
  243. {
  244. m_IsLoaded = value;
  245. }
  246. }
  247. [Multiline(3)]
  248. public string Description;
  249. /// <summary>
  250. /// Config the tracking items.
  251. /// </summary>
  252. [SerializeField]
  253. TrackingItem[] items = new TrackingItem[] {
  254. };
  255. /// <summary>
  256. /// Gets the tracking items.
  257. /// </summary>
  258. /// <value>The tracking items.</value>
  259. public TrackingItem[] trackingItems
  260. {
  261. get
  262. {
  263. return items;
  264. }
  265. }
  266. [SerializeField]
  267. bool m_TrackBeacons = true;
  268. /// <summary>
  269. /// Gets a value indicating whether this <see cref="T:Ximmerse.RhinoX.ObjectTrackingProfile"/> track beacon.
  270. /// </summary>
  271. /// <value><c>true</c> if track beacon; otherwise, <c>false</c>.</value>
  272. public bool TrackBeacons
  273. {
  274. get
  275. {
  276. return m_TrackBeacons;
  277. }
  278. }
  279. /// <summary>
  280. /// Customize tracking calibration files.
  281. /// </summary>
  282. [SerializeField, Tooltip("If true, enables additional object tracking. Develoeprs may add the tracking calibration files by drag and drop to the below area.")]
  283. bool m_CustomTrackingCalibrationFiles;
  284. /// <summary>
  285. /// Gets a value indicating whether this <see cref="T:Ximmerse.RhinoX.ObjectTrackingProfile"/> customize tracking
  286. /// calibration files.
  287. /// </summary>
  288. /// <value><c>true</c> if custom tracking calibration files; otherwise, <c>false</c>.</value>
  289. public bool CustomTrackingCalibrationFiles
  290. {
  291. get
  292. {
  293. return m_CustomTrackingCalibrationFiles;
  294. }
  295. }
  296. private void OnEnable()
  297. {
  298. }
  299. private void Awake()
  300. {
  301. //Copies json and dat to internal path :
  302. if (Application.platform == RuntimePlatform.Android)
  303. {
  304. if (s_BuiltTrackingItemsToCopy == null)
  305. {
  306. s_BuiltTrackingItemsToCopy = new List<TrackingItem>();
  307. }
  308. s_BuiltTrackingItemsToCopy.AddRange(this.trackingItems);
  309. if (!copyThreadStarted)
  310. Task.Run(CopyData);
  311. }
  312. }
  313. static List<TrackingItem> s_BuiltTrackingItemsToCopy = null;
  314. private static bool copyThreadStarted = false;
  315. /// <summary>
  316. /// Copy built player's tracking files to internal storage.
  317. /// </summary>
  318. private static void CopyData()
  319. {
  320. Thread.Sleep(10);
  321. while (string.IsNullOrEmpty(SDKVariants.kTrackingDataDir_Internal))
  322. {
  323. Thread.Sleep(10);
  324. }
  325. foreach (var item in s_BuiltTrackingItemsToCopy)
  326. {
  327. string jsonPath = Path.Combine(SDKVariants.kTrackingDataDir_Internal, item.jsonName);
  328. string dataPath = Path.Combine(SDKVariants.kTrackingDataDir_Internal, Path.GetFileNameWithoutExtension(item.jsonName) + ".dat");
  329. if (!File.Exists(jsonPath))
  330. {
  331. File.WriteAllText(jsonPath, item.jsonContent);
  332. Debug.LogFormat("Tracking profile : {0} has been copied to internal path", item.jsonName);
  333. }
  334. if (!File.Exists(dataPath))
  335. {
  336. File.WriteAllBytes(dataPath, item.m_Data);
  337. Debug.LogFormat("Tracking data : {0} has been copied to internal path", item.jsonName);
  338. }
  339. }
  340. s_BuiltTrackingItemsToCopy = null;
  341. }
  342. }
  343. }