Ejemplo n.º 1
0
        public static void Read(string filename)
        {
            bchHeader header = new bchHeader();
            FileData  f      = new FileData(filename);

            f.Endian = System.IO.Endianness.Little;

            f.skip(4);
            header.backwardCompatibility = f.readByte();
            header.forwardCompatibility  = f.readByte();
            header.version = f.readShort();

            header.mainHeaderOffset  = f.readInt();
            header.stringTableOffset = f.readInt();
            header.gpuCommandsOffset = f.readInt();
            header.dataOffset        = f.readInt();
            if (header.backwardCompatibility > 0x20)
            {
                header.dataExtendedOffset = f.readInt();
            }
            header.relocationTableOffset = f.readInt();

            header.mainHeaderLength  = f.readInt();
            header.stringTableLength = f.readInt();
            header.gpuCommandsLength = f.readInt();
            header.dataLength        = f.readInt();
            if (header.backwardCompatibility > 0x20)
            {
                header.dataExtendedLength = f.readInt();
            }
            header.relocationTableLength = f.readInt();

            header.uninitializedDataSectionLength        = f.readInt();
            header.uninitializedDescriptionSectionLength = f.readInt();

            if (header.backwardCompatibility > 7)
            {
                header.flags        = f.readShort();
                header.addressCount = f.readShort();
            }

            // Relocation table
            for (int i = 0; i < header.relocationTableLength; i += 4)
            {
                f.seek(header.relocationTableOffset + i);
                int  val  = f.readInt();
                int  off  = val & 0x1FFFFFF;
                byte flag = (byte)(val >> 25);

                switch (flag)
                {
                case 0:
                    f.seek((off * 4) + header.mainHeaderOffset);
                    f.writeInt((off * 4) + header.mainHeaderOffset, f.readInt() + header.mainHeaderOffset);
                    break;

                case 1:
                    f.seek(off + header.mainHeaderOffset);
                    f.writeInt((off) + header.mainHeaderOffset, f.readInt() + header.stringTableOffset);
                    break;

                case 2:
                    f.seek((off * 4) + header.mainHeaderOffset);
                    f.writeInt((off * 4) + header.mainHeaderOffset, f.readInt() + header.gpuCommandsOffset);
                    break;

                case 0xc:
                    f.seek((off * 4) + header.mainHeaderOffset);
                    f.writeInt((off * 4) + header.mainHeaderOffset, f.readInt() + header.dataOffset);
                    break;
                }

                f.seek((off * 4) + header.gpuCommandsOffset);
                if (header.backwardCompatibility < 6)
                {
                    switch (flag)
                    {
                    case 0x23: f.writeInt((off * 4) + header.gpuCommandsOffset, f.readInt() + header.dataOffset); break;     //Texture

                    case 0x25: f.writeInt((off * 4) + header.gpuCommandsOffset, f.readInt() + header.dataOffset); break;     //Vertex

                    //case 0x26: f.writeInt((off * 4) + header.gpuCommandsOffset, ((f.readInt() + header.dataOffset) & 0x7fffffff) | 0x80000000); break; //Index 16 bits mode
                    case 0x27: f.writeInt((off * 4) + header.gpuCommandsOffset, (f.readInt() + header.dataOffset) & 0x7fffffff); break;     //Index 8 bits mode
                    }
                }
                else if (header.backwardCompatibility < 8)
                {
                    switch (flag)
                    {
                    case 0x24: f.writeInt((off * 4) + header.gpuCommandsOffset, f.readInt() + header.dataOffset); break;     //Texture

                    case 0x26: f.writeInt((off * 4) + header.gpuCommandsOffset, f.readInt() + header.dataOffset); break;     //Vertex

                    //case 0x27: writer.Write(((peek(input) + header.dataOffset) & 0x7fffffff) | 0x80000000); break; //Index 16 bits mode
                    case 0x28: f.writeInt((off * 4) + header.gpuCommandsOffset, (f.readInt() + header.dataOffset) & 0x7fffffff); break;     //Index 8 bits mode
                    }
                }
                else if (header.backwardCompatibility < 0x21)
                {
                    switch (flag)
                    {
                    case 0x25: f.writeInt((off * 4) + header.gpuCommandsOffset, f.readInt() + header.dataOffset); break;     //Texture

                    case 0x27: f.writeInt((off * 4) + header.gpuCommandsOffset, f.readInt() + header.dataOffset); break;     //Vertex

                    //case 0x28: writer.Write(((peek(input) + header.dataOffset) & 0x7fffffff) | 0x80000000); break; //Index 16 bits mode
                    case 0x29: f.writeInt((off * 4) + header.gpuCommandsOffset, (f.readInt() + header.dataOffset) & 0x7fffffff); break;     //Index 8 bits mode
                    }
                }
                else
                {
                    switch (flag)
                    {
                    case 0x25: f.writeInt((off * 4) + header.gpuCommandsOffset, f.readInt() + header.dataOffset); break;     //Texture

                    case 0x26: f.writeInt((off * 4) + header.gpuCommandsOffset, f.readInt() + header.dataOffset); break;     //Vertex relative to Data Offset

                    //case 0x27: writer.Write(((peek(input) + header.dataOffset) & 0x7fffffff) | 0x80000000); break; //Index 16 bits mode relative to Data Offset
                    case 0x28: f.writeInt((off * 4) + header.gpuCommandsOffset, (f.readInt() + header.dataOffset) & 0x7fffffff); break; //Index 8 bits mode relative to Data Offset

                    case 0x2b: f.writeInt((off * 4) + header.gpuCommandsOffset, f.readInt() + header.dataExtendedOffset); break;        //Vertex relative to Data Extended Offset

                    //case 0x2c: writer.Write(((peek(input) + header.dataExtendedOffset) & 0x7fffffff) | 0x80000000); break; //Index 16 bits mode relative to Data Extended Offset
                    case 0x2d: f.writeInt((off * 4) + header.gpuCommandsOffset, (f.readInt() + header.dataExtendedOffset) & 0x7fffffff); break;     //Index 8 bits mode relative to Data Extended Offset
                    }
                }
            }


            // Content Header
            f.seek(header.mainHeaderOffset);
            bchContentHeader content = new bchContentHeader();
            {
                content.modelsPointerTableOffset        = f.readInt();
                content.modelsPointerTableEntries       = f.readInt();
                content.modelsNameOffset                = f.readInt();
                content.materialsPointerTableOffset     = f.readInt();
                content.materialsPointerTableEntries    = f.readInt();
                content.materialsNameOffset             = f.readInt();
                content.shadersPointerTableOffset       = f.readInt();
                content.shadersPointerTableEntries      = f.readInt();
                content.shadersNameOffset               = f.readInt();
                content.texturesPointerTableOffset      = f.readInt();
                content.texturesPointerTableEntries     = f.readInt();
                content.texturesNameOffset              = f.readInt();
                content.materialsLUTPointerTableOffset  = f.readInt();
                content.materialsLUTPointerTableEntries = f.readInt();
                content.materialsLUTNameOffset          = f.readInt();
                content.lightsPointerTableOffset        = f.readInt();
                content.lightsPointerTableEntries       = f.readInt();
                content.lightsNameOffset                = f.readInt();
                content.camerasPointerTableOffset       = f.readInt();
                content.camerasPointerTableEntries      = f.readInt();
                content.camerasNameOffset               = f.readInt();
                content.fogsPointerTableOffset          = f.readInt();
                content.fogsPointerTableEntries         = f.readInt();
                content.fogsNameOffset = f.readInt();
                content.skeletalAnimationsPointerTableOffset    = f.readInt();
                content.skeletalAnimationsPointerTableEntries   = f.readInt();
                content.skeletalAnimationsNameOffset            = f.readInt();
                content.materialAnimationsPointerTableOffset    = f.readInt();
                content.materialAnimationsPointerTableEntries   = f.readInt();
                content.materialAnimationsNameOffset            = f.readInt();
                content.visibilityAnimationsPointerTableOffset  = f.readInt();
                content.visibilityAnimationsPointerTableEntries = f.readInt();
                content.visibilityAnimationsNameOffset          = f.readInt();
                content.lightAnimationsPointerTableOffset       = f.readInt();
                content.lightAnimationsPointerTableEntries      = f.readInt();
                content.lightAnimationsNameOffset           = f.readInt();
                content.cameraAnimationsPointerTableOffset  = f.readInt();
                content.cameraAnimationsPointerTableEntries = f.readInt();
                content.cameraAnimationsNameOffset          = f.readInt();
                content.fogAnimationsPointerTableOffset     = f.readInt();
                content.fogAnimationsPointerTableEntries    = f.readInt();
                content.fogAnimationsNameOffset             = f.readInt();
                content.scenePointerTableOffset             = f.readInt();
                content.scenePointerTableEntries            = f.readInt();
                content.sceneNameOffset = f.readInt();
            }


            //Skeletal animation
            TreeNode ThisAnimation = new TreeNode()
            {
                Text = filename
            };

            MainForm.animNode.Nodes.Add(ThisAnimation);

            for (int index1 = 0; index1 < content.skeletalAnimationsPointerTableEntries; index1++)//
            {
                f.seek(content.skeletalAnimationsPointerTableOffset + (index1 * 4));
                int dataOffset = f.readInt();
                f.seek(dataOffset);


                string skeletalAnimationName = f.readString(f.readInt(), -1);
                int    animationFlags        = f.readInt();
                //int skeletalAnimationloopMode = f.readByte();  //pas ça du tout
                float skeletalAnimationframeSize = f.readFloat();
                int   boneTableOffset            = f.readInt();
                int   boneTableEntries           = f.readInt();
                int   metaDataPointerOffset      = f.readInt();

                //Runtime.Animations.Add(skeletalAnimationName, a);
                //MainForm.animNode.Nodes.Add(skeletalAnimationName);

                Debug.WriteLine("Animation Name: " + skeletalAnimationName);
                Animation a = new Animation(skeletalAnimationName);
                ThisAnimation.Nodes.Add(a);

                for (int i = 0; i < boneTableEntries; i++)
                {
                    f.seek(boneTableOffset + (i * 4));
                    int offset = f.readInt();

                    Animation.KeyNode bone = new Animation.KeyNode("");
                    a.Bones.Add(bone);
                    f.seek(offset);
                    bone.Text = f.readString(f.readInt(), -1);
                    //Console.WriteLine("Bone Name: " + bone.name);
                    int  animationTypeFlags = f.readInt();
                    uint flags = (uint)f.readInt();

                    OSegmentType segmentType = (OSegmentType)((animationTypeFlags >> 16) & 0xf);
                    switch (segmentType)
                    {
                    case OSegmentType.transform:
                        f.seek(offset + 0xC);
                        //Console.WriteLine(f.pos().ToString("x") + " " + flags.ToString("x"));

                        uint notExistMask = 0x10000;
                        uint constantMask = 0x40;

                        for (int j = 0; j < 3; j++)
                        {
                            for (int axis = 0; axis < 3; axis++)
                            {
                                bool notExist = (flags & notExistMask) > 0;
                                bool constant = (flags & constantMask) > 0;
                                //Console.WriteLine(notExist + " " + constant);

                                Animation.KeyGroup group = new Animation.KeyGroup();
                                //frame.exists = !notExist;
                                if (!notExist)
                                {
                                    if (constant)
                                    {
                                        Animation.KeyFrame frame = new Animation.KeyFrame();
                                        frame.InterType = Animation.InterpolationType.LINEAR;
                                        frame.Value     = f.readFloat();
                                        frame.Frame     = 0;
                                        group.Keys.Add(frame);
                                    }
                                    else
                                    {
                                        int frameOffset = f.readInt();
                                        int position    = f.pos();
                                        f.seek(frameOffset);
                                        float c = 0;
                                        getAnimationKeyFrame(f, group, out c);
                                        if (c > a.FrameCount)
                                        {
                                            a.FrameCount = (int)c;
                                        }
                                        f.seek(position);
                                    }
                                }
                                else
                                {
                                    f.seek(f.pos() + 0x04);
                                }
                                bone.RotType = Animation.RotationType.EULER;

                                if (j == 0)
                                {
                                    switch (axis)
                                    {
                                    case 0: bone.XSCA = group; break;

                                    case 1: bone.YSCA = group; break;

                                    case 2: bone.ZSCA = group; break;
                                    }
                                }
                                else
                                if (j == 1)
                                {
                                    switch (axis)
                                    {
                                    case 0: bone.XROT = group; break;

                                    case 1: bone.YROT = group; break;

                                    case 2: bone.ZROT = group; break;
                                    }
                                }
                                else
                                if (j == 2)
                                {
                                    switch (axis)
                                    {
                                    case 0: bone.XPOS = group; break;

                                    case 1: bone.YPOS = group; break;

                                    case 2: bone.ZPOS = group; break;
                                    }
                                }

                                notExistMask <<= 1;
                                constantMask <<= 1;
                            }
                            if (j == 1)
                            {
                                constantMask <<= 1;
                            }
                        }

                        break;

                    /*case OSegmentType.transformQuaternion:
                     *  bone.isFrameFormat = true;
                     *
                     *  int scaleOffset = f.readInt();
                     *  int rotationOffset = f.readInt();
                     *  int translationOffset = f.readInt();
                     *
                     *  if ((flags & 0x20) == 0)
                     *  {
                     *      bone.scale.exists = true;
                     *      f.seek(scaleOffset);
                     *
                     *      if ((flags & 4) > 0)
                     *      {
                     *          bone.scale.vector.Add(new Vector4(
                     *              f.readFloat(),
                     *              f.readFloat(),
                     *              f.readFloat(),
                     *              0));
                     *      }
                     *      else
                     *      {
                     *          bone.scale.startFrame = f.readFloat();
                     *          bone.scale.endFrame = f.readFloat();
                     *
                     *          int scaleFlags = f.readInt();
                     *          int scaleDataOffset = f.readInt();
                     *          int scaleEntries = f.readInt();
                     *
                     *          f.seek(scaleDataOffset);
                     *          for (int j = 0; j < scaleEntries; j++)
                     *          {
                     *              bone.scale.vector.Add(new Vector4(
                     *                  f.readFloat(),
                     *                  f.readFloat(),
                     *                  f.readFloat(),
                     *                  0));
                     *          }
                     *      }
                     *  }
                     *
                     *  if ((flags & 0x10) == 0)
                     *  {
                     *      bone.rotationQuaternion.exists = true;
                     *      f.seek(rotationOffset);
                     *
                     *      if ((flags & 2) > 0)
                     *      {
                     *          bone.rotationQuaternion.vector.Add(new Vector4(
                     *              f.readFloat(),
                     *              f.readFloat(),
                     *              f.readFloat(),
                     *              f.readFloat()));
                     *      }
                     *      else
                     *      {
                     *          bone.rotationQuaternion.startFrame = f.readFloat();
                     *          bone.rotationQuaternion.endFrame = f.readFloat();
                     *
                     *          int rotationFlags = f.readInt();
                     *          int rotationDataOffset = f.readInt();
                     *          int rotationEntries = f.readInt();
                     *
                     *          f.seek(rotationDataOffset);
                     *          for (int j = 0; j < rotationEntries; j++)
                     *          {
                     *              bone.rotationQuaternion.vector.Add(new Vector4(
                     *                  f.readFloat(),
                     *                  f.readFloat(),
                     *                  f.readFloat(),
                     *                  f.readFloat()));
                     *          }
                     *      }
                     *  }
                     *
                     *  if ((flags & 8) == 0)
                     *  {
                     *      bone.translation.exists = true;
                     *      f.seek(translationOffset);
                     *
                     *      if ((flags & 1) > 0)
                     *      {
                     *          bone.translation.vector.Add(new Vector4(
                     *              f.readFloat(),
                     *              f.readFloat(),
                     *              f.readFloat(),
                     *              0));
                     *      }
                     *      else
                     *      {
                     *          bone.translation.startFrame = f.readFloat();
                     *          bone.translation.endFrame = f.readFloat();
                     *
                     *          int translationFlags = f.readInt();
                     *          int translationDataOffset = f.readInt();
                     *          int translationEntries = f.readInt();
                     *
                     *          f.seek(translationDataOffset);
                     *          for (int j = 0; j < translationEntries; j++)
                     *          {
                     *              bone.translation.vector.Add(new Vector4(
                     *                  f.readFloat(),
                     *                  f.readFloat(),
                     *                  f.readFloat(),
                     *                  0));
                     *          }
                     *      }
                     *  }
                     *
                     *  break;
                     * case OSegmentType.transformMatrix:
                     *  bone.isFullBakedFormat = true;
                     *
                     *  f.readInt();
                     *  f.readInt();
                     *  int matrixOffset = f.readInt();
                     *  int entries = f.readInt();
                     *
                     *  f.seek(matrixOffset);
                     *  for (int j = 0; j < entries; j++)
                     *  {
                     *      OMatrix transform = new OMatrix();
                     *      transform.M11 = f.readFloat();
                     *      transform.M21 = f.readFloat();
                     *      transform.M31 = f.readFloat();
                     *      transform.M41 = f.readFloat();
                     *
                     *      transform.M12 = f.readFloat();
                     *      transform.M22 = f.readFloat();
                     *      transform.M32 = f.readFloat();
                     *      transform.M42 = f.readFloat();
                     *
                     *      transform.M13 = f.readFloat();
                     *      transform.M23 = f.readFloat();
                     *      transform.M33 = f.readFloat();
                     *      transform.M43 = f.readFloat();
                     *
                     *      bone.transform.Add(transform);
                     *  }
                     *
                     *  break;*/
                    default: throw new Exception(string.Format("BCH: Unknow Segment Type {0} on Skeletal Animation bone {1}! STOP!", segmentType, bone.Text));
                    }

                    //skeletalAnimation.bone.Add(bone);
                }
            }
            //return a;
        }
