Ejemplo n.º 1
0
        public static libTechModel FromSourceMdl(SourceMdl Mdl, string ShaderOverride = null)
        {
            libTechModel Model = new libTechModel();

            string[] MaterialNames = Mdl.GetMaterialNames();
            string[] BodyNames     = Mdl.GetBodyNames();

            StudioModelFile.StudioBone[] Bones = Mdl.Mdl.GetBones();
            string[] BoneNames = Mdl.Mdl.GetBoneNames();

            for (int i = 0; i < Bones.Length; i++)
            {
                Model.Bones.Add(BoneNames[i], new libTechBone(BoneNames[i], Bones[i]));
            }


            // BODIES
            for (int BodyPartIdx = 0; BodyPartIdx < Mdl.Mdl.BodyPartCount; BodyPartIdx++)
            {
                StudioModelFile.StudioModel[] StudioModels = Mdl.Mdl.GetModels(BodyPartIdx).ToArray();

                // MODELS
                for (int ModelIdx = 0; ModelIdx < StudioModels.Length; ModelIdx++)
                {
                    ref StudioModelFile.StudioModel StudioModel  = ref StudioModels[ModelIdx];
                    StudioModelFile.StudioMesh[]    StudioMeshes = Mdl.Mdl.GetMeshes(ref StudioModel).ToArray();

                    // MESHES
                    for (int MeshIdx = 0; MeshIdx < StudioMeshes.Length; MeshIdx++)
                    {
                        ref StudioModelFile.StudioMesh StudioMesh = ref StudioMeshes[MeshIdx];

                        StudioVertex[] StudioVerts = new StudioVertex[Mdl.Tris.GetVertexCount(BodyPartIdx, ModelIdx, 0, MeshIdx)];
                        Mdl.Tris.GetVertices(BodyPartIdx, ModelIdx, 0, MeshIdx, StudioVerts);

                        int[] Indices = new int[Mdl.Tris.GetIndexCount(BodyPartIdx, ModelIdx, 0, MeshIdx)];
                        Mdl.Tris.GetIndices(BodyPartIdx, ModelIdx, 0, MeshIdx, Indices);

                        List <Vertex3> Vts = new List <Vertex3>();
                        for (int i = 0; i < Indices.Length; i++)
                        {
                            ref StudioVertex V = ref StudioVerts[Indices[i]];
                            Vts.Add(new Vertex3(new Vector3(V.Position.X, V.Position.Y, V.Position.Z), new Vector2(V.TexCoordX, 1.0f - V.TexCoordY), Color.White));
                        }

                        string   MatName = MaterialNames[StudioMesh.Material];
                        Material Mat     = Engine.GetMaterial(MatName);

                        if (Mat == Engine.GetMaterial("error"))
                        {
                            Mat = ValveMaterial.CreateMaterial(MatName);

                            if (Mat != Engine.GetMaterial("error"))
                            {
                                Engine.RegisterMaterial(Mat);
                            }
                        }

                        libTechMesh Msh = new libTechMesh(Vts.ToArray(), Mat);
                        Msh.Name = StudioModel.Name;
                        Model.AddMesh(Msh);
                    }
Ejemplo n.º 2
0
        static FoamModel LoadMdl(string FilePath)
        {
            FilePath = FilePath.Substring(0, FilePath.Length - Path.GetExtension(FilePath).Length);

            StudioModelFile Mdl = StudioModelFile.FromProvider(FilePath + ".mdl", VFS);

            if (Mdl == null)
            {
                throw new FileNotFoundException("File not found", FilePath + ".mdl");
            }

            ValveVertexFile   Verts = ValveVertexFile.FromProvider(FilePath + ".vvd", VFS);
            ValveTriangleFile Tris  = ValveTriangleFile.FromProvider(FilePath + ".dx90.vtx", Mdl, Verts, VFS);


            // Foam stuff

            List <FoamMaterial> FoamMaterials = new List <FoamMaterial>();

            string[] TexNames = Mdl.TextureNames.ToArray();

            for (int i = 0; i < Mdl.MaterialCount; i++)
            {
                string MatName      = Mdl.GetMaterialName(i, VFS);
                string ShortMatName = Path.GetFileNameWithoutExtension(MatName);

                ValveMaterialFile VMF = ValveMaterialFile.FromProvider(MatName, VFS);
                FoamMaterials.Add(new FoamMaterial(ShortMatName, new FoamTexture[] { new FoamTexture(TexNames[i], FoamTextureType.Diffuse) }));
            }

            List <FoamMesh> FoamMeshes = new List <FoamMesh>();
            List <FoamBone> FoamBones  = new List <FoamBone>();

            StudioModelFile.StudioBone[] Bones = Mdl.GetBones();
            for (int i = 0; i < Bones.Length; i++)
            {
                string BoneName = Mdl.GetBoneName(i);
                FoamBones.Add(new FoamBone(BoneName, Bones[i].Parent, Matrix4x4.Identity));
            }

            // BODIES
            for (int BodyPartIdx = 0; BodyPartIdx < Mdl.BodyPartCount; BodyPartIdx++)
            {
                StudioModelFile.StudioModel[] StudioModels = Mdl.GetModels(BodyPartIdx).ToArray();

                // MODELS
                for (int ModelIdx = 0; ModelIdx < StudioModels.Length; ModelIdx++)
                {
                    ref StudioModelFile.StudioModel StudioModel  = ref StudioModels[ModelIdx];
                    StudioModelFile.StudioMesh[]    StudioMeshes = Mdl.GetMeshes(ref StudioModel).ToArray();

                    // MESHES
                    for (int MeshIdx = 0; MeshIdx < StudioMeshes.Length; MeshIdx++)
                    {
                        ref StudioModelFile.StudioMesh StudioMesh = ref StudioMeshes[MeshIdx];

                        StudioVertex[] StudioVerts = new StudioVertex[Tris.GetVertexCount(BodyPartIdx, ModelIdx, 0, MeshIdx)];
                        Tris.GetVertices(BodyPartIdx, ModelIdx, 0, MeshIdx, StudioVerts);

                        int[] Indices = new int[Tris.GetIndexCount(BodyPartIdx, ModelIdx, 0, MeshIdx)];
                        Tris.GetIndices(BodyPartIdx, ModelIdx, 0, MeshIdx, Indices);


                        // Foam converted
                        List <FoamVertex3> FoamVerts = new List <FoamVertex3>(StudioVerts.Select(V => {
                            // TODO: CCW
                            nVector2 UV = new nVector2(V.TexCoordX, V.TexCoordY);
                            return(new FoamVertex3(Conv(V.Position), UV, nVector2.Zero, Conv(V.Normal), nVector3.Zero, FoamColor.White));
                        }));

                        List <FoamBoneInfo> FoamInfo = new List <FoamBoneInfo>(StudioVerts.Select(V => {
                            FoamBoneInfo Info = new FoamBoneInfo();
                            Info.Bone1        = V.BoneWeights.Bone0;
                            Info.Bone2        = V.BoneWeights.Bone1;
                            Info.Bone3        = V.BoneWeights.Bone1;

                            Info.Weight1 = V.BoneWeights.Weight0;
                            Info.Weight2 = V.BoneWeights.Weight1;
                            Info.Weight3 = V.BoneWeights.Weight2;
                            return(Info);
                        }));

                        List <ushort> FoamInds = new List <ushort>(Indices.Select(I => (ushort)I));

                        if (ConvertToCCW)
                        {
                            FoamInds.Reverse();
                        }

                        FoamMeshes.Add(new FoamMesh(FoamVerts.ToArray(), FoamInds.ToArray(), FoamInfo.ToArray(), StudioModel.Name + ";" + MeshIdx, StudioMesh.Material));

                        /*List<FoamVertex3> Vts = new List<FoamVertex3>();
                         * for (int i = 0; i < Indices.Length; i++) {
                         *      ref StudioVertex V = ref StudioVerts[Indices[i]];
                         *      Vts.Add(new Vertex3(new Vector3(V.Position.X, V.Position.Y, V.Position.Z), new Vector2(V.TexCoordX, 1.0f - V.TexCoordY), Color.White));
                         * }*/

                        /*string MatName = MaterialNames[StudioMesh.Material];
                         * Material Mat = Engine.GetMaterial(MatName);
                         *
                         * if (Mat == Engine.GetMaterial("error")) {
                         *      Mat = ValveMaterial.CreateMaterial(MatName);
                         *
                         *      if (Mat != Engine.GetMaterial("error"))
                         *              Engine.RegisterMaterial(Mat);
                         * }
                         *
                         * libTechMesh Msh = new libTechMesh(Vts.ToArray(), Mat);
                         * Msh.Name = StudioModel.Name;
                         * Model.AddMesh(Msh);*/
                    }
                }