Exemple #1
0
        private static List <STKeyFrame> ParseKeyFrames(JsonReader reader, STInterpoaltionType type)
        {
            if (type == STInterpoaltionType.Constant)
            {
                return(new List <STKeyFrame>());
            }

            List <STKeyFrame> keyFrames = new List <STKeyFrame>();

            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.EndObject)
                {
                    break;
                }

                if (reader.Value == null)
                {
                    continue;
                }

                if (reader.Value.Equals("Frame"))
                {
                    if (type == STInterpoaltionType.Bezier)
                    {
                        STBezierKeyFrame keyFrame = new STBezierKeyFrame();
                        keyFrame.Frame = (float)reader.ReadAsDecimal();
                        reader.Read(); //Value
                        keyFrame.Value = (float)reader.ReadAsDecimal();
                        reader.Read(); //in
                        keyFrame.SlopeIn = (float)reader.ReadAsDecimal();
                        reader.Read(); //out
                        keyFrame.SlopeOut = (float)reader.ReadAsDecimal();
                        keyFrames.Add(keyFrame);
                    }
                    else
                    {
                        STKeyFrame keyFrame = new STKeyFrame();
                        keyFrame.Frame = (float)reader.ReadAsDecimal();
                        reader.Read(); //Value
                        keyFrame.Value = (float)reader.ReadAsDecimal();
                        keyFrames.Add(keyFrame);
                    }
                }
            }
            return(keyFrames);
        }
        public override float GetFrameValue(float frame, float startFrame = 0)
        {
            if (InterpolationType == STInterpoaltionType.Constant)
            {
                return(Constant);
            }

            if (KeyFrames.Count == 0)
            {
                return(0);
            }
            if (KeyFrames.Count == 1)
            {
                return(KeyFrames[0].Value);
            }

            STKeyFrame LK = KeyFrames.First();
            STKeyFrame RK = KeyFrames.Last();

            float Frame = frame - startFrame;

            foreach (STKeyFrame keyFrame in KeyFrames)
            {
                if (keyFrame.Frame <= Frame)
                {
                    LK = keyFrame;
                }
                if (keyFrame.Frame >= Frame && keyFrame.Frame < RK.Frame)
                {
                    RK = keyFrame;
                }
            }

            if (LK.Frame != RK.Frame)
            {
                float FrameDiff = Frame - LK.Frame;
                float Weight    = FrameDiff / (RK.Frame - LK.Frame);

                switch (InterpolationType)
                {
                case STInterpoaltionType.Constant: return(LK.Value);

                case STInterpoaltionType.Step: return(LK.Value);

                case STInterpoaltionType.Linear: return(InterpolationHelper.Lerp(LK.Value, RK.Value, Weight));

                case STInterpoaltionType.Bezier:
                {
                    return(InterpolationHelper.Lerp(LK.Value, RK.Value, Weight));

                    STBezierKeyFrame bezierKeyLK = (STBezierKeyFrame)LK;
                    STBezierKeyFrame bezierKeyRK = (STBezierKeyFrame)RK;

                    float length = RK.Frame - LK.Frame;

                    return(InterpolationHelper.BezierInterpolate(frame,
                                                                 bezierKeyLK.Frame,
                                                                 bezierKeyRK.Frame,
                                                                 bezierKeyLK.SlopeIn,
                                                                 bezierKeyRK.SlopeOut,
                                                                 bezierKeyLK.Value,
                                                                 bezierKeyRK.Value));
                }

                case STInterpoaltionType.Hermite:
                {
                    STHermiteKeyFrame hermiteKeyLK = (STHermiteKeyFrame)LK;
                    STHermiteKeyFrame hermiteKeyRK = (STHermiteKeyFrame)RK;

                    float length = RK.Frame - LK.Frame;

                    return(InterpolationHelper.HermiteInterpolate(frame,
                                                                  hermiteKeyLK.Frame,
                                                                  hermiteKeyRK.Frame,
                                                                  hermiteKeyLK.TangentIn,
                                                                  hermiteKeyLK.TangentOut,
                                                                  hermiteKeyLK.Value,
                                                                  hermiteKeyRK.Value));
                }
                }
            }

            return(LK.Value);
        }