NRHandPointer.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 System.Collections;
  12. using System.Collections.Generic;
  13. using UnityEngine;
  14. /// <summary> A hand pointer. </summary>
  15. public class NRHandPointer : MonoBehaviour
  16. {
  17. /// <summary> The handEnum. </summary>
  18. public HandEnum handEnum;
  19. /// <summary> The raycaster. </summary>
  20. public NRPointerRaycaster raycaster;
  21. /// <summary> The default hand enum. </summary>
  22. private ControllerHandEnum m_controllerHandEnum;
  23. /// <summary> True if is enabled, false if not. </summary>
  24. private bool m_PointerEnabled;
  25. /// <summary> Awakes this object. </summary>
  26. private void Awake()
  27. {
  28. m_controllerHandEnum = handEnum == HandEnum.RightHand ? ControllerHandEnum.Right : ControllerHandEnum.Left;
  29. raycaster.RelatedHand = m_controllerHandEnum;
  30. }
  31. /// <summary> Executes the 'enable' action. </summary>
  32. private void OnEnable()
  33. {
  34. NRInput.OnControllerRecentering += OnRecentering;
  35. NRInput.OnControllerStatesUpdated += OnControllerStatesUpdated;
  36. }
  37. /// <summary> Executes the 'disable' action. </summary>
  38. private void OnDisable()
  39. {
  40. NRInput.OnControllerRecentering -= OnRecentering;
  41. NRInput.OnControllerStatesUpdated -= OnControllerStatesUpdated;
  42. }
  43. /// <summary> Executes the 'controller states updated' action. </summary>
  44. private void OnControllerStatesUpdated()
  45. {
  46. UpdateTracker();
  47. }
  48. /// <summary> Updates the tracker. </summary>
  49. private void UpdateTracker()
  50. {
  51. var handState = NRInput.Hands.GetHandState(handEnum);
  52. m_PointerEnabled = NRInput.RaycastersActive && NRInput.RaycastMode == RaycastModeEnum.Laser && NRInput.Hands.IsRunning && handState.pointerPoseValid;
  53. raycaster.gameObject.SetActive(m_PointerEnabled);
  54. if (m_PointerEnabled)
  55. {
  56. TrackPose();
  57. }
  58. }
  59. /// <summary> Track pose. </summary>
  60. private void TrackPose()
  61. {
  62. transform.position = NRInput.GetPosition(m_controllerHandEnum);
  63. transform.localRotation = NRInput.GetRotation(m_controllerHandEnum);
  64. }
  65. /// <summary> Executes the 'recentering' action. </summary>
  66. private void OnRecentering() { }
  67. }
  68. }