This is all being done programmatically, not with any kind of 3D editor.
I am being delivered a shape like this:

The UV coordinates that map that checkerboard are just the x,y coordiates of the shape, but I can count on anything I want to map onto this being 1.0,1.0 in size.
Now, my job is to extrude a cylinder from this shape, programmatically. Easy enough:
I just took the UV coordinates from the top and kept them on the vertex on the bottom. But now, the spec says that the final cylinder should look something like this (this is mocked up, I have not been able to generate it):
I thought I was just going to tweak my UV coordinates to account for the height, and get checkers on the side, but what I end up getting is a swirled texture like this:
I understand why. I created that by just adding n to the V coordinate. But since the v coordinate isn't always on a checker boundary when it gets extruded down, it ends up like the image.
I believe I need some kind of directional vector to add the UV coordinate at the bottom of the cylinder to make the side checkers straight, but everything I try produces a worse mess.
How could I UV map those sides correctly?



Imagine unwrapping a cylinder (for the sake of simplicity, forget about the top and bottom caps). Aren't you left with a rectangle? Now, let us think how the horizontal and vertical coordinates are calculated.
When you're calculating the radians of your ellipse (base shape of your cylinder) you also calculate the U mapping coords. What you still need are the coordinates in the V direction: the bottom of your cylinder could be the
v0and the top thev1coordinate.Formally:
Addendum
I've overlooked the fact, that you're not calculating the coords by yourself (you get them from somewhere else) and your intent is to calculate the u-coord from the existing uv-coords. For that, we'll have to take the arctanget (
atan2) of each uv-coordinate to get the angle (in radians).The return value of
atan2is in the range[-PI, PI]. Since we need the range[0, 2*PI]to map the result to the range[0, 1], all you have to do is to addPIto the result and divide by2*PIoratan2(v, u) / (2 * PI) + 0.5.Depending on your number of height segments, the v-coord is simply the y-coord of the vertex (mapped to the range
[0, 1]). In case of just the bottom and top caps, that would be either0or1.Pseudocode:
See also: https://en.wikipedia.org/wiki/Atan2