Closest face to mouse

If you always have some mesh a ray can collide with (like ground, which you seem to have):

  1. Create a collision box (Cube or Box3 with size that will determine your selection radius.)
  2. Ray-cast mouse location on the scene and move the collision box to where the collision happens.
  3. Check which models’ bounding boxes collide with the collision box.
  4. For each colliding model detect which faces overlap with the collision box (intersectsTriangle may also help), ignore faces which point in the opposite direction.

Otherwise, if you don’t have a ground mesh, and I understood correctly, you can’t really do what you are trying to do, since cursor has no depth - some faces that are visually next to the cursor in 2D can be far apart in 3D. One more thing you could do it cast more rays with some offset - create a Vector2 with (mouse.x + offset , mouse.y), duplicate it 8 times and rotate around the origin by 45°. Additional rays will then pick up objects that are nearby the mouse position, with some precision. If performance gets hit - you can do the collision testing on an offscreen canvas worker and return only the collisions to the main thread.

1 Like