Exemple #1
0
        public static void ApplyRelativedSkeleton(ref SkeletonV1 targetPose, Animator animator, Transform container, SkeletonV1 tpose,
                                                  JointMask mask, float deltaTime)
        {
            for (int i = 0; i < targetPose.joints.Length - 1; i++)
            {
                var j    = targetPose.joints[i];
                var bone = animator.GetBoneTransform((HumanBodyBones)j.type);
                if (bone == null)
                {
                    //Debug.LogFormat("Model {0} doesn't have bone {1}", animator.gameObject.name, (HumanBodyBones)j.type);
                    continue;
                }

                /*
                 * SKIP If TPoseRot doesn't contain the joint
                 * Means we don't have startup data for that bone
                 * We'll get incorrect rotation anyway
                 */
                if (tpose.HasJoint(j.type) == false)
                {
                    continue;
                }

                var def = tpose.GetJoint(j.type);

                /*
                 * If mask out UpperBody
                 */
                if (mask.HasFlag(JointMask.UpperBody) == false)
                {
                    if (Chiron.Skeleton.Utility.IsUpperBody((HumanBodyBones)j.type))
                    {
                        continue;
                    }
                }

                /*
                 * If mask out LowerBody
                 */
                if (mask.HasFlag(JointMask.LowerBody) == false)
                {
                    if (Chiron.Skeleton.Utility.IsLowerBody((HumanBodyBones)j.type))
                    {
                        continue;
                    }
                }

                /*
                 * If mask out Hip
                 */
                if (mask.HasFlag(JointMask.Hip) == false)
                {
                    if (Chiron.Skeleton.Utility.IsHip((HumanBodyBones)j.type))
                    {
                        continue;
                    }
                }

                var t   = animator.transform;
                var rlt = t ? Quaternion.Inverse(t.rotation) * j.rot :
                          j.rot;
                var rot = rlt * def.rot;
                //rot.x *= flip.x;
                //rot.y *= flip.y;
                //rot.z *= flip.z;
                //rot.w *= flip.w;

                var pos = j.pos + def.pos;
                //bone.rotation = Quaternion.Lerp(bone.rotation, rot, normalize);
                bone.rotation = Quaternion.Lerp(bone.rotation, rot, deltaTime);

                var posDiff = pos - bone.localPosition;
                bone.localPosition = Vector3.Lerp(bone.localPosition, pos, deltaTime);

                //Guessing root transform
                var ch = container;
                ch.localPosition = Vector3.Lerp(ch.localPosition, targetPose.centerOffset, deltaTime);
            }
        }