private static List <xxTexture> Export(string dest, xxParser parser, List <xxFrame> meshParents, bool worldCoords) { List <xxTexture> usedTextures = new List <xxTexture>(parser.TextureList.Count); DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(dest)); if (!dir.Exists) { dir.Create(); } List <int> materialList = new List <int>(parser.MaterialList.Count); using (StreamWriter writer = new StreamWriter(dest, false)) { for (int i = 0; i < meshParents.Count; i++) { xxMesh meshListSome = meshParents[i].Mesh; for (int j = 0; j < meshListSome.SubmeshList.Count; j++) { xxSubmesh meshObj = meshListSome.SubmeshList[j]; int meshObjMatIdx = meshObj.MaterialIndex; if ((meshObjMatIdx >= 0) && (meshObjMatIdx < parser.MaterialList.Count)) { if (!materialList.Contains(meshObjMatIdx)) { materialList.Add(meshObjMatIdx); } } else { Report.ReportLog("Warning: Mesh " + meshParents[i].Name + " Object " + j + " has an invalid material"); } } } writer.WriteLine("Metasequoia Document"); writer.WriteLine("Format Text Ver 1.0"); writer.WriteLine(); writer.WriteLine("Material " + materialList.Count + " {"); foreach (int matIdx in materialList) { xxMaterial mat = parser.MaterialList[matIdx]; string s = "\t\"" + mat.Name + "\" col(0.800 0.800 0.800 1.000) dif(0.500) amb(0.100) emi(0.500) spc(0.100) power(30.00)"; string matTexName = mat.Textures[0].Name; if (matTexName != String.Empty) { s += " tex(\"" + Path.GetFileName(matTexName) + "\")"; } writer.WriteLine(s); } writer.WriteLine("}"); Random rand = new Random(); for (int i = 0; i < meshParents.Count; i++) { Matrix transform = Matrix.Identity; if (worldCoords) { xxFrame parent = meshParents[i]; while (parent != null) { transform = parent.Matrix * transform; parent = (xxFrame)parent.Parent; } } string meshName = meshParents[i].Name; xxMesh meshListSome = meshParents[i].Mesh; for (int j = 0; j < meshListSome.SubmeshList.Count; j++) { xxSubmesh meshObj = meshListSome.SubmeshList[j]; int meshObjMatIdx = meshObj.MaterialIndex; int mqoMatIdx = -1; if ((meshObjMatIdx >= 0) && (meshObjMatIdx < parser.MaterialList.Count)) { mqoMatIdx = materialList.IndexOf(meshObjMatIdx); } float[] color = new float[3]; for (int k = 0; k < color.Length; k++) { color[k] = (float)((rand.NextDouble() / 2) + 0.5); } string mqoName = meshName + "[" + j + "]"; if (worldCoords) { mqoName += "[W]"; } writer.WriteLine("Object \"" + mqoName + "\" {"); writer.WriteLine("\tshading 1"); writer.WriteLine("\tcolor " + color[0].ToFloatString() + " " + color[1].ToFloatString() + " " + color[2].ToFloatString()); writer.WriteLine("\tcolor_type 1"); List <ImportedVertex> vertList = xx.ImportedVertexList(meshObj.VertexList, xx.IsSkinned(meshListSome)); List <ImportedFace> faceList = xx.ImportedFaceList(meshObj.FaceList); if (worldCoords) { for (int k = 0; k < vertList.Count; k++) { vertList[k].Position = Vector3.TransformCoordinate(vertList[k].Position, transform); } } ExporterCommon.WriteMeshObject(writer, vertList, faceList, mqoMatIdx, null); writer.WriteLine("}"); } } writer.WriteLine("Eof"); } foreach (int matIdx in materialList) { xxMaterial mat = parser.MaterialList[matIdx]; xxMaterialTexture matTex = mat.Textures[0]; string matTexName = matTex.Name; if (matTexName != String.Empty) { for (int i = 0; i < parser.TextureList.Count; i++) { xxTexture tex = parser.TextureList[i]; string texName = tex.Name; if ((texName == matTexName) && !usedTextures.Contains(tex)) { usedTextures.Add(tex); break; } } } } return(usedTextures); }
public Importer(string path) { try { List <string> mqoMaterials = new List <string>(); List <MqoObject> mqoObjects = new List <MqoObject>(); using (StreamReader reader = new StreamReader(path, Utility.EncodingShiftJIS)) { string line; while ((line = reader.ReadLine()) != null) { if (line.Contains("Object")) { MqoObject mqoObject = ParseObject(line, reader); if (mqoObject != null) { mqoObjects.Add(mqoObject); } } else if (line.Contains("Material")) { string[] sArray = line.Split(new string[] { "\t", " " }, StringSplitOptions.RemoveEmptyEntries); int numMaterials = Int32.Parse(sArray[1]); while ((numMaterials > 0) && (line = reader.ReadLine()).Contains("\"")) { int matNameStart = line.IndexOf('\"') + 1; int matNameEnd = line.IndexOf('\"', matNameStart); mqoMaterials.Add(line.Substring(matNameStart, matNameEnd - matNameStart)); numMaterials--; } } } } List <List <MqoObject> > groupedMeshes = new List <List <MqoObject> >(); for (int i = 0; i < mqoObjects.Count; i++) { bool found = false; for (int j = 0; j < groupedMeshes.Count; j++) { if (mqoObjects[i].name == groupedMeshes[j][0].name) { groupedMeshes[j].Add(mqoObjects[i]); found = true; break; } } if (!found) { List <MqoObject> group = new List <MqoObject>(); group.Add(mqoObjects[i]); groupedMeshes.Add(group); } } MeshList = new List <ImportedMesh>(groupedMeshes.Count); for (int i = 0; i < groupedMeshes.Count; i++) { ImportedMesh meshList = ImportMeshList(groupedMeshes[i], mqoMaterials); MeshList.Add(meshList); } } catch (Exception e) { Report.ReportLog("Error importing .mqo: " + e.Message); } }
private static void ParseFaces(StreamReader reader, MqoObject mqoObject) { List <MqoFace> faceList = null; string line; while ((line = reader.ReadLine()) != null) { int countStart = line.IndexOf("face"); if (countStart >= 0) { countStart += 5; int countEnd = line.IndexOf(' ', countStart); int faceCount = Int32.Parse(line.Substring(countStart, countEnd - countStart)); faceList = new List <MqoFace>(faceCount); for (int i = 0; i < faceCount; i++) { // get vertex indices & uv line = reader.ReadLine(); string[] sArray = line.Split(new char[] { '\t', ' ', '(', ')' }, StringSplitOptions.RemoveEmptyEntries); int numVertices = Int32.Parse(sArray[0]); if (numVertices > 3) { throw new Exception("Face " + i + " in mesh object " + mqoObject.fullname + " has more than 3 vertices. Triangulate the meshes"); } else if (numVertices < 3) { Report.ReportLog("Warning: Skipping face " + i + " in mesh object " + mqoObject.fullname + " because it has a less than 3 vertices"); } else { MqoFace face = new MqoFace(); faceList.Add(face); for (int j = 1; j < sArray.Length; j++) { if (sArray[j].ToUpper() == "V") { for (int k = 0; k < face.vertexIndices.Length; k++) { face.vertexIndices[k] = Int32.Parse(sArray[++j]); } } else if (sArray[j].ToUpper() == "M") { face.materialIndex = Int32.Parse(sArray[++j]); } else if (sArray[j].ToUpper() == "UV") { for (int k = 0; k < face.UVs.Length; k++) { face.UVs[k] = new float[2] { Utility.ParseFloat(sArray[++j]), Utility.ParseFloat(sArray[++j]) }; } } } } } break; } } mqoObject.faces = faceList.ToArray(); }