public void ToggleStyle(int index)
        {
            Frame next = GetNextStyleKey(index);

            StyleFlags[index] = !StyleFlags[index];
            int start = Index + 1;
            int end   = next == null?Data.GetTotalFrames() : next.Index - 1;

            for (int i = start; i <= end; i++)
            {
                Data.GetFrame(i).StyleFlags[index] = StyleFlags[index];
            }
            ApplyStyleKeyValues(index);
        }
示例#2
0
 public Matrix4x4 GetBoneTransformation(int index, bool mirrored, int smoothing = 0)
 {
     if (smoothing == 0)
     {
         return(Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Data.Scaling * Vector3.one) * (mirrored ? World[Data.Symmetry[index]].GetMirror(Data.GetAxis(Data.MirrorAxis)) : World[index]) * Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(Data.Source.Bones[index].Alignment), Vector3.one));            //Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(mirrored ? Data.Source.Bones[Data.Symmetry[index]].Alignment : Data.Source.Bones[index].Alignment), Vector3.one);
     }
     else
     {
         Frame[] frames = Data.GetFrames(Mathf.Clamp(Index - smoothing, 1, Data.GetTotalFrames()), Mathf.Clamp(Index + smoothing, 1, Data.GetTotalFrames()));
         Vector3 P      = Vector3.zero;
         Vector3 Z      = Vector3.zero;
         Vector3 Y      = Vector3.zero;
         float   sum    = 0f;
         for (int i = 0; i < frames.Length; i++)
         {
             float weight = 2f * (float)(i + 1) / (float)(frames.Length + 1);
             if (weight > 1f)
             {
                 weight = 2f - weight;
             }
             Matrix4x4 matrix = mirrored ? frames[i].World[Data.Symmetry[index]].GetMirror(Data.GetAxis(Data.MirrorAxis)) : frames[i].World[index];
             P   += weight * matrix.GetPosition();
             Z   += weight * matrix.GetForward();
             Y   += weight * matrix.GetUp();
             sum += weight;
         }
         P /= sum;
         Z /= sum;
         Y /= sum;
         return(Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Data.Scaling * Vector3.one) * Matrix4x4.TRS(P, Quaternion.LookRotation(Z, Y), Vector3.one));
     }
 }
 public override Module Initialise(MotionData data)
 {
     Data                  = data;
     Functions             = new StyleFunction[0];
     Keys                  = new bool[data.GetTotalFrames()];
     Keys[0]               = true;
     Keys[Keys.Length - 1] = true;
     return(this);
 }
示例#4
0
 public Sequence(MotionData data)
 {
     Data             = data;
     TransitionCopies = new int[data.Styles.Length];
     StyleCopies      = new int[data.Styles.Length];
     Copies           = new Interval[0];
     SetStart(1);
     SetEnd(data.GetTotalFrames());
 }
示例#5
0
 public override Module Init(MotionData data)
 {
     Data    = data;
     Actions = new Action[0];
     Keys    = new bool[data.GetTotalFrames()];
     Keys[0] = true;
     Keys[Keys.Length - 1] = true;
     DefaultAction();
     return(this);
 }
