Reputation: 932
Say that I have a quad, and I am applying a pool table texture to it. I know the UV coordinates for the first three vertices (highlighted in blue) but not the fourth.
For example in the image above, I know that the top left coordinates are [0, 0], the top right coordinates are [1, 0] and the bottom left are [0, 1]. How can I mathematically determine the bottom right UV coordinates are [1, 1]? I want to figure out what the fourth set of UV coordinates will be mathematically, such that the texture displayed in the triangle I do know the UV coordinates of will fit in the quad.
I need to be able to handle complex situations. The UV coordinates and X, Y and Z coordinates could be anything, except I do know that it will always be a flat face.
Finally, I need to be able to calculate this for faces with any number of vertices.
My initial approach to this involved checking how much the U coordinate changes over a certain X distance, and then the same for Y and Z until I found a relationship, but I keep finding new exceptions to that logic and I'm wondering if there's a simpler way.
How can I interpolate the UVs to calculate the fourth, fifth... nth point? In no particular coding language - I'm just looking for the approach.
Upvotes: 1
Views: 363
Reputation: 4320
Compute the barycentric coordinates of the vertices for which the U/V attributes are missing and use these coordinates to extrapolate the missing attributes (U/V or any other actually).
Here is a complete derivation and solution for an additional face vertex with position P
and U/V coordinates T
:
P1 = (x1, y1, z1), T1 = (u1, v1)
P2 = (x2, y2, z2), T2 = (u2, v2)
P3 = (x3, y3, z3), T3 = (u3, v3)
P = (x , y , z ), T = (u , v ) = ?
A point P
on the plane supporting the triangle (P1
, P2
, P3
) (and thus the whole face) has barycentric coordinates l1
, l2
, l3
:
P = l1 P1 + l2 P2 + l3 P3
with l1 + l2 + l3 = 1
This can be rewritten as:
P - P1 = ( l1 - 1 ) P1 + l2 P2 + l3 P3
= -( l2 + l3 ) P1 + l2 P2 + l3 P3
= l2 ( P2 - P1 ) + l3 ( P3 - P1 )
with l1 = 1 - l2 - l3
Projecting the vector V = P - P1
onto the vectors V21 = P2 - P1
and V31 = P3 - P1
gives:
< V, V21 > = l2 < V21, V21 > + l3 < V31, V21 >
< V, V31 > = l2 < V21, V31 > + l3 < V31, V31 >
where < V1, V2 >
is the dot product of 3D vectors V1
and V2
.
So (l1
, l2
, l3
) can be found for P
by solving the linear system:
G L = ( < V21, V21 > < V31, V21 > ) ( l2 ) = ( < V, V21 > ) = D
( < V21, V31 > < V31, V31 > ) ( l3 ) ( < V, V31 > )
L = ( l2 ) = G^-1 D
( l3 )
and l1 = 1 - l2 - l3
This can be solved explicitly:
d = < V21, V21 > < V31, V31 > - < V21, V31 >^2
l1 = 1 - l2 - l3
l2 = ( < V31, V31 > < V, V21 > - < V21, V31 > < V, V31 > ) / d
l3 = ( < V21, V21 > < V, V31 > - < V21, V31 > < V, V21 > ) / d
Note that since the position P
of the additional vertex is outside the triangle (P1
, P2
, P3
), the inequalities l1, l2, l3 >= 0
won't necessarily hold anymore.
Finally compute the extrapolated U/V coordinates T
of the additional vertex:
T = l1 T1 + l2 T2 + l3 T3
Upvotes: 1