I want to calculate the corresponding position in 3d space(world space), for a position from uv space.
EDIT:
I want to find the world space position corresponding to a point in uv space( not screen space ) for an arbitrary geometry. I’m considering the simplest case, when there’s only one point corresponding to a point in uvspace(ie no overlapping uvs).
For example:
I have a texture that’s entirely black except for a single pixel that’s white. I apply this texture on a geometry i created in a 3d modeling package(Maya, 3ds max etc ), that has the UVs unfolded with no tiling and no overlaping. How can i calculate the coordinates in 3d space for the point on the geometry, where the white pixel is displayed ?
There’s no built-in function for this, because in the general case (not the specific example you’re giving):
Any one UV coordinate could be mapped to multiple polygons on a single mesh. (So there could be multiple object-space coordinates for a single UV coordinate)
There could be multiple models using that mesh in the scene. (So there could be multiple world-space coordinates for a single object-space coordinate)
I think the only way you can get this information is to use the Mesh class. You would need to iterate over every triangle in the mesh (or just the submesh(es) you need), and check whether the triangle formed by its vertices’ UV coordinates contains the UV point in which you’re interested.
Once you find a triangle that contains the point, you can find its position in object space by interpolating the vertices’ spatial coordinates, using the barycentric position of the UV coordinate within the UV triangle.
Then you’d transform it using the GameObject’s Transform to finally get it into world space.
Note that this doesn’t take animation into account.