public void createDebugSkeleton(SkeletonDesc skeletonDesc)
        {
            JointDesc jointDesc = skeletonDesc.jointDescs[0];

            root     = new MGJoint();
            skeleton = root.createGameObjectfromDesc(jointDesc, skeletonDesc, gameObject, skeletonJointTag);
        }
Example #2
0
        private void applyJointRotation(MGJoint joint, AnimationClip clip)
        {
            Keyframe[] k_qx = new Keyframe[poses.Length];
            Keyframe[] k_qy = new Keyframe[poses.Length];
            Keyframe[] k_qz = new Keyframe[poses.Length];
            Keyframe[] k_qw = new Keyframe[poses.Length];
            int        o    = 0;

            for (int frameIdx = 0; frameIdx < poses.Length; frameIdx++)
            {
                float t = frame_time * frameIdx;
                o = joint.indexInJointSequence * 4 + 3;
                k_qx[frameIdx] = new Keyframe(t, poses[frameIdx][o + 1]);
                k_qy[frameIdx] = new Keyframe(t, poses[frameIdx][o + 2]);
                k_qz[frameIdx] = new Keyframe(t, poses[frameIdx][o + 3]);
                k_qw[frameIdx] = new Keyframe(t, poses[frameIdx][o]);
            }
            AnimationCurve rot_curve_x = new AnimationCurve(k_qx);
            AnimationCurve rot_curve_y = new AnimationCurve(k_qy);
            AnimationCurve rot_curve_z = new AnimationCurve(k_qz);
            AnimationCurve rot_curve_w = new AnimationCurve(k_qw);

            clip.SetCurve(joint.relativePath, typeof(Transform), "localRotation.x", rot_curve_x);
            clip.SetCurve(joint.relativePath, typeof(Transform), "localRotation.y", rot_curve_y);
            clip.SetCurve(joint.relativePath, typeof(Transform), "localRotation.z", rot_curve_z);
            clip.SetCurve(joint.relativePath, typeof(Transform), "localRotation.w", rot_curve_w);
        }
        /// <summary>
        /// Assigns recursively a joint struct to a transform of the skeleton in the scene based on a mapping defined in a JointDesc instance.
        /// </summary>
        /// <param name="jointDesc">Contains name of the joint in the source and target skeleton</param>
        /// <param name="skeletonDesc">Has joint sequence property that is needed to look up the JointDesc structures of the child joints.</param>
        /// <param name="parent">"Root transform of the skeleton in the scene"</param>
        public void assignJointTransformFromDesc(JointDesc jointDesc, SkeletonDesc skeletonDesc, Transform parent)
        {
            name = jointDesc.name;
            if (jointDesc.targetName != "none")
            {
                transform = targetSearchDF(parent, jointDesc.targetName, 0, false);
                if (transform != null)
                {
                    Debug.Log("Assigned " + name + " to " + transform.name);
                }
                else
                {
                    Debug.Log("Could not assign " + name);
                }
            }
            else // skip joints without target
            {
                Debug.Log("Ignore " + name);
            }


            foreach (string name in jointDesc.children)
            {
                JointDesc childDesc = findJointDesc(skeletonDesc.jointDescs, name);
                if (childDesc == null)
                {
                    continue;
                }

                MGJoint childJoint = new MGJoint();
                childJoint.assignJointTransformFromDesc(childDesc, skeletonDesc, parent);
                children.Add(childJoint);
            }
        }