Ejemplo n.º 2
0
        private static void getAnimationKeyFrame(FileData input, Animation.KeyGroup group, out float endFrame)
        {
            float startFrame = input.readFloat();

            endFrame = input.readFloat();

            uint frameFlags = (uint)input.readInt();
            //Debug.WriteLine(frameFlags.ToString("x"));
            //int preRepeat = (RenderBase.ORepeatMethod)(frameFlags & 0xf);
            //int postRepeat = (RenderBase.ORepeatMethod)((frameFlags >> 8) & 0xf);

            uint  segmentFlags  = (uint)input.readInt();
            int   interpolation = ((int)segmentFlags & 0xf);
            uint  quantization  = ((segmentFlags >> 8) & 0xff);
            uint  entries       = segmentFlags >> 16;
            float valueScale    = input.readFloat();
            float valueOffset   = input.readFloat();
            float frameScale    = input.readFloat();
            float frameOffset   = input.readFloat();

            uint offset = (uint)input.readInt();

            if (offset < input.size())
            {
                input.seek((int)offset);
            }
            for (int key = 0; key < entries; key++)
            {
                Animation.KeyFrame keyFrame = new Animation.KeyFrame();
                //Console.WriteLine(quantization);
                switch (quantization)
                {
                /*case RenderBase.OSegmentQuantization.hermite128:
                 *  keyFrame.frame = input.ReadSingle();
                 *  keyFrame.value = input.ReadSingle();
                 *  keyFrame.inSlope = input.ReadSingle();
                 *  keyFrame.outSlope = input.ReadSingle();
                 *  break;
                 * case RenderBase.OSegmentQuantization.hermite64:
                 *  uint h64Value = input.ReadUInt32();
                 *  keyFrame.frame = h64Value & 0xfff;
                 *  keyFrame.value = h64Value >> 12;
                 *  keyFrame.inSlope = input.ReadInt16() / 256f;
                 *  keyFrame.outSlope = input.ReadInt16() / 256f;
                 *  break;
                 * case RenderBase.OSegmentQuantization.hermite48:
                 *  keyFrame.frame = input.ReadByte();
                 *  keyFrame.value = input.ReadUInt16();
                 *  byte slope0 = input.ReadByte();
                 *  byte slope1 = input.ReadByte();
                 *  byte slope2 = input.ReadByte();
                 *  keyFrame.inSlope = IOUtils.signExtend(slope0 | ((slope1 & 0xf) << 8), 12) / 32f;
                 *  keyFrame.outSlope = IOUtils.signExtend((slope1 >> 4) | (slope2 << 4), 12) / 32f;
                 *  break;
                 * case RenderBase.OSegmentQuantization.unifiedHermite96:
                 *  keyFrame.frame = input.ReadSingle();
                 *  keyFrame.value = input.ReadSingle();
                 *  keyFrame.inSlope = input.ReadSingle();
                 *  keyFrame.outSlope = keyFrame.inSlope;
                 *  break;
                 * case RenderBase.OSegmentQuantization.unifiedHermite48:
                 *  keyFrame.frame = input.ReadUInt16() / 32f;
                 *  keyFrame.value = input.ReadUInt16();
                 *  keyFrame.inSlope = input.ReadInt16() / 256f;
                 *  keyFrame.outSlope = keyFrame.inSlope;
                 *  break;
                 * case RenderBase.OSegmentQuantization.unifiedHermite32:
                 *  keyFrame.frame = input.ReadByte();
                 *  ushort uH32Value = input.ReadUInt16();
                 *  keyFrame.value = uH32Value & 0xfff;
                 *  keyFrame.inSlope = IOUtils.signExtend((uH32Value >> 12) | (input.ReadByte() << 4), 12) / 32f;
                 *  keyFrame.outSlope = keyFrame.inSlope;
                 *  break;
                 * case RenderBase.OSegmentQuantization.stepLinear64:
                 *  keyFrame.frame = input.ReadSingle();
                 *  keyFrame.value = input.ReadSingle();
                 *  break;*/
                case 7:    // RenderBase.OSegmentQuantization.stepLinear32:
                    uint sL32Value = (uint)input.readInt();
                    keyFrame.Frame = sL32Value & 0xfff;
                    keyFrame.Value = sL32Value >> 12;
                    break;

                default: Console.WriteLine("Unknown type " + quantization); break;
                }

                keyFrame.Frame = (keyFrame.Frame * frameScale) + frameOffset;
                keyFrame.Value = (keyFrame.Value * valueScale) + valueOffset;

                group.Keys.Add(keyFrame);
            }
        }
