TagLoadingManager.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using Unity.XR.CoreUtils;
  7. using UnityEngine;
  8. using Ximmerse.XR.Internal;
  9. using Ximmerse.XR.Utils;
  10. using static Ximmerse.XR.PluginVioFusion;
  11. namespace Ximmerse.XR.Tag
  12. {
  13. public class TagLoadingManager : MonoBehaviour
  14. {
  15. #region Property
  16. private bool Fixstart = false;
  17. private Vector3 XRpos;
  18. private XROrigin xROrigin;
  19. private float xrx, xry, xrz;
  20. protected Thread threadLoad;
  21. private List<GameObject> _groundplanelist = new List<GameObject>();
  22. private List<TagGroundPlane> _debugTagGround = new List<TagGroundPlane>();
  23. private List<int> _trackingtaglist = new List<int>();
  24. int beacon_id;
  25. long beacon_timestamp;
  26. float beacon_pos0;
  27. float beacon_pos1;
  28. float beacon_pos2;
  29. float beacon_rot0;
  30. float beacon_rot1;
  31. float beacon_rot2;
  32. float beacon_rot3;
  33. float beacon_tracking_confidence;
  34. float beacon_min_distance;
  35. float beacon_correct_weight;
  36. private int _tagfusion;
  37. private long predTimestampNano = 0;
  38. int index = 0;
  39. long timestamp = 0;
  40. int state = 0;
  41. float posX = 0;
  42. float posY = 0;
  43. float posZ = 0;
  44. float rotX = 0;
  45. float rotY = 0;
  46. float rotZ = 0;
  47. float rotW = 0;
  48. float confidence = 0;
  49. float marker_distance = 0;
  50. bool trakingstate;
  51. //private XDevicePlugin.XAttrTrackingInfo trackingInfo;
  52. public List<TagGroundPlane> DebugTagGroundList
  53. {
  54. get => _debugTagGround;
  55. set => _debugTagGround = value;
  56. }
  57. public List<GameObject> GroundPlaneList
  58. {
  59. get => _groundplanelist;
  60. set => _groundplanelist = value;
  61. }
  62. public List<int> TrackingTagList
  63. {
  64. get => _trackingtaglist;
  65. }
  66. public int TagFusion
  67. {
  68. get => _tagfusion;
  69. }
  70. public Thread ThreadLoad
  71. {
  72. get => threadLoad;
  73. }
  74. #endregion
  75. #region Unity
  76. private void Update()
  77. {
  78. GetTrackingStateAndData();
  79. }
  80. #endregion
  81. #region Method
  82. /// <summary>
  83. /// Get the coordinates of the Tag ground plane in the scene and enable large space positioning
  84. /// </summary>
  85. private void GroundPlaneStart()
  86. {
  87. #if !UNITY_EDITOR
  88. PluginVioFusion.plugin_vio_fusion_reset(0);
  89. //TagGroundPlane[] enemies = UnityEngine.Object.FindObjectsOfType<TagGroundPlane>();
  90. if (TagGroundPlane.tagGroundPlaneList != null)
  91. {
  92. foreach (var item in TagGroundPlane.tagGroundPlaneList)
  93. {
  94. PluginVioFusion.XAttrBeaconInWorldInfo beacon_in_world_info = item.beacon_info;
  95. Debug.Log("id£º" + beacon_in_world_info.beacon_id);
  96. PluginVioFusion.plugin_vio_fusion_set_param(ref beacon_in_world_info);
  97. }
  98. PluginVioFusion.plugin_vio_fusion_run(0);
  99. }
  100. #endif
  101. }
  102. /// <summary>
  103. /// Get the coordinates of the Tag ground plane in the text and enable large space positioning
  104. /// </summary>
  105. /// <param name="go"></param>
  106. private void GroundPlaneStart(List<GameObject> go)
  107. {
  108. #if !UNITY_EDITOR
  109. PluginVioFusion.plugin_vio_fusion_reset(0);
  110. if (go != null)
  111. {
  112. foreach (var item in go)
  113. {
  114. PluginVioFusion.XAttrBeaconInWorldInfo beacon_in_world_info = item.GetComponent<TagGroundPlane>().beacon_info;
  115. Debug.Log(beacon_in_world_info.beacon_id);
  116. PluginVioFusion.plugin_vio_fusion_set_param(ref beacon_in_world_info);
  117. }
  118. PluginVioFusion.plugin_vio_fusion_run(0);
  119. }
  120. #endif
  121. }
  122. /// <summary>
  123. /// Start enabling the Large Space component
  124. /// </summary>
  125. protected IEnumerator StartFusion()
  126. {
  127. while (threadLoad ==null || threadLoad.ThreadState==ThreadState.Running)
  128. {
  129. yield return null;
  130. }
  131. xROrigin = GameObject.FindObjectOfType<XROrigin>();
  132. if (xROrigin != null)
  133. {
  134. xrx = xROrigin.transform.position.x;
  135. xry = xROrigin.transform.position.y;
  136. xrz = xROrigin.transform.position.z;
  137. XRpos = new Vector3(xrx, xry, xrz);
  138. StartCoroutine(FixTransform());
  139. Fixstart = true;
  140. }
  141. if (CreatesGroundPlaneByJson.Instance == null)
  142. {
  143. SettingTagData();
  144. }
  145. else
  146. {
  147. if (!CreatesGroundPlaneByJson.Instance.autoCreates)
  148. {
  149. SettingTagData();
  150. }
  151. }
  152. }
  153. /// <summary>
  154. /// Correct large spatial coordinates
  155. /// </summary>
  156. /// <returns></returns>
  157. IEnumerator FixTransform()
  158. {
  159. while (true)
  160. {
  161. if (XRpos != xROrigin.transform.position)
  162. {
  163. SettingTagData();
  164. xrx = xROrigin.transform.position.x;
  165. xry = xROrigin.transform.position.y;
  166. xrz = xROrigin.transform.position.z;
  167. XRpos = new Vector3(xrx, xry, xrz);
  168. }
  169. yield return new WaitForSeconds(1);
  170. }
  171. }
  172. /// <summary>
  173. /// Refresh and re-acquire the coordinate information of the large spatial positioning board.
  174. /// </summary>
  175. protected void RefreshBeacon()
  176. {
  177. if (Fixstart)
  178. {
  179. StopCoroutine(FixTransform());
  180. }
  181. SettingTagData();
  182. xrx = xROrigin.transform.position.x;
  183. xry = xROrigin.transform.position.y;
  184. xrz = xROrigin.transform.position.z;
  185. XRpos = new Vector3(xrx, xry, xrz);
  186. StartCoroutine(FixTransform());
  187. Fixstart = true;
  188. }
  189. /// <summary>
  190. /// Clearing the algorithm data invalidates the large spatial positioning function.
  191. /// </summary>
  192. protected void CleanBeacon()
  193. {
  194. #if !UNITY_EDITOR
  195. PluginVioFusion.plugin_vio_fusion_reset(0);
  196. #endif
  197. }
  198. /// <summary>
  199. /// Setting Tag Data
  200. /// </summary>
  201. protected void SettingTagData()
  202. {
  203. if (CreatesGroundPlaneByJson.Instance != null)
  204. {
  205. if (CreatesGroundPlaneByJson.Instance.autoCreates)
  206. {
  207. GroundPlaneStart(GroundPlaneList);
  208. }
  209. else
  210. {
  211. GroundPlaneStart();
  212. }
  213. }
  214. else
  215. {
  216. GroundPlaneStart();
  217. }
  218. }
  219. /// <summary>
  220. /// Get the tracking status and the ID of the fusion
  221. /// </summary>
  222. protected void GetTrackingStateAndData()
  223. {
  224. #if !UNITY_EDITOR
  225. int ret = NativePluginApi.Unity_TagPredict(0);
  226. _trackingtaglist.Clear();
  227. if (ret >= 0)
  228. {
  229. for (int i = 0; i <= 227; i++)
  230. {
  231. bool ret2 = NativePluginApi.Unity_getTagTracking2(i,
  232. ref index, ref timestamp, ref state,
  233. ref posX, ref posY, ref posZ,
  234. ref rotX, ref rotY, ref rotZ, ref rotW,
  235. ref confidence, ref marker_distance);
  236. if (ret2 && state > 0)
  237. {
  238. _trackingtaglist.Add(i);
  239. }
  240. }
  241. }
  242. #endif
  243. _tagfusion = GetTagFusionState();
  244. }
  245. /// <summary>
  246. /// Gets a valid Tag ID
  247. /// </summary>
  248. /// <returns></returns>
  249. protected int GetTagFusionState()
  250. {
  251. #if !UNITY_EDITOR
  252. NativePluginApi.Unity_getFusionResult(predTimestampNano, ref beacon_id,
  253. ref beacon_timestamp,
  254. ref beacon_pos0,
  255. ref beacon_pos1,
  256. ref beacon_pos2,
  257. ref beacon_rot0,
  258. ref beacon_rot1,
  259. ref beacon_rot2,
  260. ref beacon_rot3,
  261. ref beacon_tracking_confidence,
  262. ref beacon_min_distance,
  263. ref beacon_correct_weight);
  264. #endif
  265. return beacon_id;
  266. }
  267. #endregion
  268. }
  269. }