Get the intersecting object from a raycast that is closest to a vector that extends from the from center of the frustrum?

I have a ThreeJS app that lets users “paint” surfaces in a room, either a wall, ceiling, or floor panel. What I’ve noticed is that the raycaster doesn’t seem to favor the object that is closest to a vector extended from dead center of the frustrum (i.e. - what the user is “focused” on, line of sight from the camera…

Instead, it appears to order the objects by whatever objects intersect the frustrum, then sub-ordered by closest to the camera. I say this because if I am far from a wall, even if the camera is pointed dead at that wal, the raycast operation will return the floor panel just “in front of my feet” first in the intersecting objects list, instead of the wall I’m looking dead at.

How can I get the object that is intersected by the vector that extends from dead center of the frustrum, and not intersecting the edges of the frustrum, closest to the camera?

  • Is there a way to tell the raycaster to order the returned objects in my desired order?

  • If not, is there a mathematical operation I can perform between the camera and each object in the intersecting objects list to determine that for myself? If so, is there an existing ThreeJS function that does that (and what is its name and what object is attached to?) Or if not, where can I find a code snippet to do that?

Hi @MeJayEss

I think you can use something like this…

const dir = new THREE.Vector3()
raycaster.setFromCamera( dir, camera)

This should send a Ray from the dead center of the camera, along the forward vector of the camera out into the scene.