123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- //==========================================================
- //
- // Copyright (c) Guangzhou Shixiang Technology Co.,Ltd.
- // All rights reserved.
- //
- //==========================================================
- using System;
- using GxrSdk;
- using TMPro;
- using UnityEngine;
- public class StartStop3Dof : MonoBehaviour, IGxrControllerPoseHandler, IGxrButtonHandler, IGxrTouchHandler
- {
- [SerializeField]
- private Transform poseVisualizer;
- [SerializeField]
- private Transform rotateObject;
- [SerializeField]
- private TextMeshPro textHomeButton;
- [SerializeField]
- private TextMeshPro textBackButton;
- [SerializeField]
- private TextMeshPro textTouchPadButton;
- [SerializeField]
- private TextMeshPro textTiggerButton;
- [SerializeField]
- private TextMeshPro textTouchPosition;
- [SerializeField]
- private TextMeshPro textTouchSwipe;
- private bool _isTouching = false;
- private bool _isRotating = false;
- private Vector2 _lastTouchPos;
- private Vector2 _beginTouchPos;
- private long _touchBeginTime;
- private Vector2 _rotateSpeed;
- private const float TouchToRotateFactor = 60;//touchpad的触摸位置偏移转旋转时的系数
- private const float TouchToSpeedFactor = 10;//touchpad的触摸位置偏移转旋转速度时的系数
- private const float ThresholdSpeed = 40;//触发惯性旋转阈值
- void OnEnable()
- {
- // 注册控制器姿态监听器
- GxrSystemAccessor.InputSystem?.RegisterHandler<IGxrControllerPoseHandler>(this);
- // 注册按键事件监听器
- GxrSystemAccessor.InputSystem?.RegisterHandler<IGxrButtonHandler>(this);
- // 注册触摸事件监听器
- GxrSystemAccessor.InputSystem?.RegisterHandler<IGxrTouchHandler>(this);
- }
- void OnDisable()
- {
- // 注销控制器姿态监听器
- GxrSystemAccessor.InputSystem?.UnregisterHandler<IGxrControllerPoseHandler>(this);
- // 注销按键事件监听器
- GxrSystemAccessor.InputSystem?.UnregisterHandler<IGxrButtonHandler>(this);
- // 注销触摸事件监听器
- GxrSystemAccessor.InputSystem?.UnregisterHandler<IGxrTouchHandler>(this);
- }
- private void LateUpdate()
- {
- UpdateRotate();
- }
- /// <summary>
- /// 控制器姿态更新
- /// </summary>
- /// <param name="eventData">控制器姿态事件数据</param>
- public void OnControllerPoseUpdated(GxrControllerPoseEventData eventData)
- {
- if (poseVisualizer != null)
- {
- poseVisualizer.rotation = eventData.CurrentRotation;
- }
- }
- /// <summary>
- /// 按键按下
- /// </summary>
- /// <param name="eventData">按键事件数据</param>
- public void OnButtonDown(GxrButtonEventData eventData)
- {
- GxrKeyCode keyCode = eventData.KeyCode;
- UpdateButtonTextState(keyCode, true);
- }
- /// <summary>
- /// summary>
- /// 按键松开
- /// </summary>
- /// <param name="eventData">按键事件数据</param>
- public void OnButtonUp(GxrButtonEventData eventData)
- {
- GxrKeyCode keyCode = eventData.KeyCode;
- UpdateButtonTextState(keyCode, false);
- }
- /// <summary>
- /// 按键点击
- /// </summary>
- /// <param name="eventData">按键事件数据</param>
- public void OnButtonClick(GxrButtonEventData eventData)
- {
- GxrKeyCode keyCode = eventData.KeyCode;
- if (keyCode == GxrKeyCode.A)
- {
- GxrInputSystem.StopHeadDisplay3DofTracking();
- }
- else if (keyCode == GxrKeyCode.Trigger || keyCode == GxrKeyCode.TouchPad)
- {
- GxrInputSystem.StartHeadDisplay3DofTracking();
- }
- }
- /// <summary>
- /// 按键长点击
- /// </summary>
- /// <param name="eventData">按键事件数据</param>
- public void OnButtonLongClick(GxrButtonEventData eventData)
- {
- }
- /// <summary>
- /// 按键双击
- /// </summary>
- /// <param name="eventData">按键事件数据</param>
- public void OnButtonDoubleClick(GxrButtonEventData eventData)
- {
- }
- /// <summary>
- /// 触摸开始
- /// </summary>
- /// <param name="eventData">触摸事件数据</param>
- public void OnTouchBegin(GxrTouchEventData eventData)
- {
- UpdateTouchPositionTextState(true);
- Vector2 curTouchPos = eventData.TouchPosition;
- _lastTouchPos = curTouchPos;
- _beginTouchPos = curTouchPos;
- _touchBeginTime = DateTime.Now.Ticks;
- _isTouching = true;
- _isRotating = false;
- _rotateSpeed.Set(0, 0);
- }
- /// <summary>
- /// 触摸结束
- /// </summary>
- /// <param name="eventData">触摸事件数据</param>
- public void OnTouchEnd(GxrTouchEventData eventData)
- {
- UpdateTouchPositionTextState(false);
- if (_isTouching)
- {
- _isTouching = false;
- long deltaTime = DateTime.Now.Ticks - _touchBeginTime;
- Vector2 deltaPos = _lastTouchPos - _beginTouchPos;
- _rotateSpeed = deltaPos / deltaTime * TouchToSpeedFactor * 10000000;//纳秒转秒
- if (Math.Abs(_rotateSpeed.x) > Math.Abs(_rotateSpeed.y))
- {
- _rotateSpeed.y = 0;
- }
- else
- {
- _rotateSpeed.x = 0;
- }
- _isRotating = (Math.Abs(_rotateSpeed.x) > ThresholdSpeed) || (Math.Abs(_rotateSpeed.y) > ThresholdSpeed);
- }
- }
- /// <summary>
- /// 触摸滑动方向
- /// </summary>
- /// <param name="eventData">触摸事件数据</param>
- public void OnTouchSwipe(GxrTouchEventData eventData)
- {
- GxrTouchSwipeDirection direction = eventData.SwipeDirection;
- UpdateSwipeDirectionText(direction);
- }
- /// <summary>
- /// 触摸位置更新
- /// </summary>
- /// <param name="eventData">触摸事件数据</param>
- public void OnTouchPositionUpdated(GxrTouchEventData eventData)
- {
- if (_isTouching)
- {
- Vector2 touchPosition = eventData.TouchPosition;
- UpdateTouchPositionText(touchPosition);
- Vector2 touchDelta = touchPosition - _lastTouchPos;
- _lastTouchPos = touchPosition;
- RotateObject(touchDelta * TouchToRotateFactor);
- }
- }
- private void UpdateButtonTextState(GxrKeyCode keyCode, bool isDown)
- {
- TextMeshPro textMesh = GetButtonText(keyCode);
- if (textMesh != null)
- {
- textMesh.color = isDown ? Color.red : Color.white;
- }
- }
- private TextMeshPro GetButtonText(GxrKeyCode keyCode)
- {
- switch (keyCode)
- {
- case GxrKeyCode.B:
- return textHomeButton;
- case GxrKeyCode.A:
- return textBackButton;
- case GxrKeyCode.TouchPad:
- return textTouchPadButton;
- case GxrKeyCode.Trigger:
- return textTiggerButton;
- }
- return null;
- }
- private void UpdateSwipeDirectionText(GxrTouchSwipeDirection direction)
- {
- textTouchSwipe.text = $"SwipeDirection: {direction}";
- }
- private void UpdateTouchPositionTextState(bool isDown)
- {
- textTouchPosition.color = isDown ? Color.red : Color.white;
- }
- private void UpdateTouchPositionText(Vector2 touchPosition)
- {
- textTouchPosition.text = $"Touch: {touchPosition.x:0.000},{touchPosition.y:0.000}";
- }
- private void UpdateRotate()
- {
- if (_isRotating)
- {
- _rotateSpeed *= 0.8f;
- if ((Math.Abs(_rotateSpeed.x) < 0.001f) && (Math.Abs(_rotateSpeed.y) < 0.001f))
- {
- _rotateSpeed.Set(0, 0);
- _isRotating = false;
- return;
- }
- RotateObject(_rotateSpeed);
- }
- }
- private void RotateObject(Vector2 angle)
- {
- if (rotateObject != null)
- {
- float angleX = angle.y;
- float angleY = -angle.x;
- rotateObject.Rotate(angleX, angleY, 0, Space.World);
- }
- }
- }
|