Advertisement
Guest User

Untitled

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