Ejemplo n.º 3
0
        public static void read(string fname, Animation a, VBN v)
        {
            StreamReader reader = File.OpenText(fname);
            string       line;

            string   current = "";
            bool     readBones = false;
            int      frame = 0, prevframe = 0;
            KeyFrame k = new KeyFrame();

            VBN vbn = v;

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

            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 KeyFrame();
                        k.frame = frame;
                        //a.addKeyframe(k);
                    }
                    continue;
                }

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

                if (current.Equals("time"))
                {
                    //Animation.KeyFrame n = new Animation.KeyFrame();

                    /*n.id = v.boneIndex(vbn.bones[int.Parse(args[0])].Text);
                     * if (n.id == -1)
                     * {
                     *  continue;
                     * }
                     * else
                     *  n.hash = v.bones[n.id].boneId;*/

                    // only if it finds the node
                    //k.addNode(n);

                    // reading the skeleton if this isn't an animation
                    if (readBones && frame == 0)
                    {
                        Bone b = vbn.bones[int.Parse(args[0])];
                        b.position    = new float[3];
                        b.rotation    = new float[3];
                        b.scale       = new float[3];
                        b.position[0] = float.Parse(args[1]);
                        b.position[1] = float.Parse(args[2]);
                        b.position[2] = float.Parse(args[3]);
                        b.rotation[0] = float.Parse(args[4]);
                        b.rotation[1] = float.Parse(args[5]);
                        b.rotation[2] = float.Parse(args[6]);
                        b.scale[0]    = 1f;
                        b.scale[1]    = 1f;
                        b.scale[2]    = 1f;

                        b.pos = new Vector3(float.Parse(args[1]), float.Parse(args[2]), float.Parse(args[3]));
                        b.rot = VBN.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);
                    }
                }
            }

            a.FrameCount = frame;

            vbn.boneCountPerType[0] = (uint)vbn.bones.Count;
            vbn.update();
        }
