ObjectTrackingResultInfo.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //==========================================================
  2. //
  3. // Copyright (c) Guangzhou Shixiang Technology Co.,Ltd.
  4. // All rights reserved.
  5. //
  6. //==========================================================
  7. using System.Collections.Generic;
  8. using System.Collections.ObjectModel;
  9. using System.Text;
  10. using GxrSdk;
  11. using TMPro;
  12. using UnityEngine;
  13. public class ObjectTrackingResultInfo : MonoBehaviour, IGxrObjectTrackingHandler
  14. {
  15. [SerializeField]
  16. private TextMeshPro textTotal;
  17. [SerializeField]
  18. private TextMeshPro textCount;
  19. [SerializeField]
  20. private TextMeshPro textResult;
  21. private readonly StringBuilder _stringBuilder = new StringBuilder();
  22. // 当前帧中跟踪到的id
  23. private readonly List<uint> _currentTrackedIds = new List<uint>();
  24. // 保存上一帧中跟踪到的id
  25. private readonly List<uint> _lastTrackedIds = new List<uint>();
  26. // 识别到的总次数
  27. // 当前帧中跟踪到的id,如果在上一帧中没有,则总次数加一
  28. private int _totalNum = 0;
  29. void OnEnable()
  30. {
  31. GxrSystemAccessor.TrackingSystem?.RegisterHandler<IGxrObjectTrackingHandler>(this);
  32. }
  33. void OnDisable()
  34. {
  35. GxrSystemAccessor.TrackingSystem?.UnregisterHandler<IGxrObjectTrackingHandler>(this);
  36. }
  37. public void OnObjectTrackingUpdated(GxrObjectTrackingEventData eventData)
  38. {
  39. var trackingResults = eventData.TrackingResults;
  40. UpdateCountText(trackingResults.Count);
  41. _stringBuilder.Clear();
  42. for (int i = 0; i < trackingResults.Count; ++i)
  43. {
  44. var trackingResult = trackingResults[i];
  45. uint id = trackingResult.TrackerId;
  46. var position = trackingResult.Pose.position;
  47. var rotation = trackingResult.Pose.rotation;
  48. var localPosition = trackingResult.LocalPose.position;
  49. var localRotation = trackingResult.LocalPose.rotation;
  50. _stringBuilder.Append($"Id:{id},Position:{position:F3},Rotation:{rotation.eulerAngles:F3}\n" +
  51. $"LocalPosition:{localPosition:F3},LocalRotation:{localRotation.eulerAngles:F3}\n");
  52. }
  53. UpdateResultText(_stringBuilder.ToString());
  54. UpdateTotalNum(trackingResults);
  55. }
  56. private void UpdateCountText(int count)
  57. {
  58. if (textCount != null)
  59. {
  60. textCount.text = $"Count: {count}";
  61. }
  62. }
  63. private void UpdateResultText(string result)
  64. {
  65. if (textResult != null)
  66. {
  67. textResult.text = result;
  68. }
  69. }
  70. /// <summary>
  71. /// 更新识别到的总次数
  72. /// 当前帧中跟踪到的id,如果在上一帧中没有,则总次数加一
  73. /// </summary>
  74. /// <param name="trackingResults"></param>
  75. private void UpdateTotalNum(ReadOnlyCollection<GxrTrackingResult> trackingResults)
  76. {
  77. _currentTrackedIds.Clear();
  78. for (var i = 0; i < trackingResults.Count; ++i)
  79. {
  80. var trackedId = trackingResults[i].TrackerId;
  81. _currentTrackedIds.Add(trackedId);
  82. if (!_lastTrackedIds.Exists(id => id == trackedId))
  83. {
  84. ++_totalNum;
  85. if (textTotal != null)
  86. {
  87. textTotal.text = $"Total: {_totalNum}";
  88. }
  89. }
  90. }
  91. _lastTrackedIds.Clear();
  92. _currentTrackedIds.ForEach(i => _lastTrackedIds.Add(i));
  93. }
  94. public void OnTargetFound()
  95. {
  96. Debug.Log($"[ObjectTrackingResultInfo][OnTargetFound] ");
  97. }
  98. public void OnTargetLost()
  99. {
  100. Debug.Log($"[ObjectTrackingResultInfo][OnTargetLost] ");
  101. }
  102. public void OnTargetUpdated(Pose worldPose)
  103. {
  104. Debug.Log($"[ObjectTrackingResultInfo][OnTargetUpdated] {worldPose.position:F3},{worldPose.rotation:F3}");
  105. }
  106. }