Can Unity's raycasting determine the point of collision?

Given a 3D object with ~50K triangles, I’d like to be able to cast a ray and determine the point of intersection. In the end, I would like to be able to convert the point of intersection to a bitmap that overlays the object, allowing me to change the color of the specific point of intersection and nearby points up to a given radius.

To do this, though, I think I need to be able to determine the point where the raycasting intersects the object and then convert that to a coordinate on the surface bitmap.

From my reading, Unity looks like the way to go but I’d like to know if this is possible before I start down a new path.

Can Unity do what I need?

Thanks!
– Steve G.

1 Answer

1

Many forms of Physics.Raycast() use a RaycastHit structure. That structure provides the ‘point’ of intersection in 3D space. You can convert this position to a local coordinate and cycle through the vertices of the mesh to find points without some specified distance of the hit. If your shader supports UV colors, you can change the color of the triangles associated with these vertices using the ‘colors’ array in the mesh.

The RaycastHit also provides the ‘triangleIndex’ of the triangle hit. From this index, it is easy and efficient to get the three vertices of the triangle. It doesn’t solve your problem directly, but it is a start for some alternate approaches.

Finally the RaycastHit gives you a ‘textureCoord’. For some object (especially if you can avoid seams), you can use the textureCoord to modify the texture rather than deal with the vertices.