コード例 #1
0
        public static void RemoveZeroWeightsInputs(AnimationMixerPlayable mixer)
        {
            int size = mixer.GetInputCount();

            for (int i = 0; i < size; i++)
            {
                if (mixer.GetInputWeight(i) <= 0)
                {
                    mixer.GetInput(i).Destroy();
                    for (int j = i + 1; j < size; j++)
                    {
                        // double localTime = ((AnimationClipPlayable)mixer.GetInput(j)).GetTime();
                        float    _weight = mixer.GetInputWeight(j);
                        Playable clip    = mixer.GetInput(j);
                        // clip.SetTime(localTime);
                        mixer.DisconnectInput(j);
                        mixer.ConnectInput(j - 1, clip, 0);
                        mixer.SetInputWeight(j - 1, _weight);
                    }
                    i--;
                    size--;
                    mixer.SetInputCount(size);
                }
            }
        }
コード例 #2
0
        public void ApplyPresentationState(GameTime time, float deltaTime)
        {
            // Find ability entity
            if (!m_EntityManager.Exists(ability))
            {
                var character = m_EntityManager.GetComponentObject <Character>(m_AnimStateOwner);
                ability = character.FindAbilityWithComponent(m_EntityManager, typeof(Ability_Emote.SerializedState));
            }

            if (ability == Entity.Null)
            {
                return;
            }

            var abilityState = m_EntityManager.GetComponentData <Ability_Emote.SerializedState>(ability);

            var blendVel = m_settings.blendTime > 0 ? 1.0f / m_settings.blendTime : 1.0f / deltaTime;

            if (abilityState.emote == CharacterEmote.None)
            {
                if (activePort != 0)
                {
                    GameDebug.Log("anim stopping");
                    activePort = 0;
                }

                m_Transition.Update(activePort, blendVel, Time.deltaTime);
                return;
            }

            var requestedPort = (int)abilityState.emote;

            if (requestedPort != activePort || abilityState.emoteCount != lastEmoteCount)
            {
                lastEmoteCount = abilityState.emoteCount;
                StartEmote(requestedPort);
                m_Transition.Update(activePort, blendVel, Time.deltaTime);
                return;
            }

            // Timeout.
            if (activePort > 0)
            {
                if (!m_settings.emoteData[activePort - 1].keepPlaying)
                {
                    var clipPlayable = m_mixer.GetInput(activePort);
                    if (clipPlayable.GetPlayState() == PlayState.Playing && clipPlayable.GetTime() >= clipPlayable.GetDuration() - m_settings.blendTime)
                    {
                        clipPlayable.Pause();
                        activePort = 0;

                        var internalState = m_EntityManager.GetComponentData <Ability_Emote.InternalState>(ability);
                        internalState.animDone = 1;
                        m_EntityManager.SetComponentData(ability, internalState);
                    }
                }
            }

            m_Transition.Update(activePort, blendVel, Time.deltaTime);
        }
コード例 #3
0
    public Playable GetInput(int index)
    {
        if (index >= m_Mixer.GetInputCount())
        {
            return(Playable.Null);
        }

        return(m_Mixer.GetInput(index));
    }
コード例 #4
0
        public override void PrepareFrame(Playable owner, FrameData info)
        {
            if (CurrentClipIndex == -1)
            {
                return;
            }

            var curClip = (AnimationClipPlayable)Mixer.GetInput(CurrentClipIndex);

            if (curClip.GetTime() > curClip.GetDuration())
            {
                owner.Play();
            }
        }
コード例 #5
0
        public void RemoveZeroWeightsInputsAnimations(
            AnimationMixerPlayable mixer,
            List <float> blendingWeights,
            List <float> currentWeights,
            List <int> curretClipsIndex,
            List <LogicAnimationsSequence> animationsSequences
            )
        {
            int size = mixer.GetInputCount();

            for (int i = 0; i < size; i++)
            {
                if (currentWeights[i] <= 0f)
                {
                    int sequenceMixerInputsCount = mixer.GetInput(i).GetInputCount();
                    if (sequenceMixerInputsCount > 0)
                    {
                        for (int mixerInputInputsIndex = 0; mixerInputInputsIndex < sequenceMixerInputsCount; mixerInputInputsIndex++)
                        {
                            mixer.GetInput(i).GetInput(mixerInputInputsIndex).Destroy();
                        }
                        for (int as_Index = 0; as_Index < animationsSequences.Count; as_Index++)
                        {
                            if (curretClipsIndex[i] == animationsSequences[as_Index].dataIndex)
                            {
                                animationsSequences.RemoveAt(as_Index);
                                break;
                            }
                        }
                    }
                    mixer.GetInput(i).Destroy();
                    blendingWeights.RemoveAt(i);
                    curretClipsIndex.RemoveAt(i);
                    currentWeights.RemoveAt(i);
                    for (int j = i + 1; j < size; j++)
                    {
                        // double localTime = ((AnimationClipPlayable)mixer.GetInput(j)).GetTime();
                        float    _weight = mixer.GetInputWeight(j);
                        Playable clip    = mixer.GetInput(j);
                        // clip.SetTime(localTime);
                        mixer.DisconnectInput(j);
                        mixer.ConnectInput(j - 1, clip, 0);
                        mixer.SetInputWeight(j - 1, _weight);
                    }
                    i--;
                    size--;
                    mixer.SetInputCount(size);
                }
            }
        }
コード例 #6
0
        //===========================================================================================

        /**
         *  @brief
         *
         *********************************************************************************************/
        public void EndPreviewLocal()
        {
            if (!m_previewActive)
            {
                return;
            }

            AnimationMixerPlayable mixer = MxMPreviewScene.Mixer;

            if (mixer.IsValid())
            {
                for (int i = 0; i < mixer.GetInputCount(); ++i)
                {
                    var clipPlayable = mixer.GetInput(0);

                    if (clipPlayable.IsValid())
                    {
                        mixer.DisconnectInput(0);
                        clipPlayable.Destroy();
                    }
                }
            }

            m_previewActive     = false;
            m_previewBlendSpace = null;
        }
コード例 #7
0
        //===========================================================================================

        /**
         *  @brief
         *
         *********************************************************************************************/
        private void AddClipToPreview(AnimationClip a_clip)
        {
            if (m_previewActive && a_clip != null)
            {
                m_blendWeights.Add(0f);

                AnimationMixerPlayable mixer = MxMPreviewScene.Mixer;

                int inputId = mixer.GetInputCount();
                mixer.SetInputCount(inputId + 1);

                var clipPlayable = AnimationClipPlayable.Create(MxMPreviewScene.PlayableGraph, a_clip);

                mixer.ConnectInput(inputId, clipPlayable, 0);

                for (int i = 0; i < mixer.GetInputCount(); ++i)
                {
                    var playable = mixer.GetInput(i);

                    if (playable.IsValid())
                    {
                        playable.SetTime(0f);
                    }
                }

                CalculateBlendWeights();
                ApplyBlendWeights();
            }
        }
コード例 #8
0
    public override void OnGraphStop(Playable playable)
    {
        //if the playable is not valid, then we are destroying, and our children won't be valid either
        if (!self.IsValid())
        {
            return;
        }

        for (int i = 0; i < m_States.Count; i++)
        {
            StateInfo state = m_States[i];
            if (state == null)
            {
                continue;
            }

            if (state.fadeSpeed == 0f && state.targetWeight == 0f)
            {
                AnimationMixerPlayable mixer = GetMixer(state.layer);
                if (mixer.Equals(AnimationMixerPlayable.Null))
                {
                    throw new Exception("Can not get mixer at layer:" + state.layer);
                }

                Playable input = mixer.GetInput(state.indexAtLayer);
                if (!input.Equals(Playable.Null))
                {
                    input.ResetTime(0f);
                }
            }
        }
    }
コード例 #9
0
    private void CleanClonedStates()
    {
        for (int i = m_States.Count - 1; i >= 0; i--)
        {
            StateInfo state = m_States[i];
            if (state == null)
            {
                continue;
            }

            if (state.isReadyForCleanup)
            {
                AnimationMixerPlayable mixer = GetMixer(state.layer);
                if (mixer.Equals(AnimationMixerPlayable.Null))
                {
                    throw new Exception("Can not get mixer at layer:" + state.layer);
                }

                Playable toDestroy = mixer.GetInput(state.indexAtLayer);
                graph.Disconnect(mixer, state.indexAtLayer);
                graph.DestroyPlayable(toDestroy);
                m_States.RemoveState(i);
            }
        }
    }
コード例 #10
0
    public override void PrepareFrame(Playable owner, FrameData info)
    {
        int ClipCount = m_mixerPlayable.GetInputCount();

        if (ClipCount == 0)
        {
            return;
        }

        m_timeToNextClip -= info.deltaTime;

        if (m_timeToNextClip <= 0.0f)
        {
            m_currentClipIndex++;
            if (m_currentClipIndex >= ClipCount)
            {
                m_currentClipIndex = 0;
            }
            var currentClip = (AnimationClipPlayable)m_mixerPlayable.GetInput(m_currentClipIndex);

            //SetTime(0),从头开始播放动画
            currentClip.SetTime(0);
            m_timeToNextClip = currentClip.GetAnimationClip().length;
        }

        //利用权重来设置当前播放的Clip
        for (int clipIndex = 0; clipIndex < ClipCount; ++clipIndex)
        {
            m_mixerPlayable.SetInputWeight(clipIndex, clipIndex == m_currentClipIndex ? 1 : 0);
        }
    }
コード例 #11
0
        public void AddClipPlayable(
            AnimationMixerPlayable mixer,
            AnimationClip animation,
            bool passIK,
            bool passFootIK  = false,
            double localTime = 0d,
            float weight     = 0f
            )
        {
            AnimationClipPlayable playable = AnimationClipPlayable.Create(this.graph, animation);

            playable.SetApplyPlayableIK(passIK);
            playable.SetApplyFootIK(passFootIK);
            mixer.AddInput(playable, 0, weight);
            mixer.GetInput(mixer.GetInputCount() - 1).SetTime(localTime);
            mixer.GetInput(mixer.GetInputCount() - 1).SetTime(localTime);
        }
コード例 #12
0
        //...
        public void UpdateClip(PlayAnimatorClip playAnimClip, float clipTime, float clipPrevious, float weight)
        {
            var index        = ports[playAnimClip];
            var clipPlayable = clipsMixer.GetInput(index);

            clipPlayable.SetTime(clipTime);
            clipsMixer.SetInputWeight(index, activeClips == 1 ? 1f : weight);
            activeClipsWeight = activeClips >= 2 ? 1 : weight;
        }
コード例 #13
0
 public void ClearMainMixerInput()
 {
     for (int i = 0; i < mixer.GetInputCount(); i++)
     {
         mixer.GetInput(i).Destroy();
     }
     mixer.SetInputCount(0);
     asBuffor = null;
 }
        void FindMixers()
        {
            m_Mixers.Clear();
            m_PoseMixer       = AnimationMixerPlayable.Null;
            m_LayerMixer      = AnimationLayerMixerPlayable.Null;
            m_MotionXPlayable = AnimationMotionXToDeltaPlayable.Null;

            var playable   = m_Output.GetSourcePlayable();
            var outputPort = m_Output.GetSourceOutputPort();

            if (!playable.IsValid() || outputPort < 0 || outputPort >= playable.GetInputCount())
            {
                return;
            }

            var mixer = FindFirstAnimationPlayable(playable.GetInput(outputPort));

            Playable motionXPlayable = mixer;

            if (motionXPlayable.IsPlayableOfType <AnimationMotionXToDeltaPlayable>())
            {
                m_MotionXPlayable = (AnimationMotionXToDeltaPlayable)motionXPlayable;
                mixer             = m_MotionXPlayable.GetInput(0);
            }

            if (mixer.IsValid() && mixer.IsPlayableOfType <AnimationMixerPlayable>())
            {
                m_PoseMixer = (AnimationMixerPlayable)mixer;
                Playable layerMixer = m_PoseMixer.GetInput(0);

                if (layerMixer.IsValid() && layerMixer.IsPlayableOfType <AnimationLayerMixerPlayable>())
                {
                    m_LayerMixer = (AnimationLayerMixerPlayable)layerMixer;
                }
            }
            else if (mixer.IsValid() && mixer.IsPlayableOfType <AnimationLayerMixerPlayable>())
            {
                m_LayerMixer = (AnimationLayerMixerPlayable)mixer;
            }


            if (!m_LayerMixer.IsValid())
            {
                return;
            }

            var count = m_LayerMixer.GetInputCount();

            for (var i = 0; i < count; i++)
            {
                FindMixers(m_LayerMixer, i, m_LayerMixer.GetInput(i));
            }
        }
        public void Update(MotionMatchingPlayableGraph graph, bool passIK, bool passFootIK, float deltaTime)
        {
            currentClipTime += deltaTime;

            float seqLocalTime = GetLocalTime();

            for (int i = 0; i < data.clips.Count; i++)
            {
                float currentPlayableTime = GetPlayableTimeInSequenceLocalTime(seqLocalTime, i);
                float currentWeight       = mixer.GetInputWeight(i);
                float desiredWeight       = GetPlayableWeightInPlayableTime(currentPlayableTime, i);

                if (currentWeight <= 0f && desiredWeight > 0f)
                {
                    mixer.GetInput(i).SetTime(currentPlayableTime - Time.deltaTime);
                    mixer.GetInput(i).SetTime(currentPlayableTime);
                }
                mixer.SetInputWeight(i, desiredWeight);
            }
            NormalizeMixerInputWeights();
        }
