示例#1
0
 public Vector3 GetBoneVelocity(int index, bool mirrored)
 {
     if (Timestamp - Data.GetDeltaTime() < 0f)
     {
         return((Data.GetFrame(Timestamp + Data.GetDeltaTime()).GetBoneTransformation(index, mirrored).GetPosition() - GetBoneTransformation(index, mirrored).GetPosition()) / Data.GetDeltaTime());
     }
     else
     {
         return((GetBoneTransformation(index, mirrored).GetPosition() - Data.GetFrame(Timestamp - Data.GetDeltaTime()).GetBoneTransformation(index, mirrored).GetPosition()) / Data.GetDeltaTime());
     }
 }
示例#2
0
 protected override void DerivedCallback(MotionEditor editor)
 {
     if (BallInstance == null)
     {
         BallInstance = editor.GetActor().FindTransform(Data.Source.Bones[Ball].Name);
     }
     if (BallInstance != null)
     {
         Matrix4x4 m = ShowSource ? editor.GetCurrentFrame().GetSourceTransformation(Ball, editor.Mirror) : editor.GetCurrentFrame().GetBoneTransformation(Ball, editor.Mirror);
         BallInstance.transform.position = m.GetPosition();
         BallInstance.transform.rotation = m.GetRotation();
     }
     if (GetRival() != null)
     {
         float      t     = editor.GetCurrentFrame().Timestamp;
         MotionData asset = GetRivalAsset(t);
         if (asset == null)
         {
             asset = Data;
         }
         int[] bones = MotionEditor.GetInstance().GetBoneMapping();
         for (int i = 0; i < GetRival().Bones.Length; i++)
         {
             Matrix4x4 m = asset.GetFrame(t).GetBoneTransformation(bones[i], editor.Mirror);
             GetRival().Bones[i].Transform.position = m.GetPosition();
             GetRival().Bones[i].Transform.rotation = m.GetRotation();
         }
     }
 }
示例#3
0
 public Vector3 GetBoneVelocity(int index, bool mirrored, float delta)
 {
     if (delta == 0f)
     {
         return(Vector3.zero);
     }
     if (Timestamp - delta < 0f)
     {
         return((Data.GetFrame(Timestamp + delta).GetBoneTransformation(index, mirrored).GetPosition() - GetBoneTransformation(index, mirrored).GetPosition()) / delta);
     }
     else
     {
         return((GetBoneTransformation(index, mirrored).GetPosition() - Data.GetFrame(Timestamp - delta).GetBoneTransformation(index, mirrored).GetPosition()) / delta);
     }
 }
示例#4
0
    public float GetSpeed(bool mirrored)
    {
        float length = 0f;

        Vector3[] positions = new Vector3[6];
        positions[0]   = GetRootPosition(mirrored);
        positions[0].y = 0f;
        for (int i = 1; i <= 5; i++)
        {
            Frame future = Data.GetFrame(Mathf.Clamp(Timestamp + (float)i / 5f, 0f, Data.GetTotalTime()));
            positions[i]   = future.GetRootPosition(mirrored);
            positions[i].y = 0f;
        }
        for (int i = 1; i <= 5; i++)
        {
            length += Vector3.Distance(positions[i - 1], positions[i]);
        }
        return(length);
    }
