Exemplo n.º 1
0
        public SkeletonV1 DeepCopy()
        {
            SkeletonV1 n = (SkeletonV1)this.MemberwiseClone();

            n.joints = new Joint[this.joints.Length];
            this.joints.CopyTo(n.joints, 0);
            return(n);
        }
Exemplo n.º 2
0
 public void Add(long videoFrame, ref SkeletonV1 pose)
 {
     if (keys == null)
     {
         keys = new List <long>();
     }
     keys.Add(videoFrame);
     if (poses == null)
     {
         poses = new List <SkeletonV1>();
     }
     poses.Add(pose);
     samples++;
 }
Exemplo n.º 3
0
        public static SkeletonV1 CreateOverrideSkeleton(Animator animator, string name)
        {
            var total = (int)HumanBodyBones.LastBone;
            var sk    = new SkeletonV1();

            sk.variation   = SkeletonV1.Variation.skeletonv1posture;
            sk.name        = name;
            sk.hasSkeleton = true;

            var list = new List <SkeletonV1.Joint>();

            Transform lShoulder    = animator.GetBoneTransform(HumanBodyBones.LeftShoulder);
            Transform rShoulder    = animator.GetBoneTransform(HumanBodyBones.RightShoulder);
            var       distShoulder = lShoulder.position - rShoulder.position;

            for (int i = 0; i < total; i++)
            {
                var joint = animator.GetBoneTransform((HumanBodyBones)i);
                if (joint)
                {
                    var jt = new SkeletonV1.Joint();
                    jt.type = i;
                    jt.pos  = joint.localPosition;
                    jt.rot  = joint.rotation;
                    //package.joints[i] = jt;
                    list.Add(jt);
                }
                else
                {
                    Debug.LogWarningFormat("This avatar doesn't have {0}", (HumanBodyBones)i);
                }
            }

            var root = new SkeletonV1.Joint();

            root.type = -1;
            root.pos  = animator.transform.localPosition;
            root.rot  = animator.transform.rotation;
            list.Add(root);

            sk.upperConf        = 1f;
            sk.lowerConf        = 1f;
            sk.overallConf      = 1f;
            sk.distanceShoulder = distShoulder;
            sk.joints           = list.ToArray();

            return(sk);
        }
Exemplo n.º 4
0
        public static void ApplyOverrideSkeleton(Animator animator, Transform container, SkeletonV1 skeleton)
        {
            for (int i = 0; i < skeleton.joints.Length; i++)
            {
                var j = skeleton.joints[i];

                if (j.type < 0)
                {
                    continue;
                }

                var b = animator.GetBoneTransform((HumanBodyBones)j.type);

                if (b)
                {
                    var rot = j.rot;
                    b.rotation = rot;
                }
                else
                {
                    Debug.LogWarningFormat("Miss bone {0}", (HumanBodyBones)j.type);
                }
            }
        }
Exemplo n.º 5
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);
            }
        }
