Raycast with a screenspace/pixel unit threshold?

The Raycaster threshold is in worldspace. It would be nice if Raycaster had an option to specify whether to use screenspace or pixel distance for the threshold, or a utility to make it easy, but this doesn’t exist.

This would especially useful for

  • precise interaction no matter how close or far the objects are. For example, no matter when you place a “button”, you do not want it to show a hover effect when the pointer is not actually over it.
  • Or for example, for screenspace-sized objects such as lines and points (with size attenuation turned off) we want a certain precision no matter how far the lines/points actually are in world space.

Is this feature acceptable to contribute in a PR?

In the meantime, here are some ideas to approximate it:

  • Set the threshold to a reasonable world-space unit based on the distance of the camera to its orbit point (for example, when using OrbitControls). Or use some other huristic (?). When you know the reasonable distance, you can determine what threshold to use assuming most objects are approximately in that distance.
  • Set it to a really small number, but in this case ensure you do not wish to select infinitely thin objects (lines, points).
  • Or drop Raycaster and instead implement “picking” based on the render output pixels (but there does not seem to be anything in Three’s API to make this easy out of the box, as it is with Raycaster).

That brings up one more question. Could a generic “Picker” class be created for Three, which would encapsulate all the steps needed to pick objects using render output pixels?

Oh, here’s an idea.

  • Intersections already have a distanceToRay property.
  • Maybe a screenspaceDistanceToRay property could be added (if a Raycaster option is enabled)
  • Then we can use a bigger threshold (world space), and ignore objects that are beyond a desired screenspaceDistanceToRay.

I haven’t checked if setting a smaller threshold improves performance, and setting a bigger value decreases it, but at least that would be useful when the performance is within your limits.