From 654ffd0acc5daaacd145148ad675132e60ce978e Mon Sep 17 00:00:00 2001 From: kovacsv Date: Fri, 22 Oct 2021 17:25:16 +0200 Subject: [PATCH] Use one main object in viewer instead of an array of meshes. --- source/threejs/threeconverter.js | 18 +++---- source/threejs/threemodelloader.js | 6 +-- source/viewer/domviewer.js | 4 +- source/viewer/viewer.js | 85 +++++++++++++++--------------- website/o3dv/js/embed.js | 8 +-- website/o3dv/js/loader.js | 4 +- website/o3dv/js/website.js | 8 +-- 7 files changed, 66 insertions(+), 67 deletions(-) diff --git a/source/threejs/threeconverter.js b/source/threejs/threeconverter.js index 756e5da..c0d37de 100644 --- a/source/threejs/threeconverter.js +++ b/source/threejs/threeconverter.js @@ -21,7 +21,7 @@ OV.ThreeConversionStateHandler = class this.callbacks = callbacks; this.texturesNeeded = 0; this.texturesLoaded = 0; - this.threeMeshes = null; + this.threeObject = null; } OnTextureNeeded () @@ -36,21 +36,21 @@ OV.ThreeConversionStateHandler = class this.Finish (); } - OnModelLoaded (threeMeshes) + OnModelLoaded (threeObject) { - this.threeMeshes = threeMeshes; + this.threeObject = threeObject; this.Finish (); } Finish () { - if (this.threeMeshes !== null && this.texturesNeeded === this.texturesLoaded) { - this.callbacks.onModelLoaded (this.threeMeshes); + if (this.threeObject !== null && this.texturesNeeded === this.texturesLoaded) { + this.callbacks.onModelLoaded (this.threeObject); } } }; -OV.ConvertModelToThreeMeshes = function (model, params, output, callbacks) +OV.ConvertModelToThreeObject = function (model, params, output, callbacks) { function CreateThreeMaterial (stateHandler, model, materialIndex, params, output) { @@ -255,7 +255,7 @@ OV.ConvertModelToThreeMeshes = function (model, params, output, callbacks) modelThreeMaterials.push (threeMaterial); } - let threeMeshes = []; + let threeObject = new THREE.Object3D (); let taskRunner = new OV.TaskRunner (); taskRunner.RunBatch (model.MeshCount (), 100, { runTask : (firstIndex, lastIndex, ready) => { @@ -263,13 +263,13 @@ OV.ConvertModelToThreeMeshes = function (model, params, output, callbacks) let mesh = model.GetMesh (meshIndex); if (mesh.TriangleCount () > 0) { let threeMesh = CreateThreeMesh (model, meshIndex, modelThreeMaterials); - threeMeshes.push (threeMesh); + threeObject.add (threeMesh); } } ready (); }, onReady : () => { - stateHandler.OnModelLoaded (threeMeshes); + stateHandler.OnModelLoaded (threeObject); } }); }; diff --git a/source/threejs/threemodelloader.js b/source/threejs/threemodelloader.js index 5126896..0b0a734 100644 --- a/source/threejs/threemodelloader.js +++ b/source/threejs/threemodelloader.js @@ -62,13 +62,13 @@ OV.ThreeModelLoader = class let params = new OV.ModelToThreeConversionParams (); params.forceMediumpForMaterials = this.hasHighpDriverIssue; let output = new OV.ModelToThreeConversionOutput (); - OV.ConvertModelToThreeMeshes (importResult.model, params, output, { + OV.ConvertModelToThreeObject (importResult.model, params, output, { onTextureLoaded : () => { this.callbacks.onTextureLoaded (); }, - onModelLoaded : (meshes) => { + onModelLoaded : (threeObject) => { this.defaultMaterial = output.defaultMaterial; - this.callbacks.onModelFinished (importResult, meshes); + this.callbacks.onModelFinished (importResult, threeObject); this.inProgress = false; } }); diff --git a/source/viewer/domviewer.js b/source/viewer/domviewer.js index ce62bdb..6a7d627 100644 --- a/source/viewer/domviewer.js +++ b/source/viewer/domviewer.js @@ -29,10 +29,10 @@ OV.Init3DViewerElement = function (parentDiv, modelUrls, parameters) onVisualizationStart : () => { progressDiv.innerHTML = 'Visualizing model...'; }, - onModelFinished : (importResult, threeMeshes) => { + onModelFinished : (importResult, threeObject) => { parentDiv.removeChild (progressDiv); canvas.style.display = 'inherit'; - viewer.AddMeshes (threeMeshes); + viewer.SetMainObject (threeObject); let boundingSphere = viewer.GetBoundingSphere ((meshUserData) => { return true; }); diff --git a/source/viewer/viewer.js b/source/viewer/viewer.js index c6bdaf5..43ee361 100644 --- a/source/viewer/viewer.js +++ b/source/viewer/viewer.js @@ -79,57 +79,56 @@ OV.ViewerGeometry = class constructor (scene) { this.scene = scene; - this.modelMeshes = []; + this.mainObject = null; } - AddModelMeshes (meshes) + SetMainObject (mainObject) { - for (let i = 0; i < meshes.length; i++) { - let mesh = meshes[i]; - this.modelMeshes.push (mesh); - this.scene.add (mesh); + this.mainObject = mainObject; + this.scene.add (this.mainObject); + } + + ClearMainObject () + { + if (this.mainObject !== null) { + this.EnumerateMeshes ((mesh) => { + mesh.geometry.dispose (); + }); + this.scene.remove (this.mainObject); + this.mainObject = null; } } - GetModelMeshes () + EnumerateMeshes (enumerator) { - return this.modelMeshes; - } - - ClearModelMeshes () - { - for (let i = 0; i < this.modelMeshes.length; i++) { - let mesh = this.modelMeshes[i]; - mesh.geometry.dispose (); - this.scene.remove (mesh); - } - this.modelMeshes = []; - } - - EnumerateModelMeshes (enumerator) - { - for (let i = 0; i < this.modelMeshes.length; i++) { - let mesh = this.modelMeshes[i]; - enumerator (mesh); + if (this.mainObject === null) { + return; } + this.mainObject.traverse ((obj) => { + if (obj.isMesh) { + enumerator (obj); + } + }); } GetModelMeshUnderMouse (mouseCoords, camera, width, height) { - let intersection = this.GetModelIntersectionUnderMouse (mouseCoords, camera, width, height); - if (intersection === null) { + if (this.mainObject === null) { return null; } - return intersection.object; - } - - GetModelPointUnderMouse (mouseCoords, camera, width, height) - { - let intersection = this.GetModelIntersectionUnderMouse (mouseCoords, camera, width, height); - if (intersection === null) { - return null; + let raycaster = new THREE.Raycaster (); + let mousePos = new THREE.Vector2 (); + mousePos.x = (mouseCoords.x / width) * 2 - 1; + mousePos.y = -(mouseCoords.y / height) * 2 + 1; + raycaster.setFromCamera (mousePos, camera); + let iSectObjects = raycaster.intersectObject (this.mainObject, true); + for (let i = 0; i < iSectObjects.length; i++) { + let iSectObject = iSectObjects[i]; + if (iSectObject.object.type === 'Mesh' && iSectObject.object.visible) { + return iSectObject.object; + } } - return new OV.Coord3D (intersection.point.x, intersection.point.y, intersection.point.z); + return null; } GetModelIntersectionUnderMouse (mouseCoords, camera, width, height) @@ -329,21 +328,21 @@ OV.Viewer = class this.renderer.render (this.scene, this.camera); } - AddMeshes (meshes) + SetMainObject (object) { - this.geometry.AddModelMeshes (meshes); + this.geometry.SetMainObject (object); this.Render (); } Clear () { - this.geometry.ClearModelMeshes (); + this.geometry.ClearMainObject (); this.Render (); } SetMeshesVisibility (isVisible) { - this.geometry.EnumerateModelMeshes ((mesh) => { + this.geometry.EnumerateMeshes ((mesh) => { let visible = isVisible (mesh.userData); if (mesh.visible !== visible) { mesh.visible = visible; @@ -363,7 +362,7 @@ OV.Viewer = class return highlightMaterials; } - this.geometry.EnumerateModelMeshes ((mesh) => { + this.geometry.EnumerateMeshes ((mesh) => { let highlighted = isHighlighted (mesh.userData); if (highlighted) { if (mesh.userData.threeMaterials === null) { @@ -399,7 +398,7 @@ OV.Viewer = class { let hasMesh = false; let boundingBox = new THREE.Box3 (); - this.geometry.EnumerateModelMeshes ((mesh) => { + this.geometry.EnumerateMeshes ((mesh) => { if (needToProcess (mesh.userData)) { boundingBox.union (new THREE.Box3 ().setFromObject (mesh)); hasMesh = true; @@ -426,7 +425,7 @@ OV.Viewer = class EnumerateMeshesUserData (enumerator) { - this.geometry.EnumerateModelMeshes ((mesh) => { + this.geometry.EnumerateMeshes ((mesh) => { enumerator (mesh.userData); }); } diff --git a/website/o3dv/js/embed.js b/website/o3dv/js/embed.js index d894f40..ca8a574 100644 --- a/website/o3dv/js/embed.js +++ b/website/o3dv/js/embed.js @@ -47,9 +47,9 @@ OV.Embed = class this.viewer.Resize (windowWidth, windowHeight); } - OnModelFinished (importResult, threeMeshes) + OnModelFinished (importResult, threeObject) { - this.viewer.AddMeshes (threeMeshes); + this.viewer.SetMainObject (threeObject); let boundingSphere = this.viewer.GetBoundingSphere ((meshUserData) => { return true; }); @@ -83,9 +83,9 @@ OV.Embed = class { }, - onFinish : (importResult, threeMeshes) => + onFinish : (importResult, threeObject) => { - this.OnModelFinished (importResult, threeMeshes); + this.OnModelFinished (importResult, threeObject); }, onRender : () => { diff --git a/website/o3dv/js/loader.js b/website/o3dv/js/loader.js index 5877278..6e85721 100644 --- a/website/o3dv/js/loader.js +++ b/website/o3dv/js/loader.js @@ -47,9 +47,9 @@ OV.InitModelLoader = function (modelLoader, callbacks) onVisualizationStart : () => { progressDialog.SetText ('Visualizing Model'); }, - onModelFinished : (importResult, threeMeshes) => { + onModelFinished : (importResult, threeObject) => { progressDialog.Hide (); - callbacks.onFinish (importResult, threeMeshes); + callbacks.onFinish (importResult, threeObject); }, onTextureLoaded : () => { callbacks.onRender (); diff --git a/website/o3dv/js/website.js b/website/o3dv/js/website.js index 9c4d7cf..836844d 100644 --- a/website/o3dv/js/website.js +++ b/website/o3dv/js/website.js @@ -131,10 +131,10 @@ OV.Website = class this.sidebar.HidePopups (); } - OnModelFinished (importResult, threeMeshes) + OnModelFinished (importResult, threeObject) { this.model = importResult.model; - this.viewer.AddMeshes (threeMeshes); + this.viewer.SetMainObject (threeObject); this.viewer.SetUpVector (importResult.upVector, false); this.navigator.FillTree (importResult); this.settingsPanel.Update (this.model); @@ -472,9 +472,9 @@ OV.Website = class this.ClearModel (); this.SetUIState (OV.WebsiteUIState.Loading); }, - onFinish : (importResult, threeMeshes) => + onFinish : (importResult, threeObject) => { - this.OnModelFinished (importResult, threeMeshes); + this.OnModelFinished (importResult, threeObject); let importedExtension = OV.GetFileExtension (importResult.mainFile); this.eventHandler.HandleEvent ('model_loaded', { extension : importedExtension }); this.SetUIState (OV.WebsiteUIState.Model);