Use one main object in viewer instead of an array of meshes.

This commit is contained in:
kovacsv 2021-10-22 17:25:16 +02:00
parent 67ca415a72
commit 654ffd0acc
7 changed files with 66 additions and 67 deletions

View File

@ -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);
}
});
};

View File

@ -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;
}
});

View File

@ -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;
});

View File

@ -79,58 +79,57 @@ 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);
}
GetModelMeshes ()
ClearMainObject ()
{
return this.modelMeshes;
}
ClearModelMeshes ()
{
for (let i = 0; i < this.modelMeshes.length; i++) {
let mesh = this.modelMeshes[i];
if (this.mainObject !== null) {
this.EnumerateMeshes ((mesh) => {
mesh.geometry.dispose ();
this.scene.remove (mesh);
});
this.scene.remove (this.mainObject);
this.mainObject = null;
}
this.modelMeshes = [];
}
EnumerateModelMeshes (enumerator)
EnumerateMeshes (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;
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;
}
}
GetModelPointUnderMouse (mouseCoords, camera, width, height)
{
let intersection = this.GetModelIntersectionUnderMouse (mouseCoords, camera, width, height);
if (intersection === null) {
return null;
}
return new OV.Coord3D (intersection.point.x, intersection.point.y, intersection.point.z);
}
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);
});
}

View File

@ -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 : () =>
{

View File

@ -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 ();

View File

@ -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);