XRManager.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Scripting;
  5. using Unity.Collections;
  6. using Unity.Jobs;
  7. using System.IO;
  8. using System;
  9. using Ximmerse.XR.Utils;
  10. using Ximmerse.XR.Internal;
  11. using Ximmerse.XR.Collections;
  12. namespace Ximmerse.XR
  13. {
  14. /// <summary>
  15. /// Track event.
  16. /// </summary>
  17. /// <param name="tracked">Array of all tracked markers.</param>
  18. /// <param name="unTracked">Array of all untracked markers.</param>
  19. /// <param name="added">Array of newly tracked markers.</param>
  20. /// <param name="lost">Array of lost tracked markers.</param>
  21. public delegate void TrackEvent(NativeArray<TrackingResult> tracked, NativeArray<TrackingResult> unTracked, NativeArray<TrackingResult> added, NativeArray<TrackingResult> lost);
  22. /// <summary>
  23. /// Ximmerse XR manager.
  24. /// </summary>
  25. [Preserve]
  26. [DefaultExecutionOrder(-20000)]
  27. [AddComponentMenu(".")]//dont add this component manually, it will be added by XR loader.
  28. public sealed class XRManager : MonoBehaviour
  29. {
  30. static XRManager instance;
  31. public static XRManager Instance
  32. {
  33. get => instance;
  34. }
  35. /// <summary>
  36. /// Event is fired per frame, describes the markers' tracking status
  37. /// </summary>
  38. public static event TrackEvent OnTrackUpdate;
  39. /// <summary>
  40. /// Event is fired per frame, after processing the tracking data.
  41. /// </summary>
  42. public static event Action OnPostTrackUpdate;
  43. xNativeList<TrackingResult> trackingResultsPerFrame = xNativeList<TrackingResult>.Create(64);
  44. Camera m_mainCam;
  45. public Camera mainCam
  46. {
  47. get
  48. {
  49. if (!m_mainCam)
  50. {
  51. m_mainCam = Camera.main;
  52. }
  53. return m_mainCam;
  54. }
  55. }
  56. Matrix4x4 mainCameraAwakePose;
  57. private void Awake()
  58. {
  59. instance = this;
  60. }
  61. private void OnDestroy()
  62. {
  63. if (ReferenceEquals(instance, this))
  64. {
  65. instance = null;
  66. }
  67. }
  68. private IEnumerator Start()
  69. {
  70. StartCoroutine(CleanJob());
  71. while (!mainCam)
  72. {
  73. yield return null;
  74. }
  75. mainCameraAwakePose = Matrix4x4.TRS(mainCam.transform.position, mainCam.transform.rotation, Vector3.one);
  76. }
  77. private void Update()
  78. {
  79. if (!SDKVariants.IsSupported || !Application.isPlaying || !mainCam)
  80. {
  81. return;
  82. }
  83. SvrPluginAndroid.Unity_setFrame(Time.frameCount);
  84. //Matrix4x4 trans = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one);
  85. //if (mainCam.transform.parent)
  86. //{
  87. // var m1 = mainCam.transform.root.localToWorldMatrix;
  88. // var m2 = mainCam.transform.GetChild(0).localToWorldMatrix;
  89. // trans = m1 * m2;
  90. //}
  91. XDevicePlugin.UpdateTracking(mainCam.transform.parent ? mainCam.transform.parent.localToWorldMatrix : mainCameraAwakePose, out TrackingSummary summary);
  92. trackingResultsPerFrame.Clear();
  93. trackingResultsPerFrame.AddRange(summary.tracked);
  94. try
  95. {
  96. //Debug.LogFormat("Summary of tracking: {0}, unTracked = {1}, newTracked = {2}, lost = {3}", summary.tracked.Length, summary.unTracked.Length, summary.newTracked.Length, summary.lostTracked.Length);
  97. OnTrackUpdate?.Invoke(summary.tracked, summary.unTracked, summary.newTracked, summary.lostTracked);
  98. }
  99. catch (Exception e)
  100. {
  101. Debug.LogException(e);
  102. }
  103. //Draw gizmos of tracked marker:
  104. if (SDKVariants.DrawTrackedMarkerGizmos)
  105. {
  106. foreach (var tracked in summary.tracked)
  107. {
  108. float size = tracked.size;
  109. //if(markerCfgInfo.markerType == ConfigMarkerType.MarkerGroup_Submarker || markerCfgInfo.markerType == ConfigMarkerType.SingleMarker)
  110. {
  111. RxDraw.DrawWirePlane(tracked.worldPose.position, tracked.worldPose.rotation, size, size, new Color(0.2f, 0.88f, 0.2f, 1));
  112. var textRotation = Quaternion.LookRotation(transform.position - mainCam.transform.position);
  113. textRotation = textRotation.PitchNYaw();
  114. string debugTxt = tracked.id.ToString();
  115. if (SDKVariants.DrawDetailTrackedInfo)
  116. {
  117. Vector3 p = tracked.worldPose.position;
  118. Vector3 e = tracked.worldPose.rotation.eulerAngles.PrettyAngle();
  119. float confidence = tracked.trackedConfidence;
  120. debugTxt =
  121. $"{tracked.id.ToString()}\r\n P={p.ToString("F2")} \r\n Q={e.ToString("F2")} \r\n Distance={tracked.trackedDistance.ToString("F2")}m \r\n Confidence = {confidence.ToString("F3")}";
  122. }
  123. RxDraw.Text3D(tracked.worldPose.position, tracked.worldPose.rotation, 0.012f, debugTxt, new Color(0.96f, 0.9f, 0.93f, 1));
  124. }
  125. RxDraw.DrawTranslateGizmos(tracked.worldPose.position, tracked.worldPose.rotation, size * 0.85f);
  126. }
  127. }
  128. summary.Dispose();
  129. OnPostTrackUpdate?.Invoke();
  130. }
  131. IEnumerator CleanJob()
  132. {
  133. var eof = new WaitForEndOfFrame();
  134. while (true)
  135. {
  136. yield return eof;
  137. trackingResultsPerFrame.Clear();
  138. }
  139. }
  140. public static TrackingResult GetTrackingResult(int id)
  141. {
  142. if (!instance)
  143. {
  144. return default(TrackingResult);
  145. }
  146. var l = instance.trackingResultsPerFrame;
  147. foreach (var t in l)
  148. {
  149. if (t.id == id)
  150. {
  151. return t;
  152. }
  153. }
  154. return default(TrackingResult);
  155. }
  156. }
  157. }