Search code examples
graphics3duv-mapping

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!!


Solution

  • 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.