示例#6
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)
                {
                    string fileName = Files[f].Object.Name.Replace(".bvh", "");
                    if (!Directory.Exists(destination + "/" + fileName))
                    {
                        AssetDatabase.CreateFolder(destination, fileName);
                        MotionData data = ScriptableObject.CreateInstance <MotionData>();
                        data.name = fileName;
                        AssetDatabase.CreateAsset(data, destination + "/" + fileName + "/" + 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 = FileUtility.ReadFloat(subEntries[subEntry + 1]);
                                            offset.y = FileUtility.ReadFloat(subEntries[subEntry + 2]);
                                            offset.z = FileUtility.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 = FileUtility.ReadFloat(entries[entry + 1]);
                                    offset.y = FileUtility.ReadFloat(entries[entry + 2]);
                                    offset.z = FileUtility.ReadFloat(entries[entry + 3]);
                                    break;
                                }
                                else if (entries[entry].Contains("CHANNELS"))
                                {
                                    channel = new int[FileUtility.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;
                                }
                            }
                        }

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

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

                        //Compute Frames
                        index += 1;
                        for (int i = index; i < lines.Length; i++)
                        {
                            motions.Add(FileUtility.ReadArray(lines[i]));
                        }
                        for (int k = 0; k < data.GetTotalFrames(); k++)
                        {
                            data.Frames[k] = new 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
                                Matrix4x4 local = Matrix4x4.TRS(position, rotation, Vector3.one);
                                if (Flip)
                                {
                                    local = local.GetMirror(Axis);
                                }
                                data.Frames[k].World[i] = info.Parent == "None" ? local : data.Frames[k].World[data.Source.FindBone(info.Parent).Index] * local;
                            }

                            /*
                             * 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);
                             * }
                             */
                        }

                        if (data.GetTotalFrames() == 1)
                        {
                            Frame reference = data.Frames.First();
                            ArrayExtensions.Resize(ref data.Frames, Mathf.RoundToInt(data.Framerate));
                            for (int k = 0; k < data.GetTotalFrames(); k++)
                            {
                                data.Frames[k]       = new Frame(data, k + 1, (float)k / data.Framerate);
                                data.Frames[k].World = (Matrix4x4[])reference.World.Clone();
                            }
                        }

                        //Detect Symmetry
                        data.DetectSymmetry();

                        //Add Scene
                        data.CreateScene();
                        data.AddSequence();

                        //Save
                        EditorUtility.SetDirty(data);
                    }
                    else
                    {
                        Debug.Log("File with name " + fileName + " already exists.");
                    }

                    yield return(new WaitForSeconds(0f));
                }
            }
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            Importing = false;
        }
        yield return(new WaitForSeconds(0f));
    }
示例#7
0
 public void SetStart(int value)
 {
     Start = Mathf.Clamp(value, 1, Data.GetTotalFrames());
 }
示例#8
0
 public Sequence(MotionData data)
 {
     Data = data;
     SetStart(1);
     SetEnd(data.GetTotalFrames());
 }
    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;
            Character.transform.position = Vector3.zero;
            Character.transform.rotation = Quaternion.identity;
            for (int f = 0; f < Files.Length; f++)
            {
                if (Files[f].Import)
                {
                    if (!Directory.Exists(destination + "/" + Files[f].Object.name))
                    {
                        AssetDatabase.CreateFolder(destination, Files[f].Object.name);
                        MotionData data = ScriptableObject.CreateInstance <MotionData>();
                        data.name = "Data";
                        AssetDatabase.CreateAsset(data, destination + "/" + Files[f].Object.name + "/" + data.name + ".asset");
                        AnimationClip clip = (AnimationClip)AssetDatabase.LoadAssetAtPath(AssetDatabase.GetAssetPath(Files[f].Object), typeof(AnimationClip));

                        //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 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].World[j] = Character.Bones[j].Transform.GetWorldMatrix();
                            }
                        }

                        //Detect Symmetry
                        data.DetectSymmetry();

                        //Add Scene
                        data.CreateScene();
                        data.AddSequence();

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

        yield return(new WaitForSeconds(0f));
    }
示例#10
0
 public Frame GetPreviousFrame()
 {
     return(Data.GetFrame(Mathf.Clamp(Index - 1, 1, Data.GetTotalFrames())));
 }
