TrackState.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. namespace Ximmerse.XR.InputSystems.GazeAndGestureInteraction
  6. {
  7. /// <summary>
  8. /// Interface : hand tracking state.
  9. /// </summary>
  10. public interface I_HandTrackingState
  11. {
  12. bool IsTracking
  13. {
  14. get;
  15. }
  16. GazeAndHandInteractionSystem.GestureType gestureType
  17. {
  18. get;
  19. }
  20. /// <summary>
  21. /// Delta position movement
  22. /// </summary>
  23. Vector3 DeltaPositionWorld
  24. {
  25. get;
  26. }
  27. /// <summary>
  28. /// Delta angular rotation
  29. /// </summary>
  30. Vector3 DeltaAngluarWorld
  31. {
  32. get;
  33. }
  34. }
  35. /// <summary>
  36. /// 更新 tracking state 的状态控制器
  37. /// </summary>
  38. public partial class GazeAndHandInteractionSystem
  39. {
  40. /// <summary>
  41. /// Impl of hand track state.
  42. /// </summary>
  43. internal partial class TrackState : I_HandTrackingState
  44. {
  45. public bool IsTracking
  46. {
  47. get => trackingState == HandTrackingStatus.Tracking;
  48. }
  49. /// <summary>
  50. /// 跟踪判断变更条件.
  51. /// </summary>
  52. public struct HandTrackStateChangeCondition
  53. {
  54. public int frameCount;
  55. public float timer;
  56. }
  57. /// <summary>
  58. /// Condition : valid to invalid
  59. /// </summary>
  60. public HandTrackStateChangeCondition valid2invalid = new HandTrackStateChangeCondition()
  61. {
  62. timer = 0.1f,
  63. frameCount = 3,
  64. };
  65. /// <summary>
  66. /// Condition : invalid to valid
  67. /// </summary>
  68. public HandTrackStateChangeCondition invalid2valid = new HandTrackStateChangeCondition()
  69. {
  70. timer = 0.1f,
  71. frameCount = 3,
  72. };
  73. public enum HandTrackingStatus : byte
  74. {
  75. /// <summary>
  76. /// 无有效跟踪
  77. /// </summary>
  78. Invalid = 0,
  79. /// <summary>
  80. /// 有效跟踪状态
  81. /// </summary>
  82. Tracking = 1,
  83. }
  84. /// <summary>
  85. /// 跟踪状态
  86. /// </summary>
  87. public HandTrackingStatus trackingState
  88. {
  89. get; private set;
  90. }
  91. public event Action<HandTrackingStatus> OnTrackingStatusChanged;
  92. internal TrackState()
  93. {
  94. trackingState = HandTrackingStatus.Invalid;
  95. }
  96. /// <summary>
  97. /// 合理/非合理帧计数器
  98. /// </summary>
  99. int validTrackingCounter = 0, invalidTrackingCounter = 0;
  100. /// <summary>
  101. /// 合理/非合理帧计时器
  102. /// </summary>
  103. float validTrackingDeltaTimer = 0, invalidTrackingDeltaTimer = 0;
  104. float previousStateChangeTime;
  105. const float kMinAllowStateChangeTime = 0.1f;
  106. public bool IsEnabled
  107. {
  108. get; private set;
  109. }
  110. public void OnEnable()
  111. {
  112. IsEnabled = true;
  113. }
  114. public void Tick()
  115. {
  116. TickGestureState();
  117. }
  118. void InternalStateChange(HandTrackingStatus newState)
  119. {
  120. if (newState != this.trackingState)
  121. {
  122. this.trackingState = newState;
  123. previousStateChangeTime = Time.realtimeSinceStartup;
  124. OnTrackingStatusChanged?.Invoke(newState);
  125. }
  126. }
  127. public void OnDisable()
  128. {
  129. IsEnabled = false;
  130. }
  131. }
  132. }
  133. }