Skip to content

Commit 5da75de

Browse files
arjxn-pytrungleduc
andauthored
Refactor Bounding Box Logic (#555)
* Refactor bounding box logic * Update packages/base/src/3dview/mainview.tsx Co-authored-by: Duc Trung Le <[email protected]> --------- Co-authored-by: Duc Trung Le <[email protected]>
1 parent 5c50fe4 commit 5da75de

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

packages/base/src/3dview/helpers.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,21 @@ export function buildShape(options: {
251251
edgeIdx++;
252252
}
253253

254+
const bbox = new THREE.Box3().setFromObject(mainMesh);
255+
const size = new THREE.Vector3();
256+
bbox.getSize(size);
257+
const center = new THREE.Vector3();
258+
bbox.getCenter(center);
259+
260+
const boundingBox = new THREE.LineSegments(
261+
new THREE.EdgesGeometry(new THREE.BoxGeometry(size.x, size.y, size.z)),
262+
new THREE.LineBasicMaterial({ color: BOUNDING_BOX_COLOR, depthTest: false })
263+
);
264+
boundingBox.position.copy(center);
265+
boundingBox.visible = false;
266+
boundingBox.name = SELECTION_BOUNDING_BOX;
267+
meshGroup.add(boundingBox);
268+
254269
meshGroup.add(mainMesh);
255270

256271
return { meshGroup, mainMesh, edgesMeshes };

packages/base/src/3dview/mainview.tsx

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,11 +1032,14 @@ export class MainView extends React.Component<IProps, IStates> {
10321032
selectedMesh.material.color = originalColor;
10331033
}
10341034

1035-
const groupBoundingBox = this._meshGroup?.getObjectByName(
1035+
const parentGroup = this._meshGroup?.getObjectByName(
1036+
selectedMesh.name
1037+
)?.parent;
1038+
const boundingBox = parentGroup?.getObjectByName(
10361039
SELECTION_BOUNDING_BOX
1037-
);
1038-
if (groupBoundingBox) {
1039-
this._meshGroup?.remove(groupBoundingBox);
1040+
) as THREE.Mesh;
1041+
if (boundingBox) {
1042+
boundingBox.visible = false;
10401043
}
10411044

10421045
const material = selectedMesh.material as THREE.Material & {
@@ -1058,6 +1061,10 @@ export class MainView extends React.Component<IProps, IStates> {
10581061
continue;
10591062
}
10601063

1064+
if (!selectedMesh.visible) {
1065+
continue;
1066+
}
1067+
10611068
if (selectedMesh.name.startsWith('edge')) {
10621069
// Highlight edges using the old method
10631070
if (!selectedMesh.userData.originalColor) {
@@ -1080,29 +1087,16 @@ export class MainView extends React.Component<IProps, IStates> {
10801087
// Highlight non-edges using a bounding box
10811088
this._selectedMeshes.push(selectedMesh);
10821089

1083-
// Create and add bounding box
1084-
const geometry = new THREE.BoxGeometry(1, 1, 1);
1085-
const material = new THREE.LineBasicMaterial({
1086-
color: BOUNDING_BOX_COLOR,
1087-
depthTest: false
1088-
});
1089-
const boundingBox = new THREE.LineSegments(
1090-
new THREE.EdgesGeometry(geometry),
1091-
material
1092-
);
1093-
boundingBox.name = SELECTION_BOUNDING_BOX;
1094-
1095-
// Set the bounding box size and position
1096-
const bbox = new THREE.Box3().setFromObject(selectedMesh);
1097-
const size = new THREE.Vector3();
1098-
bbox.getSize(size);
1099-
boundingBox.scale.copy(size);
1090+
const parentGroup = this._meshGroup?.getObjectByName(
1091+
selectedMesh.name
1092+
)?.parent;
1093+
const boundingBox = parentGroup?.getObjectByName(
1094+
SELECTION_BOUNDING_BOX
1095+
) as THREE.Mesh;
11001096

1101-
const center = new THREE.Vector3();
1102-
bbox.getCenter(center);
1103-
boundingBox.position.copy(center);
1104-
1105-
this._meshGroup?.add(boundingBox);
1097+
if (boundingBox) {
1098+
boundingBox.visible = true;
1099+
}
11061100
}
11071101
}
11081102
}

0 commit comments

Comments
 (0)