示例#11
0
    private IEnumerator ImportMotionData()
    {
        foreach (Asset a in Assets)
        {
            a.Imported = false;
        }
        string destination = "Assets/" + Destination;

        if (!AssetDatabase.IsValidFolder(destination))
        {
            Debug.Log("Folder " + "'" + destination + "'" + " is not valid.");
        }
        else
        {
            int added = 0;
            Importing = true;
            foreach (Asset asset in Assets)
            {
                if (asset.Selected)
                {
                    string assetName = asset.Object.name.Replace(".fbx", "");
                    if (!Directory.Exists(destination + "/" + asset.Object.name))
                    {
                        AssetDatabase.CreateFolder(destination, asset.Object.name);
                        MotionData data = ScriptableObject.CreateInstance <MotionData>();
                        data.name = assetName;
                        AssetDatabase.CreateAsset(data, destination + "/" + asset.Object.name + "/" + data.name + ".asset");
                        AnimationClip clip = (AnimationClip)AssetDatabase.LoadAssetAtPath(AssetDatabase.GetAssetPath(asset.Object), typeof(AnimationClip));

                        //Create Actor
                        GameObject instance = Instantiate(asset.Object) as GameObject;
                        instance.name = asset.Object.name;
                        Actor    actor = instance.AddComponent <Actor>();
                        string[] names = actor.GetBoneNames();
                        ArrayExtensions.RemoveAt(ref names, 0);
                        actor.ExtractSkeleton(names);

                        //Create Source Data
                        data.Source = new MotionData.Hierarchy();
                        for (int i = 0; i < actor.Bones.Length; i++)
                        {
                            data.Source.AddBone(actor.Bones[i].GetName(), actor.Bones[i].GetParent() == null ? "None" : actor.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++)
                        {
                            clip.SampleAnimation(instance, (float)i / data.Framerate);
                            Matrix4x4[] transformations = new Matrix4x4[actor.Bones.Length];
                            for (int j = 0; j < transformations.Length; j++)
                            {
                                transformations[j] = Matrix4x4.TRS(Scale * actor.Bones[j].Transform.position, actor.Bones[j].Transform.rotation, Vector3.one);
                            }
                            data.Frames[i] = new Frame(data, i + 1, (float)i / data.Framerate, transformations);
                        }

                        //Remove Actor
                        Utility.Destroy(instance);

                        //Detect Symmetry
                        data.DetectSymmetry();

                        //Add Scene
                        data.CreateScene();
                        data.AddSequence();

                        EditorUtility.SetDirty(data);

                        added += 1;
                    }
                    else
                    {
                        Debug.Log("Asset with name " + asset.Object.name + " already exists.");
                    }
                }
            }
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            Importing = false;
            foreach (Asset a in Assets)
            {
                a.Imported = false;
            }

            Debug.Log("Added " + added + " new assets.");
        }

        yield return(new WaitForSeconds(0f));
    }
示例#12
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 (!Directory.Exists(destination + "/" + Files[f].Object.Name))
                    {
                        AssetDatabase.CreateFolder(destination, Files[f].Object.Name);
                        MotionData data = ScriptableObject.CreateInstance <MotionData>();
                        data.name = "Data";
                        AssetDatabase.CreateAsset(data, destination + "/" + Files[f].Object.Name + "/" + data.name + ".asset");

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

                        //Get the Order for Channel
                        int      len_channel     = 3;
                        int[]    channel         = new int[len_channel];
                        int      index           = 0;
                        string[] entries         = System.Text.RegularExpressions.Regex.Replace(lines[index], @"\s+", " ").Split(whitespace);
                        string[] entries_channel = entries[2].Split('/');
                        for (int i = 0; i < entries_channel.Length; i++)
                        {
                            if (entries_channel[i].Contains("X"))
                            {
                                channel[i] = 0;
                            }
                            else if (entries_channel[i].Contains("Y"))
                            {
                                channel[i] = 1;
                            }
                            else if (entries_channel[i].Contains("Z"))
                            {
                                channel[i] = 2;
                            }
                        }

                        //Set Framerate, Number of Joints, Number of Frames
                        index         += 2;
                        entries        = System.Text.RegularExpressions.Regex.Replace(lines[index], @"\s+", " ").Split(whitespace);
                        data.Framerate = FileUtility.ReadInt(entries[0]);
                        ArrayExtensions.Resize(ref data.Frames, FileUtility.ReadInt(entries[2]));
                        int num_joint = FileUtility.ReadInt(entries[3]);

                        //Record Joint Names/ Build Skeleton
                        index      += 1;
                        data.Source = new MotionData.Hierarchy();
                        string Parent = "None";
                        entries = System.Text.RegularExpressions.Regex.Replace(lines[index], @"\s+", " ").Split(whitespace);
                        for (int i = 0; i < num_joint; i++)
                        {
                            data.Source.AddBone(entries[i + 2], Parent);
                            //if(i==0){
                            //	Parent = entries[i+2];
                            //}
                        }

                        //Set Joint Positions
                        index += 2;
                        Vector3    position = Vector3.zero;
                        Quaternion rotation = Quaternion.identity;
                        for (int i = 0; i < data.GetTotalFrames(); i++)
                        {
                            data.Frames[i] = new Frame(data, i + 1, (float)i / data.Framerate);
                            entries        = System.Text.RegularExpressions.Regex.Replace(lines[index + i], @"\s+", " ").Split(whitespace);
                            for (int j = 0; j < num_joint; j++)
                            {
                                position[channel[0]] = FileUtility.ReadFloat(entries[j * 3 + 2]);
                                position[channel[1]] = FileUtility.ReadFloat(entries[j * 3 + 3]);
                                position[channel[2]] = FileUtility.ReadFloat(entries[j * 3 + 4]);
                                Matrix4x4 local = Matrix4x4.TRS(position / 100f, rotation, Vector3.one);
                                data.Frames[i].World[j] = local;
                                //Debug.Log("frame"+ i + " " + position[channel[0]]+ " " + position[channel[1]]+ " " + position[channel[2]]);
                            }
                        }

                        //If only one frame in the data
                        if (data.GetTotalFrames() == 1)
                        {
                            Frame reference = data.Frames.First();
                            ArrayExtensions.Resize(ref data.Frames, Mathf.RoundToInt(data.Framerate));
                            for (int i = 0; i < data.GetTotalFrames(); i++)
                            {
                                data.Frames[i]       = new Frame(data, i + 1, (float)i / data.Framerate);
                                data.Frames[i].World = (Matrix4x4[])reference.World.Clone();
                            }
                        }

                        //Detect Symmetry
                        data.DetectSymmetry();

                        //Add Scene
                        data.CreateScene();
                        data.AddSequence();

                        //Save
                        EditorUtility.SetDirty(data);
                    }
                    else
                    {
                        Debug.Log("File with name " + Files[f].Object.Name + " already exists.");
                    }

                    yield return(new WaitForSeconds(0f));
                }
            }
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            Importing = false;
        }
        yield return(new WaitForSeconds(0f));
    }
