I have a scene with hundreds of thousands of point objects and since I implemented a raycaster the performance tanked and it only improved by lowering the number of points. I just want to be able to intersect the objects near the camera, so I thought that by setting a small far value it wouldn’t have affected performance too much since it would just need to check a few dozen points, but instead it seems like the performance is the same independently of whether the far value is 10 or infinity
I’m probably missing something on how the raycaster actually works. Wouldn’t it be possible to “tell” the raycaster to ignore 99% of what’s in the scene (farther than a certain value)? Or even other ways to optimize it
The Raycaster itself does not account for distance or do any spatial filtering before performing the intersection checks. If you know there is a limited set of objects you’re interested in raycasting against it’s best to generate that filtered list yourself and pass that into
Raycaster.intersectObjects to improve performance.
The Raycaster.far and Raycaster.near field may be a bit misleading. It might even be worth removing them and recommending that users filter the list of results if they only want results in a certain range.
Ok thanks, is there a preset function to do such spatial filtering or I’d have to basically calculate the distance between all the objects and the camera and only include the closest ones?
You’ll have to implement it yourself. There’s no one “best” way to do this and it depends on your use case so it’s left to the user to choose the method.