Reputation: 159
i have a short question: I know how to calculate the boxes of my (imported) 3dobjects f.e.
var box = new THREE.Box3().setFromObject(obj);
With this i can compute boxes for my objects and i can merge them together if i want.
The problem is, now i have these 2 objects https://i.sstatic.net/dcjw2.jpg
The solution seems quite simple: i need to compute the left and right spherebox and put them together, but both of these 2 objects are imported with stlloader. I'm not sure how stlloader exactly works, (it seems for me like its all 1 huge mesh) so i'm not even sure if this is possible.
so my questions: 1. how can i compute a box with the shape of a sphere of my sphere object. 2. Is this even possible for my stl object? (I will try when i get the answer for question 1)
Edit: Question 1 should somehow be working with .computeBoundingSphere.. is there a way to make this visible?
Upvotes: 1
Views: 667
Reputation: 31076
- how can i compute a box with the shape of a sphere of my sphere object.
Well, in three.js
you have the choice between two bounding volumes. THREE.Box3
represents an axis-aligned bounding box (AABB) whereas THREE.Sphere
represents a bounding sphere. If you need a box with the shape of a sphere, use THREE.Sphere
.
- Is this even possible for my stl object?
The method setFromObject()
does only exist for THREE.Box3
. However, you can compute the bounding sphere via THREE.BufferGeometry.computeBoundingSphere()
. This sphere is defined in local space, however. You can use THREE.Sphere.applyMatrix4()
to transform it into world space by passing in the world matrix of the 3D object.
is there a way to make this visible?
There is no helper class for bounding spheres. But you can easily create a helper mesh based on THREE.SphereBufferGeometry
. Something like:
const geometry = new THREE.SphereBufferGeometry( boundingSphere.radius );
const material = new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } );
const mesh = new THREE.Mesh( geometry, material );
mesh.position.copy( boundingSphere.center );
scene.add( mesh );
three.js R109
Upvotes: 1