What would be the easiest way to find the closest vertex to an intersection point on a mesh (specifically a StaticMesh)?
I am trying to implement terrain modification, and need to know "where" on the mesh I should be operating.
Find Vertex From Raycast (on StaticMesh)?
Re: Find Vertex From Raycast (on StaticMesh)?
The StaticMesh.Mesh.Tree.GetOverlaps method offers a variety of ways to find the triangles whose bounding boxes overlap some query. The Ray override would do the trick. It fills a list of integers. The integers are the locations of triangles in the mesh's index buffer. For example, if the index buffer was {0, 1, 2, 2, 1, 3} there would be two triangles. If it hit the second triangle (represented by the indices 2,1,3), the integer in the list would be 3 corresponding to the location of the first element of the triangle.
The StaticMesh.Mesh.Data.GetTriangle method will convert a triangle index into three vertex positions.
The StaticMesh.RayCast uses the GetOverlaps internally to perform the raycast, so if you do a GetOverlaps call after a normal ray cast, it's doing a bit of redundant work. You could bypass this by creating a custom ray cast, though it would take more effort to implement and the amount of extra cost probably won't matter from a performance perspective.
The StaticMesh.Mesh.Data.GetTriangle method will convert a triangle index into three vertex positions.
The StaticMesh.RayCast uses the GetOverlaps internally to perform the raycast, so if you do a GetOverlaps call after a normal ray cast, it's doing a bit of redundant work. You could bypass this by creating a custom ray cast, though it would take more effort to implement and the amount of extra cost probably won't matter from a performance perspective.