I have created Object3D and Added four cube geometry inside it and calculated the bounding box for Object3D which holds four cube objects and use BoxHelper to see if the bounding box is working it is working. now I want all corners(i.e XYZ, coordinates of corner) of the bounding box.
Basically, I am trying to achieve explode and implode effect where my four cubes reach four corners of the bounding box from an initial position of the center of the bounding box.
var objectContainer = new THREE.Object3D();
scene.add(objectContainer);
var objectAGeom = new THREE.BoxGeometry(1,1,1);
objectAMesh = new THREE.Mesh(objectAGeom, objectAMaterial);
scene.add(objectAMesh);
objectContainer.add(objectAMesh);
var objectBGeom = new THREE.BoxGeometry(1,1,1);
var objectBMesh = new THREE.Mesh(objectBGeom, objectBMaterial);
scene.add(objectBMesh);
objectContainer.add(objectBMesh);
var objectCGeom = new THREE.BoxGeometry(1,1,1);
var objectCMesh = new THREE.Mesh(objectCGeom, objectCMaterial);
scene.add(objectCMesh);
objectContainer.add(objectCMesh);
var objectDGeom = new THREE.BoxGeometry(1,1,1);
var objectDMesh = new THREE.Mesh(objectDGeom, objectDMaterial);
scene.add(objectDMesh);
objectContainer.add(objectDMesh);`
helper = new THREE.BoxHelper(objectContainer);
helper.material.color.set(0xbbddff);
scene.add(helper);
boundingBox = new THREE.Box3();
boundingBox.setFromObject(objectContainer);`
For starters, adding a mesh to the scene, and then immediately adding it to an Object3D
makes the first command useless because a mesh can only have one parent at a time.
scene.add(objectAMesh); // This does nothing...
objectContainer.add(objectAMesh); // Because you're immediately adding it here
ObjectContainer is already in the scene, so you don't need to add the Meshes to the scene two separate ways.
Secondly, you need to use a THREE.Box3
to get the bounding box, which you're already doing. This will give you the .min
and .max
values of the BoundingBox. You can use this to extrapolate all 8 corners:
boundingBox = new THREE.Box3();
boundingBox.setFromObject(objectContainer);
const low = boundingBox.min;
const high = boundingBox.max;
const corner1 = new THREE.Vector3(low.x, low.y, low.z);
const corner2 = new THREE.Vector3(high.x, low.y, low.z);
const corner3 = new THREE.Vector3(low.x, high.y, low.z);
const corner4 = new THREE.Vector3(low.x, low.y, high.z);
const corner5 = new THREE.Vector3(high.x, high.y, low.z);
const corner6 = new THREE.Vector3(high.x, low.y, high.z);
const corner7 = new THREE.Vector3(low.x, high.y, high.z);
const corner8 = new THREE.Vector3(high.x, high.y, high.z);