GSXRDebugHud.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. public class GSXRDebugHud : MonoBehaviour, GSXRManager.SlamEventListener
  6. {
  7. public GSXRManager slamManager;
  8. public GameObject EventDisplay;
  9. public GameObject PositionWarning;
  10. public GameObject FramesPerSecond;
  11. public GameObject Orientation;
  12. public GameObject Position;
  13. public GameObject Eyes;
  14. private Text _eventText;
  15. private Text _warningText;
  16. private Text _fpsText;
  17. private Text _orientationText;
  18. private Text _positionText;
  19. private Text _eyesText;
  20. private float _framesPerSecond = 0;
  21. private void Awake()
  22. {
  23. _eventText = EventDisplay.GetComponent<Text>();
  24. _warningText = PositionWarning.GetComponent<Text>();
  25. _fpsText = FramesPerSecond.GetComponent<Text>();
  26. _orientationText = Orientation.GetComponent<Text>();
  27. _positionText = Position.GetComponent<Text>();
  28. _eyesText = Eyes.GetComponent<Text>();
  29. }
  30. private void Start()
  31. {
  32. slamManager = GSXRManager.Instance;
  33. Debug.Assert(slamManager != null, "GSXRManager object not found");
  34. if (slamManager != null)
  35. {
  36. slamManager.AddEventListener(this); // Register for SlamEvents
  37. StartCoroutine(CalculateFramesPerSecond());
  38. StartCoroutine(DisplayEvents());
  39. if (_eyesText != null) _eyesText.enabled = slamManager.settings.trackEyes /*&& (GSXRPlugin.Instance.GetTrackingMode() & (int)GSXRPlugin.TrackingMode.kTrackingEye) != 0*/;
  40. if (_positionText != null) _positionText.enabled = slamManager.settings.trackPosition /*&& (GSXRPlugin.Instance.GetTrackingMode() & (int)GSXRPlugin.TrackingMode.kTrackingPosition) != 0*/;
  41. }
  42. if (_eventText != null) _eventText.gameObject.SetActive(false);
  43. if (_warningText != null) _warningText.gameObject.SetActive(false);
  44. }
  45. private void LateUpdate ()
  46. {
  47. if (slamManager == null)
  48. return;
  49. var headTransform = Camera.main.transform;
  50. transform.position = headTransform.position;
  51. transform.rotation = headTransform.rotation;
  52. Quaternion orientation = headTransform.localRotation;
  53. if (_orientationText != null && _orientationText.isActiveAndEnabled)
  54. {
  55. //_orientationText.text = string.Format("{0:F3}, {1:F3}, {2:F3}, {3:F3}", orientation.x, orientation.y, orientation.z, orientation.w);
  56. var rotation = orientation.eulerAngles;
  57. _orientationText.text = string.Format("Pitch {0:F2}, Yaw {1:F2}, Roll {2:F2}",
  58. rotation.x > 180 ? rotation.x - 360 : rotation.x,
  59. rotation.y > 180 ? rotation.y - 360 : rotation.y,
  60. rotation.z > 180 ? rotation.z - 360 : rotation.z);
  61. _orientationText.color = (slamManager.status.pose & (int)GSXRPlugin.TrackingMode.kTrackingOrientation) == 0 ? Color.red : Color.green;
  62. }
  63. Vector3 position = headTransform.localPosition;
  64. if (_positionText != null && _positionText.isActiveAndEnabled)
  65. {
  66. _positionText.text = string.Format("X {0:F2}, Y {1:F2}, Z {2:F2}", position.x, position.y, position.z);
  67. _positionText.color = (slamManager.status.pose & (int)GSXRPlugin.TrackingMode.kTrackingPosition) == 0 ? Color.red : Color.green;
  68. }
  69. if (_eyesText != null && _eyesText.isActiveAndEnabled)
  70. {
  71. float eyeLeftOpenness = GSXRManager.Instance.eyePose.leftOpenness;
  72. float eyeRightOpenness = GSXRManager.Instance.eyePose.rightOpenness;
  73. _eyesText.text = string.Format("{0:F2} Eye Openness {1:F2}", eyeLeftOpenness, eyeRightOpenness);
  74. _eyesText.color = (slamManager.status.pose & (int)GSXRPlugin.TrackingMode.kTrackingEye) == 0 ? Color.red : Color.green;
  75. }
  76. if (_fpsText != null && _fpsText.isActiveAndEnabled)
  77. {
  78. int fps = Mathf.RoundToInt(_framesPerSecond);
  79. int refreshRate = Mathf.RoundToInt(GSXRPlugin.Instance.deviceInfo.displayRefreshRateHz);
  80. _fpsText.text = string.Format("{0} / {1} FPS", fps, refreshRate);
  81. _fpsText.color = fps < refreshRate ? Color.yellow : Color.green;
  82. }
  83. if (_warningText != null && slamManager.settings.trackPosition && slamManager.settings.trackPosition && (GSXRPlugin.Instance.GetTrackingMode() & (int)GSXRPlugin.TrackingMode.kTrackingPosition) != 0)
  84. {
  85. var isValid = (slamManager.status.pose & (int)GSXRPlugin.TrackingMode.kTrackingPosition) != 0;
  86. _warningText.gameObject.SetActive(!isValid);
  87. }
  88. }
  89. private IEnumerator CalculateFramesPerSecond()
  90. {
  91. int lastFrameCount = 0;
  92. while (true)
  93. {
  94. yield return new WaitForSecondsRealtime(1.0f);
  95. var currentFrameCount = slamManager.FrameCount;
  96. var elapsedFrames = currentFrameCount - lastFrameCount;
  97. _framesPerSecond = elapsedFrames / 1.0f;
  98. lastFrameCount = currentFrameCount;
  99. }
  100. }
  101. private List<GSXRManager.slamEventType> events = new List<GSXRManager.slamEventType>();
  102. private IEnumerator DisplayEvents()
  103. {
  104. while (true)
  105. {
  106. yield return new WaitForSecondsRealtime(2.0f);
  107. if (!slamManager.IsOverlayFading())
  108. {
  109. if (events.Count > 0)
  110. {
  111. var eventType = events[0];
  112. _eventText.gameObject.SetActive(true);
  113. _eventText.text = eventType.ToString();
  114. events.RemoveAt(0);
  115. }
  116. else if (_eventText.gameObject.activeSelf)
  117. {
  118. _eventText.gameObject.SetActive(false);
  119. }
  120. }
  121. }
  122. }
  123. public void OnSlamEvent(GSXRManager.SlamEvent ev)
  124. {
  125. switch (ev.eventType)
  126. {
  127. case GSXRManager.slamEventType.kEventNone:
  128. break;
  129. default:
  130. if (!events.Contains(ev.eventType)) events.Add(ev.eventType);
  131. break;
  132. }
  133. }
  134. private void OnDestroy()
  135. {
  136. StopAllCoroutines();
  137. }
  138. }