GazeTracker.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using UnityEngine;
  12. /// <summary> A gaze tracker. </summary>
  13. public class GazeTracker : MonoBehaviour
  14. {
  15. /// <summary> The raycaster. </summary>
  16. [SerializeField]
  17. private NRPointerRaycaster m_Raycaster;
  18. /// <summary> True if is enabled, false if not. </summary>
  19. private bool m_IsEnabled;
  20. /// <summary> Gets the camera center. </summary>
  21. /// <value> The camera center. </value>
  22. private Transform CameraCenter
  23. {
  24. get
  25. {
  26. return NRInput.CameraCenter;
  27. }
  28. }
  29. /// <summary> Starts this object. </summary>
  30. private void Start()
  31. {
  32. OnControllerStatesUpdated();
  33. }
  34. /// <summary> Executes the 'enable' action. </summary>
  35. private void OnEnable()
  36. {
  37. NRInput.OnControllerStatesUpdated += OnControllerStatesUpdated;
  38. }
  39. /// <summary> Executes the 'disable' action. </summary>
  40. private void OnDisable()
  41. {
  42. NRInput.OnControllerStatesUpdated -= OnControllerStatesUpdated;
  43. }
  44. /// <summary> Executes the 'controller states updated' action. </summary>
  45. private void OnControllerStatesUpdated()
  46. {
  47. UpdateTracker();
  48. }
  49. /// <summary> Updates the tracker. </summary>
  50. private void UpdateTracker()
  51. {
  52. if (CameraCenter == null)
  53. return;
  54. m_IsEnabled = (NRInput.RaycastersActive && NRInput.RaycastMode == RaycastModeEnum.Gaze);
  55. m_Raycaster.gameObject.SetActive(m_IsEnabled);
  56. if (m_IsEnabled)
  57. {
  58. transform.position = CameraCenter.position;
  59. transform.rotation = CameraCenter.rotation;
  60. }
  61. }
  62. }
  63. }