OVRSkeleton.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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(-80)]
  17. public class OVRSkeleton : MonoBehaviour
  18. {
  19. public interface IOVRSkeletonDataProvider
  20. {
  21. SkeletonType GetSkeletonType();
  22. SkeletonPoseData GetSkeletonPoseData();
  23. }
  24. public struct SkeletonPoseData
  25. {
  26. public OVRPlugin.Posef RootPose { get; set; }
  27. public float RootScale { get; set; }
  28. public OVRPlugin.Quatf[] BoneRotations { get; set; }
  29. public bool IsDataValid { get; set; }
  30. public bool IsDataHighConfidence { get; set; }
  31. }
  32. public enum SkeletonType
  33. {
  34. None = OVRPlugin.SkeletonType.None,
  35. HandLeft = OVRPlugin.SkeletonType.HandLeft,
  36. HandRight = OVRPlugin.SkeletonType.HandRight,
  37. }
  38. public enum BoneId
  39. {
  40. Invalid = OVRPlugin.BoneId.Invalid,
  41. Hand_Start = OVRPlugin.BoneId.Hand_Start,
  42. Hand_WristRoot = OVRPlugin.BoneId.Hand_WristRoot, // root frame of the hand, where the wrist is located
  43. Hand_ForearmStub = OVRPlugin.BoneId.Hand_ForearmStub, // frame for user's forearm
  44. Hand_Thumb0 = OVRPlugin.BoneId.Hand_Thumb0, // thumb trapezium bone
  45. Hand_Thumb1 = OVRPlugin.BoneId.Hand_Thumb1, // thumb metacarpal bone
  46. Hand_Thumb2 = OVRPlugin.BoneId.Hand_Thumb2, // thumb proximal phalange bone
  47. Hand_Thumb3 = OVRPlugin.BoneId.Hand_Thumb3, // thumb distal phalange bone
  48. Hand_Index1 = OVRPlugin.BoneId.Hand_Index1, // index proximal phalange bone
  49. Hand_Index2 = OVRPlugin.BoneId.Hand_Index2, // index intermediate phalange bone
  50. Hand_Index3 = OVRPlugin.BoneId.Hand_Index3, // index distal phalange bone
  51. Hand_Middle1 = OVRPlugin.BoneId.Hand_Middle1, // middle proximal phalange bone
  52. Hand_Middle2 = OVRPlugin.BoneId.Hand_Middle2, // middle intermediate phalange bone
  53. Hand_Middle3 = OVRPlugin.BoneId.Hand_Middle3, // middle distal phalange bone
  54. Hand_Ring1 = OVRPlugin.BoneId.Hand_Ring1, // ring proximal phalange bone
  55. Hand_Ring2 = OVRPlugin.BoneId.Hand_Ring2, // ring intermediate phalange bone
  56. Hand_Ring3 = OVRPlugin.BoneId.Hand_Ring3, // ring distal phalange bone
  57. Hand_Pinky0 = OVRPlugin.BoneId.Hand_Pinky0, // pinky metacarpal bone
  58. Hand_Pinky1 = OVRPlugin.BoneId.Hand_Pinky1, // pinky proximal phalange bone
  59. Hand_Pinky2 = OVRPlugin.BoneId.Hand_Pinky2, // pinky intermediate phalange bone
  60. Hand_Pinky3 = OVRPlugin.BoneId.Hand_Pinky3, // pinky distal phalange bone
  61. Hand_MaxSkinnable = OVRPlugin.BoneId.Hand_MaxSkinnable,
  62. // Bone tips are position only. They are not used for skinning but are useful for hit-testing.
  63. // NOTE: Hand_ThumbTip == Hand_MaxSkinnable since the extended tips need to be contiguous
  64. Hand_ThumbTip = OVRPlugin.BoneId.Hand_ThumbTip, // tip of the thumb
  65. Hand_IndexTip = OVRPlugin.BoneId.Hand_IndexTip, // tip of the index finger
  66. Hand_MiddleTip = OVRPlugin.BoneId.Hand_MiddleTip, // tip of the middle finger
  67. Hand_RingTip = OVRPlugin.BoneId.Hand_RingTip, // tip of the ring finger
  68. Hand_PinkyTip = OVRPlugin.BoneId.Hand_PinkyTip, // tip of the pinky
  69. Hand_End = OVRPlugin.BoneId.Hand_End,
  70. // add new bones here
  71. Max = OVRPlugin.BoneId.Max
  72. }
  73. [SerializeField]
  74. private SkeletonType _skeletonType = SkeletonType.None;
  75. [SerializeField]
  76. private IOVRSkeletonDataProvider _dataProvider;
  77. private bool _isInitialized;
  78. [SerializeField]
  79. private bool _updateRootPose = false;
  80. [SerializeField]
  81. private bool _updateRootScale = false;
  82. [SerializeField]
  83. private bool _enablePhysicsCapsules = false;
  84. private GameObject _bonesGO;
  85. private GameObject _bindPosesGO;
  86. private GameObject _capsulesGO;
  87. private List<OVRBone> _bones;
  88. private List<OVRBone> _bindPoses;
  89. private List<OVRBoneCapsule> _capsules;
  90. public IList<OVRBone> Bones { get; private set; }
  91. public IList<OVRBone> BindPoses { get; private set; }
  92. public IList<OVRBoneCapsule> Capsules { get; private set; }
  93. private void Awake()
  94. {
  95. if (_dataProvider == null)
  96. {
  97. _dataProvider = GetComponent<IOVRSkeletonDataProvider>();
  98. }
  99. _bones = new List<OVRBone>();
  100. Bones = _bones.AsReadOnly();
  101. _bindPoses = new List<OVRBone>();
  102. BindPoses = _bindPoses.AsReadOnly();
  103. _capsules = new List<OVRBoneCapsule>();
  104. Capsules = _capsules.AsReadOnly();
  105. if (_dataProvider != null)
  106. {
  107. _skeletonType = _dataProvider.GetSkeletonType();
  108. }
  109. }
  110. private void Start()
  111. {
  112. if (_skeletonType != SkeletonType.None)
  113. {
  114. Initialize();
  115. }
  116. }
  117. private void Initialize()
  118. {
  119. var skeleton = new OVRPlugin.Skeleton();
  120. if (OVRPlugin.GetSkeleton((OVRPlugin.SkeletonType)_skeletonType, out skeleton))
  121. {
  122. if (!_bonesGO)
  123. {
  124. _bonesGO = new GameObject("Bones");
  125. _bonesGO.transform.SetParent(transform, false);
  126. _bonesGO.transform.localPosition = Vector3.zero;
  127. _bonesGO.transform.localRotation = Quaternion.identity;
  128. }
  129. if (!_bindPosesGO)
  130. {
  131. _bindPosesGO = new GameObject("BindPoses");
  132. _bindPosesGO.transform.SetParent(transform, false);
  133. _bindPosesGO.transform.localPosition = Vector3.zero;
  134. _bindPosesGO.transform.localRotation = Quaternion.identity;
  135. }
  136. if (_enablePhysicsCapsules)
  137. {
  138. if (!_capsulesGO)
  139. {
  140. _capsulesGO = new GameObject("Capsules");
  141. _capsulesGO.transform.SetParent(transform, false);
  142. _capsulesGO.transform.localPosition = Vector3.zero;
  143. _capsulesGO.transform.localRotation = Quaternion.identity;
  144. }
  145. }
  146. _bones = new List<OVRBone>(new OVRBone[skeleton.NumBones]);
  147. Bones = _bones.AsReadOnly();
  148. _bindPoses = new List<OVRBone>(new OVRBone[skeleton.NumBones]);
  149. BindPoses = _bindPoses.AsReadOnly();
  150. // pre-populate bones list before attempting to apply bone hierarchy
  151. for (int i = 0; i < skeleton.NumBones; ++i)
  152. {
  153. BoneId id = (OVRSkeleton.BoneId)skeleton.Bones[i].Id;
  154. short parentIdx = skeleton.Bones[i].ParentBoneIndex;
  155. Vector3 pos = skeleton.Bones[i].Pose.Position.FromFlippedZVector3f();
  156. Quaternion rot = skeleton.Bones[i].Pose.Orientation.FromFlippedZQuatf();
  157. var boneGO = new GameObject(id.ToString());
  158. boneGO.transform.localPosition = pos;
  159. boneGO.transform.localRotation = rot;
  160. _bones[i] = new OVRBone(id, parentIdx, boneGO.transform);
  161. var bindPoseGO = new GameObject(id.ToString());
  162. bindPoseGO.transform.localPosition = pos;
  163. bindPoseGO.transform.localRotation = rot;
  164. _bindPoses[i] = new OVRBone(id, parentIdx, bindPoseGO.transform);
  165. }
  166. for (int i = 0; i < skeleton.NumBones; ++i)
  167. {
  168. if (((OVRPlugin.BoneId)skeleton.Bones[i].ParentBoneIndex) == OVRPlugin.BoneId.Invalid)
  169. {
  170. _bones[i].Transform.SetParent(_bonesGO.transform, false);
  171. _bindPoses[i].Transform.SetParent(_bindPosesGO.transform, false);
  172. }
  173. else
  174. {
  175. _bones[i].Transform.SetParent(_bones[_bones[i].ParentBoneIndex].Transform, false);
  176. _bindPoses[i].Transform.SetParent(_bindPoses[_bones[i].ParentBoneIndex].Transform, false);
  177. }
  178. }
  179. if (_enablePhysicsCapsules)
  180. {
  181. _capsules = new List<OVRBoneCapsule>(new OVRBoneCapsule[skeleton.NumBoneCapsules]);
  182. Capsules = _capsules.AsReadOnly();
  183. for (int i = 0; i < skeleton.NumBoneCapsules; ++i)
  184. {
  185. var capsule = skeleton.BoneCapsules[i];
  186. Transform bone = Bones[capsule.BoneIndex].Transform;
  187. var capsuleRigidBodyGO = new GameObject((_bones[capsule.BoneIndex].Id).ToString() + "_CapsuleRigidBody");
  188. capsuleRigidBodyGO.transform.SetParent(_capsulesGO.transform, false);
  189. capsuleRigidBodyGO.transform.localPosition = bone.position;
  190. capsuleRigidBodyGO.transform.localRotation = bone.rotation;
  191. var capsuleRigidBody = capsuleRigidBodyGO.AddComponent<Rigidbody>();
  192. capsuleRigidBody.mass = 1.0f;
  193. capsuleRigidBody.isKinematic = true;
  194. capsuleRigidBody.useGravity = false;
  195. #if UNITY_2018_3_OR_NEWER
  196. capsuleRigidBody.collisionDetectionMode = CollisionDetectionMode.ContinuousSpeculative;
  197. #else
  198. capsuleRigidBody.collisionDetectionMode = CollisionDetectionMode.Continuous;
  199. #endif
  200. var capsuleColliderGO = new GameObject((_bones[capsule.BoneIndex].Id).ToString() + "_CapsuleCollider");
  201. capsuleColliderGO.transform.SetParent(capsuleRigidBodyGO.transform, false);
  202. var capsuleCollider = capsuleColliderGO.AddComponent<CapsuleCollider>();
  203. var p0 = capsule.Points[0].FromFlippedZVector3f();
  204. var p1 = capsule.Points[1].FromFlippedZVector3f();
  205. var delta = p1 - p0;
  206. var mag = delta.magnitude;
  207. var rot = Quaternion.FromToRotation(capsuleRigidBodyGO.transform.localRotation * Vector3.right, delta);
  208. capsuleCollider.radius = capsule.Radius;
  209. capsuleCollider.height = mag + capsule.Radius * 2.0f;
  210. capsuleCollider.isTrigger = false;
  211. capsuleCollider.direction = 0;
  212. capsuleColliderGO.transform.localPosition = p0;
  213. capsuleColliderGO.transform.localRotation = rot;
  214. capsuleCollider.center = Vector3.right * mag * 0.5f;
  215. _capsules[i] = new OVRBoneCapsule(capsule.BoneIndex, capsuleRigidBody, capsuleCollider);
  216. }
  217. }
  218. _isInitialized = true;
  219. }
  220. }
  221. private void Update()
  222. {
  223. if (!_isInitialized || _dataProvider == null)
  224. return;
  225. var data = _dataProvider.GetSkeletonPoseData();
  226. if (data.IsDataValid)
  227. {
  228. if (_updateRootPose)
  229. {
  230. transform.localPosition = data.RootPose.Position.FromFlippedZVector3f();
  231. transform.localRotation = data.RootPose.Orientation.FromFlippedZQuatf();
  232. }
  233. if (_updateRootScale)
  234. {
  235. transform.localScale = new Vector3(data.RootScale, data.RootScale, data.RootScale);
  236. }
  237. for (var i = 0; i < _bones.Count; ++i)
  238. {
  239. _bones[i].Transform.localRotation = data.BoneRotations[i].FromFlippedZQuatf();
  240. }
  241. }
  242. }
  243. private void FixedUpdate()
  244. {
  245. if (!_isInitialized || _dataProvider == null)
  246. return;
  247. Update();
  248. if (_enablePhysicsCapsules)
  249. {
  250. var data = _dataProvider.GetSkeletonPoseData();
  251. for (int i = 0; i < _capsules.Count; ++i)
  252. {
  253. OVRBoneCapsule capsule = _capsules[i];
  254. var capsuleGO = capsule.CapsuleRigidbody.gameObject;
  255. if (data.IsDataValid && data.IsDataHighConfidence)
  256. {
  257. Transform bone = _bones[(int)capsule.BoneIndex].Transform;
  258. if (capsuleGO.activeSelf)
  259. {
  260. capsule.CapsuleRigidbody.MovePosition(bone.position);
  261. capsule.CapsuleRigidbody.MoveRotation(bone.rotation);
  262. }
  263. else
  264. {
  265. capsuleGO.SetActive(true);
  266. capsule.CapsuleRigidbody.position = bone.position;
  267. capsule.CapsuleRigidbody.rotation = bone.rotation;
  268. }
  269. }
  270. else
  271. {
  272. if (capsuleGO.activeSelf)
  273. {
  274. capsuleGO.SetActive(false);
  275. }
  276. }
  277. }
  278. }
  279. }
  280. public BoneId GetCurrentStartBoneId()
  281. {
  282. switch (_skeletonType)
  283. {
  284. case SkeletonType.HandLeft:
  285. case SkeletonType.HandRight:
  286. return BoneId.Hand_Start;
  287. case SkeletonType.None:
  288. default:
  289. return BoneId.Invalid;
  290. }
  291. }
  292. public BoneId GetCurrentEndBoneId()
  293. {
  294. switch (_skeletonType)
  295. {
  296. case SkeletonType.HandLeft:
  297. case SkeletonType.HandRight:
  298. return BoneId.Hand_End;
  299. case SkeletonType.None:
  300. default:
  301. return BoneId.Invalid;
  302. }
  303. }
  304. private BoneId GetCurrentMaxSkinnableBoneId()
  305. {
  306. switch (_skeletonType)
  307. {
  308. case SkeletonType.HandLeft:
  309. case SkeletonType.HandRight:
  310. return BoneId.Hand_MaxSkinnable;
  311. case SkeletonType.None:
  312. default:
  313. return BoneId.Invalid;
  314. }
  315. }
  316. public int GetCurrentNumBones()
  317. {
  318. switch (_skeletonType)
  319. {
  320. case SkeletonType.HandLeft:
  321. case SkeletonType.HandRight:
  322. return GetCurrentEndBoneId() - GetCurrentStartBoneId();
  323. case SkeletonType.None:
  324. default:
  325. return 0;
  326. }
  327. }
  328. public int GetCurrentNumSkinnableBones()
  329. {
  330. switch (_skeletonType)
  331. {
  332. case SkeletonType.HandLeft:
  333. case SkeletonType.HandRight:
  334. return GetCurrentMaxSkinnableBoneId() - GetCurrentStartBoneId();
  335. case SkeletonType.None:
  336. default:
  337. return 0;
  338. }
  339. }
  340. }
  341. public class OVRBone
  342. {
  343. public OVRSkeleton.BoneId Id { get; private set; }
  344. public short ParentBoneIndex { get; private set; }
  345. public Transform Transform { get; private set; }
  346. public OVRBone(OVRSkeleton.BoneId id, short parentBoneIndex, Transform trans)
  347. {
  348. Id = id;
  349. ParentBoneIndex = parentBoneIndex;
  350. Transform = trans;
  351. }
  352. }
  353. public class OVRBoneCapsule
  354. {
  355. public short BoneIndex { get; private set; }
  356. public Rigidbody CapsuleRigidbody { get; private set; }
  357. public CapsuleCollider CapsuleCollider { get; private set; }
  358. public OVRBoneCapsule(short boneIndex, Rigidbody capsuleRigidBody, CapsuleCollider capsuleCollider)
  359. {
  360. BoneIndex = boneIndex;
  361. CapsuleRigidbody = capsuleRigidBody;
  362. CapsuleCollider = capsuleCollider;
  363. }
  364. }