예제 #1
0
        public MDLFile(Stream FileInput, Boolean parseAnims = false, Boolean parseHitboxes = false)
        {
            using (uReader FileStream = new uReader(FileInput))
            {
                FileStream.ReadTypeFixed(ref MDL_Header, 392);

                if (MDL_Header.id != 0x54534449)
                {
                    throw new FileLoadException("File signature does not match 'IDST'");
                }

                //Bones
                #region Bones
                //Bones
                MDL_StudioBones = new mstudiobone_t[MDL_Header.bone_count];
                MDL_BoneNames   = new String[MDL_Header.bone_count];
                for (Int32 BoneID = 0; BoneID < MDL_Header.bone_count; BoneID++)
                {
                    Int32 boneOffset = MDL_Header.bone_offset + (216 * BoneID);
                    FileStream.ReadTypeFixed(ref MDL_StudioBones[BoneID], 216, boneOffset);
                    MDL_BoneNames[BoneID] = FileStream.ReadNullTerminatedString(boneOffset + MDL_StudioBones[BoneID].sznameindex);
                }
                //Bones
                #endregion

                #region Hitboxes
                if (parseHitboxes)
                {
                    MDL_Hitboxsets = new mstudiohitboxset_t[MDL_Header.hitbox_count];
                    Hitboxes       = new Hitbox[MDL_Header.hitbox_count][];
                    for (Int32 HitboxsetID = 0; HitboxsetID < MDL_Header.hitbox_count; HitboxsetID++)
                    {
                        Int32 HitboxsetOffset = MDL_Header.hitbox_offset + (12 * HitboxsetID);
                        FileStream.ReadTypeFixed(ref MDL_Hitboxsets[HitboxsetID], 12, HitboxsetOffset);
                        Hitboxes[HitboxsetID] = new Hitbox[MDL_Hitboxsets[HitboxsetID].numhitboxes];

                        for (Int32 HitboxID = 0; HitboxID < MDL_Hitboxsets[HitboxsetID].numhitboxes; HitboxID++)
                        {
                            Int32 HitboxOffset = HitboxsetOffset + (68 * HitboxID) + MDL_Hitboxsets[HitboxsetID].hitboxindex;
                            Hitboxes[HitboxsetID][HitboxID].BBox = new mstudiobbox_t();

                            FileStream.ReadTypeFixed(ref Hitboxes[HitboxsetID][HitboxID].BBox, 68, HitboxOffset);
                        }
                    }
                }
                #endregion

                #region Animations
                if (parseAnims)
                {
                    try
                    {
                        //Animations
                        MDL_AniDescriptions = new mstudioanimdesc_t[MDL_Header.localanim_count];
                        Animations          = new AniInfo[MDL_Header.localanim_count];

                        for (Int32 AnimID = 0; AnimID < MDL_Header.localanim_count; AnimID++)
                        {
                            Int32 AnimOffset = MDL_Header.localanim_offset + (100 * AnimID);
                            FileStream.ReadTypeFixed(ref MDL_AniDescriptions[AnimID], 100, AnimOffset);
                            mstudioanimdesc_t StudioAnim = MDL_AniDescriptions[AnimID];

                            String StudioAnimName = FileStream.ReadNullTerminatedString(AnimOffset + StudioAnim.sznameindex);
                            Animations[AnimID] = new AniInfo {
                                name = StudioAnimName, studioAnim = StudioAnim
                            };
                            Animations[AnimID].AnimationBones = new List <AnimationBone>();

                            //mstudioanim_t
                            FileStream.BaseStream.Position = AnimOffset;

                            Int64 StartOffset = FileStream.BaseStream.Position;

                            Int32 CurrentOffset = MDL_AniDescriptions[AnimID].animindex;
                            Int16 NextOffset;
                            do
                            {
                                FileStream.BaseStream.Position = StartOffset + CurrentOffset;
                                Byte BoneIndex = FileStream.ReadByte();
                                Byte BoneFlag  = FileStream.ReadByte();
                                NextOffset     = FileStream.ReadInt16();
                                CurrentOffset += NextOffset;

                                AnimationBone AnimatedBone = new AnimationBone(BoneIndex, BoneFlag, MDL_AniDescriptions[AnimID].numframes);
                                AnimatedBone.ReadData(FileStream);
                                Animations[AnimID].AnimationBones.Add(AnimatedBone);
                            } while (NextOffset != 0);
                            //mstudioanim_t

                            List <AnimationBone> AnimationBones = Animations[AnimID].AnimationBones;
                            Int32 NumBones  = MDL_Header.bone_count;
                            Int32 NumFrames = StudioAnim.numframes;

                            //Used to avoid "Assertion failed" key count in Unity (if frames less than 2)
                            Boolean FramesLess = NumFrames < 2;
                            if (FramesLess)
                            {
                                NumFrames += 1;
                            }

                            Animations[AnimID].PosX = new Keyframe[NumFrames][];
                            Animations[AnimID].PosY = new Keyframe[NumFrames][];
                            Animations[AnimID].PosZ = new Keyframe[NumFrames][];

                            Animations[AnimID].RotX = new Keyframe[NumFrames][];
                            Animations[AnimID].RotY = new Keyframe[NumFrames][];
                            Animations[AnimID].RotZ = new Keyframe[NumFrames][];
                            Animations[AnimID].RotW = new Keyframe[NumFrames][];
                            for (Int32 FrameID = 0; FrameID < NumFrames; FrameID++)
                            {
                                Animations[AnimID].PosX[FrameID] = new Keyframe[NumBones];
                                Animations[AnimID].PosY[FrameID] = new Keyframe[NumBones];
                                Animations[AnimID].PosZ[FrameID] = new Keyframe[NumBones];

                                Animations[AnimID].RotX[FrameID] = new Keyframe[NumBones];
                                Animations[AnimID].RotY[FrameID] = new Keyframe[NumBones];
                                Animations[AnimID].RotZ[FrameID] = new Keyframe[NumBones];
                                Animations[AnimID].RotW[FrameID] = new Keyframe[NumBones];
                            }

                            for (Int32 boneID = 0; boneID < NumBones; boneID++)
                            {
                                AnimationBone AnimBone = AnimationBones.FirstOrDefault(x => x.Bone == boneID);

                                //frameIndex < 30 && studioAnimName == "@ak47_reload"
                                for (Int32 frameID = 0; frameID < NumFrames; frameID++)
                                {
                                    //get current animation time (length) by divide frame index on "fps"
                                    Single time = frameID / StudioAnim.fps;

                                    mstudiobone_t StudioBone = MDL_StudioBones[boneID];
                                    //Transform bone = Bones[boneIndex];

                                    Vector3 Position = StudioBone.pos;
                                    Vector3 Rotation = StudioBone.rot;

                                    //BINGO! All animations are corrected :p
                                    if (AnimBone != null)
                                    {
                                        if ((AnimBone.Flags & STUDIO_ANIM_RAWROT) > 0)
                                        {
                                            Rotation = MathLibrary.ToEulerAngles(AnimBone.pQuat48);
                                        }

                                        if ((AnimBone.Flags & STUDIO_ANIM_RAWROT2) > 0)
                                        {
                                            Rotation = MathLibrary.ToEulerAngles(AnimBone.pQuat64);
                                        }

                                        if ((AnimBone.Flags & STUDIO_ANIM_RAWPOS) > 0)
                                        {
                                            Position = AnimBone.pVec48;
                                        }

                                        if ((AnimBone.Flags & STUDIO_ANIM_ANIMROT) > 0)
                                        {
                                            Rotation += AnimBone.FrameAngles[(FramesLess && frameID != 0) ? frameID - 1 : frameID].Multiply(StudioBone.rotscale);
                                        }

                                        if ((AnimBone.Flags & STUDIO_ANIM_ANIMPOS) > 0)
                                        {
                                            Position += AnimBone.FramePositions[(FramesLess && frameID != 0) ? frameID - 1 : frameID].Multiply(StudioBone.posscale);
                                        }

                                        if ((AnimBone.Flags & STUDIO_ANIM_DELTA) > 0)
                                        {
                                            Position = Vector3.zero;
                                            Rotation = Vector3.zero;
                                        }
                                    }

                                    //Invert right-handed position to left-handed
                                    if (StudioBone.parent == -1)
                                    {
                                        Position = MathLibrary.SwapY(Position);
                                    }
                                    else
                                    {
                                        Position.x = -Position.x;
                                    }

                                    //Corrects global scale and convert radians to degrees
                                    Position *= uLoader.UnitScale;
                                    Rotation *= Mathf.Rad2Deg;
                                    Quaternion quat;

                                    //Fix up bone rotations from right-handed to left-handed
                                    if (StudioBone.parent == -1)
                                    {
                                        quat = Quaternion.Euler(-90, 180, -90) * MathLibrary.AngleQuaternion(Rotation);
                                    }
                                    else
                                    {
                                        quat = MathLibrary.AngleQuaternion(Rotation);
                                    }

                                    Animations[AnimID].PosX[frameID][boneID] = new Keyframe(time, Position.x);
                                    Animations[AnimID].PosY[frameID][boneID] = new Keyframe(time, Position.y);
                                    Animations[AnimID].PosZ[frameID][boneID] = new Keyframe(time, Position.z);

                                    Animations[AnimID].RotX[frameID][boneID] = new Keyframe(time, quat.x);
                                    Animations[AnimID].RotY[frameID][boneID] = new Keyframe(time, quat.y);
                                    Animations[AnimID].RotZ[frameID][boneID] = new Keyframe(time, quat.z);
                                    Animations[AnimID].RotW[frameID][boneID] = new Keyframe(time, quat.w);
                                }
                            }
                        }
                        //Animations

                        //Sequences
                        MDL_SeqDescriptions = new mstudioseqdesc_t[MDL_Header.localseq_count];
                        Sequences           = new SeqInfo[MDL_Header.localseq_count];

                        for (Int32 seqID = 0; seqID < MDL_Header.localseq_count; seqID++)
                        {
                            Int32 sequenceOffset = MDL_Header.localseq_offset + (212 * seqID);
                            FileStream.ReadTypeFixed(ref MDL_SeqDescriptions[seqID], 212, sequenceOffset);
                            mstudioseqdesc_t Sequence = MDL_SeqDescriptions[seqID];
                            Sequences[seqID] = new SeqInfo {
                                name = FileStream.ReadNullTerminatedString(sequenceOffset + Sequence.szlabelindex), seq = Sequence
                            };

                            FileStream.BaseStream.Position = sequenceOffset + Sequence.animindexindex;

                            var animID = FileStream.ReadShortArray(Sequence.groupsize[0] * Sequence.groupsize[1]);
                            //Debug.LogWarning(animIndices[0]);
                            // Just use the first animation for now
                            Sequences[seqID].ani = Animations[animID[0]];
                        }
                        //Sequences
                    }
                    catch (Exception ex)
                    {
                        Debug.LogError(String.Format("\"{0}\" Parse animation failed: {1}", MDL_Header.Name, ex));
                    }
                }
                #endregion

                #region Materials
                //Materials
                MDL_TexturesInfo = new mstudiotexture_t[MDL_Header.texture_count];
                MDL_Textures     = new String[MDL_Header.texture_count];
                for (Int32 TexID = 0; TexID < MDL_Header.texture_count; TexID++)
                {
                    Int32 TextureOffset = MDL_Header.texture_offset + (64 * TexID);
                    FileStream.ReadTypeFixed(ref MDL_TexturesInfo[TexID], 64, TextureOffset);
                    MDL_Textures[TexID] = FileStream.ReadNullTerminatedString(TextureOffset + MDL_TexturesInfo[TexID].sznameindex);
                }

                Int32[] TDirOffsets = new Int32[MDL_Header.texturedir_count];
                MDL_TDirectories = new String[MDL_Header.texturedir_count];
                for (Int32 DirID = 0; DirID < MDL_Header.texturedir_count; DirID++)
                {
                    FileStream.ReadTypeFixed(ref TDirOffsets[DirID], 4, MDL_Header.texturedir_offset + (4 * DirID));
                    MDL_TDirectories[DirID] = FileStream.ReadNullTerminatedString(TDirOffsets[DirID]);
                }
                //Materials
                #endregion

                #region BodyParts
                //Bodyparts
                MDL_Bodyparts = new StudioBodyPart[MDL_Header.bodypart_count];
                for (Int32 BodypartID = 0; BodypartID < MDL_Header.bodypart_count; BodypartID++)
                {
                    mstudiobodyparts_t BodyPart = new mstudiobodyparts_t();
                    Int32 BodyPartOffset        = MDL_Header.bodypart_offset + (16 * BodypartID);
                    FileStream.ReadTypeFixed(ref BodyPart, 16, BodyPartOffset);

                    if (BodyPart.sznameindex != 0)
                    {
                        MDL_Bodyparts[BodypartID].Name = FileStream.ReadNullTerminatedString(BodyPartOffset + BodyPart.sznameindex);
                    }
                    else
                    {
                        MDL_Bodyparts[BodypartID].Name = String.Empty;
                    }

                    MDL_Bodyparts[BodypartID].Models = new StudioModel[BodyPart.nummodels];

                    for (Int32 ModelID = 0; ModelID < BodyPart.nummodels; ModelID++)
                    {
                        mstudiomodel_t Model       = new mstudiomodel_t();
                        Int64          ModelOffset = BodyPartOffset + (148 * ModelID) + BodyPart.modelindex;
                        FileStream.ReadTypeFixed(ref Model, 148, ModelOffset);

                        MDL_Bodyparts[BodypartID].Models[ModelID].isBlank = (Model.numvertices <= 0 || Model.nummeshes <= 0);
                        MDL_Bodyparts[BodypartID].Models[ModelID].Model   = Model;

                        MDL_Bodyparts[BodypartID].Models[ModelID].Meshes = new mstudiomesh_t[Model.nummeshes];
                        for (Int32 MeshID = 0; MeshID < Model.nummeshes; MeshID++)
                        {
                            mstudiomesh_t Mesh       = new mstudiomesh_t();
                            Int64         MeshOffset = ModelOffset + (116 * MeshID) + Model.meshindex;
                            FileStream.ReadTypeFixed(ref Mesh, 116, MeshOffset);

                            MDL_Bodyparts[BodypartID].Models[ModelID].Meshes[MeshID] = Mesh;
                        }

                        MDL_Bodyparts[BodypartID].Models[ModelID].IndicesPerLod = new Dictionary <Int32, List <Int32> > [8];

                        for (Int32 i = 0; i < 8; i++)
                        {
                            MDL_Bodyparts[BodypartID].Models[ModelID].IndicesPerLod[i] = new Dictionary <Int32, List <Int32> >();
                        }

                        MDL_Bodyparts[BodypartID].Models[ModelID].VerticesPerLod = new mstudiovertex_t[8][];
                    }
                }
                //BodyParts
                #endregion
            }
        }
