OVRHand.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 System.Collections;
  14. using System.Collections.Generic;
  15. using UnityEngine;
  16. [DefaultExecutionOrder(-90)]
  17. public class OVRHand : MonoBehaviour,
  18. OVRSkeleton.IOVRSkeletonDataProvider,
  19. OVRSkeletonRenderer.IOVRSkeletonRendererDataProvider,
  20. OVRMesh.IOVRMeshDataProvider,
  21. OVRMeshRenderer.IOVRMeshRendererDataProvider
  22. {
  23. public enum Hand
  24. {
  25. None = OVRPlugin.Hand.None,
  26. HandLeft = OVRPlugin.Hand.HandLeft,
  27. HandRight = OVRPlugin.Hand.HandRight,
  28. }
  29. public enum HandFinger
  30. {
  31. Thumb = OVRPlugin.HandFinger.Thumb,
  32. Index = OVRPlugin.HandFinger.Index,
  33. Middle = OVRPlugin.HandFinger.Middle,
  34. Ring = OVRPlugin.HandFinger.Ring,
  35. Pinky = OVRPlugin.HandFinger.Pinky,
  36. }
  37. public enum TrackingConfidence
  38. {
  39. Low = OVRPlugin.TrackingConfidence.Low,
  40. High = OVRPlugin.TrackingConfidence.High
  41. }
  42. [SerializeField]
  43. private Hand HandType = Hand.None;
  44. private OVRPlugin.HandState _handState = new OVRPlugin.HandState();
  45. private bool _isInitialized = false;
  46. private GameObject _pointerPoseGO;
  47. public bool IsTracked { get; private set; }
  48. public bool IsSystemGestureInProgress { get; private set; }
  49. public bool IsPointerPoseValid { get; private set; }
  50. public Transform PointerPose { get; private set; }
  51. public float HandScale { get; private set; }
  52. public TrackingConfidence HandConfidence { get; private set; }
  53. private void Awake()
  54. {
  55. _pointerPoseGO = new GameObject();
  56. PointerPose = _pointerPoseGO.transform;
  57. GetHandState(OVRPlugin.Step.Render);
  58. }
  59. private void Update()
  60. {
  61. GetHandState(OVRPlugin.Step.Render);
  62. }
  63. private void FixedUpdate()
  64. {
  65. GetHandState(OVRPlugin.Step.Physics);
  66. }
  67. private void GetHandState(OVRPlugin.Step step)
  68. {
  69. if (OVRPlugin.GetHandState(step, (OVRPlugin.Hand)HandType, ref _handState))
  70. {
  71. IsTracked = (_handState.Status & OVRPlugin.HandStatus.HandTracked) != 0;
  72. IsSystemGestureInProgress = (_handState.Status & OVRPlugin.HandStatus.SystemGestureInProgress) != 0;
  73. IsPointerPoseValid = (_handState.Status & OVRPlugin.HandStatus.InputStateValid) != 0;
  74. PointerPose.localPosition = _handState.PointerPose.Position.FromFlippedZVector3f();
  75. PointerPose.localRotation = _handState.PointerPose.Orientation.FromFlippedZQuatf();
  76. HandScale = _handState.HandScale;
  77. HandConfidence = (TrackingConfidence)_handState.HandConfidence;
  78. _isInitialized = true;
  79. }
  80. else
  81. {
  82. _isInitialized = false;
  83. }
  84. }
  85. public bool GetFingerIsPinching(HandFinger finger)
  86. {
  87. return _isInitialized && (((int)_handState.Pinches & (1 << (int)finger)) != 0);
  88. }
  89. public float GetFingerPinchStrength(HandFinger finger)
  90. {
  91. if (_isInitialized
  92. && _handState.PinchStrength != null
  93. && _handState.PinchStrength.Length == (int)OVRPlugin.HandFinger.Max)
  94. {
  95. return _handState.PinchStrength[(int)finger];
  96. }
  97. return 0.0f;
  98. }
  99. public TrackingConfidence GetFingerConfidence(HandFinger finger)
  100. {
  101. if (_isInitialized
  102. && _handState.FingerConfidences != null
  103. && _handState.FingerConfidences.Length == (int)OVRPlugin.HandFinger.Max)
  104. {
  105. return (TrackingConfidence)_handState.FingerConfidences[(int)finger];
  106. }
  107. return TrackingConfidence.Low;
  108. }
  109. OVRSkeleton.SkeletonType OVRSkeleton.IOVRSkeletonDataProvider.GetSkeletonType()
  110. {
  111. switch (HandType)
  112. {
  113. case Hand.HandLeft:
  114. return OVRSkeleton.SkeletonType.HandLeft;
  115. case Hand.HandRight:
  116. return OVRSkeleton.SkeletonType.HandRight;
  117. case Hand.None:
  118. default:
  119. return OVRSkeleton.SkeletonType.None;
  120. }
  121. }
  122. OVRSkeleton.SkeletonPoseData OVRSkeleton.IOVRSkeletonDataProvider.GetSkeletonPoseData()
  123. {
  124. var data = new OVRSkeleton.SkeletonPoseData();
  125. data.IsDataValid = _isInitialized;
  126. if (_isInitialized)
  127. {
  128. data.RootPose = _handState.RootPose;
  129. data.RootScale = _handState.HandScale;
  130. data.BoneRotations = _handState.BoneRotations;
  131. data.IsDataHighConfidence = IsTracked && HandConfidence == TrackingConfidence.High;
  132. }
  133. return data;
  134. }
  135. OVRSkeletonRenderer.SkeletonRendererData OVRSkeletonRenderer.IOVRSkeletonRendererDataProvider.GetSkeletonRendererData()
  136. {
  137. var data = new OVRSkeletonRenderer.SkeletonRendererData();
  138. data.IsDataValid = _isInitialized;
  139. if (_isInitialized)
  140. {
  141. data.RootScale = _handState.HandScale;
  142. data.IsDataHighConfidence = IsTracked && HandConfidence == TrackingConfidence.High;
  143. }
  144. return data;
  145. }
  146. OVRMesh.MeshType OVRMesh.IOVRMeshDataProvider.GetMeshType()
  147. {
  148. switch (HandType)
  149. {
  150. case Hand.None:
  151. return OVRMesh.MeshType.None;
  152. case Hand.HandLeft:
  153. return OVRMesh.MeshType.HandLeft;
  154. case Hand.HandRight:
  155. return OVRMesh.MeshType.HandRight;
  156. default:
  157. return OVRMesh.MeshType.None;
  158. }
  159. }
  160. OVRMeshRenderer.MeshRendererData OVRMeshRenderer.IOVRMeshRendererDataProvider.GetMeshRendererData()
  161. {
  162. var data = new OVRMeshRenderer.MeshRendererData();
  163. data.IsDataValid = _isInitialized;
  164. if (_isInitialized)
  165. {
  166. data.IsDataHighConfidence = IsTracked && HandConfidence == TrackingConfidence.High;
  167. }
  168. return data;
  169. }
  170. }