StartStop3Dof.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //==========================================================
  2. //
  3. // Copyright (c) Guangzhou Shixiang Technology Co.,Ltd.
  4. // All rights reserved.
  5. //
  6. //==========================================================
  7. using System;
  8. using GxrSdk;
  9. using TMPro;
  10. using UnityEngine;
  11. public class StartStop3Dof : MonoBehaviour, IGxrControllerPoseHandler, IGxrButtonHandler, IGxrTouchHandler
  12. {
  13. [SerializeField]
  14. private Transform poseVisualizer;
  15. [SerializeField]
  16. private Transform rotateObject;
  17. [SerializeField]
  18. private TextMeshPro textHomeButton;
  19. [SerializeField]
  20. private TextMeshPro textBackButton;
  21. [SerializeField]
  22. private TextMeshPro textTouchPadButton;
  23. [SerializeField]
  24. private TextMeshPro textTiggerButton;
  25. [SerializeField]
  26. private TextMeshPro textTouchPosition;
  27. [SerializeField]
  28. private TextMeshPro textTouchSwipe;
  29. private bool _isTouching = false;
  30. private bool _isRotating = false;
  31. private Vector2 _lastTouchPos;
  32. private Vector2 _beginTouchPos;
  33. private long _touchBeginTime;
  34. private Vector2 _rotateSpeed;
  35. private const float TouchToRotateFactor = 60;//touchpad的触摸位置偏移转旋转时的系数
  36. private const float TouchToSpeedFactor = 10;//touchpad的触摸位置偏移转旋转速度时的系数
  37. private const float ThresholdSpeed = 40;//触发惯性旋转阈值
  38. void OnEnable()
  39. {
  40. // 注册控制器姿态监听器
  41. GxrSystemAccessor.InputSystem?.RegisterHandler<IGxrControllerPoseHandler>(this);
  42. // 注册按键事件监听器
  43. GxrSystemAccessor.InputSystem?.RegisterHandler<IGxrButtonHandler>(this);
  44. // 注册触摸事件监听器
  45. GxrSystemAccessor.InputSystem?.RegisterHandler<IGxrTouchHandler>(this);
  46. }
  47. void OnDisable()
  48. {
  49. // 注销控制器姿态监听器
  50. GxrSystemAccessor.InputSystem?.UnregisterHandler<IGxrControllerPoseHandler>(this);
  51. // 注销按键事件监听器
  52. GxrSystemAccessor.InputSystem?.UnregisterHandler<IGxrButtonHandler>(this);
  53. // 注销触摸事件监听器
  54. GxrSystemAccessor.InputSystem?.UnregisterHandler<IGxrTouchHandler>(this);
  55. }
  56. private void LateUpdate()
  57. {
  58. UpdateRotate();
  59. }
  60. /// <summary>
  61. /// 控制器姿态更新
  62. /// </summary>
  63. /// <param name="eventData">控制器姿态事件数据</param>
  64. public void OnControllerPoseUpdated(GxrControllerPoseEventData eventData)
  65. {
  66. if (poseVisualizer != null)
  67. {
  68. poseVisualizer.rotation = eventData.CurrentRotation;
  69. }
  70. }
  71. /// <summary>
  72. /// 按键按下
  73. /// </summary>
  74. /// <param name="eventData">按键事件数据</param>
  75. public void OnButtonDown(GxrButtonEventData eventData)
  76. {
  77. GxrKeyCode keyCode = eventData.KeyCode;
  78. UpdateButtonTextState(keyCode, true);
  79. }
  80. /// <summary>
  81. /// summary>
  82. /// 按键松开
  83. /// </summary>
  84. /// <param name="eventData">按键事件数据</param>
  85. public void OnButtonUp(GxrButtonEventData eventData)
  86. {
  87. GxrKeyCode keyCode = eventData.KeyCode;
  88. UpdateButtonTextState(keyCode, false);
  89. }
  90. /// <summary>
  91. /// 按键点击
  92. /// </summary>
  93. /// <param name="eventData">按键事件数据</param>
  94. public void OnButtonClick(GxrButtonEventData eventData)
  95. {
  96. GxrKeyCode keyCode = eventData.KeyCode;
  97. if (keyCode == GxrKeyCode.A)
  98. {
  99. GxrInputSystem.StopHeadDisplay3DofTracking();
  100. }
  101. else if (keyCode == GxrKeyCode.Trigger || keyCode == GxrKeyCode.TouchPad)
  102. {
  103. GxrInputSystem.StartHeadDisplay3DofTracking();
  104. }
  105. }
  106. /// <summary>
  107. /// 按键长点击
  108. /// </summary>
  109. /// <param name="eventData">按键事件数据</param>
  110. public void OnButtonLongClick(GxrButtonEventData eventData)
  111. {
  112. }
  113. /// <summary>
  114. /// 按键双击
  115. /// </summary>
  116. /// <param name="eventData">按键事件数据</param>
  117. public void OnButtonDoubleClick(GxrButtonEventData eventData)
  118. {
  119. }
  120. /// <summary>
  121. /// 触摸开始
  122. /// </summary>
  123. /// <param name="eventData">触摸事件数据</param>
  124. public void OnTouchBegin(GxrTouchEventData eventData)
  125. {
  126. UpdateTouchPositionTextState(true);
  127. Vector2 curTouchPos = eventData.TouchPosition;
  128. _lastTouchPos = curTouchPos;
  129. _beginTouchPos = curTouchPos;
  130. _touchBeginTime = DateTime.Now.Ticks;
  131. _isTouching = true;
  132. _isRotating = false;
  133. _rotateSpeed.Set(0, 0);
  134. }
  135. /// <summary>
  136. /// 触摸结束
  137. /// </summary>
  138. /// <param name="eventData">触摸事件数据</param>
  139. public void OnTouchEnd(GxrTouchEventData eventData)
  140. {
  141. UpdateTouchPositionTextState(false);
  142. if (_isTouching)
  143. {
  144. _isTouching = false;
  145. long deltaTime = DateTime.Now.Ticks - _touchBeginTime;
  146. Vector2 deltaPos = _lastTouchPos - _beginTouchPos;
  147. _rotateSpeed = deltaPos / deltaTime * TouchToSpeedFactor * 10000000;//纳秒转秒
  148. if (Math.Abs(_rotateSpeed.x) > Math.Abs(_rotateSpeed.y))
  149. {
  150. _rotateSpeed.y = 0;
  151. }
  152. else
  153. {
  154. _rotateSpeed.x = 0;
  155. }
  156. _isRotating = (Math.Abs(_rotateSpeed.x) > ThresholdSpeed) || (Math.Abs(_rotateSpeed.y) > ThresholdSpeed);
  157. }
  158. }
  159. /// <summary>
  160. /// 触摸滑动方向
  161. /// </summary>
  162. /// <param name="eventData">触摸事件数据</param>
  163. public void OnTouchSwipe(GxrTouchEventData eventData)
  164. {
  165. GxrTouchSwipeDirection direction = eventData.SwipeDirection;
  166. UpdateSwipeDirectionText(direction);
  167. }
  168. /// <summary>
  169. /// 触摸位置更新
  170. /// </summary>
  171. /// <param name="eventData">触摸事件数据</param>
  172. public void OnTouchPositionUpdated(GxrTouchEventData eventData)
  173. {
  174. if (_isTouching)
  175. {
  176. Vector2 touchPosition = eventData.TouchPosition;
  177. UpdateTouchPositionText(touchPosition);
  178. Vector2 touchDelta = touchPosition - _lastTouchPos;
  179. _lastTouchPos = touchPosition;
  180. RotateObject(touchDelta * TouchToRotateFactor);
  181. }
  182. }
  183. private void UpdateButtonTextState(GxrKeyCode keyCode, bool isDown)
  184. {
  185. TextMeshPro textMesh = GetButtonText(keyCode);
  186. if (textMesh != null)
  187. {
  188. textMesh.color = isDown ? Color.red : Color.white;
  189. }
  190. }
  191. private TextMeshPro GetButtonText(GxrKeyCode keyCode)
  192. {
  193. switch (keyCode)
  194. {
  195. case GxrKeyCode.B:
  196. return textHomeButton;
  197. case GxrKeyCode.A:
  198. return textBackButton;
  199. case GxrKeyCode.TouchPad:
  200. return textTouchPadButton;
  201. case GxrKeyCode.Trigger:
  202. return textTiggerButton;
  203. }
  204. return null;
  205. }
  206. private void UpdateSwipeDirectionText(GxrTouchSwipeDirection direction)
  207. {
  208. textTouchSwipe.text = $"SwipeDirection: {direction}";
  209. }
  210. private void UpdateTouchPositionTextState(bool isDown)
  211. {
  212. textTouchPosition.color = isDown ? Color.red : Color.white;
  213. }
  214. private void UpdateTouchPositionText(Vector2 touchPosition)
  215. {
  216. textTouchPosition.text = $"Touch: {touchPosition.x:0.000},{touchPosition.y:0.000}";
  217. }
  218. private void UpdateRotate()
  219. {
  220. if (_isRotating)
  221. {
  222. _rotateSpeed *= 0.8f;
  223. if ((Math.Abs(_rotateSpeed.x) < 0.001f) && (Math.Abs(_rotateSpeed.y) < 0.001f))
  224. {
  225. _rotateSpeed.Set(0, 0);
  226. _isRotating = false;
  227. return;
  228. }
  229. RotateObject(_rotateSpeed);
  230. }
  231. }
  232. private void RotateObject(Vector2 angle)
  233. {
  234. if (rotateObject != null)
  235. {
  236. float angleX = angle.y;
  237. float angleY = -angle.x;
  238. rotateObject.Rotate(angleX, angleY, 0, Space.World);
  239. }
  240. }
  241. }