Exemplo n.º 1
0
 private Vector3[] GetPivotVectors(TimelineControl.PivotSample[] samples, float width, Rect rect, float height, bool loop)
 {
     Vector3[] result;
     if (samples.Length == 0 || width < 0.33f)
     {
         result = new Vector3[0];
     }
     else
     {
         List <Vector3> list = new List <Vector3>();
         for (int i = 0; i < samples.Length; i++)
         {
             TimelineControl.PivotSample pivotSample = samples[i];
             Vector3 zero = Vector3.zero;
             zero.x = this.m_TimeArea.TimeToPixel(pivotSample.m_Time, rect);
             zero.y = height / 16f + pivotSample.m_Weight * 12f * height / 16f;
             list.Add(zero);
         }
         if (loop && list[list.Count - 1].x <= rect.width)
         {
             float          x     = list[list.Count - 1].x;
             int            num   = 0;
             int            num2  = 1;
             List <Vector3> list2 = new List <Vector3>();
             while (x < rect.width)
             {
                 if (num > list.Count - 1)
                 {
                     num = 0;
                     num2++;
                 }
                 Vector3 item = list[num];
                 item.x += (float)num2 * width;
                 x       = item.x;
                 list2.Add(item);
                 num++;
             }
             list.AddRange(list2);
         }
         List <Vector3> controls = this.GetControls(list, 0.5f);
         list.Clear();
         for (int j = 0; j < controls.Count - 3; j += 3)
         {
             Vector3 p  = controls[j];
             Vector3 p2 = controls[j + 1];
             Vector3 p3 = controls[j + 2];
             Vector3 p4 = controls[j + 3];
             if (j == 0)
             {
                 list.Add(this.CalculatePoint(0f, p, p2, p3, p4));
             }
             for (int k = 1; k <= 10; k++)
             {
                 list.Add(this.CalculatePoint((float)k / 10f, p, p2, p3, p4));
             }
         }
         result = list.ToArray();
     }
     return(result);
 }
Exemplo n.º 2
0
        private void ResampleTransition(AnimatorStateTransition transition, AvatarMask layerMask, TransitionPreview.TransitionInfo info, Animator previewObject)
        {
            this.m_IsResampling    = true;
            this.m_MustResample    = false;
            this.m_ValidTransition = true;
            bool flag = this.m_RefTransition != transition;

            this.m_RefTransition     = transition;
            this.m_RefTransitionInfo = info;
            this.m_LayerMask         = layerMask;
            if (this.m_AvatarPreview != null)
            {
                this.m_AvatarPreview.OnDestroy();
                this.m_AvatarPreview = null;
            }
            this.ClearController();
            Motion motion = this.m_RefSrcState.motion;

            this.Init(previewObject, (!(motion != null)) ? this.m_RefDstState.motion : motion);
            if (this.m_Controller == null)
            {
                this.m_IsResampling = false;
            }
            else
            {
                this.m_AvatarPreview.Animator.allowConstantClipSamplingOptimization = false;
                this.m_StateMachine.defaultState = this.m_DstState;
                this.m_Transition.mute           = true;
                AnimatorController.SetAnimatorController(this.m_AvatarPreview.Animator, this.m_Controller);
                this.m_AvatarPreview.Animator.Update(1E-05f);
                this.WriteParametersInController();
                this.m_AvatarPreview.Animator.SetLayerWeight(this.m_LayerIndex, 1f);
                float length = this.m_AvatarPreview.Animator.GetCurrentAnimatorStateInfo(this.m_LayerIndex).length;
                this.m_StateMachine.defaultState = this.m_SrcState;
                this.m_Transition.mute           = false;
                AnimatorController.SetAnimatorController(this.m_AvatarPreview.Animator, this.m_Controller);
                this.m_AvatarPreview.Animator.Update(1E-05f);
                this.WriteParametersInController();
                this.m_AvatarPreview.Animator.SetLayerWeight(this.m_LayerIndex, 1f);
                float length2 = this.m_AvatarPreview.Animator.GetCurrentAnimatorStateInfo(this.m_LayerIndex).length;
                if (this.m_LayerIndex > 0)
                {
                    this.m_AvatarPreview.Animator.stabilizeFeet = false;
                }
                float num = length2 * this.m_RefTransition.exitTime + this.m_Transition.duration * ((!this.m_RefTransition.hasFixedDuration) ? length2 : 1f) + length;
                if (num > 2000f)
                {
                    Debug.LogWarning("Transition duration is longer than 2000 second, Disabling previewer.");
                    this.m_ValidTransition = false;
                    this.m_IsResampling    = false;
                }
                else
                {
                    float num2 = (this.m_RefTransition.exitTime <= 0f) ? length2 : (length2 * this.m_RefTransition.exitTime);
                    float num3 = (num2 <= 0f) ? 0.0333333351f : Mathf.Min(Mathf.Max(num2 / 300f, 0.0333333351f), num2 / 5f);
                    float num4 = (length <= 0f) ? 0.0333333351f : Mathf.Min(Mathf.Max(length / 300f, 0.0333333351f), length / 5f);
                    num3 = Mathf.Max(num3, num / 600f);
                    num4 = Mathf.Max(num4, num / 600f);
                    float num5  = num3;
                    float num6  = 0f;
                    bool  flag2 = false;
                    bool  flag3 = false;
                    bool  flag4 = false;
                    if (this.m_RefTransition.exitTime == 0f)
                    {
                        this.m_AvatarPreview.Animator.CrossFade(0, 0f, 0, 0.9999f);
                    }
                    this.m_AvatarPreview.Animator.StartRecording(-1);
                    this.m_LeftStateWeightA = 0f;
                    this.m_LeftStateTimeA   = 0f;
                    this.m_AvatarPreview.Animator.Update(0f);
                    while (!flag4 && num6 < num)
                    {
                        this.m_AvatarPreview.Animator.Update(num5);
                        AnimatorStateInfo currentAnimatorStateInfo = this.m_AvatarPreview.Animator.GetCurrentAnimatorStateInfo(this.m_LayerIndex);
                        num6 += num5;
                        if (!flag2)
                        {
                            this.m_LeftStateWeightA = (this.m_LeftStateWeightB = currentAnimatorStateInfo.normalizedTime);
                            this.m_LeftStateTimeA   = (this.m_LeftStateTimeB = num6);
                            flag2 = true;
                        }
                        if (flag3 && num6 >= num)
                        {
                            flag4 = true;
                        }
                        if (!flag3 && currentAnimatorStateInfo.IsName(this.m_DstState.name))
                        {
                            this.m_RightStateWeightA = currentAnimatorStateInfo.normalizedTime;
                            this.m_RightStateTimeA   = num6;
                            flag3 = true;
                        }
                        if (!flag3)
                        {
                            this.m_LeftStateWeightB = currentAnimatorStateInfo.normalizedTime;
                            this.m_LeftStateTimeB   = num6;
                        }
                        if (flag3)
                        {
                            this.m_RightStateWeightB = currentAnimatorStateInfo.normalizedTime;
                            this.m_RightStateTimeB   = num6;
                        }
                        if (this.m_AvatarPreview.Animator.IsInTransition(this.m_LayerIndex))
                        {
                            num5 = num4;
                        }
                    }
                    float stopTime = num6;
                    this.m_AvatarPreview.Animator.StopRecording();
                    if (Mathf.Approximately(this.m_LeftStateWeightB, this.m_LeftStateWeightA) || Mathf.Approximately(this.m_RightStateWeightB, this.m_RightStateWeightA))
                    {
                        Debug.LogWarning("Difference in effective length between states is too big. Transition preview will be disabled.");
                        this.m_ValidTransition = false;
                        this.m_IsResampling    = false;
                    }
                    else
                    {
                        float num7 = (this.m_LeftStateTimeB - this.m_LeftStateTimeA) / (this.m_LeftStateWeightB - this.m_LeftStateWeightA);
                        float num8 = (this.m_RightStateTimeB - this.m_RightStateTimeA) / (this.m_RightStateWeightB - this.m_RightStateWeightA);
                        if (this.m_MustSampleMotions)
                        {
                            this.m_MustSampleMotions = false;
                            this.m_SrcPivotList.Clear();
                            this.m_DstPivotList.Clear();
                            num5 = num4;
                            this.m_StateMachine.defaultState = this.m_DstState;
                            this.m_Transition.mute           = true;
                            AnimatorController.SetAnimatorController(this.m_AvatarPreview.Animator, this.m_Controller);
                            this.m_AvatarPreview.Animator.Update(0f);
                            this.m_AvatarPreview.Animator.SetLayerWeight(this.m_LayerIndex, 1f);
                            this.m_AvatarPreview.Animator.Update(1E-07f);
                            this.WriteParametersInController();
                            for (num6 = 0f; num6 <= num8; num6 += num5 * 2f)
                            {
                                TimelineControl.PivotSample pivotSample = new TimelineControl.PivotSample();
                                pivotSample.m_Time   = num6;
                                pivotSample.m_Weight = this.m_AvatarPreview.Animator.pivotWeight;
                                this.m_DstPivotList.Add(pivotSample);
                                this.m_AvatarPreview.Animator.Update(num5 * 2f);
                            }
                            num5 = num3;
                            this.m_StateMachine.defaultState = this.m_SrcState;
                            this.m_Transition.mute           = true;
                            AnimatorController.SetAnimatorController(this.m_AvatarPreview.Animator, this.m_Controller);
                            this.m_AvatarPreview.Animator.Update(1E-07f);
                            this.WriteParametersInController();
                            this.m_AvatarPreview.Animator.SetLayerWeight(this.m_LayerIndex, 1f);
                            for (num6 = 0f; num6 <= num7; num6 += num5 * 2f)
                            {
                                TimelineControl.PivotSample pivotSample2 = new TimelineControl.PivotSample();
                                pivotSample2.m_Time   = num6;
                                pivotSample2.m_Weight = this.m_AvatarPreview.Animator.pivotWeight;
                                this.m_SrcPivotList.Add(pivotSample2);
                                this.m_AvatarPreview.Animator.Update(num5 * 2f);
                            }
                            this.m_Transition.mute = false;
                            AnimatorController.SetAnimatorController(this.m_AvatarPreview.Animator, this.m_Controller);
                            this.m_AvatarPreview.Animator.Update(1E-07f);
                            this.WriteParametersInController();
                        }
                        this.m_Timeline.StopTime = (this.m_AvatarPreview.timeControl.stopTime = stopTime);
                        this.m_AvatarPreview.timeControl.currentTime = this.m_Timeline.Time;
                        if (flag)
                        {
                            TimelineControl arg_80E_0 = this.m_Timeline;
                            float           num9      = this.m_AvatarPreview.timeControl.currentTime = (this.m_AvatarPreview.timeControl.startTime = 0f);
                            this.m_Timeline.StartTime = num9;
                            arg_80E_0.Time            = num9;
                            this.m_Timeline.ResetRange();
                        }
                        this.m_AvatarPreview.Animator.StartPlayback();
                        this.m_IsResampling = false;
                    }
                }
            }
        }
        private void ResampleTransition(AnimatorStateTransition transition, AvatarMask layerMask, TransitionInfo info, Animator previewObject)
        {
            m_IsResampling    = true;
            m_MustResample    = false;
            m_ValidTransition = true;

            bool resetTimeSettings = m_RefTransition != transition;

            m_RefTransition     = transition;
            m_RefTransitionInfo = info;

            m_LayerMask = layerMask;

            if (m_AvatarPreview != null)
            {
                m_AvatarPreview.OnDisable();
                m_AvatarPreview = null;
            }

            ClearController();

            Motion sourceStateMotion = m_RefSrcState.motion;

            Init(previewObject, sourceStateMotion != null ? sourceStateMotion : m_RefDstState.motion);

            if (m_Controller == null)  //  did not create controller
            {
                m_IsResampling = false;
                return;
            }

            // since transform might change during sampling, and could alter the default valuesarray, and break recording
            m_AvatarPreview.Animator.allowConstantClipSamplingOptimization = false;

            // sample all frames

            m_StateMachine.defaultState = m_DstState;
            m_Transition.mute           = true;
            AnimatorController.SetAnimatorController(m_AvatarPreview.Animator, m_Controller);
            m_AvatarPreview.Animator.Update(0.00001f);
            WriteParametersInController();
            m_AvatarPreview.Animator.SetLayerWeight(m_LayerIndex, 1);

            float nextStateDuration = m_AvatarPreview.Animator.GetCurrentAnimatorStateInfo(m_LayerIndex).length;

            m_StateMachine.defaultState = m_SrcState;
            m_Transition.mute           = false;
            AnimatorController.SetAnimatorController(m_AvatarPreview.Animator, m_Controller);
            m_AvatarPreview.Animator.Update(0.00001f);
            WriteParametersInController();
            m_AvatarPreview.Animator.SetLayerWeight(m_LayerIndex, 1);

            float currentStateDuration = m_AvatarPreview.Animator.GetCurrentAnimatorStateInfo(m_LayerIndex).length;

            if (m_LayerIndex > 0)
            {
                m_AvatarPreview.Animator.stabilizeFeet = false;
            }
            float maxDuration = (currentStateDuration * m_RefTransition.exitTime) + (m_Transition.duration * (m_RefTransition.hasFixedDuration ? 1.0f : currentStateDuration)) + nextStateDuration;

            // case 546812 disable previewer if the duration is too big, otherwise it hang Unity. 2000.0f is an arbitrary choice, it can be increase if needed.
            // in some case we got a m_Transition.duration == Infinity, bail out before unity hang.
            if (maxDuration > 2000.0f)
            {
                Debug.LogWarning("Transition duration is longer than 2000 second, Disabling previewer.");
                m_ValidTransition = false;
                m_IsResampling    = false;
                return;
            }

            float effectiveCurrentStatetime = m_RefTransition.exitTime > 0 ? currentStateDuration * m_RefTransition.exitTime : currentStateDuration;
            // We want 30 samples/sec, maxed at 300 sample for very long state, and very short animation like 1 frame should at least get 5 sample
            float currentStateStepTime = effectiveCurrentStatetime > 0 ? Mathf.Min(Mathf.Max(effectiveCurrentStatetime / 300.0f, 1.0f / 30.0f), effectiveCurrentStatetime / 5.0f) :  1.0f / 30.0f;
            float nextStateStepTime    = nextStateDuration > 0 ? Mathf.Min(Mathf.Max(nextStateDuration / 300.0f, 1.0f / 30.0f), nextStateDuration / 5.0f) : 1.0f / 30.0f;

            currentStateStepTime = Mathf.Max(currentStateStepTime, maxDuration / 600.0f);
            nextStateStepTime    = Mathf.Max(nextStateStepTime, maxDuration / 600.0f);

            float stepTime = currentStateStepTime;

            float currentTime = 0.0f;

            bool hasStarted      = false;
            bool hasTransitioned = false;
            bool hasFinished     = false;

            //For transitions with exit time == 0, skip to end of clip so transition happens on first frame
            if (m_RefTransition.exitTime == 0)
            {
                m_AvatarPreview.Animator.CrossFade(0, 0f, 0, 0.9f);
            }
            m_AvatarPreview.Animator.StartRecording(-1);

            m_AvatarPreview.Animator.Update(0.0f);
            AnimatorStateInfo currentState = m_AvatarPreview.Animator.GetCurrentAnimatorStateInfo(m_LayerIndex);

            m_LeftStateWeightA = currentState.normalizedTime;
            m_LeftStateTimeA   = currentTime;
            while (!hasFinished && currentTime < maxDuration)
            {
                m_AvatarPreview.Animator.Update(stepTime);

                currentState = m_AvatarPreview.Animator.GetCurrentAnimatorStateInfo(m_LayerIndex);
                currentTime += stepTime;

                if (!hasStarted)
                {
                    m_LeftStateWeightB = currentState.normalizedTime;
                    m_LeftStateTimeB   = currentTime;

                    hasStarted = true;
                }

                if (hasTransitioned && currentTime >= maxDuration)
                {
                    hasFinished = true;
                }

                if (!hasTransitioned && currentState.IsName(m_DstState.name))
                {
                    m_RightStateWeightA = currentState.normalizedTime;
                    m_RightStateTimeA   = currentTime;

                    hasTransitioned = true;
                }

                if (!hasTransitioned)
                {
                    m_LeftStateWeightB = currentState.normalizedTime;
                    m_LeftStateTimeB   = currentTime;
                }

                if (hasTransitioned)
                {
                    m_RightStateWeightB = currentState.normalizedTime;
                    m_RightStateTimeB   = currentTime;
                }

                if (m_AvatarPreview.Animator.IsInTransition(m_LayerIndex))
                {
                    stepTime = nextStateStepTime;
                }
            }

            float endTime = currentTime;

            m_AvatarPreview.Animator.StopRecording();

            if (Mathf.Approximately(m_LeftStateWeightB, m_LeftStateWeightA) || Mathf.Approximately(m_RightStateWeightB, m_RightStateWeightA))
            {
                Debug.LogWarning("Difference in effective length between states is too big. Transition preview will be disabled.");
                m_ValidTransition = false;
                m_IsResampling    = false;
                return;
            }

            float leftDuration  = (m_LeftStateTimeB - m_LeftStateTimeA) / (m_LeftStateWeightB - m_LeftStateWeightA);
            float rightDuration = (m_RightStateTimeB - m_RightStateTimeA) / (m_RightStateWeightB - m_RightStateWeightA);

            if (m_MustSampleMotions)
            {
                // Do this as infrequently as possible
                m_MustSampleMotions = false;
                m_SrcPivotList.Clear();
                m_DstPivotList.Clear();

                stepTime = nextStateStepTime;
                m_StateMachine.defaultState = m_DstState;
                m_Transition.mute           = true;
                AnimatorController.SetAnimatorController(m_AvatarPreview.Animator, m_Controller);
                m_AvatarPreview.Animator.Update(0.0f);
                m_AvatarPreview.Animator.SetLayerWeight(m_LayerIndex, 1);
                m_AvatarPreview.Animator.Update(0.0000001f);
                WriteParametersInController();
                currentTime = 0.0f;
                while (currentTime <= rightDuration)
                {
                    TimelineControl.PivotSample sample = new TimelineControl.PivotSample();
                    sample.m_Time   = currentTime;
                    sample.m_Weight = m_AvatarPreview.Animator.pivotWeight;
                    m_DstPivotList.Add(sample);
                    m_AvatarPreview.Animator.Update(stepTime * 2);
                    currentTime += stepTime * 2;
                }

                stepTime = currentStateStepTime;
                m_StateMachine.defaultState = m_SrcState;
                m_Transition.mute           = true;
                AnimatorController.SetAnimatorController(m_AvatarPreview.Animator, m_Controller);
                m_AvatarPreview.Animator.Update(0.0000001f);
                WriteParametersInController();
                m_AvatarPreview.Animator.SetLayerWeight(m_LayerIndex, 1);
                currentTime = 0.0f;
                while (currentTime <= leftDuration)
                {
                    TimelineControl.PivotSample sample = new TimelineControl.PivotSample();
                    sample.m_Time   = currentTime;
                    sample.m_Weight = m_AvatarPreview.Animator.pivotWeight;
                    m_SrcPivotList.Add(sample);
                    m_AvatarPreview.Animator.Update(stepTime * 2);
                    currentTime += stepTime * 2;
                }

                m_Transition.mute = false;
                AnimatorController.SetAnimatorController(m_AvatarPreview.Animator, m_Controller);
                m_AvatarPreview.Animator.Update(0.0000001f);
                WriteParametersInController();
            }

            m_Timeline.StopTime = m_AvatarPreview.timeControl.stopTime = endTime;
            m_AvatarPreview.timeControl.currentTime = m_Timeline.Time;
            if (resetTimeSettings)
            {
                m_Timeline.Time = m_Timeline.StartTime = m_AvatarPreview.timeControl.currentTime = m_AvatarPreview.timeControl.startTime = 0;
                m_Timeline.ResetRange();
            }

            m_AvatarPreview.Animator.StartPlayback();

            m_AvatarPreview.Animator.playbackTime = 0f;
            m_AvatarPreview.Animator.Update(0f);
            m_AvatarPreview.ResetPreviewFocus();

            m_IsResampling = false;
        }