NRHandControllerProvider.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. using UnityEngine;
  10. namespace NRKernal
  11. {
  12. public class NRHandControllerProvider : ControllerProviderBase
  13. {
  14. public class NRHandControllerStateParser
  15. {
  16. /// <summary> The buttons down. </summary>
  17. private bool[] m_ButtonsDown = new bool[3];
  18. /// <summary> The buttons up. </summary>
  19. private bool[] m_ButtonsUp = new bool[3];
  20. /// <summary> The buttons. </summary>
  21. private bool[] m_Buttons = new bool[3];
  22. /// <summary> The down. </summary>
  23. private bool[] m_Down = new bool[3];
  24. /// <summary> Parser controller state. </summary>
  25. /// <param name="state"> The state.</param>
  26. public void ParserControllerState(ControllerState state, HandState handState)
  27. {
  28. lock (m_Buttons)
  29. {
  30. lock (m_Down)
  31. {
  32. for (int i = 0; i < m_Buttons.Length; ++i)
  33. {
  34. m_Down[i] = m_Buttons[i];
  35. }
  36. }
  37. m_Buttons[0] = handState.pointerPoseValid && handState.isPinching; //Trigger
  38. m_Buttons[1] = false; //App
  39. m_Buttons[2] = false; //Home
  40. lock (m_ButtonsUp)
  41. {
  42. lock (m_ButtonsDown)
  43. {
  44. for (int i = 0; i < m_Buttons.Length; ++i)
  45. {
  46. m_ButtonsUp[i] = (m_Down[i] & !m_Buttons[i]);
  47. m_ButtonsDown[i] = (!m_Down[i] & m_Buttons[i]);
  48. }
  49. }
  50. }
  51. }
  52. state.buttonsState =
  53. (m_Buttons[0] ? ControllerButton.TRIGGER : 0)
  54. | (m_Buttons[1] ? ControllerButton.APP : 0)
  55. | (m_Buttons[2] ? ControllerButton.HOME : 0);
  56. state.buttonsDown =
  57. (m_ButtonsDown[0] ? ControllerButton.TRIGGER : 0)
  58. | (m_ButtonsDown[1] ? ControllerButton.APP : 0)
  59. | (m_ButtonsDown[2] ? ControllerButton.HOME : 0);
  60. state.buttonsUp =
  61. (m_ButtonsUp[0] ? ControllerButton.TRIGGER : 0)
  62. | (m_ButtonsUp[1] ? ControllerButton.APP : 0)
  63. | (m_ButtonsUp[2] ? ControllerButton.HOME : 0);
  64. }
  65. }
  66. /// <summary> The processed frame. </summary>
  67. private int m_ProcessedFrame;
  68. private NRHandControllerStateParser[] m_StateParsers = new NRHandControllerStateParser[NRInput.MAX_CONTROLLER_STATE_COUNT];
  69. /// <summary> Constructor. </summary>
  70. /// <param name="states"> The states.</param>
  71. public NRHandControllerProvider(ControllerState[] states) : base(states)
  72. {
  73. for (int i = 0; i < m_StateParsers.Length; i++)
  74. {
  75. m_StateParsers[i] = new NRHandControllerStateParser();
  76. }
  77. Inited = true;
  78. }
  79. public override int ControllerCount { get { return 2; } }
  80. public override void OnDestroy()
  81. {
  82. }
  83. public override void OnPause()
  84. {
  85. for (int i = 0; i < states.Length; i++)
  86. {
  87. states[i].Reset();
  88. }
  89. }
  90. public override void OnResume()
  91. {
  92. }
  93. public override void Update()
  94. {
  95. if (m_ProcessedFrame == Time.frameCount)
  96. return;
  97. m_ProcessedFrame = Time.frameCount;
  98. for (int i = 0; i < states.Length; i++)
  99. {
  100. UpdateControllerState(i, GetHandState(i));
  101. }
  102. }
  103. private NRHandControllerStateParser GetNRHandControllerStateParser(int index)
  104. {
  105. if(index < m_StateParsers.Length)
  106. {
  107. return m_StateParsers[index];
  108. }
  109. return null;
  110. }
  111. private HandState GetHandState(int index)
  112. {
  113. return NRInput.Hands.GetHandState(index == 0 ? HandEnum.RightHand : HandEnum.LeftHand);
  114. }
  115. private void UpdateControllerState(int index, HandState handState)
  116. {
  117. states[index].controllerType = ControllerType.CONTROLLER_TYPE_HAND;
  118. states[index].availableFeature = ControllerAvailableFeature.CONTROLLER_AVAILABLE_FEATURE_ROTATION | ControllerAvailableFeature.CONTROLLER_AVAILABLE_FEATURE_POSITION;
  119. states[index].connectionState = ControllerConnectionState.CONTROLLER_CONNECTION_STATE_CONNECTED;
  120. states[index].rotation = handState.pointerPose.rotation;
  121. states[index].position = handState.pointerPose.position;
  122. states[index].gyro = Vector3.zero;
  123. states[index].accel = Vector3.zero;
  124. states[index].mag = Vector3.zero;
  125. states[index].touchPos = Vector3.zero;
  126. states[index].isTouching = false;
  127. states[index].recentered = false;
  128. states[index].isCharging = false;
  129. states[index].batteryLevel = 0;
  130. var stateParser = GetNRHandControllerStateParser(index);
  131. if (stateParser != null)
  132. {
  133. stateParser.ParserControllerState(states[index], handState);
  134. }
  135. }
  136. }
  137. }