示例#1
0
        private static void LoadMtlib(string filePath, ref ObjItem result)
        {
            var fi = new FileInfo(filePath);
            if (!fi.Exists)
                return;

            using (var sr = new StreamReader(fi.FullName))
            {
                var material = default(ObjMaterial);
                while (!sr.EndOfStream)
                {
                    var currentLine = sr.ReadLine();

                    if (String.IsNullOrWhiteSpace(currentLine) || currentLine[0] == '#')
                        continue;

                    var fields = currentLine.Trim().Split(null, 2);
                    var keyword = fields[0].Trim().ToLower();
                    var data = fields[1].Trim();

                    switch (keyword)
                    {
                        case "newmtl":
                            var dt = data.ToLower();
                            if (result.Materials.ContainsKey(dt))
                                material = result.Materials[dt];
                            else
                            {
                                material = new ObjMaterial(data);
                                result.Materials.Add(dt, material);
                            }
                            break;
                        case "ka":
                            if (material != null)
                                material.AmbientColor = ParseVector3(data);
                            break;
                        case "kd":
                            if (material != null)
                            {
                                var color = ParseVector3(data);
                                material.DiffuseColor = new Vector4(color.X, color.Y, color.Z, 1f);
                            }
                            break;
                        case "ks":
                            if (material != null)
                                material.SpecularColor = ParseVector3(data);
                            break;
                        case "ns":
                            if (material != null)
                                material.SpecularCoefficient = StringConverter.ToFloat(data);
                            break;
                        case "ni":
                            if (material != null)
                                material.OpticalDensity = StringConverter.ToFloat(data);
                            break;
                        case "d":
                        case "tr":
                            if (material != null)
                                material.Transparency = StringConverter.ToFloat(data);
                            break;
                        case "illum":
                            if (material != null)
                                material.IlluminationModel = StringConverter.ToInt(data);
                            break;
                        case "map_ka":
                            if (material != null)
                                material.AmbientTextureMap = GetMapFullPath(data, filePath);
                            break;
                        case "map_kd":
                            if (material != null)
                                material.DiffuseTextureMap = GetMapFullPath(data, filePath);
                            break;
                        case "map_ks":
                            if (material != null)
                                material.SpecularTextureMap = GetMapFullPath(data, filePath);
                            break;
                        case "map_ns":
                            if (material != null)
                                material.SpecularHighlightTextureMap = GetMapFullPath(data, filePath);
                            break;
                        case "map_d":
                            if (material != null)
                                material.TransparentTextureMap = GetMapFullPath(data, filePath);
                            break;
                        case "map_bump":
                        case "bump":
                            if (material != null)
                                material.BumpMap = GetMapFullPath(data, filePath);
                            break;
                        case "disp":
                            if (material != null)
                                material.DisplacementMap = GetMapFullPath(data, filePath);
                            break;
                        case "decal":
                            if (material != null)
                                material.StencilDecalMap = GetMapFullPath(data, filePath);
                            break;
                    }
                }
            }
        }
