GroundSpace.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. using Unity.XR.CoreUtils;
  2. using UnityEngine;
  3. using Ximmerse.XR.Utils;
  4. using Ximmerse.XR.Internal;
  5. using System.Collections;
  6. namespace Ximmerse.XR.Tag
  7. {
  8. /// <summary>
  9. /// Implement large spatial positioning capabilities and handle appropriate events.
  10. /// </summary>
  11. public class GroundSpace : MonoBehaviour
  12. {
  13. #region Property
  14. private TagGoManager _tagGoManager;
  15. private XROrigin xr;
  16. private GameObject tagground_clone;
  17. private float px, py, pz, rx, ry, rz, rw;
  18. private long predTimestampNano = 0;
  19. private bool first = false;
  20. private bool exit = false;
  21. private bool onfirstTrackingEnter = false;
  22. private bool onTrackingEnter = false;
  23. private bool onTrackingStay = false;
  24. private bool onTrackingExit = false;
  25. private bool isTrakingState;
  26. private bool isvalid;
  27. private GameObject debugaxis;
  28. protected TrackingEvent trackingEvent;
  29. [Header("--- Marker Setting ---")]
  30. [SerializeField]
  31. protected int trackID = 65;
  32. [SerializeField]
  33. protected float m_Confidence = 0.9f;
  34. [Header("--- Vio Drift Threshold ---")]
  35. [SerializeField]
  36. protected float m_distance = 1.0f;
  37. [SerializeField]
  38. protected float m_angle = 1.0f;
  39. [Header("--- Tracking distance ---")]
  40. [SerializeField]
  41. protected float m_minDistance = 0.1f;
  42. [SerializeField]
  43. protected float m_maxDistance = 1.8f;
  44. [Header("--- Debug Setting ---")]
  45. [SerializeField]
  46. protected bool m_debugView;
  47. [SerializeField]
  48. protected float m_size = 0.17f;
  49. //private XDevicePlugin.XAttrTrackingInfo trackingInfo;
  50. #endregion
  51. #region Unity
  52. protected virtual void Awake()
  53. {
  54. }
  55. protected virtual void OnDestroy()
  56. {
  57. }
  58. private void Start()
  59. {
  60. CloneGamGround();
  61. trackingEvent = GetComponent<TrackingEvent>();
  62. }
  63. private void Update()
  64. {
  65. if (TagProfileLoading.Instance != null)
  66. {
  67. if (trackingEvent != null || m_debugView)
  68. {
  69. isTrakingState = IsTracking();
  70. if (trackingEvent != null)
  71. {
  72. if (OnFirstTrackingEnter())
  73. {
  74. trackingEvent.onFirstTrackingEvent?.Invoke();
  75. }
  76. if (OnTrackingEnter())
  77. {
  78. trackingEvent.onTrackingEnter?.Invoke();
  79. }
  80. if (OnTrackingStay())
  81. {
  82. trackingEvent.onTrackingStay?.Invoke();
  83. }
  84. if (OnTrackingExit())
  85. {
  86. trackingEvent.onTrackingExit?.Invoke();
  87. }
  88. }
  89. if (m_debugView && IsValid())
  90. {
  91. DrawDebugView(m_size);
  92. }
  93. }
  94. }
  95. }
  96. #endregion
  97. #region Method
  98. protected Vector3 FixXRPosition()
  99. {
  100. CloneGamGround();
  101. if (xr != null)
  102. {
  103. px = gameObject.transform.position.x * Mathf.Cos(xr.transform.eulerAngles.y * Mathf.Deg2Rad) - gameObject.transform.position.z * Mathf.Sin(xr.transform.eulerAngles.y * Mathf.Deg2Rad) - (xr.transform.position.x * Mathf.Cos(xr.transform.eulerAngles.y * Mathf.Deg2Rad) - xr.transform.position.z * Mathf.Sin(xr.transform.eulerAngles.y * Mathf.Deg2Rad));
  104. py = tagground_clone.transform.position.y;
  105. pz = gameObject.transform.position.x * Mathf.Sin(xr.transform.eulerAngles.y * Mathf.Deg2Rad) + gameObject.transform.position.z * Mathf.Cos(xr.transform.eulerAngles.y * Mathf.Deg2Rad) - (xr.transform.position.x * Mathf.Sin(xr.transform.eulerAngles.y * Mathf.Deg2Rad) + xr.transform.position.z * Mathf.Cos(xr.transform.eulerAngles.y * Mathf.Deg2Rad));
  106. }
  107. else
  108. {
  109. px = gameObject.transform.position.x;
  110. py = gameObject.transform.position.y;
  111. pz = gameObject.transform.position.z;
  112. }
  113. Vector3 fixpos = new Vector3(px, py, pz);
  114. return fixpos;
  115. }
  116. protected Quaternion FixXRRotation()
  117. {
  118. CloneGamGround();
  119. if (xr != null)
  120. {
  121. rx = tagground_clone.transform.rotation.x;
  122. ry = tagground_clone.transform.rotation.y;
  123. rz = tagground_clone.transform.rotation.z;
  124. rw = tagground_clone.transform.rotation.w;
  125. }
  126. else
  127. {
  128. rx = gameObject.transform.rotation.x;
  129. ry = gameObject.transform.rotation.y;
  130. rz = gameObject.transform.rotation.z;
  131. rw = gameObject.transform.rotation.w;
  132. }
  133. Quaternion fixrot = new Quaternion(rx, ry, rz, rw);
  134. return fixrot;
  135. }
  136. /// <summary>
  137. /// Correct the coordinate information.
  138. /// </summary>
  139. private void CloneGamGround()
  140. {
  141. if (xr == null)
  142. {
  143. xr = FindObjectOfType<XROrigin>();
  144. }
  145. _tagGoManager = TagGoManager.Instance;
  146. if (_tagGoManager == null)
  147. {
  148. GameObject tGO = new GameObject("_TagGoManager");
  149. tGO.AddComponent<TagGoManager>();
  150. _tagGoManager = tGO.GetComponent<TagGoManager>();
  151. }
  152. if (tagground_clone == null)
  153. {
  154. tagground_clone = new GameObject();
  155. tagground_clone.transform.parent = _tagGoManager.transform;
  156. }
  157. tagground_clone.transform.localPosition = gameObject.transform.position;
  158. tagground_clone.transform.localRotation = gameObject.transform.rotation;
  159. tagground_clone.name = gameObject.name + "_tagground_clone(clone)";
  160. }
  161. /// <summary>
  162. /// Plot the axes
  163. /// </summary>
  164. /// <param name="size"></param>
  165. protected void DrawDebugView(float size)
  166. {
  167. RxDraw.DrawWirePlane(transform.position, transform.rotation, size, size, Color.green);
  168. if (xr != null)
  169. {
  170. Quaternion textRotation = Quaternion.LookRotation(transform.position - xr.Camera.transform.position);
  171. textRotation = textRotation.PitchNYaw();
  172. string debugTxt = trackID.ToString();
  173. RxDraw.Text3D(transform.position, textRotation, 0.02f, debugTxt, Color.green);
  174. }
  175. else
  176. {
  177. Quaternion textRotation = Quaternion.LookRotation(transform.position - FindObjectOfType<Camera>().transform.position);
  178. textRotation = textRotation.PitchNYaw();
  179. string debugTxt = trackID.ToString();
  180. RxDraw.Text3D(transform.position, textRotation, 0.02f, debugTxt, Color.green);
  181. }
  182. RxDraw.DrawTranslateGizmos(transform.position, transform.rotation, size * 0.85f);
  183. }
  184. /// <summary>
  185. /// Get the Tag tracking status
  186. /// </summary>
  187. /// <returns></returns>
  188. protected bool IsTracking()
  189. {
  190. if (TagProfileLoading.Instance.TrackingTagList.Contains(trackID))
  191. {
  192. return true;
  193. }
  194. else return false;
  195. }
  196. /// <summary>
  197. /// First identification
  198. /// </summary>
  199. /// <returns></returns>
  200. protected bool OnFirstTrackingEnter()
  201. {
  202. if (isTrakingState && first == false)
  203. {
  204. first = true;
  205. onfirstTrackingEnter = true;
  206. }
  207. else if (isTrakingState && first == true)
  208. {
  209. onfirstTrackingEnter = false;
  210. }
  211. return onfirstTrackingEnter;
  212. }
  213. /// <summary>
  214. /// Identify
  215. /// </summary>
  216. /// <returns></returns>
  217. protected bool OnTrackingEnter()
  218. {
  219. if (isTrakingState && onTrackingEnter == false && onTrackingStay == false)
  220. {
  221. onTrackingEnter = true;
  222. onTrackingStay = true;
  223. }
  224. else
  225. {
  226. onTrackingEnter = false;
  227. }
  228. return onTrackingEnter;
  229. }
  230. /// <summary>
  231. /// Identifying
  232. /// </summary>
  233. /// <returns></returns>
  234. protected bool OnTrackingStay()
  235. {
  236. if (isTrakingState)
  237. {
  238. onTrackingStay = true;
  239. exit = true;
  240. }
  241. else
  242. {
  243. onTrackingStay = false;
  244. }
  245. return onTrackingStay;
  246. }
  247. /// <summary>
  248. /// lose
  249. /// </summary>
  250. /// <returns></returns>
  251. protected bool OnTrackingExit()
  252. {
  253. if (isTrakingState == false && exit)
  254. {
  255. exit = false;
  256. onTrackingExit = true;
  257. }
  258. else
  259. {
  260. onTrackingExit = false;
  261. }
  262. return onTrackingExit;
  263. }
  264. /// <summary>
  265. /// Whether the tag is valid
  266. /// </summary>
  267. /// <returns></returns>
  268. protected bool IsValid()
  269. {
  270. if (trackID ==TagProfileLoading.Instance.TagFusion)
  271. {
  272. isvalid = true;
  273. }
  274. else
  275. {
  276. isvalid = false;
  277. }
  278. return isvalid;
  279. }
  280. /// <summary>
  281. /// Displays the axis model
  282. /// </summary>
  283. protected void DisplaysAxisModel()
  284. {
  285. if (m_debugView && IsValid())
  286. {
  287. if (debugaxis == null)
  288. {
  289. debugaxis = GameObject.Instantiate(Resources.Load("Tag/Prefabs/Debug Axis")) as GameObject;
  290. debugaxis.transform.parent = gameObject.transform;
  291. debugaxis.transform.localPosition = new Vector3(0, 0, 0);
  292. debugaxis.transform.localEulerAngles = new Vector3(0, 0, 0);
  293. debugaxis.transform.localScale = new Vector3(m_size, m_size, m_size);
  294. }
  295. debugaxis.SetActive(true);
  296. }
  297. else
  298. {
  299. if (debugaxis != null)
  300. {
  301. debugaxis.SetActive(false);
  302. }
  303. }
  304. }
  305. #endregion
  306. }
  307. }