GazeAndHandInteractionSystem.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Ximmerse.XR.InputSystems.GazeAndGestureInteraction
  5. {
  6. /// <summary>
  7. /// Gaze and hand interaction system manages how XR user interacts world objects withe eye reticle and hand gesture.
  8. /// </summary>
  9. public partial class GazeAndHandInteractionSystem : MonoBehaviour
  10. {
  11. private EyeReticle _eyeReticle;
  12. public static GazeAndHandInteractionSystem instance
  13. {
  14. get; private set;
  15. }
  16. private void Awake()
  17. {
  18. instance = this;
  19. }
  20. public EyeReticle eyeReticle
  21. {
  22. get => _eyeRay.GetComponent<EyeReticle>();
  23. }
  24. static Camera sMainCamera;
  25. TrackState m_TrackingState = new TrackState();
  26. UIObjectsInteractionState m_UIInteractionState = new UIObjectsInteractionState();
  27. /// <summary>
  28. /// Hand tracking state.
  29. /// </summary>
  30. public I_HandTrackingState TrackingState
  31. {
  32. get => m_TrackingState;
  33. }
  34. public enum CursorStateImage
  35. {
  36. Default,
  37. Custom,
  38. }
  39. public enum EyeRayGameobject
  40. {
  41. Default,
  42. Custom,
  43. }
  44. public CursorStateImage _cursorStateImage = CursorStateImage.Default;
  45. public EyeRayGameobject EyeRayGO = EyeRayGameobject.Default;
  46. public Sprite normal;
  47. public Sprite tracking;
  48. public Sprite select;
  49. public GameObject _eyeRay;
  50. private GestureXRInteractionManager _gestureXRInteractionManager;
  51. private void Start()
  52. {
  53. _gestureXRInteractionManager = new GameObject("Gesture XR InteractionManager").AddComponent<GestureXRInteractionManager>();
  54. _gestureXRInteractionManager.CursorManagergo.Normal = normal;
  55. _gestureXRInteractionManager.CursorManagergo.Tracking = tracking;
  56. _gestureXRInteractionManager.CursorManagergo.None = select;
  57. }
  58. // Update is called once per frame
  59. void Update()
  60. {
  61. if (HandTracking.IsHandTrackingEnable)
  62. {
  63. if (!m_TrackingState.IsEnabled)
  64. {
  65. m_TrackingState.OnEnable();
  66. }
  67. m_TrackingState.Tick();
  68. if (!m_UIInteractionState.IsEnabled)
  69. {
  70. m_UIInteractionState.OnEnable();
  71. }
  72. m_UIInteractionState.Tick();
  73. }
  74. else
  75. {
  76. if (m_TrackingState.IsEnabled)
  77. m_TrackingState.OnDisable();
  78. if (m_UIInteractionState.IsEnabled)
  79. m_UIInteractionState.OnDisable();
  80. }
  81. }
  82. /// <summary>
  83. /// Get eye reticle local pose to main camera
  84. /// </summary>
  85. /// <returns></returns>
  86. public static bool GetEyeReticleLocalPose(out Pose pose)
  87. {
  88. if (!sMainCamera)
  89. {
  90. sMainCamera = Camera.main;
  91. if (!sMainCamera)
  92. {
  93. pose = default(Pose);
  94. return false;
  95. }
  96. }
  97. pose = new Pose(Vector3.zero, Quaternion.identity);
  98. if (sMainCamera.transform.parent)
  99. {
  100. pose.position = sMainCamera.transform.parent.InverseTransformPoint(sMainCamera.transform.position);
  101. pose.rotation = Quaternion.Inverse(sMainCamera.transform.parent.rotation) * (sMainCamera.transform.rotation);
  102. }
  103. else
  104. {
  105. pose.position = sMainCamera.transform.position;
  106. pose.rotation = sMainCamera.transform.rotation;
  107. }
  108. return true;
  109. }
  110. }
  111. }