Ejemplo n.º 4
0
        public static Animation read(string filename, VBN 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 *= (float)(Math.PI / 180f);
                        }
                        //k.intan = (args [2]);
                        //k.outtan = (args [3]);
                        if (args.Length > 7 && att.Weighted)
                        {
                            k.In  = float.Parse(args[7]) * (float)(Math.PI / 180f);
                            k.Out = float.Parse(args[8]) * (float)(Math.PI / 180f);
                        }
                    }

                    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 = Animation.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);
        }
Ejemplo n.º 5
0
            public void Read(ResFile TargetWiiUBFRES, AnimationGroupNode ThisAnimation, ResNSW.ResFile b)
            {
                Console.WriteLine("Reading Skeleton Animations ...");

                if (b != null)
                {
                    TreeNode SkeletonAnimation = new TreeNode()
                    {
                        Text = "Skeleton Animations"
                    };

                    ThisAnimation.Nodes.Add(SkeletonAnimation);

                    TreeNode dummy = new TreeNode()
                    {
                        Text = "Animation Set"
                    };

                    int i = 0;
                    foreach (ResNSW.SkeletalAnim ska in b.SkeletalAnims)
                    {
                        if (i == 0)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "0 - 100"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }
                        if (i == 100)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "100 - 200"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }
                        if (i == 200)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "200 - 300"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }
                        if (i == 300)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "300 - 400"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }
                        if (i == 400)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "400 - 500"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }
                        if (i == 500)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "500 - 600"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }
                        if (i == 600)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "600 - 700"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }
                        if (i == 700)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "700 - 800"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }
                        if (i == 800)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "800 - 900"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }
                        if (i == 900)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "900 - 1000"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }
                        if (i == 1000)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "1000+"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }

                        Animation a = new Animation(ska.Name);
                        SkeletonAnimations.Add(a);

                        a.FrameCount = ska.FrameCount;

                        if (i >= 0 && i < 100)
                        {
                            SkeletonAnimation.Nodes[0].Nodes.Add(a);
                        }
                        if (i >= 100 && i < 200)
                        {
                            SkeletonAnimation.Nodes[1].Nodes.Add(a);
                        }
                        if (i >= 200 && i < 300)
                        {
                            SkeletonAnimation.Nodes[2].Nodes.Add(a);
                        }
                        if (i >= 300 && i < 400)
                        {
                            SkeletonAnimation.Nodes[3].Nodes.Add(a);
                        }
                        if (i >= 400 && i < 500)
                        {
                            SkeletonAnimation.Nodes[4].Nodes.Add(a);
                        }
                        if (i >= 500 && i < 600)
                        {
                            SkeletonAnimation.Nodes[5].Nodes.Add(a);
                        }
                        if (i >= 600 && i < 700)
                        {
                            SkeletonAnimation.Nodes[6].Nodes.Add(a);
                        }
                        if (i >= 700 && i < 800)
                        {
                            SkeletonAnimation.Nodes[7].Nodes.Add(a);
                        }
                        if (i >= 800 && i < 900)
                        {
                            SkeletonAnimation.Nodes[8].Nodes.Add(a);
                        }
                        if (i >= 900 && i < 1000)
                        {
                            SkeletonAnimation.Nodes[9].Nodes.Add(a);
                        }

                        i++;
                        try
                        {
                            foreach (ResNSW.BoneAnim bn in ska.BoneAnims)
                            {
                                FSKANode bonean = new FSKANode(bn);

                                Animation.KeyNode bone = new Animation.KeyNode("");
                                a.Bones.Add(bone);
                                if (ska.FlagsRotate == ResNSW.SkeletalAnimFlagsRotate.EulerXYZ)
                                {
                                    bone.RotType = Animation.RotationType.EULER;
                                }
                                else
                                {
                                    bone.RotType = Animation.RotationType.QUATERNION;
                                }

                                bone.Text = bonean.Text;

                                for (int Frame = 0; Frame < ska.FrameCount; Frame++)
                                {
                                    //Set base/start values for bones.
                                    //Note. BOTW doesn't use base values as it uses havok engine. Need to add option to disable these
                                    if (Frame == 0)
                                    {
                                        if (bn.FlagsBase.HasFlag(ResNSW.BoneAnimFlagsBase.Scale))
                                        {
                                            bone.XSCA.Keys.Add(new Animation.KeyFrame()
                                            {
                                                Frame = 0, Value = bonean.sca.X
                                            });
                                            bone.YSCA.Keys.Add(new Animation.KeyFrame()
                                            {
                                                Frame = 0, Value = bonean.sca.Y
                                            });
                                            bone.ZSCA.Keys.Add(new Animation.KeyFrame()
                                            {
                                                Frame = 0, Value = bonean.sca.Z
                                            });
                                        }
                                        if (bn.FlagsBase.HasFlag(ResNSW.BoneAnimFlagsBase.Rotate))
                                        {
                                            bone.XROT.Keys.Add(new Animation.KeyFrame()
                                            {
                                                Frame = 0, Value = bonean.rot.X
                                            });
                                            bone.YROT.Keys.Add(new Animation.KeyFrame()
                                            {
                                                Frame = 0, Value = bonean.rot.Y
                                            });
                                            bone.ZROT.Keys.Add(new Animation.KeyFrame()
                                            {
                                                Frame = 0, Value = bonean.rot.Z
                                            });
                                            bone.WROT.Keys.Add(new Animation.KeyFrame()
                                            {
                                                Frame = 0, Value = bonean.rot.W
                                            });
                                        }
                                        if (bn.FlagsBase.HasFlag(ResNSW.BoneAnimFlagsBase.Translate))
                                        {
                                            bone.XPOS.Keys.Add(new Animation.KeyFrame()
                                            {
                                                Frame = 0, Value = bonean.pos.X
                                            });
                                            bone.YPOS.Keys.Add(new Animation.KeyFrame()
                                            {
                                                Frame = 0, Value = bonean.pos.Y
                                            });
                                            bone.ZPOS.Keys.Add(new Animation.KeyFrame()
                                            {
                                                Frame = 0, Value = bonean.pos.Z
                                            });
                                        }
                                    }
                                    foreach (FSKATrack track in bonean.tracks)
                                    {
                                        Animation.KeyFrame frame = new Animation.KeyFrame();
                                        frame.InterType = Animation.InterpolationType.HERMITE;
                                        frame.Frame     = Frame;

                                        FSKAKey left  = track.GetLeft(Frame);
                                        FSKAKey right = track.GetRight(Frame);
                                        float   value;



                                        value = Animation.Hermite(Frame, left.frame, right.frame, 0, 0, left.unk1, right.unk1);

                                        // interpolate the value and apply
                                        switch (track.flag)
                                        {
                                        case (int)TrackType.XPOS: frame.Value = value; bone.XPOS.Keys.Add(frame); break;

                                        case (int)TrackType.YPOS: frame.Value = value; bone.YPOS.Keys.Add(frame); break;

                                        case (int)TrackType.ZPOS: frame.Value = value; bone.ZPOS.Keys.Add(frame); break;

                                        case (int)TrackType.XROT: frame.Value = value; bone.XROT.Keys.Add(frame); break;

                                        case (int)TrackType.YROT: frame.Value = value; bone.YROT.Keys.Add(frame); break;

                                        case (int)TrackType.ZROT: frame.Value = value; bone.ZROT.Keys.Add(frame); break;

                                        case (int)TrackType.XSCA: frame.Value = value; bone.XSCA.Keys.Add(frame); break;

                                        case (int)TrackType.YSCA: frame.Value = value; bone.YSCA.Keys.Add(frame); break;

                                        case (int)TrackType.ZSCA: frame.Value = value; bone.ZSCA.Keys.Add(frame); break;
                                        }
                                    }
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                }
                else
                {
                    TreeNode SkeletonAnimation = new TreeNode()
                    {
                        Text = "Skeleton Animations"
                    };

                    ThisAnimation.Nodes.Add(SkeletonAnimation);

                    TreeNode dummy = new TreeNode()
                    {
                        Text = "Animation Set"
                    };

                    int i = 0;
                    foreach (SkeletalAnim ska in TargetWiiUBFRES.SkeletalAnims.Values)
                    {
                        if (i == 0)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "0 - 100"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }
                        if (i == 100)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "100 - 200"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }
                        if (i == 200)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "200 - 300"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }
                        if (i == 300)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "300 - 400"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }
                        if (i == 400)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "400 - 500"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }
                        if (i == 500)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "500 - 600"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }
                        if (i == 600)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "600 - 700"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }
                        if (i == 700)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "700 - 800"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }
                        if (i == 800)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "800 - 900"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }
                        if (i == 900)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "900 - 1000"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }
                        if (i == 1000)
                        {
                            dummy = new TreeNode()
                            {
                                Text = "Animation Set " + "1000+"
                            };
                            SkeletonAnimation.Nodes.Add(dummy);
                        }

                        Animation a = new Animation(ska.Name);

                        if (i >= 0 && i < 100)
                        {
                            SkeletonAnimation.Nodes[0].Nodes.Add(a);
                        }
                        if (i >= 100 && i < 200)
                        {
                            SkeletonAnimation.Nodes[1].Nodes.Add(a);
                        }
                        if (i >= 200 && i < 300)
                        {
                            SkeletonAnimation.Nodes[2].Nodes.Add(a);
                        }
                        if (i >= 300 && i < 400)
                        {
                            SkeletonAnimation.Nodes[3].Nodes.Add(a);
                        }
                        if (i >= 400 && i < 500)
                        {
                            SkeletonAnimation.Nodes[4].Nodes.Add(a);
                        }
                        if (i >= 500 && i < 600)
                        {
                            SkeletonAnimation.Nodes[5].Nodes.Add(a);
                        }
                        if (i >= 600 && i < 700)
                        {
                            SkeletonAnimation.Nodes[6].Nodes.Add(a);
                        }
                        if (i >= 700 && i < 800)
                        {
                            SkeletonAnimation.Nodes[7].Nodes.Add(a);
                        }
                        if (i >= 800 && i < 900)
                        {
                            SkeletonAnimation.Nodes[8].Nodes.Add(a);
                        }
                        if (i >= 900 && i < 1000)
                        {
                            SkeletonAnimation.Nodes[9].Nodes.Add(a);
                        }


                        a.FrameCount = ska.FrameCount;
                        i++;
                        try
                        {
                            foreach (BoneAnim bn in ska.BoneAnims)
                            {
                                FSKANodeWiiU bonean = new FSKANodeWiiU(bn);

                                Animation.KeyNode bone = new Animation.KeyNode("");
                                a.Bones.Add(bone);
                                if (ska.FlagsRotate == SkeletalAnimFlagsRotate.EulerXYZ)
                                {
                                    bone.RotType = Animation.RotationType.EULER;
                                }
                                else
                                {
                                    bone.RotType = Animation.RotationType.QUATERNION;
                                }

                                bone.Text = bonean.Text;


                                for (int Frame = 0; Frame < ska.FrameCount; Frame++)
                                {
                                    //Set base/start values for bones.
                                    //Note. BOTW doesn't use base values as it uses havok engine. Need to add option to disable these
                                    if (Frame == 0)
                                    {
                                        if (bn.FlagsBase.HasFlag(BoneAnimFlagsBase.Scale))
                                        {
                                            bone.XSCA.Keys.Add(new Animation.KeyFrame()
                                            {
                                                Frame = 0, Value = bonean.sca.X
                                            });
                                            bone.YSCA.Keys.Add(new Animation.KeyFrame()
                                            {
                                                Frame = 0, Value = bonean.sca.Y
                                            });
                                            bone.ZSCA.Keys.Add(new Animation.KeyFrame()
                                            {
                                                Frame = 0, Value = bonean.sca.Z
                                            });
                                        }
                                        if (bn.FlagsBase.HasFlag(BoneAnimFlagsBase.Rotate))
                                        {
                                            bone.XROT.Keys.Add(new Animation.KeyFrame()
                                            {
                                                Frame = 0, Value = bonean.rot.X
                                            });
                                            bone.YROT.Keys.Add(new Animation.KeyFrame()
                                            {
                                                Frame = 0, Value = bonean.rot.Y
                                            });
                                            bone.ZROT.Keys.Add(new Animation.KeyFrame()
                                            {
                                                Frame = 0, Value = bonean.rot.Z
                                            });
                                            bone.WROT.Keys.Add(new Animation.KeyFrame()
                                            {
                                                Frame = 0, Value = bonean.rot.W
                                            });
                                        }
                                        if (bn.FlagsBase.HasFlag(BoneAnimFlagsBase.Translate))
                                        {
                                            bone.XPOS.Keys.Add(new Animation.KeyFrame()
                                            {
                                                Frame = 0, Value = bonean.pos.X
                                            });
                                            bone.YPOS.Keys.Add(new Animation.KeyFrame()
                                            {
                                                Frame = 0, Value = bonean.pos.Y
                                            });
                                            bone.ZPOS.Keys.Add(new Animation.KeyFrame()
                                            {
                                                Frame = 0, Value = bonean.pos.Z
                                            });
                                        }
                                    }
                                    foreach (FSKATrack track in bonean.tracks)
                                    {
                                        Animation.KeyFrame frame = new Animation.KeyFrame();
                                        frame.InterType = Animation.InterpolationType.HERMITE;
                                        frame.Frame     = Frame;

                                        FSKAKey left  = track.GetLeft(Frame);
                                        FSKAKey right = track.GetRight(Frame);
                                        float   value;



                                        value = Animation.Hermite(Frame, left.frame, right.frame, 0, 0, left.unk1, right.unk1);

                                        // interpolate the value and apply
                                        switch (track.flag)
                                        {
                                        case (int)TrackType.XPOS: frame.Value = value; bone.XPOS.Keys.Add(frame); break;

                                        case (int)TrackType.YPOS: frame.Value = value; bone.YPOS.Keys.Add(frame); break;

                                        case (int)TrackType.ZPOS: frame.Value = value; bone.ZPOS.Keys.Add(frame); break;

                                        case (int)TrackType.XROT: frame.Value = value; bone.XROT.Keys.Add(frame); break;

                                        case (int)TrackType.YROT: frame.Value = value; bone.YROT.Keys.Add(frame); break;

                                        case (int)TrackType.ZROT: frame.Value = value; bone.ZROT.Keys.Add(frame); break;

                                        case (int)TrackType.XSCA: frame.Value = value; bone.XSCA.Keys.Add(frame); break;

                                        case (int)TrackType.YSCA: frame.Value = value; bone.YSCA.Keys.Add(frame); break;

                                        case (int)TrackType.ZSCA: frame.Value = value; bone.ZSCA.Keys.Add(frame); break;
                                        }
                                    }
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }