public void Write_Stream_Valid() { byte[] buffer; using (var stream = new MemoryStream()) { var obj = new ObjFile(); obj.ShadowObjectFileName = "a.a"; obj.WriteTo(stream); buffer = stream.ToArray(); } string text; using (var stream = new MemoryStream(buffer, false)) using (var reader = new StreamReader(stream)) { text = reader.ReadToEnd(); } string expected = @"shadow_obj a.a "; AssertExtensions.TextEqual(expected, text); }
static void Main(string[] args) { if (args.Length == 0 || string.IsNullOrEmpty(args[0])) { Console.Error.WriteLine("Input file is required"); return; } string input = args[0]; string output = input.Replace(".model", ".obj"); if (args.Length > 1 && !string.IsNullOrEmpty(args[1])) { output = args[1]; } ModelImporter importer = new ModelImporter(); ObjFile obj = importer.Import(input); string directory = Path.GetDirectoryName(output); if (!Directory.Exists(directory)) { Directory.CreateDirectory(directory); } obj.WriteTo(output); }
private void SaveObj(string directory, string filename, params string[] meshnames) { var obj = new ObjFile(); var referenced_materials = new List <PortableMaterial>(); foreach (var name in meshnames) { var pmesh = meshes[name]; for (int i = 0; i < pmesh.triangles.Count; i++) { var triangle = pmesh.triangles[i]; var face = new ObjFace(); face.ObjectName = name; foreach (var vertex in triangle.vertices) { obj.Vertices.Add(vertex.position); obj.VertexNormals.Add(vertex.normal); obj.TextureVertices.Add(vertex.uv); var index = obj.Vertices.Count; face.Vertices.Add(new ObjTriplet(index, index, index)); } face.MaterialName = triangle.material.name; obj.Faces.Add(face); referenced_materials.Add(triangle.material); } } var mtl = new ObjMaterialFile(); var materials = referenced_materials.Distinct(); foreach (var m in materials) { var material = new ObjMaterial(m.name); material.DiffuseColor = new ObjMaterialColor(m.colour.x, m.colour.y, m.colour.z); if (m.texture != null) { material.DiffuseMap = new ObjMaterialMap(m.texture.filename); } material.IsAntiAliasingEnabled = false; mtl.Materials.Add(material); } mtl.WriteTo(directory + Path.DirectorySeparatorChar + filename + ".mtl"); obj.MaterialLibraries.Add(filename + ".mtl"); obj.WriteTo(directory + Path.DirectorySeparatorChar + filename + ".obj"); }
private static string WriteObj(ObjFile obj) { byte[] buffer; using (var stream = new MemoryStream()) { obj.WriteTo(stream); buffer = stream.ToArray(); } string text; using (var stream = new MemoryStream(buffer, false)) using (var reader = new StreamReader(stream)) { text = reader.ReadToEnd(); } return(text); }
public void Write_File_Valid() { var temp = Path.GetTempFileName(); try { var obj = new ObjFile(); obj.ShadowObjectFileName = "a.a"; obj.WriteTo(temp); var text = File.ReadAllText(temp); string expected = @"shadow_obj a.a "; AssertExtensions.TextEqual(expected, text); } finally { File.Delete(temp); } }
public void Write_NullStream_Throws() { var obj = new ObjFile(); Assert.Throws <ArgumentNullException>("stream", () => obj.WriteTo((Stream)null)); }
private static void WriteObj(Int32Rect[][] tiles, BitmapSource[][][] imagedata, ConvertOptions options) { var positions = new Dictionary <Vector3, int>(); var normals = new Dictionary <Vector3, int>(); var uvs = new Dictionary <Vector2, int>(); var normal = new Vector3(0, 0, 1); var xcount = (options.ImageWidth.Value + options.TileWidth - 1) / options.TileWidth; var ycount = (options.ImageHeight.Value + options.TileHeight - 1) / options.TileHeight; var folder = Path.GetDirectoryName(options.InputFileName); var filename = Path.GetFileNameWithoutExtension(options.InputFileName); var mtl = new ObjMaterialFile(); var obj = new ObjFile(); obj.MaterialLibraries.Add($"{filename}.mtl"); for (var y = 0; y < ycount; y++) { for (var x = 0; x < xcount; x++) { var tile = tiles[y][x]; var texture = imagedata[0][y][x]; var encoder = new PngBitmapEncoder(); encoder.Frames.Add(BitmapFrame.Create(texture)); var matname = $"{filename}_{y:D3}_{x:D3}"; using (var outfile = File.Open(Path.Combine(folder, $"{matname}.png"), FileMode.Create, FileAccess.Write, FileShare.None)) { encoder.Save(outfile); } var material = new ObjMaterial(matname); material.DiffuseMap = new ObjMaterialMap($"{matname}.png"); mtl.Materials.Add(material); var face = new ObjFace(); face.MaterialName = matname; face.Vertices.Add(new ObjTriplet( positions.AddUniqueIndex(new Vector3(tile.X, -(tile.Y), 0)), uvs.AddUniqueIndex(new Vector2(0, (float)tile.Height / texture.PixelHeight)), normals.AddUniqueIndex(normal) )); face.Vertices.Add(new ObjTriplet( positions.AddUniqueIndex(new Vector3(tile.X + tile.Width, -(tile.Y), 0)), uvs.AddUniqueIndex(new Vector2((float)tile.Width / texture.PixelWidth, (float)tile.Height / texture.PixelHeight)), normals.AddUniqueIndex(normal) )); face.Vertices.Add(new ObjTriplet( positions.AddUniqueIndex(new Vector3(tile.X + tile.Width, -(tile.Y + tile.Height), 0)), uvs.AddUniqueIndex(new Vector2((float)tile.Width / texture.PixelWidth, 0)), normals.AddUniqueIndex(normal) )); face.Vertices.Add(new ObjTriplet( positions.AddUniqueIndex(new Vector3(tile.X, -(tile.Y + tile.Height), 0)), uvs.AddUniqueIndex(new Vector2(0, 0)), normals.AddUniqueIndex(normal) )); obj.Faces.Add(face); } } (from x in positions orderby x.Value select x.Key).ToList().ForEach(x => obj.Vertices.Add(x.ToObjVertex())); (from x in normals orderby x.Value select x.Key).ToList().ForEach(x => obj.VertexNormals.Add(x.ToObjVector3())); (from x in uvs orderby x.Value select x.Key).ToList().ForEach(x => obj.TextureVertices.Add(x.ToObjVector3())); mtl.WriteTo(Path.Combine(folder, $"{filename}.mtl")); obj.WriteTo(Path.Combine(folder, $"{filename}.obj")); }