RayCast3D get surface material of mesh

Godot Version

Godot 4.3

Question

I know this question has been asked many, many times before, but i didn’t found a workable solution to the problem…

I want to get the surface material of a mesh, from a RayCast3D. Getting the MeshInstance and the mesh reference is not a big deal, but continuing further seems either impossible or being very costly for what i have in mind.

I can’t simply assume to get surface material 0 all the time, since my meshes can have multiple surfaces.

On my research i found a possible working solution, but it involves to iterate through every vertice of the mesh, checking if thats the one where the raycast hit on, and then pulling the material information from there.

Though i fear this might be very unsuitable since i can potentially have rather large meshes and iterating through the whole thing every step seems to be not a good idea.

So… does anybody ever found a suitable solution to this?