/// <summary>
 /// binds the animation key frame.
 /// </summary>
 /// <param name="keyFrame">Animation key frame sequence</param>
 /// <param name="startTime">Start time of the animaton</param>
 /// <param name="timeScaleFactor">Time scale of the animaton</param>
 /// <param name="mode">Play mode of the animaton</param>
 public void BindKeyFrameSequence(KeyFrameSequence keyFrame,
                                  float startTime,
                                  float timeScaleFactor,
                                  AnimPlayMode mode)
 {
     SetKeyFrameSequence(keyFrame);
     SetTime(startTime);
     SetTimeScaleFactor(timeScaleFactor);
     SetPlayMode(mode);
 }
        /// <summary>
        /// calculates the key frame of the animation of the specified time.
        /// </summary>
        /// <param name="time">animation time</param>
        /// <returns>Matrix of the animation key frame</returns>
        public Matrix GetKeyFrameMatrix(float time)
        {
            if (KeyFrameSequence == null)
            {
                throw new InvalidOperationException("Sequence is not set.");
            }

            // Don't do anything if the timeScaleFactor is 0. (default scale is 1.0)
            if (ScaleFactor != 0.0f)
            {
                //  Accumulate animation local time
                localTime += (float)(ScaleFactor * time);

                switch (PlayMode)
                {
                case AnimPlayMode.Once:         //  Just play once
                {
                    if (localTime > KeyFrameSequence.Duration)
                    {
                        localTime = KeyFrameSequence.Duration;
                    }
                }
                break;

                case AnimPlayMode.Repeat:       //  Play looping
                {
                    //  Calculate time remainder after local time looping
                    if (localTime > Duration)
                    {
                        localTime = HelperMath.CalculateModulo(localTime,
                                                               KeyFrameSequence.Duration);
                    }
                }
                break;

                default:
                    throw new NotSupportedException("Not supported play mode");
                }

                return(KeyFrameSequence.GetInterpolateMatrix(localTime, PlayMode));
            }

            return(Matrix.Identity);
        }
Example #3
0
        /// <summary>
        /// inserts the new KeyFrameSequence, which is specified in the Binder.
        /// If there are two Binder’s, the first Binder will be gone and
        /// the second Binder will be moved as the first Binder,
        /// and the new KeyFrameSequence will be inserted into the second Binder.
        /// When the every Binder is empty, it will then insert to the first Binder
        /// </summary>
        /// <param name="keyFrame">Animation key frame sequence</param>
        /// <param name="startTime">Start time of the animaton</param>
        /// <param name="blendTime">Blend time of two the animaton</param>
        /// <param name="timeScaleFactor">Time scale of the animaton</param>
        /// <param name="mode">Play mode of the animaton</param>
        public void AddKeyFrameSequence(KeyFrameSequence keyFrame,
                                        float startTime,
                                        float blendTime,
                                        float timeScaleFactor,
                                        AnimPlayMode mode)
        {
            this.blendTime   = blendTime;
            this.elapsedTime = startTime;

            if (this.BlendTime == 0.0f)
            {
                ClearAllBinder();

                firstBinder.BindKeyFrameSequence(keyFrame, startTime,
                                                 timeScaleFactor, mode);
            }
            else
            {
                // If binding above two binders, push out one binder
                if (this.bindCount == 2)
                {
                    ShiftBinder();
                }

                if (this.bindCount == 0)
                {
                    firstBinder.BindKeyFrameSequence(keyFrame, startTime,
                                                     timeScaleFactor, mode);
                }
                else if (this.bindCount == 1)
                {
                    secondBinder.BindKeyFrameSequence(keyFrame, startTime,
                                                      timeScaleFactor, mode);
                }
            }

            this.bindCount++;
        }
        /// <summary>
        /// plays the bone animation by index.
        /// </summary>
        /// <param name="index">stored animation index</param>
        /// <param name="startTime">begin time of the animation</param>
        /// <param name="blendTime">blending time of the animation</param>
        /// <param name="timeScaleFactor">
        /// time scale of the animation (default is 1.0)
        /// </param>
        /// <param name="playMode">animation play mode</param>
        /// <returns></returns>
        public bool PlayAnimation(int index, float startTime, float blendTime,
                                  float timeScaleFactor, AnimPlayMode playMode)
        {
            AnimationSequence animation = GetAnimation(index);

            if (animation != null)
            {
                //  Binding the playable AnimationSequence to AnimationBinder
                for (int i = 0; i < animation.KeyFrameSequences.Count; i++)
                {
                    KeyFrameSequence sequence = animation.KeyFrameSequences[i];

                    AnimationBlender blender =
                        FindAnimationBlenderByBoneName(sequence.BoneName);
                    if (blender == null)
                    {
                        throw new InvalidOperationException(
                                  "The animation specified a bone (\"" + sequence.BoneName +
                                  "\") that the model (\"" + this.Name + "\") does not have.");
                    }

                    //  Initialize KeyFrameSequence infomation
                    blender.AddKeyFrameSequence(sequence, startTime, blendTime,
                                                timeScaleFactor, playMode);
                }

                if (traceAnimation)
                {
                    System.Diagnostics.Debug.WriteLine(
                        string.Format("Play Animtion : {0} ({1})", Name, index));
                }

                return(true);
            }

            return(false);
        }
        private KeyFrameSequence ReadKeyFrameSequence()
        {
            KeyFrameSequence keyFrameSequence = new KeyFrameSequence();

            keyFrameSequence.BoneName    = input.ReadString();
            keyFrameSequence.KeyCount    = input.ReadInt32();
            keyFrameSequence.Duration    = input.ReadSingle();
            keyFrameSequence.KeyInterval = input.ReadSingle();

            keyFrameSequence.HasTranslation = input.ReadBoolean();
            keyFrameSequence.HasRotation    = input.ReadBoolean();
            keyFrameSequence.HasScale       = input.ReadBoolean();
            keyFrameSequence.HasTime        = input.ReadBoolean();

            keyFrameSequence.FixedTranslation = input.ReadBoolean();
            keyFrameSequence.FixedRotation    = input.ReadBoolean();
            keyFrameSequence.FixedScale       = input.ReadBoolean();

            // read position values.
            int translationCount = input.ReadInt32();

            if (translationCount > 0)
            {
                keyFrameSequence.Translation = new List <Vector3>();

                for (int i = 0; i < translationCount; i++)
                {
                    keyFrameSequence.Translation.Add(input.ReadVector3());
                }
            }

            // read rotation values.
            int rotationCount = input.ReadInt32();

            if (rotationCount > 0)
            {
                keyFrameSequence.Rotation = new List <Quaternion>();

                for (int i = 0; i < rotationCount; i++)
                {
                    keyFrameSequence.Rotation.Add(input.ReadQuaternion());
                }
            }

            // read scale values.
            int scaleCount = input.ReadInt32();

            if (scaleCount > 0)
            {
                keyFrameSequence.Scale = new List <Vector3>();

                for (int i = 0; i < scaleCount; i++)
                {
                    keyFrameSequence.Scale.Add(input.ReadVector3());
                }
            }

            // read time values.
            int timeCount = input.ReadInt32();

            if (timeCount > 0)
            {
                keyFrameSequence.Time = new List <float>();

                for (int i = 0; i < timeCount; i++)
                {
                    keyFrameSequence.Time.Add(input.ReadSingle());
                }
            }

            return(keyFrameSequence);
        }
 /// <summary>
 /// sets the animation key frame structure.
 /// </summary>
 public void SetKeyFrameSequence(KeyFrameSequence keyFrame)
 {
     keyFrameSequence = keyFrame;
 }