コード例 #1
0
ファイル: SMD.cs プロジェクト: skybook888/Switch-Toolbox
        public static Animation Read(string fname, STSkeleton v)
        {
            Animation a = new Animation();

            StreamReader reader = File.OpenText(fname);
            string       line;

            string current = "";
            bool   readBones = false;
            int    frame = 0, prevframe = 0;

            Animation.KeyFrame k = new Animation.KeyFrame();

            STSkeleton vbn = v;

            if (v != null && v.bones.Count == 0)
            {
                readBones = true;
            }
            else
            {
                vbn = new STSkeleton();
            }

            while ((line = reader.ReadLine()) != null)
            {
                line = Regex.Replace(line, @"\s+", " ");
                string[] args = line.Replace(";", "").TrimStart().Split(' ');

                if (args[0].Equals("nodes") || args[0].Equals("skeleton") || args[0].Equals("end") || args[0].Equals("time"))
                {
                    current = args[0];
                    if (args.Length > 1)
                    {
                        prevframe = frame;
                        frame     = int.Parse(args[1]);

                        /*if (frame != prevframe + 1) {
                         *                              Console.WriteLine ("Needs interpolation " + frame);
                         *                      }*/

                        k       = new Animation.KeyFrame();
                        k.Frame = frame;
                        //a.addKeyframe(k);
                    }
                    continue;
                }

                if (current.Equals("nodes"))
                {
                    STBone b = new STBone(vbn);
                    b.Text        = args[1].Replace("\"", "");
                    b.parentIndex = int.Parse(args[2]);
                    //b.children = new System.Collections.Generic.List<int> ();
                    vbn.bones.Add(b);
                    Animation.KeyNode node = new Animation.KeyNode(b.Text);
                    a.Bones.Add(node);
                }

                if (current.Equals("time"))
                {
                    // reading the skeleton if this isn't an animation
                    if (readBones && frame == 0)
                    {
                        STBone b = vbn.bones[int.Parse(args[0])];
                        b.Position = new Vector3(
                            float.Parse(args[1]),
                            float.Parse(args[2]),
                            float.Parse(args[3]));
                        b.EulerRotation = new Vector3(
                            float.Parse(args[4]),
                            float.Parse(args[5]),
                            float.Parse(args[6]));
                        b.Scale = Vector3.One;

                        b.pos = new Vector3(float.Parse(args[1]), float.Parse(args[2]), float.Parse(args[3]));
                        b.rot = STSkeleton.FromEulerAngles(float.Parse(args[6]), float.Parse(args[5]), float.Parse(args[4]));

                        if (b.parentIndex != -1)
                        {
                            vbn.bones[b.parentIndex].Nodes.Add(b);
                        }
                    }
                    Animation.KeyNode bone = a.GetBone(vbn.bones[int.Parse(args[0])].Text);
                    bone.RotType = Animation.RotationType.EULER;

                    Animation.KeyFrame n = new Animation.KeyFrame();
                    n.Value = float.Parse(args[1]);
                    n.Frame = frame;
                    bone.XPOS.Keys.Add(n);

                    n       = new Animation.KeyFrame();
                    n.Value = float.Parse(args[2]);
                    n.Frame = frame;
                    bone.YPOS.Keys.Add(n);

                    n       = new Animation.KeyFrame();
                    n.Value = float.Parse(args[3]);
                    n.Frame = frame;
                    bone.ZPOS.Keys.Add(n);

                    n       = new Animation.KeyFrame();
                    n.Value = float.Parse(args[4]);
                    n.Frame = frame;
                    bone.XROT.Keys.Add(n);

                    n       = new Animation.KeyFrame();
                    n.Value = float.Parse(args[5]);
                    n.Frame = frame;
                    bone.YROT.Keys.Add(n);

                    n       = new Animation.KeyFrame();
                    n.Value = float.Parse(args[6]);
                    n.Frame = frame;
                    bone.ZROT.Keys.Add(n);

                    if (args.Length > 7)
                    {
                        n       = new Animation.KeyFrame();
                        n.Value = float.Parse(args[7]);
                        n.Frame = frame;
                        bone.XSCA.Keys.Add(n);

                        n       = new Animation.KeyFrame();
                        n.Value = float.Parse(args[8]);
                        n.Frame = frame;
                        bone.YSCA.Keys.Add(n);

                        n       = new Animation.KeyFrame();
                        n.Value = float.Parse(args[9]);
                        n.Frame = frame;
                        bone.ZSCA.Keys.Add(n);
                    }
                    else
                    {
                        bone.XSCA.Keys.Add(new Animation.KeyFrame()
                        {
                            Value = 1.0f,
                            Frame = frame,
                        });
                        bone.YSCA.Keys.Add(new Animation.KeyFrame()
                        {
                            Value = 1.0f,
                            Frame = frame,
                        });
                        bone.ZSCA.Keys.Add(new Animation.KeyFrame()
                        {
                            Value = 1.0f,
                            Frame = frame,
                        });
                    }
                }
            }

            a.FrameCount = frame;
            vbn.update();

            return(a);
        }
