Skip to content

Commit 4ad94bb

Browse files
committed
Export lines to obj.
1 parent 3195dfd commit 4ad94bb

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

source/engine/export/exporterobj.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,6 @@ export class ExporterObj extends ExporterBase
9090
let n0 = triangle.n0 + normalOffset + 1;
9191
let n1 = triangle.n1 + normalOffset + 1;
9292
let n2 = triangle.n2 + normalOffset + 1;
93-
if (triangle.mat !== null) {
94-
let material = exporterModel.GetMaterial (triangle.mat);
95-
let materialName = this.GetExportedMaterialName (material.name);
96-
if (materialName !== usedMaterialName) {
97-
objWriter.WriteArrayLine (['usemtl', materialName]);
98-
usedMaterialName = materialName;
99-
}
100-
}
10193
let u0 = '';
10294
let u1 = '';
10395
let u2 = '';
@@ -106,8 +98,32 @@ export class ExporterObj extends ExporterBase
10698
u1 = triangle.u1 + uvOffset + 1;
10799
u2 = triangle.u2 + uvOffset + 1;
108100
}
101+
if (triangle.mat !== null) {
102+
let material = exporterModel.GetMaterial (triangle.mat);
103+
let materialName = this.GetExportedMaterialName (material.name);
104+
if (materialName !== usedMaterialName) {
105+
objWriter.WriteArrayLine (['usemtl', materialName]);
106+
usedMaterialName = materialName;
107+
}
108+
}
109109
objWriter.WriteArrayLine (['f', [v0, u0, n0].join ('/'), [v1, u1, n1].join ('/'), [v2, u2, n2].join ('/')]);
110110
}
111+
for (let lineIndex = 0; lineIndex < mesh.LineCount (); lineIndex++) {
112+
let line = mesh.GetLine (lineIndex);
113+
let vertexIndices = [];
114+
for (let vertexIndex = 0; vertexIndex < line.vertices.length; vertexIndex++) {
115+
vertexIndices.push (line.vertices[vertexIndex] + vertexOffset + 1);
116+
}
117+
if (line.mat !== null) {
118+
let material = exporterModel.GetMaterial (line.mat);
119+
let materialName = this.GetExportedMaterialName (material.name);
120+
if (materialName !== usedMaterialName) {
121+
objWriter.WriteArrayLine (['usemtl', materialName]);
122+
usedMaterialName = materialName;
123+
}
124+
}
125+
objWriter.WriteArrayLine (['l', vertexIndices.join (' ')]);
126+
}
111127
vertexOffset += mesh.VertexCount ();
112128
normalOffset += mesh.NormalCount ();
113129
uvOffset += mesh.TextureUVCount ();

test/tests/exportimport_test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function CreateTestModel ()
1818

1919
let phongMaterial = new OV.PhongMaterial ();
2020
phongMaterial.name = 'Phong Material';
21+
phongMaterial.color = new OV.RGBColor (0, 0, 0);
2122
phongMaterial.emissive = new OV.RGBColor (1, 1, 1);
2223
phongMaterial.opacity = 0.1;
2324
phongMaterial.transparent = true;
@@ -30,6 +31,7 @@ function CreateTestModel ()
3031

3132
let phongMaterialTexture = new OV.PhongMaterial ();
3233
phongMaterialTexture.name = 'Phong Material With Texture';
34+
phongMaterialTexture.color = new OV.RGBColor (0, 0, 0);
3335
phongMaterialTexture.emissive = new OV.RGBColor (1, 1, 1);
3436
phongMaterialTexture.opacity = 0.1;
3537
phongMaterialTexture.transparent = true;
@@ -47,6 +49,7 @@ function CreateTestModel ()
4749

4850
let physicalMaterialTexture = new OV.PhysicalMaterial ();
4951
physicalMaterialTexture.name = 'Phong Material With Texture';
52+
physicalMaterialTexture.color = new OV.RGBColor (0, 0, 0);
5053
physicalMaterialTexture.emissive = new OV.RGBColor (1, 1, 1);
5154
physicalMaterialTexture.opacity = 0.1;
5255
physicalMaterialTexture.transparent = true;
@@ -108,6 +111,39 @@ function CreateTestModel ()
108111
return model;
109112
}
110113

114+
function CreateTestModelWithLines ()
115+
{
116+
let model = new OV.Model ();
117+
118+
let phongMaterial = new OV.PhongMaterial ();
119+
phongMaterial.name = 'Material';
120+
phongMaterial.color = new OV.RGBColor (0, 0, 0);
121+
model.AddMaterial (phongMaterial);
122+
123+
let meshOnly = new OV.Mesh ();
124+
meshOnly.SetName ('Mesh Only');
125+
meshOnly.AddVertex (new OV.Coord3D (0.0, 0.0, 0.0));
126+
meshOnly.AddVertex (new OV.Coord3D (1.0, 0.0, 0.0));
127+
meshOnly.AddVertex (new OV.Coord3D (0.0, 1.0, 0.0));
128+
meshOnly.AddTriangle (new OV.Triangle (0, 1, 2).SetMaterial (0));
129+
model.AddMeshToRootNode (meshOnly);
130+
131+
let meshAndLines = new OV.Mesh ();
132+
meshAndLines.SetName ('Meshes and Lines');
133+
meshAndLines.AddVertex (new OV.Coord3D (0.0, 0.0, 0.0));
134+
meshAndLines.AddVertex (new OV.Coord3D (1.0, 0.0, 0.0));
135+
meshAndLines.AddVertex (new OV.Coord3D (0.0, 1.0, 0.0));
136+
meshAndLines.AddTriangle (new OV.Triangle (0, 1, 2).SetMaterial (0));
137+
meshAndLines.AddVertex (new OV.Coord3D (0.0, 0.0, 1.0));
138+
meshAndLines.AddVertex (new OV.Coord3D (1.0, 0.0, 1.0));
139+
meshAndLines.AddVertex (new OV.Coord3D (0.0, 1.0, 1.0));
140+
meshAndLines.AddLine (new OV.Line ([3, 4, 5]).SetMaterial (0));
141+
model.AddMeshToRootNode (meshAndLines);
142+
143+
OV.FinalizeModel (model);
144+
return model;
145+
}
146+
111147
function ExportImport (model, format, extension, onReady)
112148
{
113149
let exporter = new OV.Exporter ();
@@ -172,6 +208,14 @@ describe ('Export-Import Test', function () {
172208
});
173209
});
174210

211+
it ('Export-Import Obj with Lines', function (done) {
212+
let model = CreateTestModelWithLines ();
213+
ExportImport (model, OV.FileFormat.Text, 'obj', (model2) => {
214+
CheckModel (model, model2);
215+
done ();
216+
});
217+
});
218+
175219
it ('Export-Import Stl Ascii', function (done) {
176220
let model = CreateTestModel ();
177221
ExportImport (model, OV.FileFormat.Text, 'stl', (model2) => {

0 commit comments

Comments
 (0)