コード例 #1
0
        public void Destroy()
        {
            if (graph.IsValid())
            {
                graph.Destroy();
            }

            asBuffor = null;
        }
コード例 #2
0
 public void ClearMainMixerInput()
 {
     for (int i = 0; i < mixer.GetInputCount(); i++)
     {
         mixer.GetInput(i).Destroy();
     }
     mixer.SetInputCount(0);
     asBuffor = null;
 }
コード例 #3
0
        private void PreviewButtonAction()
        {
            creator.gameObjectTransform.position = pos;
            creator.gameObjectTransform.rotation = rot;

            sliderTime       = 0f;
            sliderTimeBuffor = sliderTime;

            if (graph == null)
            {
                graph = new PreparingDataPlayableGraph();
                graph.Initialize(creator.gameObjectTransform.gameObject);
            }
            else if (!graph.IsValid())
            {
                graph.Initialize(creator.gameObjectTransform.gameObject);
            }

            graph.ClearMainMixerInput();

            switch (selectedCreator)
            {
            case 1:
                BlendTreeInfo blendTree = creator.blendTrees[creator.selectedBlendTree];
                if (blendTree.IsValid())
                {
                    blendTree.CreateGraphFor(
                        creator.gameObjectTransform.gameObject,
                        graph
                        );
                    isSequencePlaying = false;

                    maxSliderTime = blendTree.GetLength();
                }
                break;

            case 2:
                AnimationsSequence seq = creator.sequences[creator.selectedSequence];
                if (seq.IsValid())
                {
                    seq.CreateAnimationsInTime(0f, graph);
                    isSequencePlaying = true;
                    maxSliderTime     = seq.length;
                }
                break;
            }
        }
