Skip to content

Commit 8cb4bd2

Browse files
committed
Fix order of nodes in glTF export.
1 parent 7a6cdae commit 8cb4bd2

File tree

4 files changed

+73
-11
lines changed

4 files changed

+73
-11
lines changed

source/engine/export/exportergltf.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -273,31 +273,46 @@ export class ExporterGltf extends ExporterBase
273273
}
274274
}
275275

276+
function NodeHasVisibleChildren (model, node)
277+
{
278+
for (let meshIndex of node.GetMeshIndices ()) {
279+
let meshInstanceId = new MeshInstanceId (node.GetId (), meshIndex);
280+
if (model.IsMeshInstanceVisible (meshInstanceId)) {
281+
return true;
282+
}
283+
}
284+
for (let childNode of node.GetChildNodes ()) {
285+
if (NodeHasVisibleChildren (model, childNode)) {
286+
return true;
287+
}
288+
}
289+
return false;
290+
}
291+
276292
function AddNode (model, jsonParent, jsonNodes, node)
277293
{
278294
if (node.IsMeshNode ()) {
279295
for (let meshIndex of node.GetMeshIndices ()) {
280296
AddMeshNode (model, jsonParent, jsonNodes, node, meshIndex, true);
281297
}
282-
} else {
298+
} else if (NodeHasVisibleChildren (model, node)) {
283299
let nodeJson = {};
300+
284301
let nodeName = node.GetName ();
285302
if (nodeName.length > 0) {
286-
nodeJson.name = node.GetName ();
303+
nodeJson.name = nodeName;
287304
}
305+
288306
let transformation = node.GetTransformation ();
289307
if (!transformation.IsIdentity ()) {
290308
nodeJson.matrix = node.GetTransformation ().GetMatrix ().Get ();
291309
}
292310

293-
if (node.ChildNodeCount () > 0 || node.MeshIndexCount () > 0) {
294-
nodeJson.children = [];
295-
AddChildNodes (model, nodeJson.children, jsonNodes, node);
296-
if (nodeJson.children.length > 0) {
297-
jsonNodes.push (nodeJson);
298-
jsonParent.push (jsonNodes.length - 1);
299-
}
300-
}
311+
jsonNodes.push (nodeJson);
312+
jsonParent.push (jsonNodes.length - 1);
313+
314+
nodeJson.children = [];
315+
AddChildNodes (model, nodeJson.children, jsonNodes, node);
301316
}
302317
}
303318

source/engine/model/model.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ export class Model extends ModelObject3D
1717
return this.root;
1818
}
1919

20+
NodeCount ()
21+
{
22+
let count = 0;
23+
this.root.Enumerate ((node) => {
24+
count += 1;
25+
});
26+
return count - 1;
27+
}
28+
2029
MaterialCount ()
2130
{
2231
return this.materials.length;

test/tests/exporter_test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ describe ('Exporter', function () {
425425
onSuccess () {
426426
let importedModel = importer.GetModel ();
427427
assert.ok (OV.CheckModel (importedModel));
428+
assert.strictEqual (importedModel.NodeCount (), 5);
428429
assert.strictEqual (importedModel.MaterialCount (), 3);
429430
assert.strictEqual (importedModel.MeshCount (), 3);
430431
assert.strictEqual (importedModel.MeshInstanceCount (), 4);
@@ -459,6 +460,7 @@ describe ('Exporter', function () {
459460
onSuccess () {
460461
let importedModel = importer.GetModel ();
461462
assert.ok (OV.CheckModel (importedModel));
463+
assert.strictEqual (importedModel.NodeCount (), 4);
462464
assert.strictEqual (importedModel.MaterialCount (), 3);
463465
assert.strictEqual (importedModel.MeshCount (), 3);
464466
assert.strictEqual (importedModel.MeshInstanceCount (), 3);
@@ -493,6 +495,7 @@ describe ('Exporter', function () {
493495
onSuccess () {
494496
let importedModel = importer.GetModel ();
495497
assert.ok (OV.CheckModel (importedModel));
498+
assert.strictEqual (importedModel.NodeCount (), 3);
496499
assert.strictEqual (importedModel.MaterialCount (), 3);
497500
assert.strictEqual (importedModel.MeshCount (), 2);
498501
assert.strictEqual (importedModel.MeshInstanceCount (), 2);
@@ -501,6 +504,41 @@ describe ('Exporter', function () {
501504
});
502505
});
503506
});
507+
508+
it ('Gltf Hierarchical Export Filter 2', function (done) {
509+
let model = CreateHierarchicalTestModelForExport ();
510+
let settings = new OV.ExporterSettings ({
511+
isMeshVisible : (meshInstanceId) => {
512+
return meshInstanceId.IsEqual (new OV.MeshInstanceId (1, 0));
513+
}
514+
});
515+
Export (model, settings, OV.FileFormat.Binary, 'glb', function (result) {
516+
assert.strictEqual (result.length, 1);
517+
518+
let glbFile = result[0];
519+
assert.strictEqual (glbFile.GetName (), 'model.glb');
520+
521+
let contentBuffer = glbFile.GetBufferContent ();
522+
let importer = new OV.ImporterGltf ();
523+
importer.Import (glbFile.GetName (), 'glb', contentBuffer, {
524+
getDefaultMaterialColor () {
525+
return new OV.RGBColor (0, 0, 0);
526+
},
527+
getFileBuffer (filePath) {
528+
return null;
529+
},
530+
onSuccess () {
531+
let importedModel = importer.GetModel ();
532+
assert.ok (OV.CheckModel (importedModel));
533+
assert.strictEqual (importedModel.NodeCount (), 2);
534+
assert.strictEqual (importedModel.MaterialCount (), 3);
535+
assert.strictEqual (importedModel.MeshCount (), 1);
536+
assert.strictEqual (importedModel.MeshInstanceCount (), 1);
537+
done ();
538+
}
539+
});
540+
});
541+
});
504542
});
505543

506544
}

test/utils/testutils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ export function CreateHierarchicalTestModelForExport ()
421421

422422
let material2 = new OV.PhongMaterial ();
423423
material2.name = 'Material 2';
424-
material1.color = new OV.RGBColor (0, 255, 0);
424+
material2.color = new OV.RGBColor (0, 255, 0);
425425
model.AddMaterial (material2);
426426

427427
let material3 = new OV.PhongMaterial ();

0 commit comments

Comments
 (0)