//Key frame setup based on
        //https://github.com/gdkchan/SPICA/blob/42c4181e198b0fd34f0a567345ee7e75b54cb58b/SPICA/Formats/CtrH3D/Animation/H3DFloatKeyFrameGroup.cs
        public float GetFrameValue(float frame, float startFrame = 0)
        {
            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.Hermite:
                    return(InterpolationHelper.Herp(
                               LK.Value, RK.Value,
                               LK.Slope, RK.Slope,
                               FrameDiff,
                               Weight));
                }
            }

            return(LK.Value);
        }
Exemple #2
0
            public float GetValue(float frame)
            {
                if (Keys.Count == 0)
                {
                    return(0);
                }

                float startFrame = Keys.First().Frame;

                KeyFrame LK = Keys.First();
                KeyFrame RK = Keys.Last();

                foreach (KeyFrame keyFrame in Keys)
                {
                    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 = 1.0f / (RK.Frame - LK.Frame);
                    //  float Weight = FrameDiff / (RK.Frame - LK.Frame);

                    float FrameDiff = frame - LK.Frame;
                    float Weight    = FrameDiff / (RK.Frame - LK.Frame);

                    Console.WriteLine($"frame diff {FrameDiff} frame {frame} LK {LK.Frame} RK {RK} ratio {Weight}");

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

                    case InterpolationType.STEP: return(LK.Value);

                    case InterpolationType.LINEAR: return(InterpolationHelper.Lerp(LK.Value, RK.Value, Weight));

                    case InterpolationType.HERMITE:
                        float val = Hermite(frame, LK.Frame, RK.Frame, LK.Slope2, RK.Slope1, LK.Value, RK.Value);
                        return(val);
                    }
                }
                return(LK.Value);
            }