示例#5
0
        public void Inspector(MotionEditor editor)
        {
            UltiDraw.Begin();
            Utility.SetGUIColor(UltiDraw.Grey);
            using (new EditorGUILayout.VerticalScope("Box")) {
                Utility.ResetGUIColor();

                EditorGUILayout.BeginHorizontal();
                if (Utility.GUIButton("+", UltiDraw.DarkGrey, UltiDraw.White, 20f, 20f))
                {
                    AddBone(0);
                }
                EditorGUILayout.LabelField("Group", GUILayout.Width(40f));
                SetName(EditorGUILayout.TextField(ID, GUILayout.Width(100f)));
                EditorGUILayout.LabelField("Mask", GUILayout.Width(40));
                Mask = InternalEditorUtility.ConcatenatedLayersMaskToLayerMask(EditorGUILayout.MaskField(InternalEditorUtility.LayerMaskToConcatenatedLayersMask(Mask), InternalEditorUtility.layers, GUILayout.Width(100f)));
                EditorGUILayout.LabelField("Offset", GUILayout.Width(40f));
                Offset = EditorGUILayout.Vector3Field("", Offset, GUILayout.Width(125f));
                EditorGUILayout.LabelField("Threshold", GUILayout.Width(60f));
                Threshold = EditorGUILayout.FloatField(Threshold, GUILayout.Width(30f));
                EditorGUILayout.LabelField("Velocity", GUILayout.Width(60f));
                Velocity = EditorGUILayout.FloatField(Velocity, GUILayout.Width(30f));
                EditorGUILayout.EndHorizontal();

                for (int i = 0; i < Bones.Length; i++)
                {
                    EditorGUILayout.BeginHorizontal();
                    if (Utility.GUIButton("-", UltiDraw.DarkGrey, UltiDraw.White, 20f, 20f))
                    {
                        RemoveBone(i);
                        i--;
                    }
                    else
                    {
                        SetBone(i, EditorGUILayout.Popup(Bones[i], editor.GetAsset().Source.GetBoneNames()));
                    }
                    EditorGUILayout.EndHorizontal();
                }

                Frame      frame = editor.GetCurrentFrame();
                MotionData data  = editor.GetAsset();

                EditorGUILayout.BeginVertical(GUILayout.Height(10f));
                Rect ctrl = EditorGUILayout.GetControlRect();
                Rect rect = new Rect(ctrl.x, ctrl.y, ctrl.width, 10f);
                EditorGUI.DrawRect(rect, UltiDraw.Black);

                float startTime = frame.Timestamp - editor.GetWindow() / 2f;
                float endTime   = frame.Timestamp + editor.GetWindow() / 2f;
                if (startTime < 0f)
                {
                    endTime  -= startTime;
                    startTime = 0f;
                }
                if (endTime > data.GetTotalTime())
                {
                    startTime -= endTime - data.GetTotalTime();
                    endTime    = data.GetTotalTime();
                }
                startTime = Mathf.Max(0f, startTime);
                endTime   = Mathf.Min(data.GetTotalTime(), endTime);
                int start    = data.GetFrame(startTime).Index;
                int end      = data.GetFrame(endTime).Index;
                int elements = end - start;

                Vector3 bottom = new Vector3(0f, rect.yMax, 0f);
                Vector3 top    = new Vector3(0f, rect.yMax - rect.height, 0f);

                start = Mathf.Clamp(start, 1, Module.Data.Frames.Length);
                end   = Mathf.Clamp(end, 1, Module.Data.Frames.Length);

                //Contacts
                for (int i = start; i <= end; i++)
                {
                    if (GetContact(data.GetFrame(i), editor.Mirror) == 1f)
                    {
                        float left  = rect.xMin + (float)(i - start) / (float)elements * rect.width;
                        float right = left;
                        while (i < end && GetContact(data.GetFrame(i), editor.Mirror) == 1f)
                        {
                            i++;
                            right = rect.xMin + (float)(i - start) / (float)elements * rect.width;
                        }
                        if (left != right)
                        {
                            Vector3 a = new Vector3(left, rect.y, 0f);
                            Vector3 b = new Vector3(right, rect.y, 0f);
                            Vector3 c = new Vector3(left, rect.y + rect.height, 0f);
                            Vector3 d = new Vector3(right, rect.y + rect.height, 0f);
                            UltiDraw.DrawTriangle(a, c, b, UltiDraw.Green);
                            UltiDraw.DrawTriangle(b, c, d, UltiDraw.Green);
                        }
                    }
                }

                //Current Pivot
                top.x    = rect.xMin + (float)(frame.Index - start) / elements * rect.width;
                bottom.x = rect.xMin + (float)(frame.Index - start) / elements * rect.width;
                top.y    = rect.yMax - rect.height;
                bottom.y = rect.yMax;
                UltiDraw.DrawLine(top, bottom, UltiDraw.Yellow);

                Handles.DrawLine(Vector3.zero, Vector3.zero);                 //Somehow needed to get it working...

                EditorGUILayout.EndVertical();
            }
            UltiDraw.End();
        }
 public Vector3 GetBoneVelocity(int index, bool mirrored)
 {
     if (Index == 1)
     {
         return(mirrored ?
                ((Data.GetFrame(Index + 1).World[Data.Symmetry[index]].GetPosition() - World[Data.Symmetry[index]].GetPosition()) * Data.Framerate).GetMirror(Data.GetAxis(Data.MirrorAxis))
                         :
                (Data.GetFrame(Index + 1).World[index].GetPosition() - World[index].GetPosition()) * Data.Framerate);
     }
     else
     {
         return(mirrored ?
                ((Data.GetFrame(Index + 1).World[Data.Symmetry[index]].GetPosition() - World[Data.Symmetry[index]].GetPosition()) * Data.Framerate).GetMirror(Data.GetAxis(Data.MirrorAxis))
                         :
                (World[index].GetPosition() - Data.GetFrame(Index - 1).World[index].GetPosition()) * Data.Framerate);
     }
 }
示例#7
0
 public Frame GetPreviousFrame()
 {
     return(Data.GetFrame(Mathf.Clamp(Index - 1, 1, Data.GetTotalFrames())));
 }
        public void Inspector(MotionEditor editor)
        {
            UltiDraw.Begin();
            Utility.SetGUIColor(UltiDraw.Grey);
            using (new EditorGUILayout.VerticalScope("Box")) {
                Utility.ResetGUIColor();

                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Bone", GUILayout.Width(40f));
                Bone = EditorGUILayout.Popup(Bone, editor.GetData().Source.GetBoneNames(), GUILayout.Width(80f));
                EditorGUILayout.LabelField("Mask", GUILayout.Width(30));
                Mask = InternalEditorUtility.ConcatenatedLayersMaskToLayerMask(EditorGUILayout.MaskField(InternalEditorUtility.LayerMaskToConcatenatedLayersMask(Mask), InternalEditorUtility.layers, GUILayout.Width(75f)));
                EditorGUILayout.LabelField("Capture", GUILayout.Width(50));
                Capture = (ID)EditorGUILayout.EnumPopup(Capture, GUILayout.Width(75f));
                EditorGUILayout.LabelField("Edit", GUILayout.Width(30));
                Edit = (ID)EditorGUILayout.EnumPopup(Edit, GUILayout.Width(75f));
                EditorGUILayout.LabelField("Solve Position", GUILayout.Width(80f));
                SolvePosition = EditorGUILayout.Toggle(SolvePosition, GUILayout.Width(20f));
                EditorGUILayout.LabelField("Solve Rotation", GUILayout.Width(80f));
                SolveRotation = EditorGUILayout.Toggle(SolveRotation, GUILayout.Width(20f));
                EditorGUILayout.LabelField("Solve Distance", GUILayout.Width(80f));
                SolveDistance = EditorGUILayout.Toggle(SolveDistance, GUILayout.Width(20f));
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Offset", GUILayout.Width(40f));
                Offset = EditorGUILayout.Vector3Field("", Offset, GUILayout.Width(180f));
                EditorGUILayout.LabelField("Threshold", GUILayout.Width(70f));
                Threshold = EditorGUILayout.FloatField(Threshold, GUILayout.Width(50f));
                EditorGUILayout.LabelField("Tolerance", GUILayout.Width(70f));
                Tolerance = EditorGUILayout.FloatField(Tolerance, GUILayout.Width(50f));
                EditorGUILayout.LabelField("Velocity", GUILayout.Width(70f));
                Velocity = EditorGUILayout.FloatField(Velocity, GUILayout.Width(50f));
                EditorGUILayout.LabelField("Weight", GUILayout.Width(60f));
                Weight = EditorGUILayout.FloatField(Weight, GUILayout.Width(50f));
                EditorGUILayout.EndHorizontal();

                Frame      frame = editor.GetCurrentFrame();
                MotionData data  = editor.GetData();

                EditorGUILayout.BeginVertical(GUILayout.Height(10f));
                Rect ctrl = EditorGUILayout.GetControlRect();
                Rect rect = new Rect(ctrl.x, ctrl.y, ctrl.width, 10f);
                EditorGUI.DrawRect(rect, UltiDraw.Black);

                float startTime = frame.Timestamp - editor.GetWindow() / 2f;
                float endTime   = frame.Timestamp + editor.GetWindow() / 2f;
                if (startTime < 0f)
                {
                    endTime  -= startTime;
                    startTime = 0f;
                }
                if (endTime > data.GetTotalTime())
                {
                    startTime -= endTime - data.GetTotalTime();
                    endTime    = data.GetTotalTime();
                }
                startTime = Mathf.Max(0f, startTime);
                endTime   = Mathf.Min(data.GetTotalTime(), endTime);
                int start    = data.GetFrame(startTime).Index;
                int end      = data.GetFrame(endTime).Index;
                int elements = end - start;

                Vector3 bottom = new Vector3(0f, rect.yMax, 0f);
                Vector3 top    = new Vector3(0f, rect.yMax - rect.height, 0f);

                start = Mathf.Clamp(start, 1, Module.Data.Frames.Length);
                end   = Mathf.Clamp(end, 1, Module.Data.Frames.Length);

                //Contacts
                for (int i = start; i <= end; i++)
                {
                    if ((editor.Mirror ? InverseContacts[i - 1] : RegularContacts[i - 1]) == 1f)
                    {
                        float left  = rect.xMin + (float)(i - start) / (float)elements * rect.width;
                        float right = left;
                        while (i < end && (editor.Mirror ? InverseContacts[i - 1] : RegularContacts[i - 1]) != 0f)
                        {
                            right = rect.xMin + (float)(i - start) / (float)elements * rect.width;
                            i++;
                        }
                        if (left != right)
                        {
                            Vector3 a = new Vector3(left, rect.y, 0f);
                            Vector3 b = new Vector3(right, rect.y, 0f);
                            Vector3 c = new Vector3(left, rect.y + rect.height, 0f);
                            Vector3 d = new Vector3(right, rect.y + rect.height, 0f);
                            UltiDraw.DrawTriangle(a, c, b, UltiDraw.Green);
                            UltiDraw.DrawTriangle(b, c, d, UltiDraw.Green);
                        }
                    }
                }

                //Current Pivot
                top.x    = rect.xMin + (float)(frame.Index - start) / elements * rect.width;
                bottom.x = rect.xMin + (float)(frame.Index - start) / elements * rect.width;
                top.y    = rect.yMax - rect.height;
                bottom.y = rect.yMax;
                UltiDraw.DrawLine(top, bottom, UltiDraw.Yellow);

                Handles.DrawLine(Vector3.zero, Vector3.zero);                 //Somehow needed to get it working...

                EditorGUILayout.EndVertical();
            }
            UltiDraw.End();
        }
