示例#1
0
    public IEnumerator Bake()
    {
        if (Application.isPlaying)
        {
            Current = null;
            Baking  = true;
            string destination = Destination;
            if (!AssetDatabase.IsValidFolder(destination))
            {
                Debug.Log("Folder " + "'" + destination + "'" + " is not valid.");
            }
            else
            {
                for (int k = 0; k < Animations.Length; k++)
                {
                    if (Import[k])
                    {
                        string name = Animations[k].name + k;
                        if (!Directory.Exists(destination + "/" + name))
                        {
                            Current = Animations[k];

                            Skeleton.transform.position = Vector3.zero;
                            Skeleton.transform.rotation = Quaternion.identity;
                            Retarget.transform.position = Vector3.zero;
                            Retarget.transform.rotation = Quaternion.identity;

                            //Initialise
                            AnimatorOverrideController aoc = new AnimatorOverrideController(GetAnimator().runtimeAnimatorController);
                            var anims = new List <KeyValuePair <AnimationClip, AnimationClip> >();
                            foreach (var a in aoc.animationClips)
                            {
                                anims.Add(new KeyValuePair <AnimationClip, AnimationClip>(a, (AnimationClip)AssetDatabase.LoadAssetAtPath(AssetDatabase.GetAssetPath(Animations[k]), typeof(AnimationClip))));
                            }
                            aoc.ApplyOverrides(anims);
                            GetAnimator().runtimeAnimatorController = aoc;

                            //Start Bake
                            GetAnimator().speed = Speed;
                            GetAnimator().Play("Animation", 0, 0f);
                            yield return(new WaitForEndOfFrame());

                            Samples.Clear();
                            while (GetAnimator().GetCurrentAnimatorStateInfo(0).normalizedTime < 1f)
                            {
                                PostProcess();
                                StoreSample();
                                yield return(new WaitForEndOfFrame());
                            }
                            PostProcess();
                            StoreSample();
                            yield return(new WaitForEndOfFrame());

                            if (!Demo)
                            {
                                //Save Bake
                                AssetDatabase.CreateFolder(destination, name.Substring(name.IndexOf("|") + 1));
                                MotionData data = ScriptableObject.CreateInstance <MotionData>();
                                data.name = "Data";
                                AssetDatabase.CreateAsset(data, destination + "/" + name + "/" + data.name + ".asset");

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

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

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

                                //Compute Frames
                                List <Sample> frames = Resample();
                                for (int i = 0; i < frames.Count; i++)
                                {
                                    data.Frames[i] = new Frame(data, i + 1, frames[i].Timestamp);
                                    for (int j = 0; j < Retarget.Bones.Length; j++)
                                    {
                                        data.Frames[i].World[j] = frames[i].WorldPosture[j];
                                    }
                                }

                                //Detect Symmetry
                                data.DetectSymmetry();

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

                                //Smooth Motion
                                if (Smoothing > 0)
                                {
                                    Debug.Log("Smoothing ignored.");
                                    //Debug.Log("Applying smoothing to " + data.name + ".");
                                    //data.SmoothMotion(Smoothing);
                                }

                                EditorUtility.SetDirty(data);
                            }

                            //Stop Bake
                            GetAnimator().speed = 0f;
                            yield return(new WaitForEndOfFrame());
                        }
                        else
                        {
                            Debug.Log("File with name " + name + " already exists.");
                        }
                    }
                }
                yield return(new WaitForEndOfFrame());

                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
            }
            Current = null;
            Baking  = false;
        }
    }
示例#2
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));
    }
    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));
    }
示例#4
0
    public IEnumerator Bake()
    {
        if (Application.isPlaying)
        {
            Baking = true;
            string destination = "Assets/" + Destination;
            if (!AssetDatabase.IsValidFolder(destination))
            {
                Debug.Log("Folder " + "'" + destination + "'" + " is not valid.");
            }
            else
            {
                for (int k = 0; k < Animations.Length; k++)
                {
                    string name = Animations[k].name.Substring(Animations[k].name.IndexOf("|") + 1);
                    if (AssetDatabase.LoadAssetAtPath(destination + "/" + name + ".asset", typeof(MotionData)) == null)
                    {
                        //Initialise
                        AnimatorOverrideController aoc = new AnimatorOverrideController(GetAnimator().runtimeAnimatorController);
                        var anims = new List <KeyValuePair <AnimationClip, AnimationClip> >();
                        foreach (var a in aoc.animationClips)
                        {
                            anims.Add(new KeyValuePair <AnimationClip, AnimationClip>(a, Animations[k]));
                        }
                        aoc.ApplyOverrides(anims);
                        GetAnimator().runtimeAnimatorController = aoc;

                        //Start Bake
                        transform.position  = Vector3.zero;
                        transform.rotation  = Quaternion.identity;
                        GetAnimator().speed = Speed;
                        GetAnimator().Play("Animation", 0, 0f);
                        yield return(new WaitForEndOfFrame());

                        Samples = new List <Sample>();
                        while (GetAnimator().GetCurrentAnimatorStateInfo(0).normalizedTime < 1f)
                        {
                            Samples.Add(new Sample(GetTimestamp(), GetActor().GetWorldPosture(), GetActor().GetLocalPosture()));
                            yield return(new WaitForEndOfFrame());
                        }
                        Samples.Add(new Sample(GetTimestamp(), GetActor().GetWorldPosture(), GetActor().GetLocalPosture()));

                        //Save Bake
                        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 < GetActor().Bones.Length; i++)
                        {
                            data.Source.AddBone(GetActor().Bones[i].GetName(), GetActor().Bones[i].GetParent() == null ? "None" : GetActor().Bones[i].GetParent().GetName());
                        }

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

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

                        //Compute Frames
                        List <Sample> frames = Resample();
                        for (int i = 0; i < frames.Count; i++)
                        {
                            data.Frames[i] = new Frame(data, i + 1, frames[i].Timestamp);
                            for (int j = 0; j < GetActor().Bones.Length; j++)
                            {
                                data.Frames[i].Local[j] = frames[i].LocalPosture[j] * Mapping[j];
                                data.Frames[i].World[j] = frames[i].WorldPosture[j] * Mapping[j];
                            }
                        }

                        //Finalise
                        data.DetectSymmetry();
                        data.AddSequence();

                        EditorUtility.SetDirty(data);

                        //Stop Bake
                        GetAnimator().speed = 0f;
                        yield return(new WaitForEndOfFrame());
                    }
                    else
                    {
                        Debug.Log("File with name " + name + " already exists.");
                    }
                }
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
            }
            Baking = false;
        }
    }
示例#5
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));
    }
示例#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)
                {
                    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));
    }
示例#7
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));
    }