From 4ad94bbb924b05317fd0d89b0debce9b8c871563 Mon Sep 17 00:00:00 2001 From: kovacsv Date: Wed, 27 Dec 2023 07:53:41 +0100 Subject: [PATCH] Export lines to obj. --- source/engine/export/exporterobj.js | 32 +++++++++++++++------ test/tests/exportimport_test.js | 44 +++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 8 deletions(-) diff --git a/source/engine/export/exporterobj.js b/source/engine/export/exporterobj.js index ea23f3d..6a27efb 100644 --- a/source/engine/export/exporterobj.js +++ b/source/engine/export/exporterobj.js @@ -90,14 +90,6 @@ export class ExporterObj extends ExporterBase let n0 = triangle.n0 + normalOffset + 1; let n1 = triangle.n1 + normalOffset + 1; let n2 = triangle.n2 + normalOffset + 1; - if (triangle.mat !== null) { - let material = exporterModel.GetMaterial (triangle.mat); - let materialName = this.GetExportedMaterialName (material.name); - if (materialName !== usedMaterialName) { - objWriter.WriteArrayLine (['usemtl', materialName]); - usedMaterialName = materialName; - } - } let u0 = ''; let u1 = ''; let u2 = ''; @@ -106,8 +98,32 @@ export class ExporterObj extends ExporterBase u1 = triangle.u1 + uvOffset + 1; u2 = triangle.u2 + uvOffset + 1; } + if (triangle.mat !== null) { + let material = exporterModel.GetMaterial (triangle.mat); + let materialName = this.GetExportedMaterialName (material.name); + if (materialName !== usedMaterialName) { + objWriter.WriteArrayLine (['usemtl', materialName]); + usedMaterialName = materialName; + } + } objWriter.WriteArrayLine (['f', [v0, u0, n0].join ('/'), [v1, u1, n1].join ('/'), [v2, u2, n2].join ('/')]); } + for (let lineIndex = 0; lineIndex < mesh.LineCount (); lineIndex++) { + let line = mesh.GetLine (lineIndex); + let vertexIndices = []; + for (let vertexIndex = 0; vertexIndex < line.vertices.length; vertexIndex++) { + vertexIndices.push (line.vertices[vertexIndex] + vertexOffset + 1); + } + if (line.mat !== null) { + let material = exporterModel.GetMaterial (line.mat); + let materialName = this.GetExportedMaterialName (material.name); + if (materialName !== usedMaterialName) { + objWriter.WriteArrayLine (['usemtl', materialName]); + usedMaterialName = materialName; + } + } + objWriter.WriteArrayLine (['l', vertexIndices.join (' ')]); + } vertexOffset += mesh.VertexCount (); normalOffset += mesh.NormalCount (); uvOffset += mesh.TextureUVCount (); diff --git a/test/tests/exportimport_test.js b/test/tests/exportimport_test.js index f9bbb08..d20cd05 100644 --- a/test/tests/exportimport_test.js +++ b/test/tests/exportimport_test.js @@ -18,6 +18,7 @@ function CreateTestModel () let phongMaterial = new OV.PhongMaterial (); phongMaterial.name = 'Phong Material'; + phongMaterial.color = new OV.RGBColor (0, 0, 0); phongMaterial.emissive = new OV.RGBColor (1, 1, 1); phongMaterial.opacity = 0.1; phongMaterial.transparent = true; @@ -30,6 +31,7 @@ function CreateTestModel () let phongMaterialTexture = new OV.PhongMaterial (); phongMaterialTexture.name = 'Phong Material With Texture'; + phongMaterialTexture.color = new OV.RGBColor (0, 0, 0); phongMaterialTexture.emissive = new OV.RGBColor (1, 1, 1); phongMaterialTexture.opacity = 0.1; phongMaterialTexture.transparent = true; @@ -47,6 +49,7 @@ function CreateTestModel () let physicalMaterialTexture = new OV.PhysicalMaterial (); physicalMaterialTexture.name = 'Phong Material With Texture'; + physicalMaterialTexture.color = new OV.RGBColor (0, 0, 0); physicalMaterialTexture.emissive = new OV.RGBColor (1, 1, 1); physicalMaterialTexture.opacity = 0.1; physicalMaterialTexture.transparent = true; @@ -108,6 +111,39 @@ function CreateTestModel () return model; } +function CreateTestModelWithLines () +{ + let model = new OV.Model (); + + let phongMaterial = new OV.PhongMaterial (); + phongMaterial.name = 'Material'; + phongMaterial.color = new OV.RGBColor (0, 0, 0); + model.AddMaterial (phongMaterial); + + let meshOnly = new OV.Mesh (); + meshOnly.SetName ('Mesh Only'); + meshOnly.AddVertex (new OV.Coord3D (0.0, 0.0, 0.0)); + meshOnly.AddVertex (new OV.Coord3D (1.0, 0.0, 0.0)); + meshOnly.AddVertex (new OV.Coord3D (0.0, 1.0, 0.0)); + meshOnly.AddTriangle (new OV.Triangle (0, 1, 2).SetMaterial (0)); + model.AddMeshToRootNode (meshOnly); + + let meshAndLines = new OV.Mesh (); + meshAndLines.SetName ('Meshes and Lines'); + meshAndLines.AddVertex (new OV.Coord3D (0.0, 0.0, 0.0)); + meshAndLines.AddVertex (new OV.Coord3D (1.0, 0.0, 0.0)); + meshAndLines.AddVertex (new OV.Coord3D (0.0, 1.0, 0.0)); + meshAndLines.AddTriangle (new OV.Triangle (0, 1, 2).SetMaterial (0)); + meshAndLines.AddVertex (new OV.Coord3D (0.0, 0.0, 1.0)); + meshAndLines.AddVertex (new OV.Coord3D (1.0, 0.0, 1.0)); + meshAndLines.AddVertex (new OV.Coord3D (0.0, 1.0, 1.0)); + meshAndLines.AddLine (new OV.Line ([3, 4, 5]).SetMaterial (0)); + model.AddMeshToRootNode (meshAndLines); + + OV.FinalizeModel (model); + return model; +} + function ExportImport (model, format, extension, onReady) { let exporter = new OV.Exporter (); @@ -172,6 +208,14 @@ describe ('Export-Import Test', function () { }); }); + it ('Export-Import Obj with Lines', function (done) { + let model = CreateTestModelWithLines (); + ExportImport (model, OV.FileFormat.Text, 'obj', (model2) => { + CheckModel (model, model2); + done (); + }); + }); + it ('Export-Import Stl Ascii', function (done) { let model = CreateTestModel (); ExportImport (model, OV.FileFormat.Text, 'stl', (model2) => {