TestDeviceBaseInput.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SpatialTracking;
  5. using Ximmerse.XR.Utils;
  6. using System.Text;
  7. using UnityEngine.XR.Interaction.Toolkit;
  8. using UnityEngine.XR;
  9. namespace Ximmerse.XR.tests
  10. {
  11. public class TestDeviceBaseInput : MonoBehaviour
  12. {
  13. public XRController left, right;
  14. StringBuilder leftHandMsg = new StringBuilder();
  15. StringBuilder rightHandMsg = new StringBuilder();
  16. public bool TestHaptic = true;
  17. public InputHelpers.Button HapticButton = InputHelpers.Button.TriggerButton;
  18. static InputHelpers.Button[] xrButtons = new InputHelpers.Button[]
  19. {
  20. InputHelpers.Button.MenuButton,
  21. InputHelpers.Button.TriggerButton,
  22. InputHelpers.Button.GripButton,
  23. InputHelpers.Button.PrimaryButton,
  24. InputHelpers.Button.SecondaryButton,
  25. InputHelpers.Button.Primary2DAxisTouch,
  26. InputHelpers.Button.Primary2DAxisClick,
  27. };
  28. static InputHelpers.Button[] xrAxis = new InputHelpers.Button[]
  29. {
  30. InputHelpers.Button.Trigger,
  31. InputHelpers.Button.Grip,
  32. InputHelpers.Button.PrimaryAxis2DRight,
  33. InputHelpers.Button.PrimaryAxis2DLeft,
  34. InputHelpers.Button.PrimaryAxis2DUp,
  35. InputHelpers.Button.PrimaryAxis2DDown,
  36. };
  37. void LateUpdate()
  38. {
  39. leftHandMsg.Clear();
  40. if (left)
  41. {
  42. var _inputDevice = left.inputDevice;
  43. if (_inputDevice.TryGetFeatureValue(CommonUsages.devicePosition, out Vector3 p)
  44. && _inputDevice.TryGetFeatureValue(CommonUsages.deviceRotation, out Quaternion q))
  45. {
  46. // Debug.LogFormat("Hand input device p = {0}, q = {1} (Left)", p.ToString("F3"), q.eulerAngles.ToString("F2"));
  47. }
  48. foreach (var btn in xrButtons)
  49. {
  50. if (_inputDevice.IsPressed(btn, out bool isPressed) && isPressed)
  51. {
  52. leftHandMsg.AppendFormat(" {0} ", btn);
  53. }
  54. }
  55. foreach (var btn in xrAxis)
  56. {
  57. if (_inputDevice.TryReadSingleValue(btn, out float val) && val != 0)
  58. {
  59. leftHandMsg.AppendFormat("\r\n {0}={1} ", btn, val);
  60. }
  61. }
  62. if (leftHandMsg.Length > 0)
  63. {
  64. Matrix4x4 world = Camera.main.transform.parent.localToWorldMatrix * Matrix4x4.TRS(left.currentControllerState.position, left.currentControllerState.rotation, Vector3.one);
  65. RxDraw.Text3D(world.GetColumn(3), Quaternion.LookRotation(Camera.main.transform.forward), 0.01f, leftHandMsg.ToString(), Color.green);
  66. }
  67. if (TestHaptic && _inputDevice.IsPressed(this.HapticButton, out bool isPressedHapticBtn) && isPressedHapticBtn)
  68. {
  69. bool ret = _inputDevice.TryGetHapticCapabilities(out HapticCapabilities hapticCapabilities);
  70. // Debug.LogFormat("Unity.SendHandleHapticImpulse_Left , ret: {0} support haptic: {1}, supports buffer: {2}", ret, hapticCapabilities.supportsImpulse, hapticCapabilities.supportsBuffer);
  71. bool sentResult = _inputDevice.SendHapticImpulse(0u, 1.0f, 1f);
  72. Debug.LogFormat("Left Haptic impluse result: {0}", sentResult);
  73. // _inputDevice.SendHapticBuffer(0, new byte[10]);
  74. }
  75. }
  76. rightHandMsg.Clear();
  77. if (right)
  78. {
  79. var _inputDevice = right.inputDevice;
  80. if (_inputDevice.TryGetFeatureValue(CommonUsages.devicePosition, out Vector3 p)
  81. && _inputDevice.TryGetFeatureValue(CommonUsages.deviceRotation, out Quaternion q))
  82. {
  83. //Debug.LogFormat("Hand input device p = {0}, q = {1} (Right)", p.ToString("F3"), q.eulerAngles.ToString("F2"));
  84. }
  85. foreach (var btn in xrButtons)
  86. {
  87. if (_inputDevice.IsPressed(btn, out bool isPressed) && isPressed)
  88. {
  89. rightHandMsg.AppendFormat(" {0} ", btn);
  90. }
  91. }
  92. foreach (var btn in xrAxis)
  93. {
  94. if (_inputDevice.TryReadSingleValue(btn, out float val) && val != 0)
  95. {
  96. rightHandMsg.AppendFormat("\r\n {0}={1} ", btn, val);
  97. }
  98. }
  99. if (rightHandMsg.Length > 0)
  100. {
  101. Matrix4x4 world = Camera.main.transform.parent.localToWorldMatrix * Matrix4x4.TRS(right.currentControllerState.position, right.currentControllerState.rotation, Vector3.one);
  102. RxDraw.Text3D(world.GetColumn(3), Quaternion.LookRotation(Camera.main.transform.forward), 0.01f, rightHandMsg.ToString(), Color.green);
  103. }
  104. if (TestHaptic && _inputDevice.IsPressed(this.HapticButton, out bool isPressedHapticBtn) && isPressedHapticBtn)
  105. {
  106. bool ret = _inputDevice.TryGetHapticCapabilities(out HapticCapabilities hapticCapabilities);
  107. // Debug.LogFormat("Unity.SendHandleHapticImpulse_Right , ret: {0} support haptic: {1}, supports buffer: {2}", ret, hapticCapabilities.supportsImpulse, hapticCapabilities.supportsBuffer);
  108. bool sentResult = _inputDevice.SendHapticImpulse(0u, 1.0f, 1f);
  109. Debug.LogFormat("Right Haptic impluse result: {0}", sentResult);
  110. // _inputDevice.SendHapticBuffer(0, new byte[10]);
  111. }
  112. }
  113. }
  114. }
  115. }