Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.8 KiB

  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Test SteamVR_Controller support.
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using System.Collections.Generic;
  8. using Valve.VR;
  9. public class SteamVR_TestController : MonoBehaviour
  10. {
  11. List<int> controllerIndices = new List<int>();
  12. private void OnDeviceConnected(int index, bool connected)
  13. {
  14. var system = OpenVR.System;
  15. if (system == null || system.GetTrackedDeviceClass((uint)index) != ETrackedDeviceClass.Controller)
  16. return;
  17. if (connected)
  18. {
  19. Debug.Log(string.Format("Controller {0} connected.", index));
  20. PrintControllerStatus(index);
  21. controllerIndices.Add(index);
  22. }
  23. else
  24. {
  25. Debug.Log(string.Format("Controller {0} disconnected.", index));
  26. PrintControllerStatus(index);
  27. controllerIndices.Remove(index);
  28. }
  29. }
  30. void OnEnable()
  31. {
  32. SteamVR_Events.DeviceConnected.Listen(OnDeviceConnected);
  33. }
  34. void OnDisable()
  35. {
  36. SteamVR_Events.DeviceConnected.Remove(OnDeviceConnected);
  37. }
  38. void PrintControllerStatus(int index)
  39. {
  40. var device = SteamVR_Controller.Input(index);
  41. Debug.Log("index: " + device.index);
  42. Debug.Log("connected: " + device.connected);
  43. Debug.Log("hasTracking: " + device.hasTracking);
  44. Debug.Log("outOfRange: " + device.outOfRange);
  45. Debug.Log("calibrating: " + device.calibrating);
  46. Debug.Log("uninitialized: " + device.uninitialized);
  47. Debug.Log("pos: " + device.transform.pos);
  48. Debug.Log("rot: " + device.transform.rot.eulerAngles);
  49. Debug.Log("velocity: " + device.velocity);
  50. Debug.Log("angularVelocity: " + device.angularVelocity);
  51. var l = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Leftmost);
  52. var r = SteamVR_Controller.GetDeviceIndex(SteamVR_Controller.DeviceRelation.Rightmost);
  53. Debug.Log((l == r) ? "first" : (l == index) ? "left" : "right");
  54. }
  55. EVRButtonId[] buttonIds = new EVRButtonId[] {
  56. EVRButtonId.k_EButton_ApplicationMenu,
  57. EVRButtonId.k_EButton_Grip,
  58. EVRButtonId.k_EButton_SteamVR_Touchpad,
  59. EVRButtonId.k_EButton_SteamVR_Trigger
  60. };
  61. EVRButtonId[] axisIds = new EVRButtonId[] {
  62. EVRButtonId.k_EButton_SteamVR_Touchpad,
  63. EVRButtonId.k_EButton_SteamVR_Trigger
  64. };
  65. public Transform point, pointer;
  66. void Update()
  67. {
  68. foreach (var index in controllerIndices)
  69. {
  70. var overlay = SteamVR_Overlay.instance;
  71. if (overlay && point && pointer)
  72. {
  73. var t = SteamVR_Controller.Input(index).transform;
  74. pointer.transform.localPosition = t.pos;
  75. pointer.transform.localRotation = t.rot;
  76. var results = new SteamVR_Overlay.IntersectionResults();
  77. var hit = overlay.ComputeIntersection(t.pos, t.rot * Vector3.forward, ref results);
  78. if (hit)
  79. {
  80. point.transform.localPosition = results.point;
  81. point.transform.localRotation = Quaternion.LookRotation(results.normal);
  82. }
  83. continue;
  84. }
  85. foreach (var buttonId in buttonIds)
  86. {
  87. if (SteamVR_Controller.Input(index).GetPressDown(buttonId))
  88. Debug.Log(buttonId + " press down");
  89. if (SteamVR_Controller.Input(index).GetPressUp(buttonId))
  90. {
  91. Debug.Log(buttonId + " press up");
  92. if (buttonId == EVRButtonId.k_EButton_SteamVR_Trigger)
  93. {
  94. SteamVR_Controller.Input(index).TriggerHapticPulse();
  95. PrintControllerStatus(index);
  96. }
  97. }
  98. if (SteamVR_Controller.Input(index).GetPress(buttonId))
  99. Debug.Log(buttonId);
  100. }
  101. foreach (var buttonId in axisIds)
  102. {
  103. if (SteamVR_Controller.Input(index).GetTouchDown(buttonId))
  104. Debug.Log(buttonId + " touch down");
  105. if (SteamVR_Controller.Input(index).GetTouchUp(buttonId))
  106. Debug.Log(buttonId + " touch up");
  107. if (SteamVR_Controller.Input(index).GetTouch(buttonId))
  108. {
  109. var axis = SteamVR_Controller.Input(index).GetAxis(buttonId);
  110. Debug.Log("axis: " + axis);
  111. }
  112. }
  113. }
  114. }
  115. }