示例#9
0
    private IEnumerator Process()
    {
        if (Editor != null)
        {
            Processing = true;
            foreach (Asset a in Assets)
            {
                a.Processed = false;
            }
            int count = 0;

            if (Pipeline == PIPELINE.Basketball)
            {
                string     referenceBone = string.Empty;
                MotionData referenceData = null;
                foreach (Asset a in Assets)
                {
                    if (a.Selected)
                    {
                        count += 1;
                        MotionData data = OfflineProcessing ? Editor.GetAsset(a.GUID) : Editor.LoadData(a.GUID);
                        while (!OfflineProcessing && !data.GetScene().isLoaded)
                        {
                            Debug.Log("Waiting for scene being loaded...");
                            yield return(new WaitForSeconds(0f));
                        }
                        //START OF PROCESSING

                        // Reset motion data
                        foreach (Frame frame in data.Frames)
                        {
                            frame.ResetTransformations();
                        }

                        // Global
                        data.RemoveAllModules();
                        data.MirrorAxis = Axis.ZPositive;

                        // 1on1 Ball Copying
                        {
                            string GetID(MotionData asset)
                            {
                                return(asset.GetName().Substring(0, asset.GetName().LastIndexOf("_P0")));
                            }

                            referenceBone = "Player 01:Ball";
                            if (data.GetName().Contains("_P0"))
                            {
                                if (data.GetName().Contains("_P01"))
                                {
                                    referenceData = data;
                                }
                                else if (GetID(data) == GetID(referenceData))
                                {
                                    data.SampleTransformations(referenceData, referenceBone);
                                }
                                else
                                {
                                    Debug.LogError("Skipping asset " + data.GetName() + " as information of P01 is not of same capture.");
                                }
                            }
                            else
                            {
                                Debug.LogError("Skipping asset " + data.GetName() + " as it does not contain player information.");
                            }
                        }

                        {
                            RootModule root = data.AddModule <RootModule>();
                            root.Root          = data.Source.FindBone("Player 01:Hips").Index;
                            root.RightShoulder = data.Source.FindBone("Player 01:LeftShoulder").Index;
                            root.LeftShoulder  = data.Source.FindBone("Player 01:RightShoulder").Index;
                            root.RightHip      = data.Source.FindBone("Player 01:LeftUpLeg").Index;
                            root.LeftHip       = data.Source.FindBone("Player 01:RightUpLeg").Index;
                            root.Neck          = data.Source.FindBone("Player 01:Neck").Index;
                            root.Hips          = data.Source.FindBone("Player 01:Hips").Index;
                            root.Smooth        = true;
                            root.Topology      = RootModule.TOPOLOGY.Biped;
                            root.ForwardAxis   = Axis.XPositive;
                        }

                        {
                            ContactModule contact = data.AddModule <ContactModule>();
                            contact.Clear();
                            contact.AddSensor("Player 01:LeftFootEnd", "Player 01:LeftFootEnd", Vector3.zero, 0.075f, 1f, -1);
                            contact.AddSensor("Player 01:RightFootEnd", "Player 01:RightFootEnd", Vector3.zero, 0.075f, 1f, -1);
                            contact.AddSensor("Player 01:LeftHand", "Player 01:LeftHand", new Vector3(-0.1f, 0f, 0f), 0.075f, 0f, -1);
                            contact.AddSensor("Player 01:RightHand", "Player 01:RightHand", new Vector3(-0.1f, 0f, 0f), 0.075f, 0f, -1);
                            contact.AddSensor("Player 01:Ball", "Player 01:Ball", Vector3.zero, 0.2f, 0f, LayerMask.GetMask("Ground"));
                            contact.CaptureContacts(Editor);
                        }

                        {
                            DribbleModule dribble = data.AddModule <DribbleModule>();
                            dribble.Area   = 2.5f;
                            dribble.Radius = 0.125f;
                            dribble.Axis   = Axis.YPositive;
                            Matrix4x4[] motion = dribble.CleanupBallTransformations(false);
                            for (int i = 0; i < data.Frames.Length; i++)
                            {
                                data.Frames[i].Transformations[dribble.Ball] = motion[i];
                            }
                            data.GetModule <ContactModule>().CaptureContacts(Editor);
                        }

                        {
                            StyleModule   style   = data.AddModule <StyleModule>();
                            RootModule    root    = data.GetModule <RootModule>();
                            DribbleModule dribble = data.GetModule <DribbleModule>();
                            style.Clear();
                            StyleModule.StyleFunction standing  = style.AddStyle("Stand");
                            StyleModule.StyleFunction moving    = style.AddStyle("Move");
                            StyleModule.StyleFunction dribbling = style.AddStyle("Dribble");
                            StyleModule.StyleFunction holding   = style.AddStyle("Hold");
                            StyleModule.StyleFunction shooting  = style.AddStyle("Shoot");
                            float threshold = 1f;
                            for (int f = 0; f < data.Frames.Length; f++)
                            {
                                float[] timestamps = data.SimulateTimestamps(data.Frames[f], 30);
                                float[] weights    = new float[timestamps.Length];
                                for (int j = 0; j < timestamps.Length; j++)
                                {
                                    weights[j] = Mathf.Clamp(root.GetRootVelocity(timestamps[j], false).magnitude, 0f, threshold);
                                    weights[j] = weights[j].Normalize(0f, threshold, 0, 1f);
                                    weights[j] = weights[j].SmoothStep(2f, 0.5f);
                                }
                                float weight = weights.Gaussian().SmoothStep(2f, 0.5f);
                                standing.Values[f]  = 1f - weight;
                                moving.Values[f]    = weight;
                                dribbling.Values[f] = dribble.IsDribbling(data.Frames[f].Timestamp, false) ? 1f : 0f;
                                holding.Values[f]   = dribble.IsHolding(data.Frames[f].Timestamp, false) ? 1f : 0f;
                                shooting.Values[f]  = dribble.IsShooting(data.Frames[f].Timestamp, false) ? 1f : 0f;
                            }
                            style.Mode = StyleModule.DRAWING.Frames;
                            style.GenerateKeys();
                        }

                        {
                            PhaseModule   phase   = data.AddModule <PhaseModule>();
                            ContactModule contact = data.GetModule <ContactModule>();
                            phase.Inspect = true;
                            phase.SetFunctions(contact.GetNames());
                            phase.ShowNormalized   = true;
                            phase.ShowHighlighted  = true;
                            phase.ShowValues       = true;
                            phase.ShowFitting      = true;
                            phase.ShowZero         = true;
                            phase.ShowPhase        = true;
                            phase.ShowWindow       = false;
                            phase.DisplayValues    = false;
                            phase.MaxIterations    = 50;
                            phase.Individuals      = 50;
                            phase.Elites           = 5;
                            phase.Exploration      = 0.2f;
                            phase.Memetism         = 0.1f;
                            phase.MaxFrequency     = 4f;
                            phase.RescalingMethod  = PhaseModule.Rescaling.Window;
                            phase.ApplyButterworth = true;
                            phase.StartFitting();
                            while (phase.IsFitting())
                            {
                                yield return(new WaitForSeconds(0f));
                            }
                        }

                        //END OF PROCESSING
                        data.MarkDirty();
                        a.Processed = true;
                    }
                }
                foreach (Asset a in Assets)
                {
                    if (a.Selected)
                    {
                        MotionData data = Editor.LoadData(a.GUID);
                        while (!data.GetScene().isLoaded)
                        {
                            Debug.Log("Waiting for scene being loaded...");
                            yield return(new WaitForSeconds(0f));
                        }
                        //START OF PROCESSING

                        data.GetModule <DribbleModule>().ComputeInteraction();

                        //END OF PROCESSING
                        if (SaveAfterProcessing)
                        {
                            data.MarkDirty(true, !OfflineProcessing);
                        }
                        a.Processed = true;
                        yield return(new WaitForSeconds(0f));
                    }
                }
                if (SaveAfterProcessing)
                {
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();
                }
            }

            if (Pipeline == PIPELINE.Quadruped)
            {
                foreach (Asset a in Assets)
                {
                    if (a.Selected)
                    {
                        count += 1;
                        MotionData data = OfflineProcessing ? Editor.GetAsset(a.GUID) : Editor.LoadData(a.GUID);
                        while (!OfflineProcessing && !data.GetScene().isLoaded)
                        {
                            Debug.Log("Waiting for scene being loaded...");
                            yield return(new WaitForSeconds(0f));
                        }
                        // START OF PROCESSING

                        // Reset motion data
                        foreach (Frame frame in data.Frames)
                        {
                            frame.ResetTransformations();
                        }

                        // Global
                        data.RemoveAllModules();
                        data.Scale      = 0.01f;
                        data.MirrorAxis = Axis.ZPositive;
                        data.Source.FindBone("Head").Alignment      = new Vector3(90f, 0f, 0f);
                        data.Source.FindBone("Tail").Alignment      = new Vector3(-45f, 0f, 0f);
                        data.Source.FindBone("Tail1").Alignment     = new Vector3(-45f, 0f, 0f);
                        data.Source.FindBone("Tail1Site").Alignment = new Vector3(-45f, 0f, 0f);

                        {
                            ContactModule contact = data.AddModule <ContactModule>();
                            contact.Clear();
                            contact.AddSensor("Hips", "Hips", Vector3.zero, 0.2f, 1f, LayerMask.GetMask("Ground"));
                            contact.AddSensor("Neck", "Neck", Vector3.zero, 0.25f, 1f, LayerMask.GetMask("Ground"));
                            contact.AddSensor("LeftHandSite", new string[] { "LeftForeArm", "LeftHandSite" }, Vector3.zero, 1f / 30f, 1f, LayerMask.GetMask("Ground"));
                            contact.AddSensor("RightHandSite", new string[] { "RightForeArm", "RightHandSite" }, Vector3.zero, 1f / 30f, 1f, LayerMask.GetMask("Ground"));
                            contact.AddSensor("LeftFootSite", "LeftFootSite", Vector3.zero, 1f / 30f, 1f, LayerMask.GetMask("Ground"));
                            contact.AddSensor("RightFootSite", "RightFootSite", Vector3.zero, 1f / 30f, 1f, LayerMask.GetMask("Ground"));
                            contact.CaptureContacts(Editor);
                        }

                        {
                            StyleModule style = data.AddModule <StyleModule>();
                            RootModule  root  = data.AddModule <RootModule>();
                            root.Topology = RootModule.TOPOLOGY.Quadruped;
                            ContactModule contact = data.GetModule <ContactModule>();
                            style.Clear();
                            StyleModule.StyleFunction idling   = style.AddStyle("Idle");
                            StyleModule.StyleFunction moving   = style.AddStyle("Move");
                            StyleModule.StyleFunction sitting  = style.AddStyle("Sit");
                            StyleModule.StyleFunction resting  = style.AddStyle("Rest");
                            StyleModule.StyleFunction standing = style.AddStyle("Stand");
                            StyleModule.StyleFunction jumping  = style.AddStyle("Jump");
                            StyleModule.StyleFunction speed    = style.AddStyle("Speed");
                            float[]        timeWindow          = data.GetTimeWindow(MotionEditor.GetInstance().PastWindow + MotionEditor.GetInstance().FutureWindow, 1f);
                            float[]        contactHeights      = new float[data.Frames.Length];
                            List <float[]> sitPatterns         = new List <float[]>()
                            {
                                new float[] { 1f, 0f, 1f, 1f, 1f, 1f },
                                new float[] { 1f, 0f, 0f, 1f, 1f, 1f },
                                new float[] { 1f, 0f, 1f, 0f, 1f, 1f }
                            };
                            List <float[]> restPatterns = new List <float[]>()
                            {
                                new float[] { 1f, 1f, 1f, 1f, 1f, 1f }
                            };
                            List <float[]> standPatterns = new List <float[]>()
                            {
                                new float[] { 1f, 0f, 0f, 0f, 1f, 1f }
                            };
                            List <float[]> jumpPatterns = new List <float[]>()
                            {
                                new float[] { 0f, 0f, 0f, 0f, 0f, 0f }
                            };
                            for (int i = 0; i < data.Frames.Length; i++)
                            {
                                for (int j = 0; j < contact.Sensors.Length; j++)
                                {
                                    contactHeights[i] += data.Frames[i].GetBoneTransformation(contact.Sensors[j].Bones.Last(), false).GetPosition().y;
                                }
                                contactHeights[i] /= contact.Sensors.Length;
                            }
                            for (int f = 0; f < data.Frames.Length; f++)
                            {
                                float weight = GetMovementWeight(data.Frames[f].Timestamp, 0.5f, 0.5f);
                                idling.Values[f] = 1f - weight;
                                moving.Values[f] = weight;
                                float   sit     = GetContactsWeight(data.Frames[f].Timestamp, 0.5f, contact, sitPatterns, 0f, 1f);
                                float   rest    = GetContactsWeight(data.Frames[f].Timestamp, 0.5f, contact, restPatterns, 0f, 1f);
                                float   stand   = GetContactsWeight(data.Frames[f].Timestamp, 0.5f, contact, standPatterns, 0f, 1f);
                                float   jump    = GetContactsWeight(data.Frames[f].Timestamp, 0.5f, contact, jumpPatterns, 0.3f, 0.1f);
                                float[] actions = new float[] { sit, rest, stand, jump };
                                Utility.SoftMax(ref actions);
                                sitting.Values[f]  = sit;
                                resting.Values[f]  = rest;
                                standing.Values[f] = stand;
                                jumping.Values[f]  = jump;
                                speed.Values[f]    = GetRootSpeed(data.Frames[f].Timestamp);
                            }

                            float GetRootSpeed(float timestamp)
                            {
                                return(Compute());

                                float Compute()
                                {
                                    Vector3[] positions = new Vector3[timeWindow.Length];
                                    for (int i = 0; i < timeWindow.Length; i++)
                                    {
                                        positions[i] = root.GetRootPosition(timestamp + timeWindow[i], false);
                                    }
                                    float length = 0f;

                                    for (int i = 1; i < positions.Length; i++)
                                    {
                                        length += Vector3.Distance(positions[i - 1], positions[i]);
                                    }
                                    return(length / (timeWindow.Last() - timeWindow.First()));
                                }
                            }

                            float GetMovementWeight(float timestamp, float window, float threshold)
                            {
                                float[] weights = new float[timeWindow.Length];
                                for (int j = 0; j < timeWindow.Length; j++)
                                {
                                    weights[j] = Mathf.Clamp(root.GetRootVelocity(timestamp + timeWindow[j], false).magnitude, 0f, threshold) / threshold;
                                }

                                float[] gradients = new float[weights.Length - 1];
                                for (int i = 0; i < gradients.Length; i++)
                                {
                                    gradients[i] = (weights[i + 1] - weights[i]) / (timeWindow[i + 1] - timeWindow[i]);
                                }
                                float gradient = Mathf.Abs(gradients.Gaussian());

                                return(weights.Gaussian(gradient).SmoothStep(2f, 0.5f));
                            }

                            float GetContactsWeight(float timestamp, float window, ContactModule module, List <float[]> patterns, float heightThreshold, float power)
                            {
                                float ContactGaussian(float t)
                                {
                                    float[] weights = new float[timeWindow.Length];
                                    for (int j = 0; j < timeWindow.Length; j++)
                                    {
                                        bool match = false;
                                        for (int i = 0; i < patterns.Count; i++)
                                        {
                                            float[] contacts = module.GetContacts(t + timeWindow[j], false);
                                            match = ArrayExtensions.Equal(contacts, patterns[i]).All(true);
                                            if (match)
                                            {
                                                break;
                                            }
                                        }
                                        if (match && heightThreshold != 0f && contactHeights[data.GetFrame(t).Index - 1] < heightThreshold)
                                        {
                                            match = false;
                                        }
                                        weights[j] = match ? 1f : 0f;
                                    }
                                    return(weights.Gaussian());
                                }

                                float weight = ContactGaussian(timestamp);

                                weight = Mathf.Pow(weight, 1f - weight);
                                return(Mathf.Pow(weight, power));
                            }

                            style.Mode = StyleModule.DRAWING.Frames;
                        }

                        {
                            PhaseModule phase = data.AddModule <PhaseModule>();
                            phase.Inspect = true;
                            RootModule    root    = data.GetModule <RootModule>();
                            ContactModule contact = data.GetModule <ContactModule>();
                            phase.SetFunctions(contact.GetNames());
                            phase.ShowNormalized   = true;
                            phase.ShowHighlighted  = true;
                            phase.ShowValues       = true;
                            phase.ShowFitting      = true;
                            phase.ShowZero         = true;
                            phase.ShowPhase        = true;
                            phase.ShowWindow       = false;
                            phase.DisplayValues    = false;
                            phase.MaxIterations    = 50;
                            phase.Individuals      = 100;
                            phase.Elites           = 10;
                            phase.Exploration      = 0.2f;
                            phase.Memetism         = 0.1f;
                            phase.MaxFrequency     = 4f;
                            phase.RescalingMethod  = PhaseModule.Rescaling.Window;
                            phase.ApplyButterworth = true;

                            phase.StartFitting();
                            while (phase.IsFitting())
                            {
                                yield return(new WaitForSeconds(0f));
                            }
                        }

                        //END OF PROCESSING
                        if (SaveAfterProcessing)
                        {
                            data.MarkDirty(true, !OfflineProcessing);
                        }
                        a.Processed = true;
                        yield return(new WaitForSeconds(0f));
                    }
                }

                for (int i = 0; i < Editor.Assets.Length; i++)
                {
                    Editor.GetAsset(i).ResetSequences();
                    Editor.GetAsset(i).Export = false;
                }
                Editor.GetAsset(0).Export = true;
                Editor.GetAsset(0).SetSequence(0, 180, 1531);
                Editor.GetAsset(2).Export = true;
                Editor.GetAsset(2).SetSequence(0, 680, 820);
                Editor.GetAsset(6).Export = true;
                Editor.GetAsset(6).SetSequence(0, 90, 593);
                Editor.GetAsset(7).Export = true;
                Editor.GetAsset(7).SetSequence(0, 290, 1072);
                Editor.GetAsset(8).Export = true;
                Editor.GetAsset(8).SetSequence(0, 1, 50);
                Editor.GetAsset(8).SetSequence(1, 400, 911);
                Editor.GetAsset(9).Export  = true;
                Editor.GetAsset(10).Export = true;
                Editor.GetAsset(10).SetSequence(0, 230, 548);
                Editor.GetAsset(11).Export = true;
                Editor.GetAsset(11).SetSequence(0, 400, 567);
                Editor.GetAsset(12).Export = true;
                Editor.GetAsset(13).Export = true;
                Editor.GetAsset(14).Export = true;
                Editor.GetAsset(16).Export = true;
                Editor.GetAsset(16).SetSequence(0, 200, 550);
                Editor.GetAsset(17).Export = true;
                Editor.GetAsset(17).SetSequence(0, 470, 720);
                Editor.GetAsset(18).Export = true;
                Editor.GetAsset(18).SetSequence(0, 175, 395);
                Editor.GetAsset(19).Export = true;
                Editor.GetAsset(19).SetSequence(0, 300, 750);
                Editor.GetAsset(19).SetSequence(1, 1040, 1079);
                Editor.GetAsset(20).Export = true;
                Editor.GetAsset(21).Export = true;
                Editor.GetAsset(21).SetSequence(0, 1, 1300);
                Editor.GetAsset(21).SetSequence(1, 2950, 3530);
                Editor.GetAsset(21).SetSequence(2, 3730, 4200);
                Editor.GetAsset(22).Export = true;
                Editor.GetAsset(23).Export = true;
                Editor.GetAsset(23).Export = true;
                Editor.GetAsset(24).Export = true;
                Editor.GetAsset(24).SetSequence(0, 200, 630);
                Editor.GetAsset(25).Export = true;
                Editor.GetAsset(25).SetSequence(0, 1, 2690);
                Editor.GetAsset(25).SetSequence(1, 2760, 4336);
                Editor.GetAsset(26).Export = true;
                Editor.GetAsset(27).Export = true;
                Editor.GetAsset(27).SetSequence(0, 1, 1100);
                Editor.GetAsset(27).SetSequence(1, 2820, 3940);
                Editor.GetAsset(27).SetSequence(2, 4100, 4500);
                Editor.GetAsset(27).SetSequence(3, 5660, 6010);
                Editor.GetAsset(27).SetSequence(4, 6600, 7200);
                Editor.GetAsset(27).SetSequence(5, 12300, 12850);
                Editor.GetAsset(27).SetSequence(6, 13200, 13399);
                Editor.GetAsset(28).Export = true;
                Editor.GetAsset(28).SetSequence(0, 920, 985);
                Editor.GetAsset(28).SetSequence(1, 1700, 1907);
                Editor.GetAsset(29).Export = true;
                Editor.GetAsset(29).SetSequence(0, 250, 790);
                Editor.GetAsset(29).SetSequence(1, 970, 1575);
                Editor.GetAsset(29).SetSequence(2, 1630, 1750);
                Editor.GetAsset(30).Export = true;
                Editor.GetAsset(30).SetSequence(0, 1790, 1920);
                Editor.GetAsset(30).SetSequence(1, 2070, 2470);
                Editor.GetAsset(30).SetSequence(2, 2770, 3025);
                Editor.GetAsset(31).Export = true;
                Editor.GetAsset(31).SetSequence(0, 170, 500);
                Editor.GetAsset(31).SetSequence(1, 1250, 2460);
                Editor.GetAsset(31).SetSequence(2, 3040, 3200);
                Editor.GetAsset(31).SetSequence(3, 4680, 6550);
                Editor.GetAsset(31).SetSequence(4, 7600, 9450);
                Editor.GetAsset(31).SetSequence(5, 11540, 11691);
                Editor.GetAsset(32).Export = true;
                Editor.GetAsset(32).SetSequence(0, 1, 300);
                Editor.GetAsset(32).SetSequence(1, 1360, 1540);
                Editor.GetAsset(32).SetSequence(2, 2380, 3086);
                Editor.GetAsset(33).Export = true;
                Editor.GetAsset(33).SetSequence(0, 1, 1170);
                Editor.GetAsset(33).SetSequence(1, 1980, 2160);
                Editor.GetAsset(33).SetSequence(2, 7830, 8090);
                Editor.GetAsset(34).Export = true;
                Editor.GetAsset(34).SetSequence(0, 1, 270);
                Editor.GetAsset(34).SetSequence(1, 2490, 2856);
                Editor.GetAsset(35).Export = true;
                Editor.GetAsset(37).Export = true;
                Editor.GetAsset(38).Export = true;
                Editor.GetAsset(38).SetSequence(0, 3330, 3900);
                Editor.GetAsset(39).Export = true;
                Editor.GetAsset(39).SetSequence(0, 880, 920);
                Editor.GetAsset(39).SetSequence(1, 1280, 5052);
                Editor.GetAsset(41).Export = true;
                Editor.GetAsset(41).SetSequence(0, 4690, 6190);
                Editor.GetAsset(42).Export = true;
                Editor.GetAsset(42).SetSequence(0, 900, 3594);
                Editor.GetAsset(43).Export = true;
                Editor.GetAsset(43).SetSequence(0, 1, 500);
                Editor.GetAsset(43).SetSequence(1, 4340, 4577);
                Editor.GetAsset(44).Export = true;
                Editor.GetAsset(44).SetSequence(0, 1, 700);
                Editor.GetAsset(44).SetSequence(1, 950, 2000);
                Editor.GetAsset(45).Export = true;
                Editor.GetAsset(45).SetSequence(0, 1, 410);
                Editor.GetAsset(45).SetSequence(1, 680, 778);
                Editor.GetAsset(46).Export = true;
                Editor.GetAsset(46).SetSequence(0, 175, 235);
                Editor.GetAsset(47).Export = true;
                Editor.GetAsset(47).SetSequence(0, 275, 498);
                Editor.GetAsset(48).Export = true;
                Editor.GetAsset(48).SetSequence(0, 1, 220);
                Editor.GetAsset(48).SetSequence(1, 675, 748);
                Editor.GetAsset(49).Export = true;
                Editor.GetAsset(49).SetSequence(0, 1, 700);
                Editor.GetAsset(49).SetSequence(1, 1510, 8300);
                Editor.GetAsset(50).Export = true;
                Editor.GetAsset(50).SetSequence(0, 200, 1000);
                Editor.GetAsset(50).SetSequence(1, 1850, 2100);
                Editor.GetAsset(50).SetSequence(2, 4150, 4700);
                Editor.GetAsset(50).SetSequence(3, 5030, 5356);

                //Mark for saving
                for (int i = 0; i < Editor.Assets.Length; i++)
                {
                    Editor.GetAsset(i).MarkDirty(true, false);
                }

                if (SaveAfterProcessing)
                {
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();
                }
            }

            Processing = false;
            foreach (Asset a in Assets)
            {
                a.Processed = false;
            }
            yield return(new WaitForSeconds(0f));

            Debug.Log("Finished processing " + count + " assets.");
        }
    }
