Sample Depth Buffer by Clicking on Screen

Godot Version

4.3.5 dev build


I’m working on an editor plugin that needs to be able to click on objects in the 3D viewport and determine what is the nearest mesh/vertex to the location that was clicked.

I want to avoid having to do a raycast, because that will require colliders - I want this plugin to work with any general purpose mesh.

Is it possible to sample the depth buffer at the location that was clicked in screen space and reverse engineer what mesh face is at that screen space x, y position and depth?

For example, imagine you click at the location of the red X in the editor 3D viewport:

With the mesh instance locked, we can call _input to get the x,y position that was clicked.

My next step would be to attempt to sample the depth buffer at that location. Possible? Or no?