PXR_Hand.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*******************************************************************************
  2. Copyright © 2015-2022 PICO Technology Co., Ltd.All rights reserved.
  3. NOTICE:All information contained herein is, and remains the property of
  4. PICO Technology Co., Ltd. The intellectual and technical concepts
  5. contained herein are proprietary to PICO Technology Co., Ltd. and may be
  6. covered by patents, patents in process, and are protected by trade secret or
  7. copyright law. Dissemination of this information or reproduction of this
  8. material is strictly forbidden unless prior written permission is obtained from
  9. PICO Technology Co., Ltd.
  10. *******************************************************************************/
  11. using System.Collections.Generic;
  12. using UnityEngine;
  13. using Unity.XR.PXR;
  14. public class PXR_Hand : MonoBehaviour
  15. {
  16. public HandType handType;
  17. [HideInInspector]
  18. public List<Transform> handJoints = new List<Transform>(new Transform[(int)HandJoint.JointMax]);
  19. public bool Computed { get; private set; }
  20. public Posef RayPose { get; private set; }
  21. public bool RayValid { get; private set; }
  22. public bool Pinch { get; private set; }
  23. public float PinchStrength { get; private set; }
  24. private HandJointLocations handJointLocations = new HandJointLocations();
  25. private HandAimState aimState = new HandAimState();
  26. [SerializeField]
  27. private Transform rayPose;
  28. [SerializeField]
  29. private GameObject defaultRay;
  30. private SkinnedMeshRenderer[] touchRenders;
  31. private void Start()
  32. {
  33. if (defaultRay != null)
  34. {
  35. touchRenders = defaultRay.GetComponentsInChildren<SkinnedMeshRenderer>();
  36. }
  37. }
  38. private void Update()
  39. {
  40. UpdateHandJoints();
  41. UpdateAimState();
  42. UpdateRayPose();
  43. }
  44. private void UpdateHandJoints()
  45. {
  46. if (PXR_HandTracking.GetJointLocations(handType, ref handJointLocations))
  47. {
  48. if (handJointLocations.isActive == 0) return;
  49. transform.localScale = Vector3.one*handJointLocations.handScale;
  50. for (int i = 0; i < handJoints.Count; ++i)
  51. {
  52. if (handJoints[i] == null) continue;
  53. if (i == (int)HandJoint.JointWrist)
  54. {
  55. #if UNITY_2021_3_OR_NEWER
  56. handJoints[i].SetLocalPositionAndRotation(handJointLocations.jointLocations[i].pose.Position.ToVector3(), handJointLocations.jointLocations[i].pose.Orientation.ToQuat());
  57. #else
  58. handJoints[i].localPosition = handJointLocations.jointLocations[i].pose.Position.ToVector3();
  59. handJoints[i].localRotation = handJointLocations.jointLocations[i].pose.Orientation.ToQuat();
  60. #endif
  61. }
  62. else
  63. {
  64. Pose parentPose = Pose.identity;
  65. if (i == (int)HandJoint.JointPalm ||
  66. i == (int)HandJoint.JointThumbMetacarpal ||
  67. i == (int)HandJoint.JointIndexMetacarpal ||
  68. i == (int)HandJoint.JointMiddleMetacarpal ||
  69. i == (int)HandJoint.JointRingMetacarpal ||
  70. i == (int)HandJoint.JointLittleMetacarpal)
  71. {
  72. parentPose = new Pose(handJointLocations.jointLocations[1].pose.Position.ToVector3(), handJointLocations.jointLocations[1].pose.Orientation.ToQuat());
  73. }
  74. else
  75. {
  76. parentPose = new Pose(handJointLocations.jointLocations[i-1].pose.Position.ToVector3(), handJointLocations.jointLocations[i-1].pose.Orientation.ToQuat());
  77. }
  78. var inverseParentRotation = Quaternion.Inverse(parentPose.rotation);
  79. handJoints[i].localRotation = inverseParentRotation * handJointLocations.jointLocations[i].pose.Orientation.ToQuat();
  80. }
  81. }
  82. }
  83. }
  84. private void UpdateAimState()
  85. {
  86. if (PXR_HandTracking.GetAimState(handType, ref aimState))
  87. {
  88. Computed = (aimState.aimStatus&HandAimStatus.AimComputed) != 0;
  89. RayPose = aimState.aimRayPose;
  90. RayValid = (aimState.aimStatus&HandAimStatus.AimRayValid) != 0;
  91. Pinch = (aimState.aimStatus&HandAimStatus.AimRayTouched) != 0;
  92. PinchStrength = aimState.touchStrengthRay;
  93. }
  94. }
  95. private void UpdateRayPose()
  96. {
  97. if (rayPose == null) return;
  98. if (RayValid)
  99. {
  100. rayPose.gameObject.SetActive(true);
  101. #if UNITY_2021_3_OR_NEWER
  102. rayPose.SetLocalPositionAndRotation(RayPose.Position.ToVector3(), RayPose.Orientation.ToQuat());
  103. #else
  104. rayPose.localPosition = RayPose.Position.ToVector3();
  105. rayPose.localRotation = RayPose.Orientation.ToQuat();
  106. #endif
  107. if (defaultRay != null)
  108. {
  109. foreach (var touchRender in touchRenders)
  110. {
  111. touchRender.SetBlendShapeWeight(0, aimState.touchStrengthRay*100);
  112. }
  113. }
  114. }
  115. else
  116. {
  117. rayPose.gameObject.SetActive(false);
  118. }
  119. }
  120. }
粤ICP备19079148号