示例#10
0
    private IEnumerator ExportData()
    {
        if (Editor == null)
        {
            Debug.Log("No editor found.");
        }
        else if (!System.IO.Directory.Exists(GetExportPath()))
        {
            Debug.Log("No export folder found at " + GetExportPath() + ".");
        }
        else
        {
            Aborting  = false;
            Exporting = true;
            Progress  = 0f;

            int      sequence  = 0;
            int      items     = 0;
            int      samples   = 0;
            DateTime timestamp = Utility.GetTimestamp();

            StreamWriter S = CreateFile("Sequences");
            Data         X = new Data(CreateFile("Input"), CreateFile("InputNorm"), CreateFile("InputLabels"));
            Data         Y = new Data(CreateFile("Output"), CreateFile("OutputNorm"), CreateFile("OutputLabels"));
            StreamWriter CreateFile(string name)
            {
                return(File.CreateText(GetExportPath() + "/" + name + ".txt"));
            }

            for (int i = 0; i < Assets.Length; i++)
            {
                Assets[i].Exported = false;
            }
            for (int i = 0; i < Assets.Length; i++)
            {
                if (Aborting)
                {
                    break;
                }
                if (Assets[i].Selected)
                {
                    MotionData data = Editor.LoadData(Assets[i].GUID);
                    while (!data.GetScene().isLoaded)
                    {
                        Debug.Log("Waiting for scene being loaded...");
                        yield return(new WaitForSeconds(0f));
                    }
                    if (!data.Export)
                    {
                        Debug.Log("Skipping Asset: " + data.GetName());
                        yield return(new WaitForSeconds(0f));

                        continue;
                    }
                    for (int m = 1; m <= 2; m++)
                    {
                        if (m == 1)
                        {
                            Editor.SetMirror(false);
                        }
                        if (m == 2)
                        {
                            Editor.SetMirror(true);
                        }
                        if (!Editor.Mirror || WriteMirror && Editor.Mirror)
                        {
                            // Debug.Log("Exporting asset " + data.GetName() + " " + (Editor.Mirror ? "[Mirror]" : "[Default]"));
                            for (int shift = 0; shift <= FrameShifts; shift++)
                            {
                                foreach (Sequence seq in data.Sequences)
                                {
                                    sequence += 1;
                                    float start = Editor.CeilToTargetTime(data.GetFrame(seq.Start).Timestamp);
                                    float end   = Editor.FloorToTargetTime(data.GetFrame(seq.End).Timestamp);
                                    int   index = 0;
                                    while (start + (index + 1) / Editor.TargetFramerate + shift / data.Framerate <= end)
                                    {
                                        Editor.SetRandomSeed(Editor.GetCurrentFrame().Index);
                                        S.WriteLine(sequence.ToString());

                                        float tCurrent = start + index / Editor.TargetFramerate + shift / data.Framerate;
                                        float tNext    = start + (index + 1) / Editor.TargetFramerate + shift / data.Framerate;

                                        if (Pipeline == PIPELINE.Basketball)
                                        {
                                            BasketballSetup.Export(this, X, Y, tCurrent, tNext);
                                        }
                                        if (Pipeline == PIPELINE.Quadruped)
                                        {
                                            QuadrupedSetup.Export(this, X, Y, tCurrent, tNext);
                                        }

                                        X.Store();
                                        Y.Store();

                                        index   += 1;
                                        Progress = (index / Editor.TargetFramerate) / (end - start);
                                        items   += 1;
                                        samples += 1;
                                        if (items >= FrameBuffer)
                                        {
                                            Performance = items / (float)Utility.GetElapsedTime(timestamp);
                                            timestamp   = Utility.GetTimestamp();
                                            items       = 0;
                                            yield return(new WaitForSeconds(0f));
                                        }
                                    }
                                    Progress = 0f;
                                }
                            }
                        }
                    }
                    Assets[i].Exported = true;
                }
            }

            S.Close();
            X.Finish();
            Y.Finish();

            Aborting  = false;
            Exporting = false;
            Progress  = 0f;
            foreach (Asset a in Assets)
            {
                a.Exported = false;
            }
            yield return(new WaitForSeconds(0f));

            Debug.Log("Exported " + samples + " samples.");
        }
    }