Example #4
0
 private void applyJointRotations(MGJoint joint, AnimationClip clip)
 {
     applyJointRotation(joint, clip);
     foreach (MGJoint child in joint.children)
     {
         applyJointRotations(child, clip);
     }
 }
        private void setTransform(MGJoint joint, CPose pose)
        {
            var r = pose.rotations[joint.indexInJointSequence];
            var t = pose.translations[joint.indexInJointSequence];

            joint.rotation = new Quaternion(r.x, r.y, r.z, r.w);
            joint.position = new Vector3(t.x, t.y, t.z);
        }
        public GameObject createGameObjectfromDesc(JointDesc jointDesc, SkeletonDesc skeletonDesc, GameObject parent, string skeletonJointTag = "SKELETON_JOINT", GameObject prefab = null, float scale = 1.0f)
        {
            name = jointDesc.name;
            GameObject jointObj = new GameObject();

            jointObj.tag = skeletonJointTag;
            var   q           = new Quaternion();
            float magnitude   = 0;
            float prefabScale = 1f;

            if (prefab != null && jointDesc.children.Length > 0)
            {
                var REF_VECTOR = new Vector3(0, 0, 1);
                foreach (var name in jointDesc.children)
                {
                    JointDesc childDesc = findJointDesc(skeletonDesc.jointDescs, name);
                    if (childDesc == null)
                    {
                        continue;
                    }
                    //int idx = Array.IndexOf(skeletonDesc.jointSequence, name);
                    //JointDesc childDesc = skeletonDesc.jointDescs[idx];
                    var offset = new Vector3(childDesc.offset[0], childDesc.offset[1], childDesc.offset[2]);
                    if (offset.magnitude > 0)
                    {
                        q         = Quaternion.FromToRotation(REF_VECTOR, offset.normalized);
                        magnitude = offset.magnitude;
                        float      heightScale = magnitude / 2;
                        GameObject visObject   = GameObject.Instantiate(prefab, new Vector3(0, 0, 0), q);
                        visObject.transform.localScale = new Vector3(prefabScale, prefabScale, heightScale * prefabScale);
                        visObject.transform.parent     = jointObj.transform;
                    }
                }
            }

            jointObj.transform.parent        = parent.transform;
            jointObj.name                    = name;
            jointObj.transform.localPosition = new Vector3(jointDesc.offset[0] * scale, jointDesc.offset[1] * scale, jointDesc.offset[2] * scale);
            if (jointObj != null)
            {
                transform = jointObj.transform;

                foreach (string name in jointDesc.children)
                {
                    JointDesc childDesc = findJointDesc(skeletonDesc.jointDescs, name);
                    if (childDesc == null)
                    {
                        continue;
                    }

                    MGJoint childJoint = new MGJoint();
                    childJoint.createGameObjectfromDesc(childDesc, skeletonDesc, jointObj, skeletonJointTag, prefab, scale);
                    children.Add(childJoint);
                }
            }
            return(jointObj);
        }
 private void buildMGJoints(MGJoint parentJoint, JointDesc parentJointDescription)
 {
     foreach (string childName in parentJointDescription.children)
     {
         MGJoint   childJoint = createJoint(parentJoint, childName);
         JointDesc childDesc  = skeletonDescription.jointDescs[childJoint.indexInJointSequence];
         buildMGJoints(childJoint, childDesc);
     }
 }
        private MGJoint buildMGJointStructure()
        {
            JointDesc rootJointDescription = skeletonDescription.jointDescs[0];
            MGJoint   root = createJoint(null, rootJointDescription.name);

            root.relativePath = "character_grp/characterScale_grp/animation_controls_grp/body_grp/" + root.relativePath;
            //root.relativePath = "transform1/" + root.relativePath;
            buildMGJoints(root, rootJointDescription);
            return(root);
        }
Example #9
0
        public GameObject createDebugSkeleton(SkeletonDesc skeletonDesc)
        {
            JointDesc jointDesc = skeletonDesc.jointDescs[0];
            var       root      = new MGJoint();

            GameObject skeleton = root.createGameObjectfromDesc(jointDesc, skeletonDesc, gameObject, skeletonJointTag, jointPrefab, scale);
            string     name     = skeletonDesc.name;

            rootJoints[name] = root;
            return(skeleton);
        }
 public void initSkeletonFromExistingCharacter(Transform rootTransform, SkeletonDesc skeletonDesc)
 {
     if (rootTransform != null)
     {
         JointDesc jointDesc = skeletonDesc.jointDescs[0];
         root = new MGJoint();
         root.assignJointTransformFromDesc(jointDesc, skeletonDesc, rootTransform);
     }
     else
     {
         Debug.Log("no root transform defined");
     }
 }
        private MGJoint createJoint(MGJoint parent, string name)
        {
            MGJoint joint = new MGJoint();

            joint.name = name;
            joint.indexInJointSequence = Array.IndexOf(skeletonDescription.jointSequence, joint.name);
            setTransform(joint, skeletonDescription.referencePose);
            joint.parent = parent;
            if (parent != null)
            {
                joint.relativePath = parent.relativePath + "/" + joint.name;
                parent.children.Add(joint);
            }
            else
            {
                joint.relativePath = joint.name;
            }
            return(joint);
        }
