OVRHandTest.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /************************************************************************************
  2. Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
  3. Licensed under the Oculus Utilities SDK License Version 1.31 (the "License"); you may not use
  4. the Utilities SDK except in compliance with the License, which is provided at the time of installation
  5. or download, or which otherwise accompanies this software in either electronic or hard copy form.
  6. You may obtain a copy of the License at
  7. https://developer.oculus.com/licenses/utilities-1.31
  8. Unless required by applicable law or agreed to in writing, the Utilities SDK distributed
  9. under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  10. ANY KIND, either express or implied. See the License for the specific language governing
  11. permissions and limitations under the License.
  12. ************************************************************************************/
  13. using UnityEngine;
  14. using UnityEngine.UI;
  15. using System.Collections;
  16. using System.Collections.Generic;
  17. using System.Text;
  18. public class OVRHandTest : MonoBehaviour
  19. {
  20. public class BoolMonitor
  21. {
  22. public delegate bool BoolGenerator();
  23. private string m_name = "";
  24. private BoolGenerator m_generator;
  25. private bool m_prevValue = false;
  26. private bool m_currentValue = false;
  27. private bool m_currentValueRecentlyChanged = false;
  28. private float m_displayTimeout = 0.0f;
  29. private float m_displayTimer = 0.0f;
  30. public BoolMonitor(string name, BoolGenerator generator, float displayTimeout = 0.5f)
  31. {
  32. m_name = name;
  33. m_generator = generator;
  34. m_displayTimeout = displayTimeout;
  35. }
  36. public void Update()
  37. {
  38. m_prevValue = m_currentValue;
  39. m_currentValue = m_generator();
  40. if (m_currentValue != m_prevValue)
  41. {
  42. m_currentValueRecentlyChanged = true;
  43. m_displayTimer = m_displayTimeout;
  44. }
  45. if (m_displayTimer > 0.0f)
  46. {
  47. m_displayTimer -= Time.deltaTime;
  48. if (m_displayTimer <= 0.0f)
  49. {
  50. m_currentValueRecentlyChanged = false;
  51. m_displayTimer = 0.0f;
  52. }
  53. }
  54. }
  55. public void AppendToStringBuilder(ref StringBuilder sb)
  56. {
  57. sb.Append(m_name);
  58. if (m_currentValue && m_currentValueRecentlyChanged)
  59. sb.Append(": *True*\n");
  60. else if (m_currentValue)
  61. sb.Append(": True \n");
  62. else if (!m_currentValue && m_currentValueRecentlyChanged)
  63. sb.Append(": *False*\n");
  64. else if (!m_currentValue)
  65. sb.Append(": False \n");
  66. }
  67. }
  68. public Text uiText;
  69. private List<BoolMonitor> monitors;
  70. private StringBuilder data;
  71. private OVRPlugin.HandState hs_LH = new OVRPlugin.HandState();
  72. private OVRPlugin.HandState hs_RH = new OVRPlugin.HandState();
  73. private OVRPlugin.Skeleton skel_LH = new OVRPlugin.Skeleton();
  74. private OVRPlugin.Skeleton skel_RH = new OVRPlugin.Skeleton();
  75. private OVRPlugin.Mesh mesh_LH = new OVRPlugin.Mesh();
  76. private OVRPlugin.Mesh mesh_RH = new OVRPlugin.Mesh();
  77. private bool result_skel_LH = false;
  78. private bool result_skel_RH = false;
  79. private bool result_mesh_LH = false;
  80. private bool result_mesh_RH = false;
  81. void Start()
  82. {
  83. if (uiText != null)
  84. {
  85. uiText.supportRichText = false;
  86. }
  87. data = new StringBuilder(2048);
  88. monitors = new List<BoolMonitor>()
  89. {
  90. new BoolMonitor("WasRecentered", () => OVRInput.GetControllerWasRecentered()),
  91. new BoolMonitor("One", () => OVRInput.Get(OVRInput.Button.One)),
  92. };
  93. result_skel_LH = OVRPlugin.GetSkeleton(OVRPlugin.SkeletonType.HandLeft, out skel_LH);
  94. result_skel_RH = OVRPlugin.GetSkeleton(OVRPlugin.SkeletonType.HandRight, out skel_RH);
  95. result_mesh_LH = OVRPlugin.GetMesh(OVRPlugin.MeshType.HandLeft, out mesh_LH);
  96. result_mesh_RH = OVRPlugin.GetMesh(OVRPlugin.MeshType.HandRight, out mesh_RH);
  97. }
  98. static string prevConnected = "";
  99. static BoolMonitor controllers = new BoolMonitor("Controllers Changed", () => { return OVRInput.GetConnectedControllers().ToString() != prevConnected; });
  100. void Update()
  101. {
  102. data.Length = 0;
  103. OVRInput.Controller activeController = OVRInput.GetActiveController();
  104. string activeControllerName = activeController.ToString();
  105. data.AppendFormat("Active: {0}\n", activeControllerName);
  106. string connectedControllerNames = OVRInput.GetConnectedControllers().ToString();
  107. data.AppendFormat("Connected: {0}\n", connectedControllerNames);
  108. data.AppendFormat("PrevConnected: {0}\n", prevConnected);
  109. controllers.Update();
  110. controllers.AppendToStringBuilder(ref data);
  111. prevConnected = connectedControllerNames;
  112. Vector3 pos = OVRInput.GetLocalControllerPosition(activeController);
  113. data.AppendFormat("Position: ({0:F2}, {1:F2}, {2:F2})\n", pos.x, pos.y, pos.z);
  114. Quaternion rot = OVRInput.GetLocalControllerRotation(activeController);
  115. data.AppendFormat("Orientation: ({0:F2}, {1:F2}, {2:F2}, {3:F2})\n", rot.x, rot.y, rot.z, rot.w);
  116. data.AppendFormat("HandTrackingEnabled: {0}\n", OVRPlugin.GetHandTrackingEnabled());
  117. bool result_hs_LH = OVRPlugin.GetHandState(OVRPlugin.Step.Render, OVRPlugin.Hand.HandLeft, ref hs_LH);
  118. data.AppendFormat("LH HS Query Res: {0}\n", result_hs_LH);
  119. data.AppendFormat("LH HS Status: {0}\n", hs_LH.Status);
  120. data.AppendFormat("LH HS Pose: {0}\n", hs_LH.RootPose);
  121. data.AppendFormat("LH HS HandConf: {0}\n", hs_LH.HandConfidence);
  122. bool result_hs_RH = OVRPlugin.GetHandState(OVRPlugin.Step.Render, OVRPlugin.Hand.HandRight, ref hs_RH);
  123. data.AppendFormat("RH HS Query Res: {0}\n", result_hs_RH);
  124. data.AppendFormat("RH HS Status: {0}\n", hs_RH.Status);
  125. data.AppendFormat("RH HS Pose: {0}\n", hs_RH.RootPose);
  126. data.AppendFormat("RH HS HandConf: {0}\n", hs_RH.HandConfidence);
  127. data.AppendFormat("LH Skel Query Res: {0}\n", result_skel_LH);
  128. data.AppendFormat("LH Skel Type: {0}\n", skel_LH.Type);
  129. data.AppendFormat("LH Skel NumBones: {0}\n", skel_LH.NumBones);
  130. data.AppendFormat("RH Skel Query Res: {0}\n", result_skel_RH);
  131. data.AppendFormat("RH Skel Type: {0}\n", skel_RH.Type);
  132. data.AppendFormat("RH Skel NumBones: {0}\n", skel_RH.NumBones);
  133. data.AppendFormat("LH Mesh Query Res: {0}\n", result_mesh_LH);
  134. data.AppendFormat("LH Mesh Type: {0}\n", mesh_LH.Type);
  135. data.AppendFormat("LH Mesh NumVers: {0}\n", mesh_LH.NumVertices);
  136. data.AppendFormat("RH Mesh Query Res: {0}\n", result_mesh_RH);
  137. data.AppendFormat("RH Mesh Type: {0}\n", mesh_RH.Type);
  138. data.AppendFormat("RH Mesh NumVers: {0}\n", mesh_RH.NumVertices);
  139. for (int i = 0; i < monitors.Count; i++)
  140. {
  141. monitors[i].Update();
  142. monitors[i].AppendToStringBuilder(ref data);
  143. }
  144. if (uiText != null)
  145. {
  146. uiText.text = data.ToString();
  147. }
  148. }
  149. }