Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically generating UV coordinates algorithms

I'm writing my own uv editor for a tool of mine, and I'm trying to incorporate as many algorithms as I can for projections. I need to take an arbitrary mesh, and make uv coordinates for each vertex.

So far, I have planar, and Least Squares Conformal Map.

I'd like to incorporate more, such as tri-planar, cylinder, spherical, but I'm having a very difficult time locating the information to perform the algorithms. The tri-planar appear to generate a color, but I need to get everything in UV coordinates.

Help would be very appreciated!!

like image 682
Mary Ellen Bench Avatar asked Sep 02 '13 18:09

Mary Ellen Bench


2 Answers

Tri-planar

Forget about it: it is not a projection algorithm (an algorithm that gives you UV coordinates), and there is no way you can get UV coordinates out of it. It is a rendering algorithm that gives you a color obtained by blending what color you would obtained using each X-Y-Z planar projections separately.

Cylindrincal, Spherical

Like planar, those are very simple projection algorithms that give you a UV value directly from a XYZ value, without taking into account the connectivity with other vertices.

  • For cylindrical: convert (x, y, z) into Cylindrical Coordinates (ρ, φ, z), and use as UV coordinates u = φ and v = z
  • For spherical: convert (x, y, z) in Spherical Coordinates (r, θ, φ), and use as UV coordinates u = θ and v = φ

Of course, you can switch the roles of X, Y and Z to project using a different axis, or perform some translation/rotation/scaling to have more control (the same way that you can control the size and orientation of the plane you use for the planar projection).

Cubic

First, you need to determine to which "projection face" you assign each face of your mesh. I name the projection faces X, -X, Y, -Y, Z and -Z as in the figure below (where I assume the X, Y, and Z axis have respectively the colors Red, Green, and Blue):

enter image description here

For this, you simply find which coordinate of the normal (nx, ny, nz) has the greatest absolute value, and assign it to the face corresponding to this axis and sign. For instance:

  • if n = (0.8, 0.5, 0.3), then the corresponding face is X (|nx| is the greatest and nx is positive)
  • if n = (0.3, 0.8, 0.5), then the corresponding face is Y (|ny| is the greatest and ny is positive)
  • if n = (0.3, -0.8, 0.5), then the corresponding face is -Y (|ny| is the greatest and ny is negative)

Then, once you know to which projection face you assign every face of your mesh, you can apply the corresponding planar projection to the vertices around this face to get a temporary value (u_temp, v_temp) ∈ [0,1] x [0,1].

The next step is to transform this value uv_temp ∈ [0,1] x [0,1] into a value uv included in the smaller square as represented in the image A above. For instance, if you applied the projection "X", then you want uv ∈ [2/3, 3/3] x [2/4, 3/4], then you would do:

u = 2./3. + u_temp/3.;
v = 2./4. + v_temp/4.; 

Finally, the last step is not to forget to duplicate the UV vertices that belong to two faces with different planar projection (the borders between the different colors on the picture). Indeed, some vertices of the mesh can (and should in most cases) be split in several positions in the UV map to give decent results.

like image 171
Boris Dalstein Avatar answered Oct 12 '22 01:10

Boris Dalstein


Cubic Mapping

The standard method of doing this, based on a (rx, ry, rz) vector is to first look up some values in a table. These values are used for the (s,t) (or (u,v)) texture coordinate per vertex.

First find the Reflected Vector R = 2(N dot V)N - V, Where V = Vertex, N = Normal , R Reflected Vector(rx,ry,rz)

                      major axis 
                      direction      sc     tc     ma 
                      ---------      ---    ---    -- 
                      +rx            -rz    -ry    rx 
                      -rx            +rz    -ry    rx 
                      +ry            +rx    +rz    ry 
                      -ry            +rx    -rz    ry 
                      +rz            +rx    -ry    rz 
                      -rz            -rx    -ry    rz 

Once sc, tc, and ma has been assigned values, (s,t) coordinates for that face can be calculated with the following formulas.

if((rx >= ry) && (rx  >= rz)) 
{ 
sc = -rz; 
tc = -ry; 
ma = fabs(rx);  //absolute value
s = ((sc/ma) + 1) / 2; 
t = ((tc/ma) + 1) / 2; 

cout << "+rx (" << s << "," << t << ")" << endl; 
} 

  if((rx <= ry) && (rx  <= rz)) 
{ 
sc = +rz; 
tc = -ry; 
ma = fabs(rx); 
s = ((sc/ma) + 1) / 2; 
t = ((tc/ma) + 1) / 2; 

cout << "-rx (" << s << "," << t << ")" << endl; 
} 

if((ry >= rz) && (ry >= rx)) 
{ 
sc = +rx; 
tc = +rz; 
ma = fabs(ry); 
s = ((sc/ma) + 1) / 2; 
t = ((tc/ma) + 1) / 2; 

cout << "+ry (" << s << "," << t << ")" << endl; 
} 

if((ry <= rz) && (ry <= rx)) 
{ 
sc = +rx; 
tc = -rz; 
ma = fabs(ry); 
s = ((sc/ma) + 1) / 2; 
t = ((tc/ma) + 1) / 2; 

cout << "-ry (" << s << "," << t << ")" << endl; 
} 

if((rz >= ry) && (rz >= rx)) 
{ 
sc = +rx; 
tc = -ry; 
ma = fabs(rz); 
s = ((sc/ma) + 1) / 2; 
t = ((tc/ma) + 1) / 2; 

cout << "+rz (" << s << "," << t << ")" << endl; 
} 

if((rz <= ry) && (rz <= rx)) 
{ 
sc = -rx; 
tc = -ry; 
ma = fabs(rz); 
s = ((sc/ma) + 1) / 2; 
t = ((tc/ma) + 1) / 2; 

cout << "-rz (" << s << "," << t << ")" << endl; 
} 

Reference http://www.unc.edu/~zimmons/cs238/maps/cubeind.html

Spherical, Cubic, and Parabolic Environment Mappings http://www.unc.edu/~zimmons/cs238/maps/environment.html

OP would you please share your Least Squares Conformal Mapping algorithm for generating UV coordinates. Thank you.

like image 30
DeJean Avatar answered Oct 12 '22 03:10

DeJean