# 3D position of an arbitrary UV coordinate

I have a UV map for a 3D mesh that encodes "links" between pairs of UV coordinates. I have previously defined this links (or pairs), one to one. The links are enconded using the function rgb_color(u1, v1) = (u2, v2, 0.0). Therefore, in the fragment shader, given UV coordinates of the current fragment, I can get the UV coordinates of the fragment "linked" with the current one.

What I want to do is to draw this links in the 3D space(i.e. draw a line between the current fragment with texture coordinates (u1, v1) and its "link" texture coordinate (u2,v2). As far as I know this is a hard problem to solve because in the fragment shader you cannot access to the 3D coordinates (u2,v2).

Edit: Notice that the UV map is fragmented, therefore you cannot just draw this lines in the UV domain.

Do you have any idea how to approach this problem?

• You can't draw lines in fragment shader, or technically you can using UAV's but I don't think you want to go there. Draw line primitives instead and implement a vertex shader which maps the vertices properly. – JarkkoL Sep 14 '14 at 1:47
• @JarkkoL Thanks for your reply. I probably need to rephrase it, but actually my question is still the same: how can I access to an arbitrary UV coordinate associate 3D position? – Dan Sep 14 '14 at 2:54
• I'm sorry but your question doesn't make sense. You said you wanted to draw lines between mappings defined in a texture, and now you say you want to sample arbitrary UV coordinates with 3D position. Try to clarify what you want to do exactly. – JarkkoL Sep 14 '14 at 3:00
• @JarkkoL Well having both the current UV coordinate and 3D Vertex,I can match the "associate" UV using the mapping mentioned in my question. Now I need to convert this UV in 3D space. I need to solve this UV to 3D conversion if I want to draw a line. – Dan Sep 14 '14 at 4:49
• Ah ok, I though you wanted to draw the line in UV-space, not 3D. What you say is not possible because you don't have inverse mapping from uv-coordinates to 3D. Could be that for given uv-coordinate you don't even have the mapping. So you have to either add the 3D coordinate to the mapping or instead of storing UV-coordinate, store the vertex index where you can fetch the information. – JarkkoL Sep 14 '14 at 13:50

I think that it is impossible to solve your problem just by using a pair of tricky fragment and vertex shaders. That is why: You select a set of UV points in 2D space. Then, using the function you have defined, you can get another "linked" set of UV points. But you still have to get somehow 3D coordinates from the surface UV coordinates. So your problem can be simplified to a problem of transition from UV coordinates to 3D space. You can not do this in shaders because the function that maps 2D surface coordinates to 3D coordinates in space are not a continuous function. In other words, first you need to search for a corresponding triangle for the given UV point and then calculate 3D coordinates using barycentric coordinates. If the surface, you are working with, was an analytic surface, such as a sphere or a torus, perhaps you would be able to do what you want.

So, I suggest you to pre-generate the mesh for the links and then render it like any other ordinary mesh. The steps, listed below, is my point of view how to do so:

1. Generate two sets of points in surface UV coordinates.
2. Find a corresponding triangle (in surface coordinates) for every point. The point should belong to this triangle in the UV coordinate space.

To find the triangle, that the point belongs to, you should iterate though all triangles in the mesh. Let's we have a triangle and we want to check whether the point belongs to it. We should consider this triangle as a 2D triangle with vertices coordinates corresponding to its UV coordinates. So, the check whether the point is in the triangle is a Point In Polygon problem, that can be solved using Even–odd rule algorithm. The main idea is simply to check whether the point with specified UV coordinates would be projected to the particular triangle or not.

3. Find 3D coordinates of every point using barycentric coordinates. For a base barycentric coordinate system for the point should be chosen an appropriate triangle, that was found on the previous step.

4. Generate a mesh of 3D lines.

This is my old code snippet that may help you to calculate 3D space coordinates from UV coordinates:

vector2D tUV;    // uv coordinates of the triangle vertices
vector3D t3D;    // 3d space coordinates of the triangle vertices
vector2D pUV;       // uv coordinates of the given point

double T;
double iT;

T = tUV.u - tUV.u;  T = tUV.u - tUV.u;
T = tUV.v - tUV.v;  T = tUV.v - tUV.v;

double d = T * T - T * T;

iT =  T / d;   iT = -T / d;
iT = -T / d;    iT =  T / d;

double lambda0 = iT * (pUV.u - tUV.u) + iT * (pUV.v - tUV.v);
double lambda1 = iT * (pUV.u - tUV.u) + iT * (pUV.v - tUV.v);
double lambda2 = 1.0 - lambda0 - lambda1;

double x = t3D.x * lambda0 + t3D.x * lambda1 + t3D.x * lambda2;
double y = t3D.y * lambda0 + t3D.y * lambda1 + t3D.y * lambda2;
double z = t3D.z * lambda0 + t3D.z * lambda1 + t3D.z * lambda2;

• Thanks for your detailed answer! The idea of pre-generating a mesh with the links sounds good! I was awere of the barycentric approach for UV to 3D, but I could not figure out how to find to what triangle a UV point belongs... and I still don't see it! Do you have any suggestion? Could you please elaborate more on point (2) or point me to relevant links? Thanks a lot! – Dan Sep 14 '14 at 17:19
• I have added more information on point (2). Not for that! – Podgorskiy Sep 14 '14 at 19:20
• I am almost done with this! I am now doing the barycentric interpolation, but I´ve just realized that your code has some errors... if T is declared as double T, how can you then access to T (and many other access out of range)? Thanks! – Dan Sep 26 '14 at 5:39

I am also looking for some code to make this conversion but..Only God knows why the code above is using that array indexes lol...

Maybe he meant something like this? (in case other people wants to use the code too)

  PVector [] tUV = new PVector;    // 2d uv coordinates of the triangle vertices
PVector [] t3D = new PVector;    // 3d space coordinates of the triangle vertices
PVector pUV = new PVector();        // 2d uv coordinates of the given point

double [][]T = new double;
double [][]iT = new double;

T = tUV.x - tUV.x;
T = tUV.x - tUV.x;
T = tUV.y - tUV.y;
T = tUV.y - tUV.y;

double d = T * T - T * T;

iT =  T / d;
iT = -T / d;
iT = -T / d;
iT =  T / d;

double lambda0 = iT * (pUV.x - tUV.x) + iT * (pUV.y - tUV.y);
double lambda1 = iT * (pUV.x - tUV.x) + iT * (pUV.y - tUV.y);
double lambda2 = 1.0 - lambda0 - lambda1;

double x = t3D.x * lambda0 + t3D.x * lambda1 + t3D.x * lambda2;
double y = t3D.y * lambda0 + t3D.y * lambda1 + t3D.y * lambda2;
double z = t3D.z * lambda0 + t3D.z * lambda1 + t3D.z * lambda2;