コード例 #4
0
        public bool CreateAnimationDataPlayables(MotionMatchingData data, float time = 0f)
        {
            currentMMData = data;
            if (this.IsValid())
            {
                this.ClearMainMixerInput();
                switch (data.dataType)
                {
                case AnimationDataType.SingleAnimation:
                    AddClipPlayable(data.clips[0], time, 1f);
                    break;

                case AnimationDataType.BlendTree:
                    for (int i = 0; i < data.blendTreeWeights.Length; i++)
                    {
                        AddClipPlayable(data.clips[i], time, data.blendTreeWeights[i]);
                    }
                    break;

                case AnimationDataType.AnimationSequence:
                    asBuffor = new AnimationsSequence("seq");
                    for (int i = 0; i < data.clips.Count; i++)
                    {
                        asBuffor.AddClip(data.clips[i]);
                        asBuffor.neededInfo[i] = data.animationSeqInfos[i];
                    }

                    asBuffor.CreateAnimationsInTime(time, this);
                    break;
                }

                this.Evaluate(0);
                return(true);
            }

            return(false);
        }
        public static void DrawSequencesList(DataCreator creator, EditorWindow editor)
        {
            GUILayoutElements.DrawHeader("Sequences", GUIResources.GetDarkHeaderStyle_MD());

            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Add"))
            {
                creator.sequences.Add(new AnimationsSequence("new Sequence"));
            }
            //if (GUILayout.Button("Clear"))
            //{
            //    creator.sequences.Clear();
            //}
            GUILayout.EndHorizontal();
            GUILayout.Space(5);

            GUILayout.BeginHorizontal();
            GUILayout.Label("Find", GUILayout.Width(50));
            creator.findingSequence = EditorGUILayout.TextField(creator.findingSequence);
            GUILayout.EndHorizontal();

            GUILayout.Space(5);

            for (int i = 0; i < creator.sequences.Count; i++)
            {
                if (creator.findingSequence != "" && !creator.sequences[i].name.ToLower().Contains(creator.findingSequence.ToLower()))
                {
                    continue;
                }
                GUILayout.BeginHorizontal();
                GUILayout.Space(5);
                GUILayout.Label(
                    creator.sequences[i].name,
                    i == creator.selectedSequence ? GUIResources.GetDarkHeaderStyle_SM() : GUIResources.GetLightHeaderStyle_SM()
                    );


                Event e = Event.current;
                Rect  r = GUILayoutUtility.GetLastRect();

                if (r.Contains(e.mousePosition) && e.type == EventType.MouseDown && e.button == 0)
                {
                    if (creator.selectedSequence == i)
                    {
                        creator.selectedSequence = -1;
                    }
                    else
                    {
                        creator.selectedSequence = i;
                    }
                    e.Use();
                    editor.Repaint();
                }

                if (GUILayout.Button("Copy", GUILayout.Width(40)))
                {
                    AnimationsSequence info = new AnimationsSequence(creator.sequences[i].name + "_New");

                    for (int j = 0; j < creator.sequences[i].clips.Count; j++)
                    {
                        info.clips.Add(creator.sequences[i].clips[j]);
                        info.neededInfo.Add(creator.sequences[i].neededInfo[j]);
                        info.findPoseInClip.Add(creator.sequences[i].findPoseInClip[j]);
                    }

                    creator.sequences.Insert(i + 1, info);
                }
                if (GUILayout.Button("X", GUILayout.Width(25)))
                {
                    creator.sequences.RemoveAt(i);
                    i--;
                }
                GUILayout.Space(10);
                GUILayout.EndHorizontal();
                GUILayout.Space(5);
            }
            GUILayout.Space(5);
        }
        private static void DrawSequence(DataCreator creator, AnimationsSequence seq, int index, float rectWidth)
        {
            if (seq.findPoseInClip.Count != seq.clips.Count)
            {
                for (int i = 0; i < seq.clips.Count; i++)
                {
                    seq.findPoseInClip.Add(true);
                }
            }
            GUILayoutElements.DrawHeader(
                seq.name,
                GUIResources.GetLightHeaderStyle_MD()
                );

            GUILayout.Space(5);

            seq.name = EditorGUILayout.TextField(
                new GUIContent("Animation sequence name"),
                seq.name
                );

            //seq.loop = EditorGUILayout.Toggle(
            //    new GUIContent("Loop"),
            //    seq.loop
            //    );

            seq.findInYourself  = EditorGUILayout.Toggle(new GUIContent("Find in yourself"), seq.findInYourself);
            seq.blendToYourself = EditorGUILayout.Toggle(new GUIContent("Blend to yourself"), seq.blendToYourself);

            GUILayout.Space(5);
            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Add clip"))
            {
                seq.AddClip(null);
            }
            GUILayout.EndHorizontal();
            GUILayout.Space(5);
            float floatWidth  = 60f;
            float buttonWidth = 25f;
            float findPose    = 60f;

            GUILayout.BeginHorizontal();
            GUILayout.Label("Animation");
            GUILayout.Label("Find pose", GUILayout.Width(findPose));
            GUILayout.Label("Start", GUILayout.Width(floatWidth));
            GUILayout.Label("End", GUILayout.Width(floatWidth));
            GUILayout.Label("Blend", GUILayout.Width(floatWidth));
            GUILayout.Space(buttonWidth);
            GUILayout.EndHorizontal();
            for (int i = 0; i < seq.clips.Count; i++)
            {
                GUILayout.BeginHorizontal();
                //GUILayout.Label(string.Format("{0}.", i + 1));
                seq.clips[i] = (AnimationClip)EditorGUILayout.ObjectField(
                    seq.clips[i],
                    typeof(AnimationClip),
                    true
                    );

                seq.findPoseInClip[i] = EditorGUILayout.Toggle(seq.findPoseInClip[i], GUILayout.Width(findPose));

                float x = seq.neededInfo[i].x;
                float y = seq.neededInfo[i].y;
                float z = seq.neededInfo[i].z;

                //GUILayout.Label("Start time");
                x = EditorGUILayout.FloatField(x, GUILayout.Width(floatWidth));
                //GUILayout.Label("Blend start time");
                y = EditorGUILayout.FloatField(y, GUILayout.Width(floatWidth));
                //GUILayout.Label("Blend time");
                z = EditorGUILayout.FloatField(z, GUILayout.Width(floatWidth));

                seq.neededInfo[i] = new Vector3(x, y, z);

                if (GUILayout.Button("X", GUILayout.Width(buttonWidth)))
                {
                    seq.RemoveAnimationsAt(i);
                }
                GUILayout.EndHorizontal();
            }

            GUILayout.Space(10);

            //deltaTimeCaculation = Time.realtimeSinceStartup;

            seq.CalculateLength();
            GUILayout.Label(string.Format("Sequence length: \t {0}", seq.length));


            GUILayout.Space(10);
        }
        public static MotionMatchingData CalculateAnimationSequenceData(
            string name,
            AnimationsSequence seq,
            GameObject go,
            PreparingDataPlayableGraph graph,
            List <Transform> bonesMask,
            List <Vector2> bonesWeights,
            int sampling,
            bool loop,
            Transform root,
            List <float> trajectoryStepTimes,
            bool blendToYourself,
            bool findInYourself
            )
        {
            if (!graph.IsValid())
            {
                graph.Initialize(go);
            }

            go.transform.position = Vector3.zero;
            go.transform.rotation = Quaternion.identity;

            seq.CalculateLength();

            #region need floats
            float frameTime      = 1f / (float)sampling;
            int   numberOfFrames = Mathf.FloorToInt(seq.length / frameTime) + 1;
            #endregion

            MotionMatchingData data = new MotionMatchingData(
                seq.clips.ToArray(),
                seq.neededInfo.ToArray(),
                sampling,
                name,
                loop,
                seq.length,
                findInYourself,
                blendToYourself,
                AnimationDataType.AnimationSequence
                );

            FrameData  frameBuffer;
            BoneData   boneBuffer;
            PoseData   poseBuffor;
            Trajectory trajectoryBuffor;

            NeedValueToCalculateData[] previuData = new NeedValueToCalculateData[bonesMask.Count];
            NeedValueToCalculateData[] nextData   = new NeedValueToCalculateData[bonesMask.Count];

            int seqDeltaSampling = 3;
            //seq.CreatePlayableGraph(playableGraph, go);
            //seq.Update(-frameTime, playableGraph, seqDeltaSampling);

            seq.CreateAnimationsInTime(0f, graph);
            graph.Evaluate(frameTime);
            seq.Update(graph, frameTime);

            int frameIndex = 0;

            for (; frameIndex < numberOfFrames; frameIndex++)
            {
                for (int i = 0; i < bonesMask.Count; i++)
                {
                    previuData[i] = GetValuesFromTransform(bonesMask[i], root);
                }

                graph.Evaluate(frameTime);
                seq.Update(graph, frameTime);
                //Debug.Log((float)animator.GetMixerInputTime(0) - clip.length);

                for (int i = 0; i < bonesMask.Count; i++)
                {
                    nextData[i] = GetValuesFromTransform(bonesMask[i], root);
                }

                poseBuffor = new PoseData(bonesMask.Count);
                for (int i = 0; i < bonesMask.Count; i++)
                {
                    float2     boneWeight    = bonesWeights[i];
                    float3     velocity      = BoneData.CalculateVelocity(previuData[i].position, nextData[i].position, frameTime);
                    float3     localPosition = previuData[i].position;
                    quaternion orientation   = previuData[i].rotation;
                    boneBuffer = new BoneData(localPosition, velocity);
                    poseBuffor.SetBone(boneBuffer, i);
                }

                trajectoryBuffor = new Trajectory(trajectoryStepTimes.Count);

                frameBuffer = new FrameData(
                    frameIndex,
                    frameIndex * frameTime,
                    trajectoryBuffor,
                    poseBuffor,
                    new FrameSections(true)
                    );
                data.AddFrame(frameBuffer);
            }


            float   clipGlobalStart;
            Vector2 clipStartAndStop;
            float   recordingClipTime;

            if (trajectoryStepTimes[0] < 0)
            {
                clipGlobalStart = trajectoryStepTimes[0];

                clipStartAndStop = new Vector2(-clipGlobalStart, -clipGlobalStart + seq.length);
            }
            else
            {
                clipGlobalStart  = 0;
                clipStartAndStop = new Vector2(0, seq.length);
            }

            if (trajectoryStepTimes[trajectoryStepTimes.Count - 1] > 0)
            {
                recordingClipTime = clipStartAndStop.y + trajectoryStepTimes[trajectoryStepTimes.Count - 1] + 0.1f;
            }
            else
            {
                recordingClipTime = clipStartAndStop.y + 0.1f;
            }

            int   samplesPerSecond = 100;
            float deltaTime        = 1f / (float)samplesPerSecond;
            int   dataCount        = Mathf.CeilToInt(recordingClipTime / deltaTime);

            NeedValueToCalculateData[] recordData = new NeedValueToCalculateData[dataCount];

            go.transform.position = Vector3.zero;
            go.transform.rotation = Quaternion.identity;

            //seq.Update(clipGlobalStart, playableGraph);

            graph.ClearMainMixerInput();

            seq.CreateAnimationsInTime(clipGlobalStart, graph);

            recordData[0] = new NeedValueToCalculateData(
                go.transform.position,
                go.transform.forward,
                go.transform.rotation
                );

            for (int i = 0; i < dataCount; i++)
            {
                graph.Evaluate(deltaTime);
                seq.Update(graph, deltaTime);
                recordData[i] = new NeedValueToCalculateData(
                    go.transform.position,
                    go.transform.forward,
                    go.transform.rotation
                    );
            }

            //clearing graph from all animations
            graph.ClearMainMixerInput();

            MotionDataCalculator.CalculateTrajectoryPointsFromRecordData(
                data,
                recordData,
                recordingClipTime,
                deltaTime,
                clipStartAndStop,
                trajectoryStepTimes
                );

            data.usedFrameCount = data.numberOfFrames;

            data.trajectoryPointsTimes = new List <float>();

            for (int i = 0; i < trajectoryStepTimes.Count; i++)
            {
                data.trajectoryPointsTimes.Add(trajectoryStepTimes[i]);
            }

            return(data);
        }