| |
| using System; |
| using System.Collections.Generic; |
| using Meta.XR.Samples; |
| using Oculus.Interaction; |
| using Oculus.Interaction.Input; |
| using Oculus.Movement.AnimationRigging; |
| using UnityEngine; |
|
|
| namespace NorthStar |
| { |
| |
| |
| |
| [MetaCodeSample("NorthStar")] |
| [CreateAssetMenu(fileName = "Synthetic Hand", menuName = "Data/Retargeting Processors/Synthetic Hand", |
| order = 1)] |
| public class SyntheticHandRetargetingProcessor : RetargetingProcessor |
| { |
| public struct FingerBoneMapping |
| { |
| public HumanBodyBones HumanBone; |
| public HandJointId HandJoint; |
| } |
|
|
| public class HandMappingData |
| { |
| public List<FingerBoneMapping> Mapping; |
| } |
|
|
| private readonly Tuple<HumanBodyBones, HandJointId>[] m_leftHandBonePairs = |
| { |
| new (HumanBodyBones.LeftThumbProximal, HandJointId.HandThumb1), |
| new (HumanBodyBones.LeftThumbIntermediate, HandJointId.HandThumb2), |
| new (HumanBodyBones.LeftThumbDistal, HandJointId.HandThumb3), |
| new (HumanBodyBones.LeftIndexProximal, HandJointId.HandIndex1), |
| new (HumanBodyBones.LeftIndexIntermediate, HandJointId.HandIndex2), |
| new (HumanBodyBones.LeftIndexDistal, HandJointId.HandIndex3), |
| new (HumanBodyBones.LeftMiddleProximal, HandJointId.HandMiddle1), |
| new (HumanBodyBones.LeftMiddleIntermediate, HandJointId.HandMiddle2), |
| new (HumanBodyBones.LeftMiddleDistal, HandJointId.HandMiddle3), |
| new (HumanBodyBones.LeftRingProximal, HandJointId.HandRing1), |
| new (HumanBodyBones.LeftRingIntermediate, HandJointId.HandRing2), |
| new (HumanBodyBones.LeftRingDistal, HandJointId.HandRing3), |
| new (HumanBodyBones.LeftLittleProximal, HandJointId.HandPinky1), |
| new (HumanBodyBones.LeftLittleIntermediate, HandJointId.HandPinky2), |
| new (HumanBodyBones.LeftLittleDistal, HandJointId.HandPinky3), |
| }; |
|
|
| private readonly Tuple<HumanBodyBones, HandJointId>[] m_rightHandBonePairs = |
| { |
| new (HumanBodyBones.RightThumbProximal, HandJointId.HandThumb1), |
| new (HumanBodyBones.RightThumbIntermediate, HandJointId.HandThumb2), |
| new (HumanBodyBones.RightThumbDistal, HandJointId.HandThumb3), |
| new (HumanBodyBones.RightIndexProximal, HandJointId.HandIndex1), |
| new (HumanBodyBones.RightIndexIntermediate, HandJointId.HandIndex2), |
| new (HumanBodyBones.RightIndexDistal, HandJointId.HandIndex3), |
| new (HumanBodyBones.RightMiddleProximal, HandJointId.HandMiddle1), |
| new (HumanBodyBones.RightMiddleIntermediate, HandJointId.HandMiddle2), |
| new (HumanBodyBones.RightMiddleDistal, HandJointId.HandMiddle3), |
| new (HumanBodyBones.RightRingProximal, HandJointId.HandRing1), |
| new (HumanBodyBones.RightRingIntermediate, HandJointId.HandRing2), |
| new (HumanBodyBones.RightRingDistal, HandJointId.HandRing3), |
| new (HumanBodyBones.RightLittleProximal, HandJointId.HandPinky1), |
| new (HumanBodyBones.RightLittleIntermediate, HandJointId.HandPinky2), |
| new (HumanBodyBones.RightLittleDistal, HandJointId.HandPinky3), |
| }; |
|
|
| [SerializeField] private HandMappingData m_leftHandData; |
| [SerializeField] private HandMappingData m_rightHandData; |
|
|
| [NonSerialized] public HandVisual LeftHandVisual; |
| [NonSerialized] public HandVisual RightHandVisual; |
|
|
| private void RetargetHand(RetargetingLayer retargetingLayer, HandVisual hand, Tuple<HumanBodyBones, HandJointId>[] handBonePairs) |
| { |
| var left = hand == LeftHandVisual; |
| var animator = retargetingLayer.GetAnimatorTargetSkeleton(); |
| var targetWrist = hand.GetTransformByHandJointId(HandJointId.HandStart); |
| var visualWrist = animator.GetBoneTransform(left ? HumanBodyBones.LeftHand : HumanBodyBones.RightHand); |
|
|
| |
| foreach (var pair in handBonePairs) |
| { |
| var correctionQuaternion = retargetingLayer.GetCorrectionQuaternion(pair.Item1); |
|
|
| var pose = hand.GetJointPose(pair.Item2, Space.World); |
| var t = animator.GetBoneTransform(pair.Item1); |
|
|
| t.position = pose.position - targetWrist.position + visualWrist.position; |
| t.rotation = pose.rotation * correctionQuaternion ?? Quaternion.identity; |
| } |
| } |
|
|
| public override void ProcessRetargetingLayer(RetargetingLayer retargetingLayer, IList<OVRBone> ovrBones) |
| { |
| |
| if (OVRInput.IsControllerConnected(OVRInput.Controller.LHand) || OVRInput.IsControllerConnected(OVRInput.Controller.LTouch)) |
| RetargetHand(retargetingLayer, LeftHandVisual, m_leftHandBonePairs); |
|
|
| if (OVRInput.IsControllerConnected(OVRInput.Controller.RHand) || OVRInput.IsControllerConnected(OVRInput.Controller.RTouch)) |
| RetargetHand(retargetingLayer, RightHandVisual, m_rightHandBonePairs); |
| } |
| } |
| } |
|
|