//========================================================== // // 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(this); // 注册按键事件监听器 GxrSystemAccessor.InputSystem?.RegisterHandler(this); // 注册触摸事件监听器 GxrSystemAccessor.InputSystem?.RegisterHandler(this); } void OnDisable() { // 注销控制器姿态监听器 GxrSystemAccessor.InputSystem?.UnregisterHandler(this); // 注销按键事件监听器 GxrSystemAccessor.InputSystem?.UnregisterHandler(this); // 注销触摸事件监听器 GxrSystemAccessor.InputSystem?.UnregisterHandler(this); } private void LateUpdate() { UpdateRotate(); } /// /// 控制器姿态更新 /// /// 控制器姿态事件数据 public void OnControllerPoseUpdated(GxrControllerPoseEventData eventData) { if (poseVisualizer != null) { poseVisualizer.rotation = eventData.CurrentRotation; } } /// /// 按键按下 /// /// 按键事件数据 public void OnButtonDown(GxrButtonEventData eventData) { GxrKeyCode keyCode = eventData.KeyCode; UpdateButtonTextState(keyCode, true); } /// /// summary> /// 按键松开 /// /// 按键事件数据 public void OnButtonUp(GxrButtonEventData eventData) { GxrKeyCode keyCode = eventData.KeyCode; UpdateButtonTextState(keyCode, false); } /// /// 按键点击 /// /// 按键事件数据 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(); } } /// /// 按键长点击 /// /// 按键事件数据 public void OnButtonLongClick(GxrButtonEventData eventData) { } /// /// 按键双击 /// /// 按键事件数据 public void OnButtonDoubleClick(GxrButtonEventData eventData) { } /// /// 触摸开始 /// /// 触摸事件数据 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); } /// /// 触摸结束 /// /// 触摸事件数据 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); } } /// /// 触摸滑动方向 /// /// 触摸事件数据 public void OnTouchSwipe(GxrTouchEventData eventData) { GxrTouchSwipeDirection direction = eventData.SwipeDirection; UpdateSwipeDirectionText(direction); } /// /// 触摸位置更新 /// /// 触摸事件数据 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); } } }