ViveTrackerDebugDisplay.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System.Globalization;
  2. using TMPro;
  3. using Tracking;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using Valve.VR;
  7. using Valve.VR.InteractionSystem;
  8. namespace Display
  9. {
  10. public class ViveTrackerDebugDisplay : MonoBehaviour
  11. {
  12. public TextMeshProUGUI posText;
  13. public TextMeshProUGUI rotText;
  14. public Image fwImage;
  15. public Image legImage;
  16. public FrontWheelTracker fwTracker;
  17. public KineticLegTracker legTracker;
  18. private SteamVR_TrackedObject fwTrackedObject;
  19. private SteamVR_TrackedObject legTrackedObject;
  20. private bool isFwTrackerNull;
  21. private bool islegTrackerNull;
  22. private bool isFwTrackedObjectNull;
  23. private bool isLegTrackedObjectNull;
  24. // Start is called before the first frame update
  25. private void Start()
  26. {
  27. isFwTrackerNull = fwTracker == null;
  28. islegTrackerNull = legTracker == null;
  29. fwTrackedObject = fwTracker.GetComponent<SteamVR_TrackedObject>();
  30. legTrackedObject = legTracker.GetComponent<SteamVR_TrackedObject>();
  31. isFwTrackedObjectNull = fwTrackedObject == null;
  32. isLegTrackedObjectNull = legTrackedObject == null;
  33. }
  34. // Update is called once per frame
  35. private void Update()
  36. {
  37. if (!isFwTrackerNull & fwTracker.isActiveAndEnabled & !isFwTrackerNull && fwTrackedObject.isValid)
  38. {
  39. posText.text = $"Pos: {fwTracker.RelativePosition.ToString()}";
  40. rotText.text = $"Rot: {fwTracker.SteerRotation:n2}";
  41. fwImage.color = Color.green;
  42. }
  43. else
  44. {
  45. posText.text = $"Pos: -";
  46. rotText.text = $"Rot: -";
  47. fwImage.color = Color.grey.ColorWithAlpha(100);
  48. }
  49. if (!islegTrackerNull & legTracker.isActiveAndEnabled && !isLegTrackedObjectNull &&
  50. legTrackedObject.isValid)
  51. {
  52. legImage.color = Color.green;
  53. }
  54. else
  55. {
  56. legImage.color = Color.grey.ColorWithAlpha(100);
  57. }
  58. }
  59. }
  60. }