示例#1
0
    private IEnumerator ImportMotionData()
    {
        string destination = "Assets/" + Destination;

        if (!AssetDatabase.IsValidFolder(destination))
        {
            Debug.Log("Folder " + "'" + destination + "'" + " is not valid.");
        }
        else
        {
            Importing = true;
            for (int f = 0; f < Files.Length; f++)
            {
                if (Files[f].Import)
                {
                    if (AssetDatabase.LoadAssetAtPath(destination + "/" + Files[f].Object.Name + ".asset", typeof(MotionData)) == null)
                    {
                        MotionData data = ScriptableObject.CreateInstance <MotionData>();
                        data.Name = Files[f].Object.Name;
                        AssetDatabase.CreateAsset(data, destination + "/" + data.Name + ".asset");

                        string[] lines      = System.IO.File.ReadAllLines(Files[f].Object.FullName);
                        char[]   whitespace = new char[] { ' ' };
                        int      index      = 0;

                        //Create Source Data
                        List <Vector3> offsets  = new List <Vector3>();
                        List <int[]>   channels = new List <int[]>();
                        List <float[]> motions  = new List <float[]>();
                        data.Source = new MotionData.Hierarchy();
                        string  name    = string.Empty;
                        string  parent  = string.Empty;
                        Vector3 offset  = Vector3.zero;
                        int[]   channel = null;
                        for (index = 0; index < lines.Length; index++)
                        {
                            if (lines[index] == "MOTION")
                            {
                                break;
                            }
                            string[] entries = lines[index].Split(whitespace);
                            for (int entry = 0; entry < entries.Length; entry++)
                            {
                                if (entries[entry].Contains("ROOT"))
                                {
                                    parent = "None";
                                    name   = entries[entry + 1];
                                    break;
                                }
                                else if (entries[entry].Contains("JOINT"))
                                {
                                    parent = name;
                                    name   = entries[entry + 1];
                                    break;
                                }
                                else if (entries[entry].Contains("End"))
                                {
                                    parent = name;
                                    name   = name + entries[entry + 1];
                                    string[] subEntries = lines[index + 2].Split(whitespace);
                                    for (int subEntry = 0; subEntry < subEntries.Length; subEntry++)
                                    {
                                        if (subEntries[subEntry].Contains("OFFSET"))
                                        {
                                            offset.x = Utility.ReadFloat(subEntries[subEntry + 1]);
                                            offset.y = Utility.ReadFloat(subEntries[subEntry + 2]);
                                            offset.z = Utility.ReadFloat(subEntries[subEntry + 3]);
                                            break;
                                        }
                                    }
                                    data.Source.AddBone(name, parent);
                                    offsets.Add(offset);
                                    channels.Add(new int[0]);
                                    index += 2;
                                    break;
                                }
                                else if (entries[entry].Contains("OFFSET"))
                                {
                                    offset.x = Utility.ReadFloat(entries[entry + 1]);
                                    offset.y = Utility.ReadFloat(entries[entry + 2]);
                                    offset.z = Utility.ReadFloat(entries[entry + 3]);
                                    break;
                                }
                                else if (entries[entry].Contains("CHANNELS"))
                                {
                                    channel = new int[Utility.ReadInt(entries[entry + 1])];
                                    for (int i = 0; i < channel.Length; i++)
                                    {
                                        if (entries[entry + 2 + i] == "Xposition")
                                        {
                                            channel[i] = 1;
                                        }
                                        else if (entries[entry + 2 + i] == "Yposition")
                                        {
                                            channel[i] = 2;
                                        }
                                        else if (entries[entry + 2 + i] == "Zposition")
                                        {
                                            channel[i] = 3;
                                        }
                                        else if (entries[entry + 2 + i] == "Xrotation")
                                        {
                                            channel[i] = 4;
                                        }
                                        else if (entries[entry + 2 + i] == "Yrotation")
                                        {
                                            channel[i] = 5;
                                        }
                                        else if (entries[entry + 2 + i] == "Zrotation")
                                        {
                                            channel[i] = 6;
                                        }
                                    }
                                    data.Source.AddBone(name, parent);
                                    offsets.Add(offset);
                                    channels.Add(channel);
                                    break;
                                }
                                else if (entries[entry].Contains("}"))
                                {
                                    name   = parent;
                                    parent = name == "None" ? "None" : data.Source.FindBone(name).Parent;
                                    break;
                                }
                            }
                        }

                        //REMOVE LATER
                        //data.Corrections = new Vector3[data.Source.Bones.Length];
                        //

                        //Set Frames
                        index += 1;
                        while (lines[index].Length == 0)
                        {
                            index += 1;
                        }
                        ArrayExtensions.Resize(ref data.Frames, Utility.ReadInt(lines[index].Substring(8)));

                        //Set Framerate
                        index         += 1;
                        data.Framerate = Mathf.RoundToInt(1f / Utility.ReadFloat(lines[index].Substring(12)));

                        //Compute Frames
                        index += 1;
                        for (int i = index; i < lines.Length; i++)
                        {
                            motions.Add(Utility.ReadArray(lines[i]));
                        }
                        for (int k = 0; k < data.GetTotalFrames(); k++)
                        {
                            data.Frames[k] = new MotionData.Frame(data, k + 1, (float)k / data.Framerate);
                            int idx = 0;
                            for (int i = 0; i < data.Source.Bones.Length; i++)
                            {
                                MotionData.Hierarchy.Bone info = data.Source.Bones[i];
                                Vector3    position            = Vector3.zero;
                                Quaternion rotation            = Quaternion.identity;
                                for (int j = 0; j < channels[i].Length; j++)
                                {
                                    if (channels[i][j] == 1)
                                    {
                                        position.x = motions[k][idx]; idx += 1;
                                    }
                                    if (channels[i][j] == 2)
                                    {
                                        position.y = motions[k][idx]; idx += 1;
                                    }
                                    if (channels[i][j] == 3)
                                    {
                                        position.z = motions[k][idx]; idx += 1;
                                    }
                                    if (channels[i][j] == 4)
                                    {
                                        rotation *= Quaternion.AngleAxis(motions[k][idx], Vector3.right); idx += 1;
                                    }
                                    if (channels[i][j] == 5)
                                    {
                                        rotation *= Quaternion.AngleAxis(motions[k][idx], Vector3.up); idx += 1;
                                    }
                                    if (channels[i][j] == 6)
                                    {
                                        rotation *= Quaternion.AngleAxis(motions[k][idx], Vector3.forward); idx += 1;
                                    }
                                }

                                position = (position == Vector3.zero ? offsets[i] : position) / 100f;                                 //unit scale
                                data.Frames[k].Local[i] = Matrix4x4.TRS(position, rotation, Vector3.one);
                                data.Frames[k].World[i] = info.Parent == "None" ? data.Frames[k].Local[i] : data.Frames[k].World[data.Source.FindBone(info.Parent).Index] * data.Frames[k].Local[i];
                            }

                            /*
                             * for(int i=0; i<data.Source.Bones.Length; i++) {
                             *      data.Frames[k].Local[i] *= Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(data.Corrections[i]), Vector3.one);
                             *      data.Frames[k].World[i] *= Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(data.Corrections[i]), Vector3.one);
                             * }
                             */
                        }

                        //Finalise
                        data.DetectHeightMapSensor();
                        data.DetectDepthMapSensor();
                        data.DetectSymmetry();
                        data.ComputeStyles();
                        data.AddSequence();
                    }
                    else
                    {
                        Debug.Log("File with name " + Files[f].Object.Name + " already exists.");
                    }

                    yield return(new WaitForSeconds(0f));
                }
            }
            Importing = false;
        }
        yield return(new WaitForSeconds(0f));
    }
