I know it is possible to get pixel/uv coordinates from a raycasthit, but is there a way to find the world space position of a specific texture coordinate?
It is possible, but it doesn’t produce a clear result. This is because a Texture (or the texture space in general) is defined per triangle. It’s not guaranteed that each area of the texture is mapped to actual geometry. On the other hand it’s also possible that the same area / pixel of the texture is mapped to multiple areas on a mesh. So for a single uv coordinate you can get 0 to (theoretically) infinity worldspace coordinates.
The best example is the Unity-default-cube. Each face is mapped to the same area (the whole texture). So a certain uv / pixel coordinate would translate to 6 different worldspace positions for this mesh.
However an easy way to transform a uv coordinate of a triangle into local space (and then into world space) is to convert the uv position into barycentric coordinates. This way it’s very easy to get the local space coordinates.
Here’s a helper function i’ve written some time ago that converts a uv coordinate into it’s barycentric representation of the 3 triangle corners:
//C#
Vector3 GetBarycentric (Vector2 v1,Vector2 v2,Vector2 v3,Vector2 p)
{
Vector3 B = new Vector3();
B.x = ((v2.y - v3.y)*(p.x-v3.x) + (v3.x - v2.x)*(p.y - v3.y)) /
((v2.y-v3.y)*(v1.x-v3.x) + (v3.x-v2.x)*(v1.y -v3.y));
B.y = ((v3.y - v1.y)*(p.x-v3.x) + (v1.x - v3.x)*(p.y - v3.y)) /
((v3.y-v1.y)*(v2.x-v3.x) + (v1.x-v3.x)*(v2.y -v3.y));
B.z = 1 - B.x - B.y;
return B;
}
This function can be used to test the barycentric coordinate if the point is inside the triangle:
bool InTriangle(Vector3 barycentric)
{
return (barycentric.x >= 0.0f) && (barycentric.x <= 1.0f)
&& (barycentric.y >= 0.0f) && (barycentric.y <= 1.0f)
&& (barycentric.z >= 0.0f); //(barycentric.z <= 1.0f)
}
I used this and it worked for me: http://answers.unity3d.com/questions/372047/find-world-position-of-a-texture2d.html.