예제 #2
0
        public VTXFile(Stream FileInput, MDLFile StudioMDL, VVDFile StudioVVD)
        {
            using (uReader FileStream = new uReader(FileInput))
            {
                studiohdr_t MDL_Header = StudioMDL.MDL_Header;
                FileStream.ReadTypeFixed(ref VTX_Header, 36);

                if (VTX_Header.checkSum != MDL_Header.checksum)
                {
                    throw new FileLoadException(String.Format("{0}: Does not match the checksum in the .mdl", MDL_Header.Name));
                }

                #region BodyParts
                Int32[] VertexLODOffsets = new Int32[8];
                for (Int32 BodypartID = 0; BodypartID < MDL_Header.bodypart_count; BodypartID++)
                {
                    BodyPartHeader_t BodyPart       = new BodyPartHeader_t();
                    Int64            BodyPartOffset = VTX_Header.bodyPartOffset + (8 * BodypartID);
                    FileStream.ReadTypeFixed(ref BodyPart, 8, BodyPartOffset);

                    StudioBodyPart StudioBodyPart = StudioMDL.MDL_Bodyparts[BodypartID];

                    #region Models
                    for (Int32 ModelID = 0; ModelID < BodyPart.numModels; ModelID++)
                    {
                        StudioModel StudioModel = StudioBodyPart.Models[ModelID];

                        if (StudioModel.isBlank)
                        {
                            Debug.Log(String.Format("Model ID - {0} in bodypart \"{1}\" is blank, skip", ModelID, StudioBodyPart.Name));
                            continue;
                        }

                        ModelHeader_t Model       = new ModelHeader_t();
                        Int64         ModelOffset = BodyPartOffset + (8 * ModelID) + BodyPart.modelOffset;
                        FileStream.ReadTypeFixed(ref Model, 8, ModelOffset);

                        StudioBodyPart.Models[ModelID].NumLODs = Model.numLODs;
                        StudioBodyPart.Models[ModelID].LODData = new ModelLODHeader_t[Model.numLODs];

                        #region LOD's
                        //TODO: Strip unused vertexes on lower lod's ("first" lod is fine)
                        for (Int32 LODID = 0; LODID < Model.numLODs; LODID++)
                        {
                            ModelLODHeader_t LOD       = new ModelLODHeader_t();
                            Int64            LODOffset = ModelOffset + (12 * LODID) + Model.lodOffset;
                            FileStream.ReadTypeFixed(ref LOD, 12, LODOffset);

                            StudioBodyPart.Models[ModelID].LODData[LODID] = LOD;

                            #region Mesh LOD
                            //Temp remember verts count per lod model
                            Int32 VertexOffset = 0;
                            //List<mstudiovertex_t> VertexesPerLod = new List<mstudiovertex_t>();
                            for (Int32 MeshID = 0; MeshID < StudioModel.Model.nummeshes; MeshID++)
                            {
                                mstudiomesh_t StudioMesh = StudioBodyPart.Models[ModelID].Meshes[MeshID];

                                //TODO: StudioModel.Meshes[MeshID].VertexData.numlodvertices[LODID]; - we no longer need this??
                                VertexOffset += StudioMesh.numvertices;
                                List <Int32> IndicesPerMesh = new List <Int32>();

                                MeshHeader_t Mesh       = new MeshHeader_t();
                                Int64        MeshOffset = LODOffset + (9 * MeshID) + LOD.meshOffset;
                                FileStream.ReadTypeFixed(ref Mesh, 9, MeshOffset);

                                #region StripGroups
                                for (Int32 StripGroupID = 0; StripGroupID < Mesh.numStripGroups; StripGroupID++)
                                {
                                    StripGroupHeader_t StripGroup = new StripGroupHeader_t();
                                    Int64 StripGroupOffset        = MeshOffset + (25 * StripGroupID) + Mesh.stripGroupHeaderOffset;
                                    FileStream.ReadTypeFixed(ref StripGroup, 25, StripGroupOffset);

                                    Vertex_t[] Vertexes = new Vertex_t[StripGroup.numVerts];
                                    FileStream.BaseStream.Position = StripGroupOffset + StripGroup.vertOffset;
                                    FileStream.ReadArrayFixed(ref Vertexes, 9);

                                    FileStream.BaseStream.Position = StripGroupOffset + StripGroup.indexOffset;
                                    Int16[] Indices = FileStream.ReadShortArray(StripGroup.numIndices);

                                    #region Strips
                                    for (Int32 StripID = 0; StripID < StripGroup.numStrips; StripID++)
                                    {
                                        StripHeader_t VTXStrip       = new StripHeader_t();
                                        Int64         VTXStripOffset = StripGroupOffset + (27 * StripID) + StripGroup.stripOffset;
                                        FileStream.ReadTypeFixed(ref VTXStrip, 27, VTXStripOffset);

                                        //TODO:
                                        //Strip / "Split" vertexes
                                        //Pseudo code:

                                        /*for (Int32 VertID = 0; VertID < maxVertsPerLod; VertID++)
                                         * {
                                         *      Int32 Index = MeshID * VTXStrip.numVerts + VertID;
                                         *
                                         *      if (Index < numStripVerts)
                                         *      {
                                         *              splitVerts.Add(verts[Index]);
                                         *              splitIndices.Add(j);
                                         *      }
                                         * }*/

                                        //Hmmmmm... Well, it's looks what we want.... but still doesn't perfect (for lod's mesh)

                                        /*Int32 NumVerts = VTXStrip.indexOffset + VTXStrip.numVerts;
                                         * for (Int32 VertID = VTXStrip.indexOffset; VertID < NumVerts; VertID++)
                                         * {
                                         *      Int32 Index0 = VertID + StudioMesh.vertexoffset + VertexLODOffsets[LODID];
                                         *      VertexesPerLod.Add(StudioVVD.tempVerts[Index0]);
                                         * }*/

                                        if ((VTXStrip.flags & VTXStripGroupTriStripFlag) > 0)
                                        {
                                            for (Int32 TempIdx = VTXStrip.indexOffset; TempIdx < VTXStrip.indexOffset + VTXStrip.numIndices - 2; TempIdx++)
                                            {
                                                Int32[] add = TempIdx % 2 == 1 ?
                                                              new[] { TempIdx + 1, TempIdx, TempIdx + 2 } :
                                                new[] { TempIdx, TempIdx + 1, TempIdx + 2 };

                                                foreach (Int32 Index in add)
                                                {
                                                    IndicesPerMesh.Add(Vertexes[Indices[Index]].origMeshVertId + StudioMesh.vertexoffset);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            for (Int32 Index = VTXStrip.indexOffset; Index < VTXStrip.indexOffset + VTXStrip.numIndices; Index++)
                                            {
                                                IndicesPerMesh.Add(Vertexes[Indices[Index]].origMeshVertId + StudioMesh.vertexoffset);
                                            }
                                        }
                                    }
                                    #endregion
                                }
                                #endregion

                                StudioMDL.SetIndices(BodypartID, ModelID, LODID, MeshID, IndicesPerMesh);
                            }
                            #endregion

                            //StudioMDL.MDL_Bodyparts[BodypartID].Models[ModelID].VerticesPerLod[LODID] = VertexesPerLod.ToArray();
                            ///TODO: Strip unused vertexes in <seealso cref="VVDFile.VVD_Vertexes"/> per lod
                            StudioMDL.SetVertices(BodypartID, ModelID, LODID, VertexOffset, VertexLODOffsets[LODID], StudioVVD.VVD_Vertexes[0]);

                            VertexLODOffsets[LODID] += VertexOffset;
                        }
                        #endregion
                    }
                    #endregion
                }
                #endregion
            }
        }