コード例 #16
0
    public Playable GetInput(int layer, int index)
    {
        AnimationMixerPlayable mixer = GetMixer(layer);

        if (mixer.Equals(AnimationMixerPlayable.Null))
        {
            return(Playable.Null);
        }
        if (index >= mixer.GetInputCount())
        {
            return(Playable.Null);
        }

        return(mixer.GetInput(index));
    }
コード例 #17
0
        // 取得可用的或新的输入索引
        private int GetInputIndex(AnimationMixerPlayable mixerPlayable)
        {
            var count = mixerPlayable.GetInputCount();

            for (int i = 0; i < count; i++)
            {
                var p = mixerPlayable.GetInput(i);
                if (p.IsNull())
                {
                    mixerPlayable.SetInputWeight(i, 0);
                    return(i);
                }
            }
            mixerPlayable.SetInputCount(count + 1);
            return(count); //index
        }
コード例 #18
0
        private void MoveToNextClip()
        {
            currentIndex++;
            if (currentIndex >= mixer.GetInputCount())
            {
                currentIndex = 0;
            }

            var currentClip = (AnimationClipPlayable)mixer.GetInput(currentIndex);


            currentClip.SetTime(0);
            timeToNextClip = currentClip.GetAnimationClip().length;

            onNewClip?.Invoke();
        }
