Jump to content
  • Advertisement
Sign in to follow this  
ajas95

Convert UV coord to 3d position

This topic is 4097 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So, I can't seem to figure this one out for some reason. I'm sure the answer involves solving a simple determinant or something similar to moller-trumbore ray/tri intersection but in reverse! Anyway, say I have a triangle and the three verts have x,y,z position and u,v coords. Now say I have an input UV coordinate, how do I find the point in 3d space in the plane of the triangle for that UV coordinate, given the UV space defined by the verts? Thanks for any help.

Share this post


Link to post
Share on other sites
Advertisement
Okay, I think I know how to do this.

Roughly, first treat the UVs of the vertices as a 2d space, and find the barycentric coords of the input UV. This gives an output UV s.t.
v0.uv + v1.uv*outU + v2.uv*outV = inUV

then outUV can be applied to the vertices positions like:

outPos = v0.xyz + v1.xyz*outU + v2.xyz*outV

and that's that. So just need the 2d triangle barycentric calculation.

Share this post


Link to post
Share on other sites
You're on the right track. What you would do is find the barycentric coordinates of the input point in world space (or whatever other space the triangle is in), and then apply those coordinates to texture space.

The trick is to translate the triangle so that one of the vertices (call it v0) is at the origin. We're allowed to do this because the barycentric coordinates are translationally invariant. This gives you the vectors (v1 - v0) and (v2 - v0) for two edges of the triangle (enough to define a planar subspace), and the input point (p - v0). Now let's plug (v1 - v0) and (v2 - v0) into the columns of a 3x2 matrix called 'A', and rename (p - v0) to 'b'. We can now use the handy projection formula (ATA)-1ATb, which will return a 2x1 vector giving us the barycentric coordinates for v1 and v2. The coordinate for v0 is just 1 - v1 - v2. Note that since 'A' is a tiny matrix, you can expand the math for the matrix multiplication and inverse fairly easily into a bunch of dot products.

You can then just use those same barycentric coordinates in UV space to determine the texture coordinate.

Share this post


Link to post
Share on other sites
if your texture coordinates (u,v) are unique over the surface of your object, you can do something like this. Render your 3d object, and in the vertex shader output the texture coordinates as the position (they will be between 0 to 1, put them in clip-space: -1 to 1). Output the actual 3D position of the vertex as texture coordinates so that they get interpolated across the fragments. And thats it. you will have all the in-between 3D positions.

Of course, this is assuming that you are trying to do something like wrapping a texture around your object that stores 3D positions at each pixel, which is what most applications are trying to do (or something similar). However if your purpose is something else, I guess you'll have to do the interpolation manually.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!