diff --git a/test/tests/exportimport_test.js b/test/tests/exportimport_test.js index 43c2ee9..fdb67e3 100644 --- a/test/tests/exportimport_test.js +++ b/test/tests/exportimport_test.js @@ -1,17 +1,119 @@ var assert = require ('assert'); var testUtils = require ('../utils/testutils.js'); +function CreateTestModel () +{ + function CreateTexture (name, url) + { + let texture = new OV.TextureMap (); + texture.name = name; + texture.url = url; + texture.buffer = new ArrayBuffer (1); + return texture; + } + + let model = new OV.Model (); + + let phongMaterial = new OV.PhongMaterial (); + phongMaterial.name = 'Phong Material'; + phongMaterial.emissive = new OV.Color (1, 1, 1); + phongMaterial.opacity = 0.1; + phongMaterial.transparent = true; + phongMaterial.alphaTest = 0.2; + phongMaterial.multiplyDiffuseMap = false; + phongMaterial.ambient = new OV.Color (2, 2, 2); + phongMaterial.specular = new OV.Color (3, 3, 3); + phongMaterial.shininess = 0.3; + model.AddMaterial (phongMaterial); + + let phongMaterialTexture = new OV.PhongMaterial (); + phongMaterialTexture.name = 'Phong Material With Texture'; + phongMaterialTexture.emissive = new OV.Color (1, 1, 1); + phongMaterialTexture.opacity = 0.1; + phongMaterialTexture.transparent = true; + phongMaterialTexture.alphaTest = 0.2; + phongMaterialTexture.multiplyDiffuseMap = false; + phongMaterialTexture.diffuseMap = CreateTexture ('diffuse.png', 'diffuse_url.png'); + phongMaterialTexture.bumpMap = CreateTexture ('bump.png', 'bump_url.png'); + phongMaterialTexture.normalMap = CreateTexture ('normal.png', 'normal_url.png'); + phongMaterialTexture.emissiveMap = CreateTexture ('emissive.png', 'emissive_url.png'); + phongMaterialTexture.ambient = new OV.Color (2, 2, 2); + phongMaterialTexture.specular = new OV.Color (3, 3, 3); + phongMaterialTexture.shininess = 0.3; + phongMaterialTexture.specularMap = CreateTexture ('specular.png', 'specular_url.png'); + model.AddMaterial (phongMaterialTexture); + + let physicalMaterialTexture = new OV.PhysicalMaterial (); + physicalMaterialTexture.name = 'Phong Material With Texture'; + physicalMaterialTexture.emissive = new OV.Color (1, 1, 1); + physicalMaterialTexture.opacity = 0.1; + physicalMaterialTexture.transparent = true; + physicalMaterialTexture.alphaTest = 0.2; + physicalMaterialTexture.multiplyDiffuseMap = false; + physicalMaterialTexture.diffuseMap = CreateTexture ('diffuse.png', 'diffuse_url.png'); + physicalMaterialTexture.bumpMap = CreateTexture ('bump.png', 'bump_url.png'); + physicalMaterialTexture.normalMap = CreateTexture ('normal.png', 'normal_url.png'); + physicalMaterialTexture.emissiveMap = CreateTexture ('emissive.png', 'emissive_url.png'); + physicalMaterialTexture.metalness = 0.3; + physicalMaterialTexture.roughness = 0.4; + physicalMaterialTexture.metalnessMap = CreateTexture ('metalness.png', 'metalness_url.png'); + model.AddMaterial (physicalMaterialTexture); + + let rootNode = model.GetRootNode (); + let node1 = new OV.Node (); + let node2 = new OV.Node (); + let node3 = new OV.Node (); + node1.SetTransformation (new OV.Transformation (new OV.Matrix ().CreateTranslation (0.0, 0.0, 1.0))); + node2.SetTransformation (new OV.Transformation (new OV.Matrix ().CreateTranslation (0.0, 0.0, 2.0))); + node3.SetTransformation (new OV.Transformation (new OV.Matrix ().CreateTranslation (0.0, 0.0, 3.0))); + rootNode.AddChildNode (node1); + rootNode.AddChildNode (node2); + rootNode.AddChildNode (node3); + + let meshNoMaterial = new OV.Mesh (); + meshNoMaterial.SetName ('No Material'); + meshNoMaterial.AddVertex (new OV.Coord3D (0.0, 0.0, 0.0)); + meshNoMaterial.AddVertex (new OV.Coord3D (1.0, 0.0, 0.0)); + meshNoMaterial.AddVertex (new OV.Coord3D (0.0, 1.0, 0.0)); + meshNoMaterial.AddTriangle (new OV.Triangle (0, 1, 2)); + rootNode.AddMeshIndex (model.AddMesh (meshNoMaterial)); + + let meshWithPhongMaterial = new OV.Mesh (); + meshWithPhongMaterial.SetName ('Phong Material'); + meshWithPhongMaterial.AddVertex (new OV.Coord3D (0.0, 0.0, 0.0)); + meshWithPhongMaterial.AddVertex (new OV.Coord3D (1.0, 0.0, 0.0)); + meshWithPhongMaterial.AddVertex (new OV.Coord3D (0.0, 1.0, 0.0)); + meshWithPhongMaterial.AddTriangle (new OV.Triangle (0, 1, 2).SetMaterial (0)); + node1.AddMeshIndex (model.AddMesh (meshWithPhongMaterial)); + + let meshWithPhongTextureMaterial = new OV.Mesh (); + meshWithPhongTextureMaterial.SetName ('Phong Material Texture'); + meshWithPhongTextureMaterial.AddVertex (new OV.Coord3D (0.0, 0.0, 0.0)); + meshWithPhongTextureMaterial.AddVertex (new OV.Coord3D (1.0, 0.0, 0.0)); + meshWithPhongTextureMaterial.AddVertex (new OV.Coord3D (0.0, 1.0, 0.0)); + meshWithPhongTextureMaterial.AddTriangle (new OV.Triangle (0, 1, 2).SetMaterial (1)); + node2.AddMeshIndex (model.AddMesh (meshWithPhongTextureMaterial)); + + let meshWithPhysicalMaterial = new OV.Mesh (); + meshWithPhysicalMaterial.SetName ('Physical Material'); + meshWithPhysicalMaterial.AddVertex (new OV.Coord3D (0.0, 0.0, 0.0)); + meshWithPhysicalMaterial.AddVertex (new OV.Coord3D (1.0, 0.0, 0.0)); + meshWithPhysicalMaterial.AddVertex (new OV.Coord3D (0.0, 1.0, 0.0)); + meshWithPhysicalMaterial.AddTriangle (new OV.Triangle (0, 1, 2).SetMaterial (2)); + node3.AddMeshIndex (model.AddMesh (meshWithPhysicalMaterial)); + + OV.FinalizeModel (model, () => { return new OV.PhongMaterial (); }); + return model; +} + function ExportImport (model, format, extension, onReady) { let exporter = new OV.Exporter (); exporter.Export (model, format, extension, { - onSuccess : function (files) { - let fileObjects = []; - for (let file of files) { - fileObjects.push (new FileObject ('', file.name, file.content)); - } + onSuccess : function (exportedFiles) { let importer = new OV.Importer (); let settings = new OV.ImportSettings (); + let fileObjects = exportedFiles.map (file => new FileObject ('', file.name, file.content)); importer.ImportFiles (fileObjects, OV.FileSource.File, settings, { onFilesLoaded : function () { @@ -20,99 +122,98 @@ function ExportImport (model, format, extension, onReady) onReady (importResult.model) }, onImportError : function (importError) { - + console.log (importError); } }); } }); } +function CheckSingleMeshModel (model, model2) +{ + assert.strictEqual (model2.MaterialCount (), 1); + assert.strictEqual (model2.MeshInstanceCount (), 1); + assert.strictEqual (model.TriangleCount (), model2.TriangleCount ()); + + let modelBounds = OV.GetBoundingBox (model); + let model2Bounds = OV.GetBoundingBox (model2); + assert (OV.CoordIsEqual3D (modelBounds.min, model2Bounds.min)); + assert (OV.CoordIsEqual3D (modelBounds.max, model2Bounds.max)); +} + +function CheckModel (model, model2) +{ + assert.strictEqual (model.MaterialCount (), model2.MaterialCount ()); + assert.strictEqual (model.MeshInstanceCount (), model2.MeshInstanceCount ()); + assert.strictEqual (model.TriangleCount (), model2.TriangleCount ()); + + let modelBounds = OV.GetBoundingBox (model); + let model2Bounds = OV.GetBoundingBox (model2); + assert (OV.CoordIsEqual3D (modelBounds.min, model2Bounds.min)); + assert (OV.CoordIsEqual3D (modelBounds.max, model2Bounds.max)); +} + describe ('Export-Import Test', function () { - it ('Obj Export-Import', function (done) { - let model = testUtils.GetTranslatedRotatedCubesModel (); - ExportImport (model, OV.FileFormat.Text, 'obj', (result) => { - assert.strictEqual (model.MeshInstanceCount (), 3); - let boundingBox = OV.GetBoundingBox (model); - assert (OV.CoordIsEqual3D (boundingBox.min, new OV.Coord3D (-1.0, 0.0, 0.0))); - assert (OV.CoordIsEqual3D (boundingBox.max, new OV.Coord3D (3.0, 3.0, 1.0))); + it ('Export-Import Obj', function (done) { + let model = CreateTestModel (); + ExportImport (model, OV.FileFormat.Text, 'obj', (model2) => { + CheckModel (model, model2); done (); }); }); - it ('Stl Ascii Export-Import', function (done) { - let model = testUtils.GetTranslatedRotatedCubesModel (); - ExportImport (model, OV.FileFormat.Text, 'stl', (result) => { - assert.strictEqual (model.MeshInstanceCount (), 3); - let boundingBox = OV.GetBoundingBox (model); - assert (OV.CoordIsEqual3D (boundingBox.min, new OV.Coord3D (-1.0, 0.0, 0.0))); - assert (OV.CoordIsEqual3D (boundingBox.max, new OV.Coord3D (3.0, 3.0, 1.0))); + it ('Export-Import Stl Ascii', function (done) { + let model = CreateTestModel (); + ExportImport (model, OV.FileFormat.Text, 'stl', (model2) => { + CheckSingleMeshModel (model, model2); done (); }); }); - it ('Stl Binary Export-Import', function (done) { - let model = testUtils.GetTranslatedRotatedCubesModel (); - ExportImport (model, OV.FileFormat.Binary, 'stl', (result) => { - assert.strictEqual (model.MeshInstanceCount (), 3); - let boundingBox = OV.GetBoundingBox (model); - assert (OV.CoordIsEqual3D (boundingBox.min, new OV.Coord3D (-1.0, 0.0, 0.0))); - assert (OV.CoordIsEqual3D (boundingBox.max, new OV.Coord3D (3.0, 3.0, 1.0))); + it ('Export-Import Stl Binary', function (done) { + let model = CreateTestModel (); + ExportImport (model, OV.FileFormat.Binary, 'stl', (model2) => { + CheckSingleMeshModel (model, model2); done (); }); }); - it ('Ply Ascii Export-Import', function (done) { - let model = testUtils.GetTranslatedRotatedCubesModel (); - ExportImport (model, OV.FileFormat.Text, 'ply', (result) => { - assert.strictEqual (model.MeshInstanceCount (), 3); - let boundingBox = OV.GetBoundingBox (model); - assert (OV.CoordIsEqual3D (boundingBox.min, new OV.Coord3D (-1.0, 0.0, 0.0))); - assert (OV.CoordIsEqual3D (boundingBox.max, new OV.Coord3D (3.0, 3.0, 1.0))); + it ('Export-Import Ply Ascii', function (done) { + let model = CreateTestModel (); + ExportImport (model, OV.FileFormat.Text, 'ply', (model2) => { + CheckSingleMeshModel (model, model2); done (); }); }); - it ('Ply Binary Export-Import', function (done) { - let model = testUtils.GetTranslatedRotatedCubesModel (); - ExportImport (model, OV.FileFormat.Binary, 'ply', (result) => { - assert.strictEqual (model.MeshInstanceCount (), 3); - let boundingBox = OV.GetBoundingBox (model); - assert (OV.CoordIsEqual3D (boundingBox.min, new OV.Coord3D (-1.0, 0.0, 0.0))); - assert (OV.CoordIsEqual3D (boundingBox.max, new OV.Coord3D (3.0, 3.0, 1.0))); + it ('Export-Import Ply Binary', function (done) { + let model = CreateTestModel (); + ExportImport (model, OV.FileFormat.Binary, 'ply', (model2) => { + CheckSingleMeshModel (model, model2); done (); }); }); - it ('glTF Ascii Export-Import', function (done) { - let model = testUtils.GetTranslatedRotatedCubesModel (); - ExportImport (model, OV.FileFormat.Text, 'gltf', (result) => { - assert.strictEqual (model.MeshInstanceCount (), 3); - let boundingBox = OV.GetBoundingBox (model); - assert (OV.CoordIsEqual3D (boundingBox.min, new OV.Coord3D (-1.0, 0.0, 0.0))); - assert (OV.CoordIsEqual3D (boundingBox.max, new OV.Coord3D (3.0, 3.0, 1.0))); + it ('Export-Import glTF Ascii', function (done) { + let model = CreateTestModel (); + ExportImport (model, OV.FileFormat.Text, 'gltf', (model2) => { + CheckModel (model, model2); done (); }); }); - it ('glTF Binary Export-Import', function (done) { - let model = testUtils.GetTranslatedRotatedCubesModel (); - ExportImport (model, OV.FileFormat.Binary, 'glb', (result) => { - assert.strictEqual (model.MeshInstanceCount (), 3); - let boundingBox = OV.GetBoundingBox (model); - assert (OV.CoordIsEqual3D (boundingBox.min, new OV.Coord3D (-1.0, 0.0, 0.0))); - assert (OV.CoordIsEqual3D (boundingBox.max, new OV.Coord3D (3.0, 3.0, 1.0))); + it ('Export-Import glTF Binary', function (done) { + let model = CreateTestModel (); + ExportImport (model, OV.FileFormat.Binary, 'glb', (model2) => { + CheckModel (model, model2); done (); }); }); - - it ('Off Export-Import', function (done) { - let model = testUtils.GetTranslatedRotatedCubesModel (); - ExportImport (model, OV.FileFormat.Text, 'off', (result) => { - assert.strictEqual (model.MeshInstanceCount (), 3); - let boundingBox = OV.GetBoundingBox (model); - assert (OV.CoordIsEqual3D (boundingBox.min, new OV.Coord3D (-1.0, 0.0, 0.0))); - assert (OV.CoordIsEqual3D (boundingBox.max, new OV.Coord3D (3.0, 3.0, 1.0))); + it ('Export-Import Off', function (done) { + let model = CreateTestModel (); + ExportImport (model, OV.FileFormat.Text, 'off', (model2) => { + CheckSingleMeshModel (model, model2); done (); }); }); diff --git a/test/utils/globals.js b/test/utils/globals.js index a129766..1fe05ef 100644 --- a/test/utils/globals.js +++ b/test/utils/globals.js @@ -86,6 +86,9 @@ global.document = { } else if (element.src.indexOf ('fflate') !== -1) { global.fflate = require (element.src); element.onload (); + } else if (element.src.indexOf ('rhino3dm') !== -1) { + global.rhino3dm = require (element.src); + element.onload (); } else { element.onerror (); }