public STKeyFrame GetFVGFR(STAnimationTrack t, float frame, float startFrame = 0)
            {
                if (t.KeyFrames.Count == 0)
                {
                    return(new STKeyFrame(frame, 0));
                }
                if (t.KeyFrames.Count == 1)
                {
                    return(t.KeyFrames[0]);
                }

                STKeyFrame RK = t.KeyFrames.Last();

                float Frame = frame - startFrame;

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

                return(RK);
            }
            public STKeyFrame GetFVGFL(STAnimationTrack t, float frame, float startFrame = 0)
            {
                if (t.KeyFrames.Count == 0)
                {
                    return(new STKeyFrame(frame, 0));
                }
                if (t.KeyFrames.Count == 1)
                {
                    return(t.KeyFrames[0]);
                }

                STKeyFrame LK = t.KeyFrames.First();

                float Frame = frame - startFrame;

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

                return(LK);
            }
Example #3
0
            private STAnimationTrack LoadTrack(float[] Frames, float[] Values)
            {
                STAnimationTrack track = new STAnimationTrack();

                track.InterpolationType = STInterpoaltionType.Linear;
                for (int i = 0; i < Values?.Length; i++)
                {
                    STKeyFrame keyFrame = new STKeyFrame();
                    keyFrame.Value = Values[i];
                    keyFrame.Value = Frames[i];
                    track.KeyFrames.Add(keyFrame);
                }
                return(track);
            }
Example #4
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 void NextFrame()
            {
                if (Frame > FrameCount || ActiveModel == null)
                {
                    return;
                }

                var skeleton = ActiveModel.Model.Skeleton;

                if (skeleton == null)
                {
                    return;
                }

                if (Frame == 0)
                {
                    skeleton.reset();
                }

                bool Updated = false; // no need to update skeleton of animations that didn't change

                foreach (var animGroup in AnimGroups)
                {
                    if (animGroup is VisibiltyGroup && ActiveModel != null)
                    {
                        var node = animGroup as VisibiltyGroup;
                        foreach (var mesh in ActiveModel.Model.GenericMeshes)
                        {
                            if (animGroup.Name == mesh.Text)
                            {
                                var value = node.BooleanTrack.GetFrameValue(Frame);
                                mesh.AnimationController.IsVisible = value == 1;
                            }
                        }
                    }
                    if (animGroup is MaterialGroup && ActiveModel != null)
                    {
                        foreach (var mat in ActiveModel.Model.GenericMaterials)
                        {
                            if (animGroup.Name == mat.Text)
                            {
                            }
                        }
                    }
                    if (animGroup is BoneGroup)
                    {
                        var node = animGroup as BoneGroup;

                        STBone b = null;

                        b = skeleton.GetBone(node.Name);
                        if (b == null)
                        {
                            continue;
                        }

                        Updated = true;

                        if (node.TranslateX.HasKeys)
                        {
                            b.pos.X = node.TranslateX.GetFrameValue(Frame);
                        }
                        if (node.TranslateY.HasKeys)
                        {
                            b.pos.Y = node.TranslateY.GetFrameValue(Frame);
                        }
                        if (node.TranslateZ.HasKeys)
                        {
                            b.pos.Z = node.TranslateZ.GetFrameValue(Frame);
                        }

                        if (node.ScaleX.HasKeys)
                        {
                            b.sca.X = node.ScaleX.GetFrameValue(Frame);
                        }
                        else
                        {
                            b.sca.X = 1;
                        }
                        if (node.ScaleY.HasKeys)
                        {
                            b.sca.Y = node.ScaleY.GetFrameValue(Frame);
                        }
                        else
                        {
                            b.sca.Y = 1;
                        }
                        if (node.ScaleZ.HasKeys)
                        {
                            b.sca.Z = node.ScaleZ.GetFrameValue(Frame);
                        }
                        else
                        {
                            b.sca.Z = 1;
                        }

                        if (node.RotationX.HasKeys || node.RotationY.HasKeys || node.RotationZ.HasKeys)
                        {
                            STKeyFrame xL = GetFVGFL(node.RotationX, Frame);
                            STKeyFrame yL = GetFVGFL(node.RotationY, Frame);
                            STKeyFrame zL = GetFVGFL(node.RotationZ, Frame);
                            STKeyFrame xR = GetFVGFR(node.RotationX, Frame);
                            STKeyFrame yR = GetFVGFR(node.RotationY, Frame);
                            STKeyFrame zR = GetFVGFR(node.RotationZ, Frame);

                            short value1 = (short)xL.Value;
                            short value2 = (short)yL.Value;
                            short value3 = (short)zL.Value;

                            Quaternion rotL = PackedToQuat(value1, value2, value3);

                            short value1r = (short)xR.Value;
                            short value2r = (short)yR.Value;
                            short value3r = (short)zR.Value;

                            Quaternion rotR = PackedToQuat(value1r, value2r, value3r);

                            float weight = (Frame - xL.Frame) / (xR.Frame - xL.Frame);
                            if (xR.Frame - xL.Frame == 0)   //NaN if divided by zero
                            {
                                weight = 0;
                            }

                            b.rot = Quaternion.Slerp(rotL, rotR, weight);
                        }
                        else
                        {
                            b.rot = EulerToQuat(b.EulerRotation.Z, b.EulerRotation.Y, b.EulerRotation.X);
                        }
                    }
                }

                if (Updated)
                {
                    skeleton.update();
                }
            }
        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);
        }