From 5da75de38785496bd328754869f3965bd2a46995 Mon Sep 17 00:00:00 2001 From: Arjun Verma Date: Wed, 30 Oct 2024 16:03:32 +0530 Subject: [PATCH] Refactor Bounding Box Logic (#555) * Refactor bounding box logic * Update packages/base/src/3dview/mainview.tsx Co-authored-by: Duc Trung Le --------- Co-authored-by: Duc Trung Le --- packages/base/src/3dview/helpers.ts | 15 +++++++++ packages/base/src/3dview/mainview.tsx | 46 ++++++++++++--------------- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/packages/base/src/3dview/helpers.ts b/packages/base/src/3dview/helpers.ts index a9be9d8d..5e25af8f 100644 --- a/packages/base/src/3dview/helpers.ts +++ b/packages/base/src/3dview/helpers.ts @@ -251,6 +251,21 @@ export function buildShape(options: { edgeIdx++; } + const bbox = new THREE.Box3().setFromObject(mainMesh); + const size = new THREE.Vector3(); + bbox.getSize(size); + const center = new THREE.Vector3(); + bbox.getCenter(center); + + const boundingBox = new THREE.LineSegments( + new THREE.EdgesGeometry(new THREE.BoxGeometry(size.x, size.y, size.z)), + new THREE.LineBasicMaterial({ color: BOUNDING_BOX_COLOR, depthTest: false }) + ); + boundingBox.position.copy(center); + boundingBox.visible = false; + boundingBox.name = SELECTION_BOUNDING_BOX; + meshGroup.add(boundingBox); + meshGroup.add(mainMesh); return { meshGroup, mainMesh, edgesMeshes }; diff --git a/packages/base/src/3dview/mainview.tsx b/packages/base/src/3dview/mainview.tsx index 3236dc9c..b23f8976 100644 --- a/packages/base/src/3dview/mainview.tsx +++ b/packages/base/src/3dview/mainview.tsx @@ -1032,11 +1032,14 @@ export class MainView extends React.Component { selectedMesh.material.color = originalColor; } - const groupBoundingBox = this._meshGroup?.getObjectByName( + const parentGroup = this._meshGroup?.getObjectByName( + selectedMesh.name + )?.parent; + const boundingBox = parentGroup?.getObjectByName( SELECTION_BOUNDING_BOX - ); - if (groupBoundingBox) { - this._meshGroup?.remove(groupBoundingBox); + ) as THREE.Mesh; + if (boundingBox) { + boundingBox.visible = false; } const material = selectedMesh.material as THREE.Material & { @@ -1058,6 +1061,10 @@ export class MainView extends React.Component { continue; } + if (!selectedMesh.visible) { + continue; + } + if (selectedMesh.name.startsWith('edge')) { // Highlight edges using the old method if (!selectedMesh.userData.originalColor) { @@ -1080,29 +1087,16 @@ export class MainView extends React.Component { // Highlight non-edges using a bounding box this._selectedMeshes.push(selectedMesh); - // Create and add bounding box - const geometry = new THREE.BoxGeometry(1, 1, 1); - const material = new THREE.LineBasicMaterial({ - color: BOUNDING_BOX_COLOR, - depthTest: false - }); - const boundingBox = new THREE.LineSegments( - new THREE.EdgesGeometry(geometry), - material - ); - boundingBox.name = SELECTION_BOUNDING_BOX; - - // Set the bounding box size and position - const bbox = new THREE.Box3().setFromObject(selectedMesh); - const size = new THREE.Vector3(); - bbox.getSize(size); - boundingBox.scale.copy(size); + const parentGroup = this._meshGroup?.getObjectByName( + selectedMesh.name + )?.parent; + const boundingBox = parentGroup?.getObjectByName( + SELECTION_BOUNDING_BOX + ) as THREE.Mesh; - const center = new THREE.Vector3(); - bbox.getCenter(center); - boundingBox.position.copy(center); - - this._meshGroup?.add(boundingBox); + if (boundingBox) { + boundingBox.visible = true; + } } } }