How to get the 3D position for the point with (0,0) UV coordinates?

Started by
14 comments, last by Stackmann0 5 years, 3 months ago

Hello, 

I have a 3D triangular mesh(vertices, indices, uv coords) that I'm rendering to the screen. Let's assume that the UV mapping is one-to-one. I'm trying to find a way to find the 3D position of the point with UV coordinates equal to (0,0). I searched the internet but I only find answers that I don't find convincing. 
The solution that I found: 

- Find, in UV space, the triangle that contains (0,0).. let's call it T 
- Calculate barycentric coordinates for (0,0) with respect to T 
- interpolate the 3D positions of T's vertices using barycentric coords to get the result. 

this seems wrong to me. Here's why: 
Let M be the mapping between 3D space and UV space that associates UV coords for every vertex. 
Let A,B and C be the vertices of T. 
Let P be the origin of UV space ( P = (0,0) ). 

We have P = alpha*A + beta*B + gamma*C (alpha,beta and gamma are the barycentric coords of P with respect to T). 
We assumed the UV mapping to be one-to-one, so let M° be the inverse of M. 
We have : 

M°(P) = M°(alpha*A + beta*B + gamma*C)


The solution in question assumes that M° is linear.. if that was the case you can have: 

M°(P) = alpha*M°(A) + beta*M°(B) + gamma*M°(C)


But that is not the case (correct me if I'm wrong). 

So is there a way to find the 3D position of a point with specific UV coords? 

Thanks in advance.

Advertisement
1 hour ago, Stackmann0 said:

- Find, in UV space, the triangle that contains (0,0).. let's call it T 
- Calculate barycentric coordinates for (0,0) with respect to T 
- interpolate the 3D positions of T's vertices using barycentric coords to get the result. 

This is essentially correct. The 3d position of a particular UV coordinate only makes sense in terms of the triangle (or triangles) that encompass that point. If you pick a UV coordinate outside of the triangles for instance, it doesn't really represent any point in 3d space.

How to get the 3D position for the point with (0,0) UV coordinates?

Anyway its quite advanced topic and i shouldnt answer that since i think its for some commercial product but its so easy that i cant resist

given x vertices for a face you compute min and max bounding rect (box)

both for uv and 3d position

knowing the min you pick any uv coord and make a vector from minuv to testuv

the length and direction correspond to to actual position...... 

It's depends of topology, triangulation, optimization and other. Do you really need it? No one need to do inverse UV calculations.

If you want, you can move through all tri's and find one with this texture coordinates as attribute. Then apply transform matrix.

1 hour ago, Makusik Fedakusik said:

No one need to do inverse UV calculations.

Well not quite true (I do inverse UV calculations in 3d paint in quite a few places for instance, it might also be used in lightmapping, ray tracing etc etc), but it is probably rarely used by developers relying on 3rd party engines and tools to do this sort of thing for them.

On 1/11/2019 at 6:38 PM, Stackmann0 said:

We assumed the UV mapping to be one-to-one, so let M° be the inverse of M.

To elaborate a little, if the OP is talking about the very specific case of using a uniform transform to get all 3d verts into uv space (such as matching a viewport transform, e.g. 'project from view' in blender), rather than the more usual uv unwrapping or complex projections, then in theory to go from UV space back to 3d space you would simply use the reverse transform (e.g. inverse matrix).

However in practice with a UV coordinate you have usually lost the 'depth' value (a degree of freedom I think this is called?), so even if you got back to the viewport x, y, you would have lost the depth information and thus the 3d position. I think also there might be issues once you are not using a simple orthographic projection (consider a fish eye lens and trying to extrapolate barycentric coordinates outside a central triangle, the relationship between uv and 3d space would break down I think). If you still had the 3d -> uv matrix and the uv depth value, then it may in some cases be possible to go directly back to a 3d vertex (don't quote me on that, I might be missing something obvious lol :) ). It is similar to any matrix transform in that respect.

Hey wat about my somution where you create a huge plane and place a bounding rect on it, now he just needs to tweak uv coords in order to find proper distances

Couldn't you just render the mesh's position into a texture (using UV-coordinates) and then do a lookup in that?


// vertex shader
gl_Position = vec4(2.0 * tcoord.x - 1.0, 2.0 * tcoord.y - 1.0, 0.0, 1.0);
worldPos = vec3(modelMatrix * vec4(pos, 1.0));

// fragment shader
color = worldPos

 

1 hour ago, l0calh05t said:

Couldn't you just render the mesh's position into a texture (using UV-coordinates) and then do a lookup in that?



// vertex shader
gl_Position = vec4(2.0 * tcoord.x - 1.0, 2.0 * tcoord.y - 1.0, 0.0, 1.0);
worldPos = vec3(modelMatrix * vec4(pos, 1.0));

// fragment shader
color = worldPos

 

You can do something like that, it is doing essentially exactly the same as the very first suggestion (using barycentric coordinates), except in an extremely roundabout fashion (going on a roundabout trip via the GPU).

Of course, it depends what the actual use case is, whether the conversion is rare or needed as a realtime lookup. There are many cases where having a UV -> 3d mapping for the entire texture is useful rather than e.g. using the barycentric method per point, and using the GPU is an option to create this. In my own use cases I've been fine using the CPU to calculate this conversion texture, however it you needed to e.g. recalculate it on a per frame basis the GPU might be an option, bearing in mind the potential for pipeline stalls if you have to read this back.

On 1/11/2019 at 8:47 PM, lawnjelly said:

This is essentially correct. The 3d position of a particular UV coordinate only makes sense in terms of the triangle (or triangles) that encompass that point. If you pick a UV coordinate outside of the triangles for instance, it doesn't really represent any point in 3d space.

I don't think that its correct. I mean, you can search for the triangle in UV space that has (x,y) (the point in uv space we're trying to find its 3d pos) inside it. But then, the fact of using barycentric coords of that point to find the 3D pos doesn't sound correct to me as the mapping between the triangles (the one in UV space and the one in 3D space) is not involved.. So, I don't know if there is a solution to this problem in the general case as you mentioned in your second reply.. or maybe I'm missing something or confusing things

12 hours ago, _WeirdCat_ said:

Hey wat about my somution where you create a huge plane and place a bounding rect on it, now he just needs to tweak uv coords in order to find proper distances

I had a little trouble conceptualizing this, I kind-of see where you are going but am not convinced it would work. You could try it out though, code it up and compare with the barycentric solution?

This topic is closed to new replies.

Advertisement