示例#2
0
    private IEnumerator ImportMotionData()
    {
        string destination = "Assets/" + Destination;

        if (Character == null)
        {
            Debug.Log("No character model assigned.");
        }
        else if (!AssetDatabase.IsValidFolder(destination))
        {
            Debug.Log("Folder " + "'" + destination + "'" + " is not valid.");
        }
        else
        {
            Importing = true;
            for (int f = 0; f < Files.Length; f++)
            {
                if (Files[f].Import)
                {
                    string name = Files[f].Object.name.Substring(Files[f].Object.name.LastIndexOf("/") + 1);
                    if (AssetDatabase.LoadAssetAtPath(destination + "/" + name + ".asset", typeof(MotionData)) == null)
                    {
                        AnimationClip clip = (AnimationClip)AssetDatabase.LoadAssetAtPath(AssetDatabase.GetAssetPath(Files[f].Object), typeof(AnimationClip));
                        MotionData    data = ScriptableObject.CreateInstance <MotionData>();

                        //Assign Name
                        data.Name = name;

                        AssetDatabase.CreateAsset(data, destination + "/" + data.Name + ".asset");

                        //Create Source Data
                        data.Source = new MotionData.Hierarchy();
                        for (int i = 0; i < Character.Bones.Length; i++)
                        {
                            data.Source.AddBone(Character.Bones[i].GetName(), Character.Bones[i].GetParent() == null ? "None" : Character.Bones[i].GetParent().GetName());
                        }

                        //Set Frames
                        ArrayExtensions.Resize(ref data.Frames, Mathf.RoundToInt((float)Framerate * clip.length));

                        //Set Framerate
                        data.Framerate = (float)Framerate;

                        //Compute Frames
                        for (int i = 0; i < data.GetTotalFrames(); i++)
                        {
                            data.Frames[i] = new MotionData.Frame(data, i + 1, (float)i / data.Framerate);
                            clip.SampleAnimation(Character.gameObject, data.Frames[i].Timestamp);
                            for (int j = 0; j < Character.Bones.Length; j++)
                            {
                                data.Frames[i].Local[j] = Character.Bones[j].Transform.GetLocalMatrix();
                                data.Frames[i].World[j] = Character.Bones[j].Transform.GetWorldMatrix();
                            }
                        }

                        //Finalise
                        data.DetectHeightMapSensor();
                        data.DetectDepthMapSensor();
                        data.DetectSymmetry();
                        data.ComputeStyles();
                        data.AddSequence();

                        EditorUtility.SetDirty(data);
                    }
                    else
                    {
                        Debug.Log("File with name " + name + " already exists.");
                    }
                }
            }
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            Importing = false;
        }

        yield return(new WaitForSeconds(0f));
    }