示例#13
0
    public void ImportFile(string FileName)
    {
        string[] lines  = File.ReadAllLines(Folder + "\\" + FileName);
        int      index  = 0;
        string   name   = string.Empty;
        string   parent = string.Empty;

        Data      = ScriptableObject.CreateInstance <MotionData>();
        Data.name = FileName;
        if (!AssetDatabase.IsValidFolder(Destination) && Destination != "Assets/")
        {
            AssetDatabase.CreateFolder("Assets", Destination.Substring(Mathf.Min(7, Destination.Length)));
            Debug.Log("No Directory Found, New Created!");
        }
        AssetDatabase.CreateAsset(Data, Destination + "\\" + FixedFileName(Data.name) + ".asset");

        List <Vector3> offsets  = new List <Vector3>();
        Vector3        offset   = Vector3.zero;
        List <int[]>   channels = new List <int[]>();

        int[]          channel = null;
        List <float[]> motions = new List <float[]>();

        for (index = 0; index < lines.Length; index++)
        {
            if (lines[index] == "MOTION")
            {
                break;
            }

            string[] entries = lines[index].Split(TextSpace);

            for (int entry = 0; entry < entries.Length; entry++)
            {
                if (entries[entry].Contains("ROOT"))
                {
                    parent = "None";
                    name   = FixedBoneName(entries[entry + 1]);
                    break;
                }
                else if (entries[entry].Contains("JOINT"))
                {
                    parent = name;
                    name   = FixedBoneName(entries[entry + 1]);
                    break;
                }
                else if (entries[entry].Contains("End"))
                {
                    parent = name;
                    name   = FixedBoneName(parent + entries[entry + 1]);

                    string[] offsetEntries = lines[index + 2].Split(TextSpace);
                    for (int offsetEntry = 0; offsetEntry < offsetEntries.Length; offsetEntry++)
                    {
                        if (offsetEntries[offsetEntry].Contains("OFFSET"))
                        {
                            offset.x = float.Parse(offsetEntries[offsetEntry + 1]);
                            offset.y = float.Parse(offsetEntries[offsetEntry + 2]);
                            offset.z = float.Parse(offsetEntries[offsetEntry + 3]);
                            break;
                        }
                    }

                    Data.Root.AddBone(name, parent);
                    offsets.Add(offset);
                    channels.Add(new int[0]);
                    index += 2;
                    break;
                }
                else if (entries[entry].Contains("OFFSET"))
                {
                    offset.x = float.Parse(entries[entry + 1]);
                    offset.y = float.Parse(entries[entry + 2]);
                    offset.z = float.Parse(entries[entry + 3]);
                    break;
                }
                else if (entries[entry].Contains("CHANNEL"))
                {
                    channel = new int[int.Parse(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.Root.AddBone(name, parent);
                    offsets.Add(offset);
                    channels.Add(channel);
                    break;
                }
                else if (entries[entry].Contains("}"))
                {
                    name   = parent;
                    parent = name == "None" ? "None" : Data.Root.FindBone(name).Parent;
                    break;
                }
            }
        }

        index += 1;
        while (lines[index].Length == 0)
        {
            index += 1;
        }
        ArrayExtensions.Resize(ref Data.Frames, int.Parse(lines[index].Substring(8)));
        index         += 1;
        Data.Framerate = Mathf.RoundToInt(1.0f / float.Parse(lines[index].Substring(12)));
        index         += 1;

        for (int i = index; i < lines.Length; i++)
        {
            motions.Add(ParseFloatArray(lines[i]));
        }

        for (int i = 0; i < Data.GetTotalFrames(); i++)
        {
            Data.Frames[i] = new Frame(Data, i + 1, (float)i / Data.Framerate);
            int channelIndex = 0;

            for (int j = 0; j < Data.Root.Bones.Length; j++)
            {
                MotionData.Hierarchy.Bone bone = Data.Root.Bones[j];
                Vector3    position            = Vector3.zero;
                Quaternion rotation            = Quaternion.identity;

                for (int k = 0; k < channels[j].Length; k++)
                {
                    if (channels[j][k] == 1)
                    {
                        position.x = motions[i][channelIndex];
                        channelIndex++;
                    }
                    if (channels[j][k] == 2)
                    {
                        position.y = motions[i][channelIndex];
                        channelIndex++;
                    }
                    if (channels[j][k] == 3)
                    {
                        position.z = motions[i][channelIndex];
                        channelIndex++;
                    }
                    if (channels[j][k] == 4)
                    {
                        rotation *= Quaternion.AngleAxis(motions[i][channelIndex], Vector3.right);
                        channelIndex++;
                    }
                    if (channels[j][k] == 5)
                    {
                        rotation *= Quaternion.AngleAxis(motions[i][channelIndex], Vector3.up);
                        channelIndex++;
                    }
                    if (channels[j][k] == 6)
                    {
                        rotation *= Quaternion.AngleAxis(motions[i][channelIndex], Vector3.forward);
                        channelIndex++;
                    }
                }

                position = (position == Vector3.zero ? offsets[j] : position) / 100.0f;
                Matrix4x4 local = Matrix4x4.TRS(position, rotation, Vector3.one);
                Data.Frames[i].World[j] = bone.Parent == "None" ? local : Data.Frames[i].World[Data.Root.FindBone(bone.Parent).Index] * local;

                EditorUtility.SetDirty(Data);
            }
        }

        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
        Debug.Log("Import File: " + FileName + " Successfully!");
    }
示例#14
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 (Import[f])
                {
                    MotionData data = ScriptableObject.CreateInstance <MotionData>();
                    data.Name = Files[f].FullName.Substring(Files[f].FullName.LastIndexOf("/") + 1);
                    if (AssetDatabase.LoadAssetAtPath(destination + "/" + data.Name + ".asset", typeof(MotionData)) == null)
                    {
                        AssetDatabase.CreateAsset(data, destination + "/" + data.Name + ".asset");
                    }
                    else
                    {
                        int i = 1;
                        while (AssetDatabase.LoadAssetAtPath(destination + "/" + data.Name + data.Name + " (" + i + ").asset", typeof(MotionData)) != null)
                        {
                            i += 1;
                        }
                        AssetDatabase.CreateAsset(data, destination + "/" + data.Name + data.Name + " (" + i + ").asset");
                    }

                    string[] lines      = File.ReadAllLines(Files[f].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();

                    yield return(new WaitForSeconds(0f));
                }
            }
            Importing = false;
        }
        yield return(new WaitForSeconds(0f));
    }