Limiting raycast on multiple objects


I have a scene of instanced glTFs and I’m trying to limit the ray test/console.log to a single intersection test. I’ve logged the intersect that I’m testing as follows, but the ray still passes through all of the instances (I also tried this for three single loaded glTFs with the same result). As indicated in the documentation, I’ve set the recursive flag to false.

const intersects = raycaster.intersectObjects(scene.children, false);

Here is my JSFiddle of what I’m currently encountering:

This returns multiple Instance Ids of the instanced objects (move the camera to the end of the ray and click). The goal is to only return one Instance Id, no matter the camera angle the ray is cast from. Is my syntax simply wrong? I tried the following for just a single object, but then the test is undefined:

const intersects = raycaster.intersectObject(scene.children, false);

Thanks for your help!

The objects in the intersects array are sorted by distance. So if you pick the first one, you get the closest intersection. There is no way to prevent the ray from passing through other objects. Testing is required to determine whether an intersection happens or not.

BTW: The second parameter of intersectObjects() is unrelated to this issue. You normally want to set it to true if the objects you are testing have descendants.

Thank you very much. This helps with my understanding of this topic a lot!