コード例 #19
0
            private PlayAtTimeInstruction MovedToTopOfQueue(PlayAtTimeInstruction playAtTime, int currentState, AnimationMixerPlayable stateMixer,
                                                            List <AnimationPlayerState> states)
            {
                switch (playAtTime.type)
                {
                case QueueStateType.WhenCurrentDone: {
                    var(_, currentStateIsDoneAt) = GetCurrentStateTimeInfo();

                    playAtTime.isDoneTime = currentStateIsDoneAt;
                    break;
                }

                case QueueStateType.AfterSeconds:
                    if (!playAtTime.CountFromQueued)
                    {
                        playAtTime.isDoneTime = Time.time + playAtTime.Seconds;
                    }
                    break;

                case QueueStateType.BeforeCurrentDone_Seconds: {
                    var(_, currentStateIsDoneAt) = GetCurrentStateTimeInfo();

                    playAtTime.isDoneTime = currentStateIsDoneAt - playAtTime.Seconds;
                    break;
                }

                case QueueStateType.BeforeCurrentDone_Relative: {
                    var(currentStateDuration, currentStateIsDoneAt) = GetCurrentStateTimeInfo();
                    playAtTime.isDoneTime = currentStateIsDoneAt - playAtTime.RelativeDuration * currentStateDuration;
                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException();
                }

                return(playAtTime);

                (float currentStateDuration, float currentStateDoneAt) GetCurrentStateTimeInfo()
                {
                    var duration          = states[currentState].Duration;
                    var playedTime        = (float)stateMixer.GetInput(currentState).GetTime();
                    var currentIsDoneTime = Time.time + (duration - playedTime);

                    return(duration, currentIsDoneTime);
                }
            }
コード例 #20
0
        public AnimationComponent(AnimationMixerPlayable animationMixer,
                                  AudioMixerPlayable audioMixer,
                                  int animation,
                                  int audio = -1)
        {
            this.animationMixer = animationMixer;
            this.audioMixer     = audioMixer;
            this.animationIndex = animation;
            this.audioIndex     = audio;

            var clip = (AnimationClipPlayable)animationMixer.GetInput(animationIndex);

            // Calculate time for processing current clip.
            totalTime = clip.GetAnimationClip().length;
            // Reset the time so that the clip starts at the correct position.
            clip.SetTime(0);
            // Reverse the animation if necessary.
            clip.SetSpeed(speed);
        }
コード例 #21
0
    /// <summary>
    /// Adds clip to Aniamtion Graph and then plays the clip.
    /// </summary>
    /// <param name="blendTime">Time taken to blend between current clip and next clip</param>
    /// <param name="clipOffset">How many seconds to offset the clip's start time</param>
    public void Play(AnimationClip clip, float blendTime = .2f, float clipOffset = 0)
    {
        if (!clip)
        {
            Debug.LogError("Cannot Play Null Clip", gameObject);
            return;
        }
        if (!playableLookup.TryGetValue(clip, out var next))
        {
            if (!graph.IsValid())
            {
                CreateGraph();
            }

            playableLookup[clip] = next = AnimationClipPlayable.Create(graph, clip);
        }

        var current = mixer.GetInput(0);

        next.SetTime(clipOffset);
        mixer.DisconnectInput(0);
        mixer.DisconnectInput(1);

        mixer.ConnectInput(0, next, 0);

        if (blendTime <= 0)
        {
            mixer.SetInputWeight(0, 1);
        }
        else
        {
            if (current.IsValid() && ((AnimationClipPlayable)current).GetAnimationClip() != ((AnimationClipPlayable)next).GetAnimationClip())
            {
                mixer.ConnectInput(1, current, 0);
            }
            mixer.SetInputWeight(0, 0);
            mixer.SetInputWeight(1, 1);
        }
        this.blendTime = blendTime;
        changeTime     = Time.time;
    }
コード例 #22
0
        private bool AnimationClipIsPlaying(AnimationMixerPlayable mixerPlayable, AnimationClip clip, out AnimationClipPlayable playable)
        {
            var count = mixerPlayable.GetInputCount();

            for (int i = 0; i < count; i++)
            {
                var p = mixerPlayable.GetInput(i);
                if (p.IsNull() == false)
                {
                    if (p.IsPlayableOfType <AnimationClipPlayable>())
                    {
                        playable = (AnimationClipPlayable)p;
                        if (((AnimationClipPlayable)p).GetAnimationClip() == clip)
                        {
                            return(true);
                        }
                    }
                }
            }
            playable = (AnimationClipPlayable)Playable.Null;
            return(false);
        }
コード例 #23
0
    private void LateUpdate()
    {
        mixerEmotionPlayable.SetInputWeight(4, 0.0f); // Set TPose to 0

        if (GoToHappy)
        {
            if (GoToHappyStart)
            {
                mixerEmotionPlayable.GetInput(1).SetTime(0f);
                mixerEmotionPlayable.GetInput(1).SetDone(false);
                GoToHappyStart = false;
            }

            currentTime += Time.deltaTime;
            float upcomingBlendWeight = Mathf.Lerp(0, 1, currentTime / lerpBlendDuration);
            mixerEmotionPlayable.SetInputWeight(1, upcomingBlendWeight);


            if (currentlyPlaying == "Happy")
            {
                mixerEmotionPlayable.SetInputWeight(0, 1f - upcomingBlendWeight);
            }
            else if (currentlyPlaying == "Angry")
            {
                mixerEmotionPlayable.SetInputWeight(2, (1f - upcomingBlendWeight));
            }


            if (currentTime >= lerpBlendDuration)
            {
                GoToHappy        = false;
                GoToHappyStart   = true;
                canPlayHappyMain = true;
                currentlyPlaying = "Happy";
                currentTime      = 0;
            }
            normalize = true;
        }
        if (canPlayHappyMain && ((mixerEmotionPlayable.GetInput(1).GetDuration() - mixerEmotionPlayable.GetInput(1).GetTime()) < 0.1)) //(mixerEmotionPlayable.GetInput(1).GetTime() >= mixerEmotionPlayable.GetInput(1).GetDuration())
        {
            canPlayHappyMain = false;
            mixerEmotionPlayable.SetInputWeight(1, 0.0f);   // Deactivate Transition
            mixerEmotionPlayable.SetInputWeight(0, 1.0f);   // Active Main
            mixerEmotionPlayable.GetInput(0).SetTime(0f);
            normalize = true;
        }

        if (GoToAngry)
        {
            GoToAngry        = false;
            canPlayAngryMain = true;
            mixerEmotionPlayable.SetInputWeight(3, 1.0f);   // Set AngryTransition In to Active
            mixerEmotionPlayable.SetInputWeight(0, 0.0f);   // Deactivate Main
            mixerEmotionPlayable.SetInputWeight(2, 0.0f);
            mixerEmotionPlayable.GetInput(3).SetTime(0f);
            mixerEmotionPlayable.GetInput(3).SetDone(false);
            normalize = true;
        }
        if (canPlayAngryMain && ((mixerEmotionPlayable.GetInput(3).GetDuration() - mixerEmotionPlayable.GetInput(3).GetTime()) < 0.1)) //(mixerEmotionPlayable.GetInput(1).GetTime() >= mixerEmotionPlayable.GetInput(1).GetDuration())
        {
            canPlayAngryMain = false;
            mixerEmotionPlayable.SetInputWeight(3, 0.0f);   // Deactivate Transition
            mixerEmotionPlayable.SetInputWeight(2, 1.0f);   // Active Main
            mixerEmotionPlayable.GetInput(2).SetTime(0f);

            currentlyPlaying = "Angry";
            normalize        = true;
        }



        if (normalize)
        {
            normalizeWeights();
        }

        //addInTPoseIfNecessary();

        //Debug.Log("Happy Wieght: " + mixerEmotionPlayable.GetInputWeight(0));
        //Debug.Log("Angry Wieght: " + mixerEmotionPlayable.GetInputWeight(2));
        //Debug.Log("TPose Wieght: " + mixerEmotionPlayable.GetInputWeight(4));
    }
コード例 #24
0
ファイル: EnemyAnimator.cs プロジェクト: svnvav/TowerDefense
 private Playable GetPlayable(Clip clip)
 {
     return(mixer.GetInput((int)clip));
 }
コード例 #25
0
    /*
     * Update
     */
    void LateUpdate()
    {
        if (HACKFixStuckBlendshapes)
        {
            animator.runtimeAnimatorController = null;          // Necessary to fix a bug where blendshapes "get stuck" on SetInputWeight changes. Reassigned at the end of Update.
        }

        // Check if new Transitions were requested and add them to the queue
        checkForNewTransitions();

        // If no transition is playing, prepare next transition
        if (!fPlayTransition && goToEmotionNext.Count > 0)
        {
            prepareNextTransition();
        }

        // Play Transition
        if (fPlayTransition)
        {
            playTransition();
        }

        // Play main emotion slightly before transition ends to avoid the 1 'neutral' frame that occurs when using "isDone()"
        if (fPlayMainAfterTransition && ((mixerEmotionPlayable.GetInput(playablesDict[transitionEmotion + "TransitionIn" + emotionNumber]).GetDuration() - mixerEmotionPlayable.GetInput(playablesDict[transitionEmotion + "TransitionIn" + emotionNumber]).GetTime()) < 0.1)) //(mixerEmotionPlayable.GetInput(1).GetTime() >= mixerEmotionPlayable.GetInput(1).GetDuration())
        {
            playMainAfterTransition();
        }

        // If close to the end of a main clip, set loop starting flag
        if (!fPlayTransition && !currentlyPlaying.Contains("TransitionIn"))
        {
            if (((mixerEmotionPlayable.GetInput(playablesDict[currentlyPlaying + emotionNumber]).GetInput(mainMixerMainIndex).GetDuration() - mixerEmotionPlayable.GetInput(playablesDict[currentlyPlaying + emotionNumber]).GetInput(mainMixerMainIndex).GetTime()) < mainLoopTriggerTime))
            {
                fPlayLoopMain = true;
            }
        }

        // Ensure a smooth loop to the beginning for the main animation. Based on linearly interpolating the running clip with a copy of itself.
        if (fPlayLoopMain)
        {
            playLoopMainToStart();
        }

        // If weights were changed, normalize them
        if (normalize)
        {
            normalizeWeights();
        }

        if (HACKFixStuckBlendshapes)
        {
            animator.runtimeAnimatorController = runtimeAnimController;
        }

        //Debug.Log("Happy Wieght: " + mixerEmotionPlayable.GetInputWeight(0));
        //Debug.Log("Angry Wieght: " + mixerEmotionPlayable.GetInputWeight(2));
        //Debug.Log("TPose Wieght: " + mixerEmotionPlayable.GetInputWeight(4));
    }
コード例 #26
0
    // Needs to be called each frame
    public void update()
    {
        if (isMixer)
        {
            // If close to the end of a main clip, set loop starting flag
            if (((mixerEmotionPlayable.GetInput(playablesDict[playablesEmotionKey]).GetInput(mainMixerMainIndex).GetDuration() -
                  mixerEmotionPlayable.GetInput(playablesDict[playablesEmotionKey]).GetInput(mainMixerMainIndex).GetTime()) <
                 mainLoopTriggerTime))
            {
                fPlayLoopMain = true;
            }

            // Ensure a smooth loop to the beginning for the main animation. Based on linearly interpolating the running clip with a copy of itself.
            if (fPlayLoopMain)
            {
                playLoopMainToStart();
            }
        }
    }
            private void UpdateStates(float deltaTime)
            {
                bool  mustUpdateWeights = false;
                float totalWeight       = 0f;
                int   count             = m_States.Count;

                for (int i = 0; i < count; i++)
                {
                    StateInfo state = m_States[i];
                    if (state == null)
                    {
                        continue;
                    }

                    state.InvalidateTime();
                    state.UpdateData(deltaTime);
                    // 处理混合树状态
                    if (state.isBlendTree)
                    {
                        bool  isChangeParam = false;
                        float lastParam     = 0;
                        float param         = m_Params.GetFloat(state.blendTreeParameter);
                        isChangeParam = !_lastParamValue.TryGetValue(state.blendTreeParameter, out lastParam);
                        if (!isChangeParam)
                        {
                            isChangeParam = lastParam != param;
                        }
                        if (isChangeParam)
                        {
                            state.CalBlendTreeWeight(param);
                            _lastParamValue[state.blendTreeParameter] = param;
                        }
                    }

                    // 处理状态过渡 设置权重
                    if (state.fading)
                    {
                        state.SetWeight(Mathf.MoveTowards(state.weight, state.targetWeight, state.fadeSpeed * deltaTime));
                        if (Mathf.Approximately(state.weight, state.targetWeight))
                        {
                            state.ForceWeight(state.targetWeight);
                            if (state.weight == 0f)
                            {
                                state.Stop();
                            }
                        }
                    }

                    // 处理状态开关
                    if (state.enabledDirty)
                    {
                        if (state.enabled)
                        {
                            state.Play();
                        }
                        else
                        {
                            state.Pause();
                        }


                        Playable input = m_Mixer.GetInput(i);
                        //if state is disabled but the corresponding input is connected, disconnect it
                        if (input.IsValid() && !state.enabled)
                        {
                            DisconnectInput(i);
                        }
                        else if (state.enabled && !input.IsValid())
                        {
                            ConnectInput(state.index);
                        }
                    }

                    if (state.enabled)
                    {
                        if (state.wrapMode == WrapMode.Once)
                        {
                            bool  stateIsDone = state.isDone;
                            float speed       = state.speed;
                            float time        = state.GetTime();
                            float duration    = state.playableDuration;

                            stateIsDone |= speed < 0f && time < 0f;
                            stateIsDone |= speed >= 0f && time >= duration;
                            if (stateIsDone)
                            {
                                state.Stop();
                                state.Disable();
                                DisconnectInput(state.index);
                            }
                        }
                        if (!state.fading)
                        {
                            state.UpdateTransitions(deltaTime, this);
                        }
                    }

                    totalWeight += state.weight;
                    if (state.weightDirty)
                    {
                        mustUpdateWeights = true;
                    }
                    state.ResetDirtyFlags();
                }

                // 有状态权重变化 更改playable权重
                if (mustUpdateWeights)
                {
                    bool hasAnyWeight = totalWeight > 0.0f;
                    for (int i = 0; i < m_States.Count; i++)
                    {
                        StateInfo state  = m_States[i];
                        float     weight = hasAnyWeight ? state.weight / totalWeight : 0.0f;
                        m_Mixer.SetInputWeight(state.index, weight);
                    }
                }
            }
コード例 #28
0
        void OnGUI()
        {
            if (!Application.isEditor || !enablePreview)
            {
                return;
            }
            if (!hadEnabled)
            {
                hadEnabled = true;
                EnablePreview();
                return;
            }

            bool clicked = false;

            scrollViewVector = GUI.BeginScrollView(new Rect(0, 0, 116, Screen.height), scrollViewVector, new Rect(0, 0, 100, proxy.BlendShapeAvatar.Clips.Count * 23 + 23));
            if (GUI.Button(new Rect(0, 0, 100, 22), "[Hide]"))
            {
                enablePreview = false;
                hadEnabled    = false;
                return;
            }
            int k = 1;

            foreach (var values in proxy.GetValues())
            {
                if (GUI.Button(new Rect(0, 23 * k, 100, 22), values.Key.Name))
                {
                    currentExpression = values.Key.Name;
                    blendShapeKey     = values.Key;
                    clicked           = true;
                }
                k++;
            }
            GUI.EndScrollView();

            if (clicked)
            {
                foreach (var values in proxy.GetValues())
                {
                    if (blendShapeKey.Name.ToUpper() == values.Key.Name.ToUpper())
                    {
                        proxy.ImmediatelySetValue(values.Key, 1f);
                    }
                    else
                    {
                        proxy.ImmediatelySetValue(values.Key, 0f);
                    }
                }
                mixer.SetInputWeight(0, 1);
                for (int i = 0; i < animations.Length; i++)
                {
                    if (animations[i].blendshapeName.ToUpper() == currentExpression.ToUpper())
                    {
                        if (toGraph[i] > -1)
                        {
                            mixer.SetInputWeight(toGraph[i], 1);
                            mixer.GetInput(toGraph[i]).SetTime(0);
                        }
                        if (animations[i].animation != null && animations[i].animation.humanMotion)
                        {
                            mixer.SetInputWeight(0, 0);
                        }
                    }
                    else
                    {
                        if (toGraph[i] > -1)
                        {
                            mixer.SetInputWeight(toGraph[i], 0);
                        }
                    }
                }
            }
        }
コード例 #29
0
    private void UpdateStates(float deltaTime)
    {
        mustUpdateWeights.Clear();
        totalWeights.Clear();

        for (int i = 0; i < m_States.Count; i++)
        {
            StateInfo state = m_States[i];

            //Skip deleted states
            if (state == null)
            {
                continue;
            }

            //Update crossfade weight
            if (state.fading)
            {
                state.SetWeight(Mathf.MoveTowards(state.weight, state.targetWeight, state.fadeSpeed * deltaTime));
                if (Mathf.Approximately(state.weight, state.targetWeight))
                {
                    state.ForceWeight(state.targetWeight);
                    if (state.weight == 0f)
                    {
                        state.Stop();
                    }
                }
            }

            if (state.layerDirty >= 0)
            {
                AnimationMixerPlayable lastMixer = GetMixer(state.layerDirty);
                if (!lastMixer.Equals(AnimationMixerPlayable.Null))
                {
                    if (lastMixer.GetInput(state.indexAtLayer).Equals(state.playable))
                    {
                        graph.Disconnect(lastMixer, state.indexAtLayer);
                    }
                }

                AnimationMixerPlayable mixer = AddMixerWhenNotExist(state.layer);
                if (mixer.Equals(AnimationMixerPlayable.Null))
                {
                    throw new Exception("Can not get mixer at layer:" + state.layer);
                }

                state.indexAtLayer = m_States.GetAvailableIndexAtLayer(state.layer, state);

                int stateCountByLayer = m_States.GetCountByLayer(state.layer);
                if (stateCountByLayer > mixer.GetInputCount())
                {
                    mixer.SetInputCount(stateCountByLayer);
                }

                graph.Connect(state.playable, 0, mixer, state.indexAtLayer);
            }

            if (state.enabledDirty)
            {
                if (state.enabled)
                {
                    state.Play();
                }
                else
                {
                    state.Pause();
                }

                if (!keepStoppedPlayablesConnected)
                {
                    AnimationMixerPlayable mixer = GetMixer(state.layer);
                    if (mixer.Equals(AnimationMixerPlayable.Null))
                    {
                        throw new Exception("Can not get mixer at layer:" + state.layer);
                    }
                    Playable input = mixer.GetInput(state.indexAtLayer);
                    //if state is disabled but the corresponding input is connected, disconnect it
                    if (input.IsValid() && !state.enabled)
                    {
                        DisconnectInput(i);
                    }
                    else if (state.enabled && !input.IsValid())
                    {
                        ConnectInput(state.index);
                    }
                }
            }

            if (state.enabled && state.wrapMode == WrapMode.Once)
            {
                bool  stateIsDone = state.isDone;
                float speed       = state.speed;
                float time        = state.GetTime();
                float duration    = state.playableDuration;

                stateIsDone |= speed < 0f && time < 0f;
                stateIsDone |= speed >= 0f && time >= duration;
                if (stateIsDone)
                {
                    state.Stop();
                    state.Disable();
                    if (!keepStoppedPlayablesConnected)
                    {
                        DisconnectInput(state.index);
                    }
                }
            }

            if (!totalWeights.ContainsKey(state.layer))
            {
                totalWeights.Add(state.layer, 0.0f);
            }
            if (!mustUpdateWeights.ContainsKey(state.layer))
            {
                mustUpdateWeights.Add(state.layer, false);
            }
            totalWeights[state.layer] += state.weight;
            if (state.weightDirty)
            {
                mustUpdateWeights[state.layer] = true;
            }
            state.ResetDirtyFlags();
        }

        var e = mustUpdateWeights.GetEnumerator();

        while (e.MoveNext())
        {
            if (e.Current.Value)
            {
                float totalWeight = totalWeights[e.Current.Key];

                bool hasAnyWeight = totalWeight > 0.0f;
                for (int i = 0; i < m_States.Count; i++)
                {
                    StateInfo state = m_States[i];
                    if (state == null || state.layer != e.Current.Key)
                    {
                        continue;
                    }
                    AnimationMixerPlayable mixer = GetMixer(state.layer);
                    if (mixer.Equals(AnimationMixerPlayable.Null))
                    {
                        throw new Exception("Can not get mixer at layer:" + state.layer);
                    }

                    float weight = hasAnyWeight ? state.weight / totalWeight : 0.0f;
                    mixer.SetInputWeight(state.indexAtLayer, weight);
                }
            }
        }

        mustUpdateWeights.Clear();
        totalWeights.Clear();
    }