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.

114 lines
3.0 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(-80)]
  17. public class OVRCustomSkeleton : OVRSkeleton
  18. {
  19. [SerializeField]
  20. private List<Transform> _customBones = new List<Transform>(new Transform[(int)BoneId.Max]);
  21. #if UNITY_EDITOR
  22. private static readonly string[] _fbxBoneNames =
  23. {
  24. "wrist",
  25. "forearm_stub",
  26. "thumb0",
  27. "thumb1",
  28. "thumb2",
  29. "thumb3",
  30. "index1",
  31. "index2",
  32. "index3",
  33. "middle1",
  34. "middle2",
  35. "middle3",
  36. "ring1",
  37. "ring2",
  38. "ring3",
  39. "pinky0",
  40. "pinky1",
  41. "pinky2",
  42. "pinky3"
  43. };
  44. private static readonly string[] _fbxFingerNames =
  45. {
  46. "thumb",
  47. "index",
  48. "middle",
  49. "ring",
  50. "pinky"
  51. };
  52. private static readonly string[] _handPrefix = { "l_", "r_" };
  53. #endif
  54. public List<Transform> CustomBones { get { return _customBones; } }
  55. #if UNITY_EDITOR
  56. public void TryAutoMapBonesByName()
  57. {
  58. BoneId start = GetCurrentStartBoneId();
  59. BoneId end = GetCurrentEndBoneId();
  60. SkeletonType skeletonType = GetSkeletonType();
  61. if (start != BoneId.Invalid && end != BoneId.Invalid)
  62. {
  63. for (int bi = (int)start; bi < (int)end; ++bi)
  64. {
  65. string fbxBoneName = FbxBoneNameFromBoneId(skeletonType, (BoneId)bi);
  66. Transform t = transform.FindChildRecursive(fbxBoneName);
  67. if (t != null)
  68. {
  69. _customBones[(int)bi] = t;
  70. }
  71. }
  72. }
  73. }
  74. private static string FbxBoneNameFromBoneId(SkeletonType skeletonType, BoneId bi)
  75. {
  76. if (bi >= BoneId.Hand_ThumbTip && bi <= BoneId.Hand_PinkyTip)
  77. {
  78. return _handPrefix[(int)skeletonType] + _fbxFingerNames[(int)bi - (int)BoneId.Hand_ThumbTip] + "_finger_tip_marker";
  79. }
  80. else
  81. {
  82. return "b_" + _handPrefix[(int)skeletonType] + _fbxBoneNames[(int)bi];
  83. }
  84. }
  85. #endif
  86. protected override void InitializeBones(OVRPlugin.Skeleton skeleton)
  87. {
  88. _bones = new List<OVRBone>(new OVRBone[skeleton.NumBones]);
  89. Bones = _bones.AsReadOnly();
  90. for (int i = 0; i < skeleton.NumBones; ++i)
  91. {
  92. BoneId id = (BoneId)skeleton.Bones[i].Id;
  93. short parentIdx = skeleton.Bones[i].ParentBoneIndex;
  94. Transform t = _customBones[(int)id];
  95. _bones[i] = new OVRBone(id, parentIdx, t);
  96. }
  97. }
  98. }