ColladaXna.Base.Animation.JointAnimationKeyFrame.Lerp C# (CSharp) Method

Lerp() public static method

Linear interpolation of two keyframes. The resulting keyframe will consist of the interpolated time and transformation.
public static Lerp ( JointAnimationKeyFrame frame1, JointAnimationKeyFrame frame2, float amount ) : JointAnimationKeyFrame
frame1 JointAnimationKeyFrame One keyframe
frame2 JointAnimationKeyFrame Another keyframe
amount float Weight of the second keyframe between 0.0 and 1.0
return JointAnimationKeyFrame
        public static JointAnimationKeyFrame Lerp(JointAnimationKeyFrame frame1,
            JointAnimationKeyFrame frame2, float amount)
        {
            // Lerp between components
            Vector3 scale = Vector3.Lerp(frame1.Scale, frame2.Scale, amount);
            Quaternion rotation = Quaternion.Lerp(frame1.Rotation, frame2.Rotation, amount);
            Vector3 translation = Vector3.Lerp(frame1.Translation, frame2.Translation, amount);

            // Lerp time
            float time = frame1.Time + (frame2.Time - frame1.Time) * amount;

            return new JointAnimationKeyFrame(time, scale, rotation, translation);
        }

Usage Example

        /// <summary>
        /// Calculates a key frame at given time by interpolating between the samples
        /// that are nearest (by time).
        /// </summary>
        /// <param name="time">Time</param>
        /// <returns>Interpolated key frame for given time key</returns>
        public JointAnimationKeyFrame GetInterpolatedKeyframe(float time)
        {
            if (time < StartTime)
            {
                switch (PreBehaviour)
                {
                case AnimationBehaviour.Constant:
                    time = StartTime;
                    break;

                case AnimationBehaviour.Cycle:
                    time = 0;
                    break;

                default:
                    // TODO: Implement other animation behaviours
                    throw new NotImplementedException("No animation behaviours " +
                                                      "other than Constant implemented yet");
                }
            }
            else if (time > EndTime)
            {
                switch (PostBehaviour)
                {
                case AnimationBehaviour.Constant:
                    time = EndTime;
                    break;

                case AnimationBehaviour.Cycle:
                    time %= EndTime;
                    break;

                default:
                    // TODO: Implement other animation behaviours
                    throw new NotImplementedException("No animation behaviours " +
                                                      "other than Constant implemented yet");
                }
            }

            int keyframeIndex = 0;

            // Find the two frames to interpolate between
            while (_keyframes[keyframeIndex].Time <= time)
            {
                keyframeIndex++;
            }

            if (keyframeIndex > 0)
            {
                keyframeIndex--;
            }

            JointAnimationKeyFrame frame1 = _keyframes[keyframeIndex];
            JointAnimationKeyFrame frame2 = _keyframes[keyframeIndex + 1];

            if (frame1.Time == time)
            {
                return(frame1);
            }

            switch (_interpolation)
            {
            case AnimationInterpolation.Linear:
                float amount = 1.0f / (frame2.Time - frame1.Time) * (time - frame1.Time);
                return(JointAnimationKeyFrame.Lerp(frame1, frame2, amount));

            default:
                // TODO: Implement other animation interpolations
                throw new NotImplementedException("No animation interpolation " +
                                                  "other than Linear implemented yet");
            }
        }