Is there a good way to get uv data from a raycast hit point?

Godot Version

Godot Mono 4.7

Question

Hi all,

Just wondering if anyone knows a good way to get UV data from a Raycast3D hit?

I have tried using the MeshDataTool to get the data from the collision Shape3D, but the collision mesh doesn’t contain any of those channels and I’m a bit stuck.

Any help would be great :slight_smile:

Thanks,

Pete

Assuming the collider shape is a convex mesh.
Get the face index from the raycast, get triangle vertex positions from that. Using those positions search for the vertices using MeshDataTool and get their UVs. Get barycentric coordinates of the hitpoint within the triangle using Geometry3D::get_triangle_barycentric_coords(). And finally, do the barycentric interpolation of corresponding UVs.

Searching for vertices by position via MeshDataTool could get expensive. If you need to do it often, pre-build an acceleration data structure that maps collider triangles to corresponding mesh triangles, for quick lookup.

Hey thanks! :slight_smile:

I could probably even do some of the calculations on importing the mesh. Anyway, That gives me a good start.

Yes, do as much as you can, as soon as possible in the pipeline.