The geometry.boundingSphere
property is a representation of the geometry itself. It's important to note that when working with multiple meshes, each scaled differently, sharing the same geometry, it's crucial to maintain the original bounding sphere of the geometry and then calculate individual bounding spheres for each mesh.
An issue that arises when scaling the bounding sphere is the potential for unequal scaling along the x, y, and z axes, or even negative values which could distort the shape into more of a spheroid than a sphere, complicating mathematical calculations.
To address this, recalculating the bounding sphere based on the updated world transformation matrix of the mesh is recommended. Using the world matrix is advised as ancestral influences on scale can affect the final result unpredictably.
// Given:
// (THREE.Mesh) yourMesh
// Clone the geometry
let geoClone = yourMesh.geometry.clone() // Though memory-intensive, it simplifies the process
// Apply the world transformation to the clone (updating vertex positions)
geoClone.applyMatrix4( yourMesh.matrixWorld )
// Convert vertices into Vector3s (memory intensive)
let vertices = []
let pos = geoClone.attributes.position.array
for( let i = 0, l = pos.length; i < l; i += 3 ){
vertices.push( new THREE.Vector3( pos[i], pos[i+1], pos[i+2] ) )
}
// Create and assign the mesh's bounding sphere
yourMesh.userData.boundingSphereWorld = new THREE.Sphere()
yourMesh.userData.boundingSphereWorld.setFromPoints( vertices )
This process establishes a world-aligned bounding sphere specific to your mesh. If you require a sphere based on local transformations, a similar approach using the local yourMesh.matrix
can be applied. Be aware that in this case, the center of the sphere will align with the mesh's local transformation/rotation, not just its scale.