/// <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);
            }
        }
        public void createDebugSkeleton(SkeletonDesc skeletonDesc)
        {
            JointDesc jointDesc = skeletonDesc.jointDescs[0];

            root     = new MGJoint();
            skeleton = root.createGameObjectfromDesc(jointDesc, skeletonDesc, gameObject, skeletonJointTag);
        }
        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);
        }
Esempio n. 6
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 JointDesc findJointDesc(JointDesc[] descs, string name)
        {
            JointDesc r = null;

            foreach (JointDesc j in descs)
            {
                if (j.name == name)
                {
                    r = j;
                    break;
                }
            }
            return(r);
        }
        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);
        }