What I need is to get the texture's pixel color where the ray hit. I need it so I can check if color = blabla, then do something. Seeing as rays hit colliders and not meshes, I don't know to get pixel color from a mesh texture. I know there is Texture2D.GetPixel, but I don't know how to get it from 3d into 2d and to the right pixel.
asked May 29 '11 at 10:53 AM
Manually reverted to 1st revision:
I am unaware of any such built in functionality, though I wouldn't be surprised if anyone made and shared a similar tool already. I'd check google if you haven't. But basically you'd need to perform a test vs every triangle to get the barycentric coordinates of intersection between the triangle and the ray. Then, you can interpolate the triangle UVs using the barycentric coordinates to get the final UV coordinate for your hit test.
When you have the UVs, you can rescale the UVs to texture pixel space. That is, the UVs are usually in the 0 to 1 range where textures are like 0 to 255 (256 pixels). Note that some models may repeat the texture so it's possible to get an UV outside of the range 0 to 1, like -1 or 2.34 or -502.2.
That said, I wouldn't be surprised if there is any built in, premade or easier solution. This is just what came to my mind.