Exemplo n.º 6
0
        public static SkeletonV1 CreateRelativedSkeleton(Animator animator, SkeletonV1 tpose, string name)
        {
            var sk = new SkeletonV1();
            var upperConf = 0f; var upperNum = 0;
            var lowerConf = 0f; var lowerNum = 0;
            var overallConf = 0f;

            sk.variation   = Chiron.Skeleton.SkeletonV1.Variation.skeletonv1default;
            sk.name        = name;
            sk.hasSkeleton = true;
            sk.joints      = new SkeletonV1.Joint[tpose.joints.Length];

            Transform lShoulder    = animator.GetBoneTransform(HumanBodyBones.LeftShoulder);
            Transform rShoulder    = animator.GetBoneTransform(HumanBodyBones.RightShoulder);
            var       distShoulder = lShoulder.position - rShoulder.position;

            Transform hip = animator.GetBoneTransform(HumanBodyBones.Hips);

            var index = 0;

            /*
             * Length of VNect joint
             * Because VNect bone not always existing in Avatar
             * Meaning you can't get correct length on VNectModel, length of 28 is incorrect
             * So we need to count it base on Avatar, then cross reference with VNectModel
             */
            var vcount = 0;

            foreach (var i in tpose.joints)
            {
                if (i.type == -1)
                {
                    continue;
                }

                var joint = animator.GetBoneTransform((HumanBodyBones)i.type);
                if (joint)
                {
                    var jt = new SkeletonV1.Joint();
                    jt.type = i.type;
                    jt.pos  = joint.localPosition - tpose.GetJoint(i.type).pos;
                    jt.rot  = joint.rotation * Quaternion.Inverse(tpose.GetJoint(i.type).rot);

                    sk.joints[index] = jt;

                    if (IsUpperBody(i.type))
                    {
                        upperConf += 1f;
                        upperNum++;
                    }
                    else if (IsLowerBody(i.type))
                    {
                        lowerConf += 1f;
                        lowerNum++;
                    }
                    overallConf += 1f;

                    vcount++;
                }
                index++;
            }

            var root = new SkeletonV1.Joint();

            root.type = -1;
            root.pos  = animator.transform.localPosition;
            root.rot  = animator.transform.rotation;
            sk.joints[sk.joints.Length - 1] = root;

            sk.upperConf        = upperConf / (float)upperNum;
            sk.lowerConf        = lowerConf / (float)lowerNum;
            sk.overallConf      = overallConf / (float)vcount;
            sk.distanceShoulder = distShoulder;
            return(sk);
        }
Exemplo n.º 7
0
        void OnGUI()
        {
            var o = EditorGUILayout.ObjectField("File", fromFile, typeof(TextAsset), false);

            if (o != null)
            {
                if (fromFile != null &&
                    fromFile != o)
                {
                    log  = string.Empty;
                    raw  = null;
                    type = null;
                }
                fromFile = o as TextAsset;
            }

            if (fromFile && raw == null)
            {
                var header = fromFile.text.ReadFirstLine();
                switch (header)
                {
                case var _ when header.Equals(SkeletonV1.type):
                    log = "This file is already SkeletonV1 type";

                    break;

                case var _ when header.Equals(HumanPoseVideo.type):
                    log = "HumanPoseVideo can't direct convert to SkeletonV1";

                    break;

                default:
                    try
                    {
                        var b = JsonUtility.FromJson <Gesture>(fromFile.text);
                        var c = JsonUtility.FromJson <Serialization <int, TransformLite> >(fromFile.text).ToDictionary();
                        if (string.IsNullOrEmpty(b.gestureName) == false)
                        {
                            log = "Type Gesture need convert via PoseRecognizer, " +
                                  "Please use original PoseRecognizer component which hold those gesture data to convert from";
                        }
                        else if (c.Count > 0)
                        {
                            type = typeof(TransformLite);
                            raw  = c;
                        }
                        break;
                    }
                    catch (System.ArgumentException)
                    {
                        log      = "Not support type";
                        fromFile = null;
                        break;
                    }
                }
            }

            if (string.IsNullOrEmpty(log) == false)
            {
                EditorGUILayout.HelpBox(log, MessageType.Error);
            }

            if (fromFile != null && type != null)
            {
                if (string.IsNullOrEmpty(result.name))
                {
                    if (type == typeof(TransformLite))
                    {
                        var               cast   = (Dictionary <int, TransformLite>)raw;
                        var               keys   = cast.Keys.ToArray();
                        var               values = cast.Values.ToArray();
                        List <Vector3>    pos    = new List <Vector3>();
                        List <Quaternion> rot    = new List <Quaternion>();
                        foreach (var v in values)
                        {
                            pos.Add(v.a);
                            rot.Add(v.b);
                        }
                        result = new SkeletonV1("tpose", keys, pos.ToArray(), rot.ToArray());
                    }
                }

                if (GUILayout.Button(string.Format("Convert {0} to SkeletonV1", type.Name)))
                {
                    var path = EditorUtility.SaveFilePanel(
                        "Save gesture as JSON",
                        "",
                        fromFile.name + "_SKV1.json",
                        "json");

                    if (string.IsNullOrEmpty(path) == false)
                    {
                        var j = JsonUtility.ToJson(result, false);
                        File.WriteAllText(path, j.AddFirstLine(SkeletonV1.type));
                        AssetDatabase.Refresh();
                    }
                }
            }
        }