diff --git a/source/threejs/threeimporter.js b/source/threejs/threeimporter.js index fe35d18..4606ecd 100644 --- a/source/threejs/threeimporter.js +++ b/source/threejs/threeimporter.js @@ -40,8 +40,9 @@ OV.ThreeImporter = class extends OV.ImporterBase return; } Promise.all (libraries).then (() => { - const loader = this.CreateLoader (); - this.LoadModel (fileContent, loader, onFinish); + const mainFileUrl = OV.CreateObjectUrl (fileContent); + const loader = this.CreateLoader (mainFileUrl); + this.LoadModel (mainFileUrl, loader, onFinish); }).catch (() => { onFinish (); }); @@ -58,11 +59,14 @@ OV.ThreeImporter = class extends OV.ImporterBase return null; } - CreateLoader () + CreateLoader (mainFileUrl) { const manager = new THREE.LoadingManager (); manager.setURLModifier ((url) => { - if (url.startsWith ('data:') || url.startsWith ('blob:')) { + if (url === mainFileUrl) { + return url; + } + if (url.startsWith ('data:')) { return url; } const fileBuffer = this.callbacks.getFileBuffer (url); @@ -76,7 +80,7 @@ OV.ThreeImporter = class extends OV.ImporterBase return loader; } - LoadModel (fileContent, loader, onFinish) + LoadModel (mainFileUrl, loader, onFinish) { function SetColor (color, threeColor) { @@ -93,40 +97,46 @@ OV.ThreeImporter = class extends OV.ImporterBase } // TODO: error handling - const mainFileUrl = OV.CreateObjectUrl (fileContent); - loader.load (mainFileUrl, (object) => { - object.traverse ((child) => { - if (child.isMesh) { - // TODO: merge same materials - // TODO: PBR materials - console.log (child); - let threeMaterial = child.material; - let material = new OV.Material (OV.MaterialType.Phong); - material.name = threeMaterial.name; - SetColor (material.color, threeMaterial.color); - if (threeMaterial.type === 'MeshPhongMaterial') { - SetColor (material.specular, threeMaterial.specular); - material.shininess = threeMaterial.shininess / 100.0; - } - const materialIndex = this.model.AddMaterial (material); - let mesh = OV.ConvertThreeGeometryToMesh (child.geometry, materialIndex); - if (child.matrixWorld !== undefined && child.matrixWorld !== null) { - const matrix = new OV.Matrix (child.matrixWorld.elements); - const transformation = new OV.Transformation (matrix); - - // TODO: flip to transform mesh - let determinant = matrix.Determinant (); - let mirrorByX = OV.IsNegative (determinant); - if (mirrorByX) { - OV.FlipMeshTrianglesOrientation (mesh); + loader.load (mainFileUrl, + (object) => { + object.traverse ((child) => { + if (child.isMesh) { + // TODO: merge same materials + // TODO: PBR materials + console.log (child); + let threeMaterial = child.material; + let material = new OV.Material (OV.MaterialType.Phong); + material.name = threeMaterial.name; + SetColor (material.color, threeMaterial.color); + if (threeMaterial.type === 'MeshPhongMaterial') { + SetColor (material.specular, threeMaterial.specular); + material.shininess = threeMaterial.shininess / 100.0; } - OV.TransformMesh (mesh, transformation); + const materialIndex = this.model.AddMaterial (material); + let mesh = OV.ConvertThreeGeometryToMesh (child.geometry, materialIndex); + if (child.matrixWorld !== undefined && child.matrixWorld !== null) { + const matrix = new OV.Matrix (child.matrixWorld.elements); + const transformation = new OV.Transformation (matrix); + const determinant = matrix.Determinant (); + const mirrorByX = OV.IsNegative (determinant); + OV.TransformMesh (mesh, transformation); + if (mirrorByX) { + OV.FlipMeshTrianglesOrientation (mesh); + } + } + this.model.AddMesh (mesh); } - // TODO: transform - this.model.AddMesh (mesh); - } - }); - onFinish (); - }); + }); + onFinish (); + }, + () => { + + }, + (err) => { + this.SetError (); + this.SetMessage (err); + onFinish (); + } + ); } };