/// <summary>
        /// Play animation.
        /// </summary>
        /// <param name="clip">Animation clip.</param>
        /// <param name="isLoop">Animation is loop.</param>
        /// <param name="speed">Animation speed.</param>
        public void PlayAnimation(AnimationClip clip, bool isLoop = true, float speed = 1.0f)
        {
            // Create cubism motion state.
            var state = CubismMotionState.CreateCubismMotionState(_playableGraph, clip, isLoop, speed);

            if (_motionStates.Count > 0)
            {
                _motionStates[_motionStates.Count - 1].ConnectClipMixer(state.ClipMixer);
            }
            else
            {
                PlayableOutput.DisconnectInput(0);
                PlayableOutput.ConnectInput(0, state.ClipMixer, 0);
                PlayableOutput.SetInputWeight(0, 1.0f);
            }

            _motionStates.Add(state);

            // Set last motion end time and fade in start time;
            if (_playingMotions.Count > 0)
            {
                var lastMotion = _playingMotions[_playingMotions.Count - 1];
                lastMotion.FadeInStartTime = Time.time;
                lastMotion.EndTime         = Time.time + lastMotion.Motion.FadeOutTime;
                _playingMotions[_playingMotions.Count - 1] = lastMotion;
            }

            // Create fade playing motion.
            var playingMotion = CreateFadePlayingMotion(clip, speed);

            _playingMotions.Add(playingMotion);

            _isFinished = false;
        }
Exemple #2
0
        /// <summary>
        /// Play animation.
        /// </summary>
        /// <param name="clip">Animation clip.</param>
        /// <param name="isLoop">Animation is loop.</param>
        /// <param name="speed">Animation speed.</param>
        public void PlayAnimation(AnimationClip clip, bool isLoop = true, float speed = 1.0f)
        {
            if (_motionState != null)
            {
                _playableGraph.Disconnect(_motionState.ClipMixer, 0);
            }

            // Create cubism motion state.
            _motionState = CubismMotionState.CreateCubismMotionState(_playableGraph, clip, isLoop, speed);


#if UNITY_2018_2_OR_NEWER
            PlayableOutput.DisconnectInput(0);
#else
            PlayableOutput.GetGraph().Disconnect(PlayableOutput, 0);
#endif
            PlayableOutput.ConnectInput(0, _motionState.ClipMixer, 0);
            PlayableOutput.SetInputWeight(0, 1.0f);


            // Set last motion end time and fade in start time;
            if ((_playingMotions.Count > 0) && (_playingMotions[_playingMotions.Count - 1].Motion != null))
            {
                var motion = _playingMotions[_playingMotions.Count - 1];

                var time = Time.time;

                var newEndTime = time + motion.Motion.FadeOutTime;

                if (newEndTime < 0.0f || newEndTime < motion.EndTime)
                {
                    motion.EndTime = newEndTime;
                }


                while (motion.IsLooping)
                {
                    if ((motion.StartTime + motion.Motion.MotionLength) >= time)
                    {
                        break;
                    }

                    motion.StartTime += motion.Motion.MotionLength;
                }


                _playingMotions[_playingMotions.Count - 1] = motion;
            }

            // Create fade playing motion.
            var playingMotion = CreateFadePlayingMotion(clip, isLoop, speed);
            _playingMotions.Add(playingMotion);

            _isFinished = false;
        }
        /// <summary>
        /// Stop animation.
        /// </summary>
        public void StopAnimationClip()
        {
            // Remove from motion state list.
            if (_motionState == null)
            {
                return;
            }

            _playableGraph.Disconnect(_motionState.ClipMixer, 0);
            _motionState = null;

            _isFinished = true;


            StopAllAnimation();
        }
Exemple #4
0
        /// <summary>
        /// Play animation.
        /// </summary>
        /// <param name="clip">Animation clip.</param>
        /// <param name="isLoop">Animation is loop.</param>
        /// <param name="speed">Animation speed.</param>
        public void PlayAnimation(AnimationClip clip, bool isLoop = true, float speed = 1.0f)
        {
            if (_motionState != null)
            {
                _playableGraph.Disconnect(_motionState.ClipMixer, 0);
            }

            // Create cubism motion state.
            _motionState = CubismMotionState.CreateCubismMotionState(_playableGraph, clip, isLoop, speed);


#if UNITY_2018_2_OR_NEWER
            PlayableOutput.DisconnectInput(0);
#else
            PlayableOutput.GetGraph().Disconnect(PlayableOutput, 0);
#endif
            PlayableOutput.ConnectInput(0, _motionState.ClipMixer, 0);
            PlayableOutput.SetInputWeight(0, 1.0f);


            // Set last motion end time and fade in start time;
            for (var i = 0; i < _playingMotions.Count; ++i)
            {
                var motion = _playingMotions[i];

                if (motion.Motion == null)
                {
                    continue;
                }

                var newEndTime = Time.time + motion.Motion.FadeOutTime;

                motion.EndTime = newEndTime;

                _playingMotions[i] = motion;
            }

            // Create fade playing motion.
            var playingMotion = CreateFadePlayingMotion(clip, speed);
            _playingMotions.Add(playingMotion);

            _isFinished = false;
        }
Exemple #5
0
        /// <summary>
        /// Create motion state.
        /// </summary>
        /// <param name="playableGraph">Playable graph.</param>
        /// <param name="clip">Animation clip.</param>
        /// <param name="isLoop">Animation is loop.</param>
        /// <param name="speed">Animation speed.</param>
        public static CubismMotionState CreateCubismMotionState(PlayableGraph playableGraph, AnimationClip clip, bool isLoop = true, float speed = 1.0f)
        {
            var ret = new CubismMotionState();

            ret.Clip = clip;

            // Create animation clip mixer.
            ret.ClipMixer = AnimationMixerPlayable.Create(playableGraph, 2);
            ret.ClipMixer.SetSpeed(speed);

            // Connect AnimationClip Playable
            ret.ClipPlayable = AnimationClipPlayable.Create(playableGraph, ret.Clip);

            if (!isLoop)
            {
                ret.ClipPlayable.SetDuration(clip.length - 0.0001f);
            }

            ret.ClipMixer.ConnectInput(0, ret.ClipPlayable, 0);
            ret.ClipMixer.SetInputWeight(0, 1.0f);

            return(ret);
        }