コード例 #2
0
ファイル: ANIM.cs プロジェクト: lanvas2008/Switch-Toolbox
        public static Animation read(string filename, STSkeleton vbn)
        {
            StreamReader reader = File.OpenText(filename);
            string       line;

            bool isHeader = true;

            string          angularUnit, linearUnit, timeUnit;
            int             startTime = 0;
            int             endTime   = 0;
            List <AnimBone> bones     = new List <AnimBone>();

            Animation.KeyNode  current = null;
            Animation.KeyFrame att     = new Animation.KeyFrame();
            bool   inKeys = false;
            string type   = "";

            Animation a = new Animation(filename);

            while ((line = reader.ReadLine()) != null)
            {
                string[] args = line.Replace(";", "").TrimStart().Split(' ');

                if (isHeader)
                {
                    if (args [0].Equals("anim"))
                    {
                        isHeader = false;
                    }
                    else if (args [0].Equals("angularUnit"))
                    {
                        angularUnit = args [1];
                    }
                    else if (args [0].Equals("endTime"))
                    {
                        endTime = (int)Math.Ceiling(float.Parse(args [1]));
                    }
                    else if (args [0].Equals("startTime"))
                    {
                        startTime = (int)Math.Ceiling(float.Parse(args [1]));
                    }
                }

                if (!isHeader)
                {
                    if (inKeys)
                    {
                        if (args[0].Equals("}"))
                        {
                            inKeys = false;
                            continue;
                        }
                        Animation.KeyFrame k = new Animation.KeyFrame();
                        //att.keys.Add (k);
                        if (type.Contains("translate"))
                        {
                            if (type.Contains("X"))
                            {
                                current.XPOS.Keys.Add(k);
                            }
                            if (type.Contains("Y"))
                            {
                                current.YPOS.Keys.Add(k);
                            }
                            if (type.Contains("Z"))
                            {
                                current.ZPOS.Keys.Add(k);
                            }
                        }
                        if (type.Contains("rotate"))
                        {
                            if (type.Contains("X"))
                            {
                                current.XROT.Keys.Add(k);
                            }
                            if (type.Contains("Y"))
                            {
                                current.YROT.Keys.Add(k);
                            }
                            if (type.Contains("Z"))
                            {
                                current.ZROT.Keys.Add(k);
                            }
                        }
                        if (type.Contains("scale"))
                        {
                            if (type.Contains("X"))
                            {
                                current.XSCA.Keys.Add(k);
                            }
                            if (type.Contains("Y"))
                            {
                                current.YSCA.Keys.Add(k);
                            }
                            if (type.Contains("Z"))
                            {
                                current.ZSCA.Keys.Add(k);
                            }
                        }
                        k.Frame = float.Parse(args [0]) - 1;
                        k.Value = float.Parse(args [1]);
                        if (type.Contains("rotate"))
                        {
                            k.Value *= Deg2Rad;
                        }
                        //k.intan = (args [2]);
                        //k.outtan = (args [3]);
                        if (args.Length > 7 && att.Weighted)
                        {
                            k.In  = float.Parse(args[7]) * Deg2Rad;
                            k.Out = float.Parse(args[8]) * Deg2Rad;
                        }
                    }

                    if (args [0].Equals("anim"))
                    {
                        inKeys = false;
                        if (args.Length == 5)
                        {
                            //TODO: finish this type
                            // can be name of attribute
                        }
                        if (args.Length == 7)
                        {
                            // see of the bone of this attribute exists
                            current = null;
                            foreach (Animation.KeyNode b in a.Bones)
                            {
                                if (b.Text.Equals(args [3]))
                                {
                                    current = b;
                                    break;
                                }
                            }
                            if (current == null)
                            {
                                current         = new Animation.KeyNode(args[3]);
                                current.RotType = Animation.RotationType.EULER;
                                a.Bones.Add(current);
                            }
                            current.Text = args [3];

                            att           = new Animation.KeyFrame();
                            att.InterType = InterpolationType.HERMITE;
                            type          = args [2];
                            //current.Nodes.Add (att);

                            // row child attribute aren't needed here
                        }
                    }

                    /*if (args [0].Equals ("input"))
                     *                          att.input = args [1];
                     *                  if (args [0].Equals ("output"))
                     *                          att.output = args [1];
                     *                  if (args [0].Equals ("preInfinity"))
                     *                          att.preInfinity = args [1];
                     *                  if (args [0].Equals ("postInfinity"))
                     *                          att.postInfinity = args [1];*/
                    if (args[0].Equals("weighted"))
                    {
                        att.Weighted = args[1].Equals("1");
                    }


                    // begining keys section
                    if (args [0].Contains("keys"))
                    {
                        inKeys = true;
                    }
                }
            }

            a.FrameCount = endTime - 1;

            reader.Close();
            return(a);
        }