mrdoob / three.js

JavaScript 3D Library.
https://threejs.org/
MIT License
103.03k stars 35.41k forks source link

Return one intersection in `Raycaster.intersectObject` #22789

Closed keaukraine closed 3 years ago

keaukraine commented 3 years ago

For fast rendering of lots of 3D objects, we batched geometries into single large objects.

These objects could have a lot of overlapping geometries. And when we raycast them, we can get up to 20k intersections which is very, very slow (raycasting can be as slow as 30 seconds and in some extreme cases even more).

We actually need only one intersection (and even not necessarily the closest to camera since we use top-down view).

For our needs, it will be useful to have a parameter for Raycaster's intersectObject method which will tell it to return only the first encountered intersection.

Please add this feature, or explain a possible workaround.

Mugen87 commented 3 years ago

In order to find out the closest intersection, you have to test all objects and their entire geometry data. You can then compute the distance from the raycaster's origin to the intersection point which is then be used to sort all found intersection points.

I suggest you use a spatial index like https://github.com/gkjohnson/three-mesh-bvh to speed up your intersection tests.