gkjohnson / three-mesh-bvh

A BVH implementation to speed up raycasting and enable spatial queries against three.js meshes.
https://gkjohnson.github.io/three-mesh-bvh/example/bundle/raycast.html
MIT License
2.5k stars 260 forks source link

Compute bounds tree after adding mesh to group/scene? #662

Closed neilrackett closed 4 months ago

neilrackett commented 4 months ago

We're using BVH to boost the performance of a small number of features in a larger app, computing the bounds tree before adding the associated mesh to either a group or a scene. This works well, but adds time at startup for something that may never get used.

What I'd like to do is compute the bounds tree, either using a Worker or on-demand, after a mesh has been added to a group and/or scene, but find that running computeBoundsTree() or new MeshBVH(geom) after the associated mesh has been added to something else, even if it hasn't been added to the scene, the script just hangs. Forever.

So I just wanted to ask if this is actually possible?

Example

const geom = new THREE.TorusKnotBufferGeometry( 10, 3, 400, 100 );
const mesh = new THREE.Mesh( geom, material );
const group = new THREE.Group();

group.add(mesh);
geom.computeBoundsTree();

// It never gets here
gkjohnson commented 4 months ago

This should work just fine and have no impact on the generation time of the BVH itself. Do you have a live example using something like jsfiddle showing the issue?

neilrackett commented 4 months ago

I'll put a fiddle or similar together -- is there an existing template I can use that takes care of the boilerplate?

gkjohnson commented 4 months ago

This official three.js one is a reasonable one to start with:

https://jsfiddle.net/v98k6oze/

neilrackett commented 4 months ago

OK, so it works fine in a fiddle, so looks like the issue is somehow specific to our viewer / models.

I'll post an update here if I identify a real bug 😉