public void Load(string path)
    {
        Name = path.Substring(path.LastIndexOf("/") + 1);

        string[] lines      = File.ReadAllLines(path);
        char[]   whitespace = new char[] { ' ' };
        int      index      = 0;

        //Read BVH
        Source = new BVHData();
        string  name   = string.Empty;
        string  parent = string.Empty;
        Vector3 offset = Vector3.zero;

        int[] channels = 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;
                        }
                    }
                    Source.AddBone(name, parent, offset, 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"))
                {
                    channels = new int[Utility.ReadInt(entries[entry + 1])];
                    for (int i = 0; i < channels.Length; i++)
                    {
                        if (entries[entry + 2 + i] == "Xposition")
                        {
                            channels[i] = 1;
                        }
                        else if (entries[entry + 2 + i] == "Yposition")
                        {
                            channels[i] = 2;
                        }
                        else if (entries[entry + 2 + i] == "Zposition")
                        {
                            channels[i] = 3;
                        }
                        else if (entries[entry + 2 + i] == "Xrotation")
                        {
                            channels[i] = 4;
                        }
                        else if (entries[entry + 2 + i] == "Yrotation")
                        {
                            channels[i] = 5;
                        }
                        else if (entries[entry + 2 + i] == "Zrotation")
                        {
                            channels[i] = 6;
                        }
                    }
                    Source.AddBone(name, parent, offset, channels);
                    break;
                }
                else if (entries[entry].Contains("}"))
                {
                    name   = parent;
                    parent = name == "None" ? "None" : Source.FindBone(name).Parent;
                    break;
                }
            }
        }

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

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

        //Read motions
        index += 1;
        for (int i = index; i < lines.Length; i++)
        {
            Source.AddMotion(Utility.ReadArray(lines[i]));
        }

        //Detect settings
        DetectHips();
        DetectHead();
        DetectSymmetry();

        //Create frames
        for (int i = 0; i < GetTotalFrames(); i++)
        {
            Frames[i] = new Frame(this, i + 1, (float)i / Framerate);
        }

        //Generate
        Generate();
    }
Пример #2
0
    public MotionData Create(string path, string currentDirectory)
    {
        Name = path.Substring(path.LastIndexOf("/") + 1);
        if (AssetDatabase.LoadAssetAtPath(currentDirectory + Name + ".asset", typeof(MotionData)) == null)
        {
            AssetDatabase.CreateAsset(this, currentDirectory + Name + ".asset");
        }
        else
        {
            int i = 1;
            while (AssetDatabase.LoadAssetAtPath(currentDirectory + Name + Name + " (" + i + ").asset", typeof(MotionData)) != null)
            {
                i += 1;
            }
            AssetDatabase.CreateAsset(this, currentDirectory + Name + Name + " (" + i + ").asset");
        }

        string[] lines      = File.ReadAllLines(path);
        char[]   whitespace = new char[] { ' ' };
        int      index      = 0;

        //Read BVH
        Source = new BVHData();
        string  name   = string.Empty;
        string  parent = string.Empty;
        Vector3 offset = Vector3.zero;

        int[] channels = 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;
                        }
                    }
                    Source.AddBone(name, parent, offset, 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"))
                {
                    channels = new int[Utility.ReadInt(entries[entry + 1])];
                    for (int i = 0; i < channels.Length; i++)
                    {
                        if (entries[entry + 2 + i] == "Xposition")
                        {
                            channels[i] = 1;
                        }
                        else if (entries[entry + 2 + i] == "Yposition")
                        {
                            channels[i] = 2;
                        }
                        else if (entries[entry + 2 + i] == "Zposition")
                        {
                            channels[i] = 3;
                        }
                        else if (entries[entry + 2 + i] == "Xrotation")
                        {
                            channels[i] = 4;
                        }
                        else if (entries[entry + 2 + i] == "Yrotation")
                        {
                            channels[i] = 5;
                        }
                        else if (entries[entry + 2 + i] == "Zrotation")
                        {
                            channels[i] = 6;
                        }
                    }
                    Source.AddBone(name, parent, offset, channels);
                    break;
                }
                else if (entries[entry].Contains("}"))
                {
                    name   = parent;
                    parent = name == "None" ? "None" : Source.FindBone(name).Parent;
                    break;
                }
            }
        }

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

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

        //Read motions
        index += 1;
        for (int i = index; i < lines.Length; i++)
        {
            Source.AddMotion(Utility.ReadArray(lines[i]));
        }

        //Detect settings
        DetectHeightMapSensor();
        DetectDepthMapSensor();
        DetectSymmetry();
        DetectCorrections();

        //Create frames
        for (int i = 0; i < GetTotalFrames(); i++)
        {
            Frames[i] = new Frame(this, i + 1, (float)i / Framerate);
        }

        //Generate
        ComputePostures();
        ComputeStyles();
        AddSequence();

        //Finish
        return(this);
    }