1using System.Collections;
2using System.Collections.Generic;
4using Microsoft.Azure.Kinect.BodyTracking;
11 [Header(
"References")]
12 [SerializeField]
private Animator avatarAnimator;
13 [SerializeField]
private Transform avatarRoot;
14 [SerializeField]
private Transform skeletonRootJoint;
18 private Dictionary<JointId, Quaternion> absoluteOffsetMap;
19 private Dictionary<JointId, Quaternion> relativeOffsetMap;
31 private static HumanBodyBones MapKinectJoint(JointId joint) {
33 case JointId.Pelvis:
return HumanBodyBones.Hips;
34 case JointId.SpineNavel:
return HumanBodyBones.Spine;
35 case JointId.SpineChest:
return HumanBodyBones.Chest;
36 case JointId.Neck:
return HumanBodyBones.Neck;
37 case JointId.Head:
return HumanBodyBones.Head;
38 case JointId.HipLeft:
return HumanBodyBones.LeftUpperLeg;
39 case JointId.KneeLeft:
return HumanBodyBones.LeftLowerLeg;
40 case JointId.AnkleLeft:
return HumanBodyBones.LeftFoot;
41 case JointId.FootLeft:
return HumanBodyBones.LeftToes;
42 case JointId.HipRight:
return HumanBodyBones.RightUpperLeg;
43 case JointId.KneeRight:
return HumanBodyBones.RightLowerLeg;
44 case JointId.AnkleRight:
return HumanBodyBones.RightFoot;
45 case JointId.FootRight:
return HumanBodyBones.RightToes;
46 case JointId.ClavicleLeft:
return HumanBodyBones.LeftShoulder;
47 case JointId.ShoulderLeft:
return HumanBodyBones.LeftUpperArm;
48 case JointId.ElbowLeft:
return HumanBodyBones.LeftLowerArm;
49 case JointId.WristLeft:
return HumanBodyBones.LeftHand;
50 case JointId.ClavicleRight:
return HumanBodyBones.RightShoulder;
51 case JointId.ShoulderRight:
return HumanBodyBones.RightUpperArm;
52 case JointId.ElbowRight:
return HumanBodyBones.RightLowerArm;
53 case JointId.WristRight:
return HumanBodyBones.RightHand;
54 default:
return HumanBodyBones.LastBone;
61 private static SkeletonBone GetSkeletonBone(Animator animator,
string boneName) {
63 string cloneName = boneName +
"(Clone)";
64 foreach (SkeletonBone sb
in animator.avatar.humanDescription.skeleton) {
65 if (sb.name == boneName || sb.name == cloneName) {
66 return animator.avatar.humanDescription.skeleton[count];
70 return new SkeletonBone();
73 private void InitRelativeOffsetMap() {
74 relativeOffsetMap =
new Dictionary<JointId, Quaternion>();
75 for (
int i = 0; i < (int)JointId.Count; i++) {
76 HumanBodyBones hbb = MapKinectJoint((JointId)i);
77 if (hbb != HumanBodyBones.LastBone) {
78 Transform boneTransform = avatarAnimator.GetBoneTransform(hbb);
79 relativeOffsetMap[(JointId)i] = GetSkeletonBone(avatarAnimator, boneTransform.name).rotation;
84 private void InitAbsoluteOffsetMap() {
86 absoluteOffsetMap =
new Dictionary<JointId, Quaternion>();
87 for (
int i = 0; i < (int)JointId.Count; i++) {
88 HumanBodyBones hbb = MapKinectJoint((JointId)i);
89 if (hbb != HumanBodyBones.LastBone) {
90 Transform boneTransform = avatarAnimator.GetBoneTransform(hbb);
91 Quaternion absOffset = GetSkeletonBone(avatarAnimator, boneTransform.name).rotation;
93 while (!ReferenceEquals(boneTransform, avatarRoot)) {
94 boneTransform = boneTransform.parent;
95 absOffset = GetSkeletonBone(avatarAnimator, boneTransform.name).rotation * absOffset;
97 absoluteOffsetMap[(JointId)i] = absOffset;
102 private void Start() {
103 InitRelativeOffsetMap();
104 InitAbsoluteOffsetMap();
107 private void LateUpdate() {
108 for (
int j = 0; j < (int)JointId.Count; j++) {
109 if (MapKinectJoint((JointId)j) != HumanBodyBones.LastBone && absoluteOffsetMap.ContainsKey((JointId)j)) {
110 Quaternion absOffset = absoluteOffsetMap[(JointId)j];
111 Transform finalJoint = avatarAnimator.GetBoneTransform(MapKinectJoint((JointId)j));
112 if (j == (
int)JointId.Pelvis) {
void SetSkeletonRootJoint(Transform skeletonRoot)
Transform GetAvatarRoot()
Transform GetSkeletonRootJoint()
Animator GetAvatarAnimator()
void SetBodyTracker(BodyTracker bodyTracker)
BodyTracker GetBodyTracker()
BodyTracker represents the BodyData from the ORBBEC sensor as a skeleton. Important: BodyTracker pref...
Quaternion GetAbsoluteJointRotation(JointId jointId)
Get the rotation of a joint, relative to the root.
Vector3 GetJointPosition(JointId jointId)
Quaternion GetRelativeJointRotation(JointId jointId)
Get the rotation of a joint, relative to its parent.