Example #12
0
        public AnimationClip ToUnityAnimationClip(MGJoint rootJoint, float scaleFactor = 100)
        {
            AnimationClip clip = new AnimationClip();

            clip.legacy = true;
            clip.name   = "mg clip " + System.DateTime.Now.ToString("yyyyMMddHHmmssffff");
            clipTitle   = clip.name;
            applyRootTranslation(clip, 1.0f / scaleFactor);
            applyJointRotations(rootJoint, clip);

            /*foreach (var e in this.events)
             * {
             *  var evt = new AnimationEvent();
             *  evt.functionName = e.eventName;
             *  evt.objectReferenceParameter = GameObject.Find(e.eventTarget);
             *  evt.time = e.keyframe * this.frame_time;
             *  clip.AddEvent(evt);
             * }*/
            return(clip);
        }
        public void SetupSkeleton()
        {
            if (skeletonDesc == null)
            {
                return;
            }

            if (showMesh)
            {
                if (skeleton != null)
                {
                    DestroySkeleton();
                }
                skeletonDesc.referencePose.ScaleTranslations(scaleFactor);
                initSkeletonFromExistingCharacter(rootTransform, skeletonDesc);

                Debug.Log("init skeleton from existing character");
            }
            else
            {
                skeletonDesc.referencePose.ScaleTranslations(scaleFactor);

                skeleton      = skeletonManager.GetSkeleton(skeletonDesc);
                rootTransform = skeleton.transform;
                root          = skeletonManager.GetRootJoint(skeletonDesc);
                skeletonManager.ShowSkeleton(skeletonDesc.name);
                root.setPose(skeletonDesc.referencePose, indexMap);
                if (skeletonDesc.referencePose.translations.Length > 0 && skeletonDesc.jointDescs.Length > 0)
                {
                    var     t        = skeletonDesc.referencePose.translations[0];
                    var     rootDesc = skeletonDesc.jointDescs[0];
                    Vector3 offset   = new Vector3(rootDesc.offset[0], rootDesc.offset[1], rootDesc.offset[2]);
                    rootTransform.position = t + offset * scaleFactor;
                }
                Debug.Log("generated skeleton");
            }
        }
        public GameObject createGameObjectfromDescOld(JointDesc jointDesc, SkeletonDesc skeletonDesc, GameObject parent, string skeletonJointTag = "SKELETON_JOINT", GameObject prefab = null)
        {
            name = jointDesc.name;
            GameObject jointObj;

            if (prefab == null)
            {
                jointObj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            }
            else
            {
                jointObj = GameObject.Instantiate(prefab, new Vector3(0, 0, 0), Quaternion.identity);
            }
            jointObj.tag = skeletonJointTag;
            var   q         = new Quaternion();
            float magnitude = 0;
            float scale     = 1f;//0.06f;//

            Mesh           mesh        = jointObj.GetComponent <MeshFilter>().mesh;
            List <Vector3> vertices    = new List <Vector3>();
            List <Vector3> normals     = new List <Vector3>();
            List <Vector3> newVertices = new List <Vector3>();
            List <Vector3> newNormals  = new List <Vector3>();

            for (int i = 0; i < mesh.vertices.Length; i++)
            {
                var     v = mesh.vertices[i];
                Vector3 n = new Vector3(v.x, v.y, v.z);
                vertices.Add(n);
                v = mesh.normals[i];
                n = new Vector3(v.x, v.y, v.z);
                normals.Add(n);
            }
            if (jointDesc.children.Length > 0)
            {
                var REF_VECTOR = new Vector3(0, 0, 1);

                foreach (var name in jointDesc.children)
                {
                    JointDesc childDesc = findJointDesc(skeletonDesc.jointDescs, name);
                    if (childDesc == null)
                    {
                        continue;
                    }
                    var offset = new Vector3(childDesc.offset[0], childDesc.offset[1], childDesc.offset[2]);
                    if (offset.magnitude > 0)
                    {
                        q         = Quaternion.FromToRotation(REF_VECTOR, offset.normalized);
                        magnitude = offset.magnitude;
                    }
                    else
                    {
                        scale = 0.00001f;
                    }

                    float heightScale = magnitude / 2;
                    for (int i = 0; i < vertices.Count; i++)
                    {
                        var     v = vertices[i];
                        Vector3 n = new Vector3(v.x * scale, v.y * scale, v.z * scale * heightScale);
                        n = q * n;
                        newVertices.Add(n);
                        v = mesh.normals[i];
                        n = new Vector3(v.x, v.y, v.z);
                        n = q * n;
                        newNormals.Add(n.normalized);
                    }
                }
            }

            jointObj.GetComponent <MeshFilter>().mesh.vertices = newVertices.ToArray();
            jointObj.GetComponent <MeshFilter>().mesh.normals  = newNormals.ToArray();

            jointObj.transform.parent = parent.transform;
            jointObj.name             = name;
            if (jointObj != null)
            {
                transform = jointObj.transform;

                foreach (string name in jointDesc.children)
                {
                    JointDesc childDesc = findJointDesc(skeletonDesc.jointDescs, name);
                    if (childDesc == null)
                    {
                        continue;
                    }

                    MGJoint childJoint = new MGJoint();
                    childJoint.createGameObjectfromDescOld(childDesc, skeletonDesc, jointObj, skeletonJointTag, prefab);
                    children.Add(childJoint);
                }
            }
            return(jointObj);
        }