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

            root     = new MGJoint();
            skeleton = root.createGameObjectfromDesc(jointDesc, skeletonDesc, gameObject, skeletonJointTag);
        }
        /// <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 #3
0
 public MGJoint GetRootJoint(SkeletonDesc skeletonDesc)
 {
     if (!rootJoints.ContainsKey(skeletonDesc.name))
     {
         skeletons[skeletonDesc.name] = createDebugSkeleton(skeletonDesc);
     }
     return(rootJoints[skeletonDesc.name]);
 }
Example #4
0
 public GameObject GetSkeleton(SkeletonDesc skeletonDesc)
 {
     if (!rootJoints.ContainsKey(skeletonDesc.name))
     {
         skeletons[skeletonDesc.name] = createDebugSkeleton(skeletonDesc);
     }
     return(skeletons[skeletonDesc.name]);
 }
        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);
        }
        protected void buildPoseParameterIndexMap(SkeletonDesc skeletonDesc)
        {
            int counter = 0;

            indexMap = new Dictionary <string, int>();
            foreach (string name in skeletonDesc.jointSequence)
            {
                indexMap[name] = counter;
                counter++;
            }
        }
        public void ProcessSkeletonString(string skeletonString)
        {
            if (skeletonManager != null)
            {
                skeletonManager.HideSkeletons();
            }
            skeletonDesc = JsonUtility.FromJson <SkeletonDesc>(skeletonString);

            buildPoseParameterIndexMap(skeletonDesc);
            SetupSkeleton();
        }
Example #8
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");
     }
 }
        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);
        }
 public JointHierarchyBuilder(string serializedDescription)
 {
     skeletonDescription = JsonUtility.FromJson <SkeletonDesc>(serializedDescription);
 }