Get all close by models within sphere radius

I would want a grass that is near to the player move. The easiest way would be to check all grass models and calculate distance for them, but since I have a lot of instantiated grass I was hoping there is some faster way to get nearby objects? Perhaps something like sphere check around player/camera or something like that, that would return all objects that are within the sphere/bounding box.
Is there anything like this in threejs, that returns nearby objects?

No. The three.js repository does not provide a solution for nearest neighbor queries. This is something you have to implement by yourself or by using a third-party plugin.

Notice that nearest neighbor queries is a complex topic and there is no solution that works best for all use cases. In certain scenarios, a very basic cell-space partitioning can be sufficient. In other cases, a more advanced hierarchical approach is required.

BTW: There is an Octree class in the repository but it works on triangles which is not what you want. You can’t use it to query nearby 3D objects.

Thanks, the cell-space solution seems like something I might try, if just basic looping through all existing models will be too heavy.

The thing with spatial indices is that they always come with a certain amount of overhead when updating the internal state and performing queries. The performance benefit from using nearest neighbor queries must be greater than its overhead (on average) otherwise you end up with a worse overall performance. Just want to leave this comment since I’ve seen apps which performed better by directly working with all objects.

If you want to be on the safe side, do performance measurements with and without nearest neighbor queries.

1 Like