示例#2
0
        public static ObjItem LoadObjFile(string filePath, bool needExporter)
        {
            var result = new ObjItem(needExporter);
            var fi = new FileInfo(filePath);
            if (!fi.Exists)
                return null;

            using (var sr = new StreamReader(fi.FullName, Encoding.Default))
            {
                var currentGroup = default(ObjGroup);
                CheckAndAttachDefaultGroup(ref currentGroup, ref result);

                var index = 0;
                var lastGroupName = String.Empty;

                if (ProgramCore.PluginMode)
                {
                    var folderPath = Path.Combine(Application.StartupPath, "Models\\Model", ProgramCore.Project.ManType.GetObjDirPath());
                    switch (ProgramCore.Project.ManType)
                    {
                        case ManType.Male:
                            LoadMtlib(Path.Combine(folderPath, "Male.mtl"), ref result);
                            break;
                        case ManType.Female:
                            LoadMtlib(Path.Combine(folderPath, "Fem.mtl"), ref result);
                            break;
                        case ManType.Child:
                            LoadMtlib(Path.Combine(folderPath, "Child.mtl"), ref result);
                            break;
                    }
                }

                while (!sr.EndOfStream)
                {
                    var currentLine = sr.ReadLine();

                    if (String.IsNullOrWhiteSpace(currentLine) || currentLine[0] == '#')
                    {
                        if (currentLine == "#Accessories")
                            result.accessoryByHeadShop = true;
                        else if (currentLine == "#HeadShop Model")
                            result.modelByHeadShop = true;
                        continue;
                    }

                    var fields = currentLine.Trim().Split(null, 2);
                    if (fields.Length < 2)
                    {
                        ProgramCore.EchoToLog(String.Format("Bad obj file format. File: '{0}'", fi.FullName), EchoMessageType.Warning);
                        continue;
                    }

                    var keyword = fields[0].Trim().ToLower();
                    var data = fields[1].Trim();

                    switch (keyword)
                    {
                        case "v":       // verticles
                            var vertex = ParseVector3(data);
                            result.Vertices.Add(vertex);
                            if (needExporter)
                                result.ObjExport.Vertices.Add(vertex);
                            break;
                        case "vt":      // texture coords
                            var textureCoord = ParseTextureCoords(data);
                            result.TextureCoords.Add(textureCoord);
                            if (needExporter)
                                result.ObjExport.TexCoords.Add(textureCoord);
                            break;
                        case "vn":      // normals
                            var normal = ParseVector3(data);
                            result.Normals.Add(normal);
                            if (needExporter)
                                result.ObjExport.Normals.Add(normal);
                            break;
                        case "f":      // faces
                            var face = ParceFace(data);
                            if (needExporter)
                            {
                                face.ObjExportIndex = result.ObjExport.Faces.Count;
                                result.ObjExport.Faces.Add(new ObjExportFace(face.Count, face.Vertices));
                            }
                            currentGroup.AddFace(face);
                            index++;
                            break;
                        case "g":      // start group
                            if (needExporter)
                            {
                                lastGroupName = data;
                                if (result.ObjExport.MaterialsGroups.Count > 0)
                                {
                                    result.ObjExport.MaterialsGroups.Last().Groups.Last().EndFaceIndex = index - 1;
                                    result.ObjExport.MaterialsGroups.Last().Groups.Add(new ObjExportGroup
                                    {
                                        Group = data,
                                        StartFaceIndex = index
                                    });
                                }
                            }
                            break;
                        case "mtllib":  //parse mtl file
                            var path = Path.Combine(fi.DirectoryName, data);
                            LoadMtlib(path, ref result);
                            break;
                        case "usemtl":
                            if (needExporter)
                            {
                                if (result.ObjExport.MaterialsGroups.Count > 0)
                                    result.ObjExport.MaterialsGroups.Last().Groups.Last().EndFaceIndex = index - 1;
                                result.ObjExport.MaterialsGroups.Add(new ObjExportMaterial
                                {
                                    Material = data,
                                    Groups = new List<ObjExportGroup>
                                        {
                                            new ObjExportGroup
                                            {
                                                Group = lastGroupName,
                                                StartFaceIndex = index
                                            }
                                        }
                                });
                            }

                            var lowerData = data.ToLower();
                            var materialKey = result.Materials.Keys.SingleOrDefault(x => x == lowerData);
                            ObjMaterial material;
                            if (materialKey == null)                        // if can't parse mtl, create default group
                            {
                                material = new ObjMaterial(lowerData);
                                result.Materials.Add(lowerData, material);
                            }
                            else
                                material = result.Materials[materialKey];

                            if (result.Groups.ContainsKey(material))
                                currentGroup = result.Groups[material];
                            else
                            {
                                currentGroup = new ObjGroup(material.Name);
                                result.Groups.Add(material, currentGroup);
                            }
                            break;
                    }
                }
                if (result.ObjExport != null && result.ObjExport.MaterialsGroups.Count > 0 && needExporter)
                    result.ObjExport.MaterialsGroups.Last().Groups.Last().EndFaceIndex = index - 1;
            }
            return result;
        }
示例#3
0
 private static void CheckAndAttachDefaultGroup(ref ObjGroup currentGroup, ref ObjItem result)
 {
     if (currentGroup == null)
     {
         currentGroup = new ObjGroup("default");
         var defaultGroupName = "HeadShopDefaultMaterial".ToLower();
         var defaultMaterial = new ObjMaterial(defaultGroupName);
         result.Groups.Add(defaultMaterial, currentGroup);
         result.Materials.Add(defaultGroupName, defaultMaterial);
     }
 }
 public BaseRenderMesh()
 {
     Id = Guid.NewGuid();
     Material = new ObjMaterial(Id.ToString().Replace("-", ""));
 }