UV to XYZ (3D Triangle / Face)

Hello

Could you please help me with the following?
Basically, I need to find the coordinates in space (X, Y, Z) of any POINT belonging to a 3D triangle, given that POINTS’s two dimensional (U, V) coords.

The data I have is:

  • three sets of (X, Y, Z) coordinates, one for each triangle’s vertex
  • three sets of corresponding (U, V) coordinates, one for each triangle’s vertex

Example data:
v1 = (0, 0, 0); uv1 = (0, 0)
v2 = (10, 0, 0); uv2 = (1, 0)
v3 = (10, 10, -10); uv3 = (1, 1)

I need to find:

  • the (X, Y, Z) coordinates of any point belonging to that triangle given the UV pair.

For example, given the
UV = (1, 0.5)
in this case the result should be:
XYZ = (10, 5, -5)

I will really appreciate if you find any solution for this problem.
It sounds pretty easy and obvious, however I haven’t managed to find any formula for this for over a week.

Thank you in advance.

While I believe it is possible to calculate such a point for certain meshes (i.e. meshes that do not have repeated uv anywhere), I can’t think of any situation where you’d have a uv coordinate, but not a position. Perhaps is you explain why you need this, someone might know an easier way of solving your problem.

If such an easier route does not exist, the solution won’t be easy as you seem to expect. I don’t see any other option than to inspect every single triangle in the mesh to see if the uv is inside of it, and then determine the exact position within that triangle.

Thank you for your reply, I’ll try to explain it in more details.

WHAT I NEED IT FOR
I was thinking about for example meshes that generate light according to their texture color. So for example black pixel generates no light and white pixel does. To achieve it I wanted to loop through all the pixels in the mesh’s texture and generate a light beam for every single one of them. But to do it, I need to know the world position of each pixel (and the normal of the face it belongs to, but this is already done). Also, finding a solution for my problem would help with casting shadows relative to the light position or generating reflections.

THE GOOD THING:
The good thing is that I’ve already managed to find a way to determine to which face a pixel belongs (no matter what the mesh). So there’s no need to analyze the whole mesh. It’s just one single face to examine.

THE POINT I’M STUCK AT:
I’m stuck only when I know exactly:

  • to which face the pixel belongs
  • the (X, Y, Z) coords of all three vertices of the face
  • the (U, V) coords of all the three vertices of the face
  • the (U, V) coords of the pixel I want to transform into (X, Y, Z)
  • and I can be sure that the (U, V) set i pass to my method has its 3D point (X, Y, Z) representation in bounds of this very face

The only thing I need to do now is to convert a (U, V) set to (X, Y, Z) set for a single face, but not the entire mesh. I’m even pretty sure one could find a proportional relation between the (X, Y, Z) and (U, V). This is because you can treat this situation as having two equal triangles, one having (X, Y, Z) coords for its vertices and the other one, looking exactly the same but having (U, V, 1) instead of (X, Y, Z). Then we could probably find a formula that converts the triangle’s (U, V) to (X, Y, Z).
Of course, each face would have its own, different formula.

Thank you once again for any help, I will really appreciate it.

My idea would be to use the following math (note: completely untested, may contain fundamental flaws, might have much simpler solution :P):

The uv and the xyz are both properties of three vertices A,B and C. We also have the point p, from which we know the uv but want to calculate the xyz. First we will only consider the uv triangle (which is 2-dimensional). We will construct a new (non-orthogonal) coordinate system for the uv triangle, the primary axis is AB.uv ( = B.uv - A.uv) and the secondary axis is AC.uv ( = C.uv - A.uv).

Now we will express the point p.uv as a sum of AB.uv and AC.uv:
p.uv = n * AB.uv + m * AC.uv
We need to find the coefficients n and m. While there may be a simpler solution, I’m sure you can find n and m by performing line-line intersection between the rays (origin: A, direction: AB) and (origin: p, direction: -AC). There are plenty of pages describing line-line intersection available through google.

Once you have n and m, calculating the corresponding position on the xyz triangle is simple:
p.xyz = n * AB.xyz + m * AC.xyz.

Again, I just made this up, so I can give no guarantees, as I may have overlooked something important.

Face (triangle) and vertex data are accessed through the Mesh class. If you already have a way to determine what face you are interested in, everything else is in the Mesh instance: mesh.triangles (array of triples of vertex index for each face); mesh.vertices (the [x,y,z] values for the three corners of each face); and mesh.uv (the UV coordinates of each vertex).

Once you have the UV coords for each vertex of the face, it’s trivial to interpolate the UV coordinate of any point within the face, since faces are triangles which, by definition, are convex.

Yeah, you’re right… it’s easier than I thought.
I found the following (not really complex, but maybe someone later will find it useful):
http://www.cc.gatech.edu/classes/AY2004/cs4451a_spring/lininter.pdf

I’m going to try the second method (“Alternative approach”). Will let you know if it worked.
Thank you once again.