示例#11
0
    public string[] GenerateLines(MotionData data)
    {
        string[] result = new string[data.Frames.Length - 60];

        for (int i = 30; i < data.Frames.Length - 30; i++)
        {
            if (data.Modules != null)
            {
                for (int k = 0; k < data.Modules.Length; k++)
                {
                    switch (data.Modules[k].GetID())
                    {
                    case Module.ID.Velocity:
                    {
                        VelocityModule module = (VelocityModule)data.GetModule(Module.ID.Velocity);
                        module.GetTransformations(data.GetFrame(i));
                        module.GetVelocities(data.GetFrame(i), 1.0f);
                        for (int j = 0; j < data.Root.Bones.Length; j++)
                        {
                            if (module.Selected[j])
                            {
                                result[i - 30] += data.Frames[i].GetBoneTransformation(j, data.Mirrored).GetPosition().x.ToString() + " ";
                                result[i - 30] += data.Frames[i].GetBoneTransformation(j, data.Mirrored).GetPosition().y.ToString() + " ";
                                result[i - 30] += data.Frames[i].GetBoneTransformation(j, data.Mirrored).GetPosition().z.ToString() + " ";
                                result[i - 30] += data.Frames[i].GetBoneTransformation(j, data.Mirrored).GetForward().x.ToString() + " ";
                                result[i - 30] += data.Frames[i].GetBoneTransformation(j, data.Mirrored).GetForward().y.ToString() + " ";
                                result[i - 30] += data.Frames[i].GetBoneTransformation(j, data.Mirrored).GetForward().z.ToString() + " ";
                                result[i - 30] += data.Frames[i].GetBoneTransformation(j, data.Mirrored).GetUp().x.ToString() + " ";
                                result[i - 30] += data.Frames[i].GetBoneTransformation(j, data.Mirrored).GetUp().y.ToString() + " ";
                                result[i - 30] += data.Frames[i].GetBoneTransformation(j, data.Mirrored).GetUp().z.ToString() + " ";
                                result[i - 30] += module.Velocities[j].x.ToString() + " ";
                                result[i - 30] += module.Velocities[j].y.ToString() + " ";
                                result[i - 30] += module.Velocities[j].z.ToString() + " ";
                            }
                        }
                        break;
                    }

                    case Module.ID.Action:
                    {
                        ActionModule module = (ActionModule)data.GetModule(Module.ID.Action);
                        result[i - 30] += module.GetHotVector(i) + " ";

                        break;
                    }

                    case Module.ID.Phase:
                    {
                        PhaseModule module = (PhaseModule)data.GetModule(Module.ID.Phase);
                        result[i - 30] += module.Phases[1].LocalPhase.Phase[i] + " ";
                        result[i - 30] += module.Phases[2].LocalPhase.Phase[i] + " ";
                        break;
                    }

                    case Module.ID.Trajectory:
                    {
                        TrajectoryModule module = (TrajectoryModule)data.GetModule(Module.ID.Trajectory);
                        for (int j = 0; j < module.Pivots.Length; j++)
                        {
                            for (int p = 0; p < module.Pivots[j].Transformations.Length; p++)
                            {
                                result[i - 30] += module.Pivots[j].Transformations[p].GetPosition().x.ToString() + " ";
                                result[i - 30] += module.Pivots[j].Transformations[p].GetPosition().y.ToString() + " ";
                                result[i - 30] += module.Pivots[j].Transformations[p].GetPosition().z.ToString() + " ";
                                result[i - 30] += module.Pivots[j].Velocities[p].x.ToString() + " ";
                                result[i - 30] += module.Pivots[j].Velocities[p].y.ToString() + " ";
                                result[i - 30] += module.Pivots[j].Velocities[p].z.ToString() + " ";
                                result[i - 30] += module.Pivots[j].HotVectors[p] + " ";
                                result[i - 30] += module.Pivots[j].Phases[p].ToString() + " ";
                            }
                        }
                        break;
                    }
                    }
                }
            }
        }

        return(result);
    }