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.

221 lines
6.5 KiB

  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. Max = OVRPlugin.HandFinger.Max,
  37. }
  38. public enum TrackingConfidence
  39. {
  40. Low = OVRPlugin.TrackingConfidence.Low,
  41. High = OVRPlugin.TrackingConfidence.High
  42. }
  43. [SerializeField]
  44. private Hand HandType = Hand.None;
  45. [SerializeField]
  46. private Transform _pointerPoseRoot = null;
  47. private GameObject _pointerPoseGO;
  48. private OVRPlugin.HandState _handState = new OVRPlugin.HandState();
  49. public bool IsDataValid { get; private set; }
  50. public bool IsDataHighConfidence { get; private set; }
  51. public bool IsTracked { get; private set; }
  52. public bool IsSystemGestureInProgress { get; private set; }
  53. public bool IsPointerPoseValid { get; private set; }
  54. public Transform PointerPose { get; private set; }
  55. public float HandScale { get; private set; }
  56. public TrackingConfidence HandConfidence { get; private set; }
  57. private void Awake()
  58. {
  59. _pointerPoseGO = new GameObject();
  60. PointerPose = _pointerPoseGO.transform;
  61. if (_pointerPoseRoot != null)
  62. {
  63. PointerPose.SetParent(_pointerPoseRoot, false);
  64. }
  65. GetHandState(OVRPlugin.Step.Render);
  66. }
  67. private void Update()
  68. {
  69. GetHandState(OVRPlugin.Step.Render);
  70. }
  71. private void FixedUpdate()
  72. {
  73. GetHandState(OVRPlugin.Step.Physics);
  74. }
  75. private void GetHandState(OVRPlugin.Step step)
  76. {
  77. if (OVRPlugin.GetHandState(step, (OVRPlugin.Hand)HandType, ref _handState))
  78. {
  79. IsTracked = (_handState.Status & OVRPlugin.HandStatus.HandTracked) != 0;
  80. IsSystemGestureInProgress = (_handState.Status & OVRPlugin.HandStatus.SystemGestureInProgress) != 0;
  81. IsPointerPoseValid = (_handState.Status & OVRPlugin.HandStatus.InputStateValid) != 0;
  82. PointerPose.localPosition = _handState.PointerPose.Position.FromFlippedZVector3f();
  83. PointerPose.localRotation = _handState.PointerPose.Orientation.FromFlippedZQuatf();
  84. HandScale = _handState.HandScale;
  85. HandConfidence = (TrackingConfidence)_handState.HandConfidence;
  86. IsDataValid = true;
  87. IsDataHighConfidence = IsTracked && HandConfidence == TrackingConfidence.High;
  88. }
  89. else
  90. {
  91. IsTracked = false;
  92. IsSystemGestureInProgress = false;
  93. IsPointerPoseValid = false;
  94. PointerPose.localPosition = Vector3.zero;
  95. PointerPose.localRotation = Quaternion.identity;
  96. HandScale = 1.0f;
  97. HandConfidence = TrackingConfidence.Low;
  98. IsDataValid = false;
  99. IsDataHighConfidence = false;
  100. }
  101. }
  102. public bool GetFingerIsPinching(HandFinger finger)
  103. {
  104. return IsDataValid && (((int)_handState.Pinches & (1 << (int)finger)) != 0);
  105. }
  106. public float GetFingerPinchStrength(HandFinger finger)
  107. {
  108. if (IsDataValid
  109. && _handState.PinchStrength != null
  110. && _handState.PinchStrength.Length == (int)OVRPlugin.HandFinger.Max)
  111. {
  112. return _handState.PinchStrength[(int)finger];
  113. }
  114. return 0.0f;
  115. }
  116. public TrackingConfidence GetFingerConfidence(HandFinger finger)
  117. {
  118. if (IsDataValid
  119. && _handState.FingerConfidences != null
  120. && _handState.FingerConfidences.Length == (int)OVRPlugin.HandFinger.Max)
  121. {
  122. return (TrackingConfidence)_handState.FingerConfidences[(int)finger];
  123. }
  124. return TrackingConfidence.Low;
  125. }
  126. OVRSkeleton.SkeletonType OVRSkeleton.IOVRSkeletonDataProvider.GetSkeletonType()
  127. {
  128. switch (HandType)
  129. {
  130. case Hand.HandLeft:
  131. return OVRSkeleton.SkeletonType.HandLeft;
  132. case Hand.HandRight:
  133. return OVRSkeleton.SkeletonType.HandRight;
  134. case Hand.None:
  135. default:
  136. return OVRSkeleton.SkeletonType.None;
  137. }
  138. }
  139. OVRSkeleton.SkeletonPoseData OVRSkeleton.IOVRSkeletonDataProvider.GetSkeletonPoseData()
  140. {
  141. var data = new OVRSkeleton.SkeletonPoseData();
  142. data.IsDataValid = IsDataValid;
  143. if (IsDataValid)
  144. {
  145. data.RootPose = _handState.RootPose;
  146. data.RootScale = _handState.HandScale;
  147. data.BoneRotations = _handState.BoneRotations;
  148. data.IsDataHighConfidence = IsTracked && HandConfidence == TrackingConfidence.High;
  149. }
  150. return data;
  151. }
  152. OVRSkeletonRenderer.SkeletonRendererData OVRSkeletonRenderer.IOVRSkeletonRendererDataProvider.GetSkeletonRendererData()
  153. {
  154. var data = new OVRSkeletonRenderer.SkeletonRendererData();
  155. data.IsDataValid = IsDataValid;
  156. if (IsDataValid)
  157. {
  158. data.RootScale = _handState.HandScale;
  159. data.IsDataHighConfidence = IsTracked && HandConfidence == TrackingConfidence.High;
  160. data.ShouldUseSystemGestureMaterial = IsSystemGestureInProgress;
  161. }
  162. return data;
  163. }
  164. OVRMesh.MeshType OVRMesh.IOVRMeshDataProvider.GetMeshType()
  165. {
  166. switch (HandType)
  167. {
  168. case Hand.None:
  169. return OVRMesh.MeshType.None;
  170. case Hand.HandLeft:
  171. return OVRMesh.MeshType.HandLeft;
  172. case Hand.HandRight:
  173. return OVRMesh.MeshType.HandRight;
  174. default:
  175. return OVRMesh.MeshType.None;
  176. }
  177. }
  178. OVRMeshRenderer.MeshRendererData OVRMeshRenderer.IOVRMeshRendererDataProvider.GetMeshRendererData()
  179. {
  180. var data = new OVRMeshRenderer.MeshRendererData();
  181. data.IsDataValid = IsDataValid;
  182. if (IsDataValid)
  183. {
  184. data.IsDataHighConfidence = IsTracked && HandConfidence == TrackingConfidence.High;
  185. data.ShouldUseSystemGestureMaterial = IsSystemGestureInProgress;
  186. }
  187. return data;
  188. }
  189. }