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 4e7a32a4..4b0545a9 100644 --- a/packages/base/src/3dview/mainview.tsx +++ b/packages/base/src/3dview/mainview.tsx @@ -1032,11 +1032,10 @@ export class MainView extends React.Component { selectedMesh.material.color = originalColor; } - const groupBoundingBox = this._meshGroup?.getObjectByName( - SELECTION_BOUNDING_BOX - ); - if (groupBoundingBox) { - this._meshGroup?.remove(groupBoundingBox); + const parentGroup = this._meshGroup?.getObjectByName(selectedMesh.name)?.parent + const boundingBox = parentGroup?.getObjectByName(SELECTION_BOUNDING_BOX) as THREE.Mesh; + if (boundingBox) { + boundingBox.visible = false; } const material = selectedMesh.material as THREE.Material & { @@ -1074,40 +1073,21 @@ export class MainView extends React.Component { if (selectedMesh?.material?.color) { selectedMesh.material.color = BOUNDING_BOX_COLOR; } - - const material = selectedMesh.material as THREE.Material & { - linewidth?: number; - }; + + const material = selectedMesh.material as THREE.Material & { linewidth?: number }; if (material?.linewidth) { material.linewidth = SELECTED_LINEWIDTH; } } else { // 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 center = new THREE.Vector3(); - bbox.getCenter(center); - boundingBox.position.copy(center); - - this._meshGroup?.add(boundingBox); + + const parentGroup = this._meshGroup?.getObjectByName(selectedMesh.name)?.parent + const boundingBox = parentGroup?.getObjectByName(SELECTION_BOUNDING_BOX) as THREE.Mesh; + + if (boundingBox) { + boundingBox.visible = true; + } } } }