# What exactly is UV and UVW Mapping?

Trying to understand some basic 3D concepts, at the moment I'm trying to figure out how textures actually work. I know that UV and UVW mapping are techniques that map 2D Textures to 3D Objects - Wikipedia told me as much. I googled for explanations but only found tutorials that assumed that I already know what it is.

From my understanding, each 3D Model is made out of Points, and several points create a face? Does each point or face have a secondary coordinate that maps to a x/y position in the 2D Texture? Or how does unwrapping manipulate the model?

Also, what does the W in UVW really do, what does it offer over UV? As I understand it, W maps to the Z coordinate, but in what situation would I have different textures for the same X/Y and different Z, wouldn't the Z part be invisible? Or am I completely misunderstanding this?

• ask two questions separately – Noob Game Developer Oct 26 '12 at 7:54
• I so hate the guys like @NoobGameDeveloper, everybody else is answering the question and all they are doing is trying to find out things that are wrong with the question. Come on let them ask questions, I am not saying that no one should follow the rules but this questions like this are completely okay to be posted here. – Daniyal Azram Jun 2 '17 at 21:07

Your understanding is close. Each 3D model is made out of vertices. Each vertex usually defines the location of a point in space, a normal (used in lighting calculations) and 1 or more texture coordinates. These are generally designated as u for the horizontal part of the texture and v for the vertical.

When an object is textured, these coordinates are used to look up which texel or pixel to plot from the texture. I find it easiest to think of them as percentages or ratios between the left edge of texture (u = 0) and the right edge of the texture (u = 1.0) and from the top of the texture (v = 0) and the bottom of it (v = 1.0). They are interpolated between the vertices and looked up for each on-screen pixel that is rendered. They can be larger or smaller than these ranges and the render state that is set when the object is rendered specifies what happens. The options for this are CLAMP and REPEAT. Clamping limits the coordinate to either 0 or 1, causing the texture to smear where it is outside of the range. Repeat causes the texture to repeat when it is outside the range; it is effectively the same as grabbing just the decimal portion of the coordinate and using that in its place.

Before the texture coordinates are applied onto an object, they are multiplied by a texture matrix to apply some transformation to them (such as scaling, translation or rotation). This effect is sometimes animated in games to make it appear as though something is moving across an object without having to move the object itself... the texture is simply scrolling across it. When the texture matrix is multiplied by the texture coordinates, it produces 2 values that are used to look up the texel to plot (lets call them s and t). These are generated automatically from u and v even when the texture matrix is not set; it is the equivalent of multiplying u and v by an identity matrix.

This is where the w coordinate comes in, though it isn't used that often. It is an extra parameter to multiply the texture matrix against and is usually used when you want to take perspective into account (such as in Shadow Mapping). It works the same as when you transform a location in object-space to screen-space via a world-view-projection matrix. By multiplying the UVW with a projection transform, you end up with 2 coordinates, the s and t which are then mapped onto a 2D texture.

• Its worth mentioning that OpenGL treats v0 as the bottom and v1.0 as the top. That cost me a lot of time in the past when i couldn't find the bug when my textures looked weird. – tkausl Dec 8 '14 at 20:52

Consider a triangle.

Each corner has a UV coordinate. You interpolate between these to get a set of UV coordinates for each pixel. (There's also perspective in play here but let's ignore that for the moment).

Then, you fetch a texel from the texture from the coordinates U and V. Which is to say, a pixel from the texture coordinate x,y - same thing, slightly different terminology since we're talking about textures.

If your texture is a truly 3-dimensional one, you'll also need a third coordinate, W.

One way to visualize this is to think of a block of wood. If you chop it somehow, you'll see that each plane inside the block contains a 2d texture of sorts.

3d textures are so rare that you can forget about them for the time being.

• Rare? What are they used for? Can you show some example in the context of AAA titles. – Quazi Irfan Nov 29 '11 at 8:55
• No, because they're generally speaking never used. There's always some cheaper way of doing the same thing. Think of the chopped items in wii sports plus; you can break the items in any way you wish, and see the "internals". They could have wasted huge amounts of memory by making them 3d textures (assuming the hardware even supports that), but it's much cheaper to simulate that with a couple of 2d textures instead. – Jari Komppa Nov 29 '11 at 10:31
• So, they are not exclusive? or this is no much needed feature for any well known things game engine do? – Quazi Irfan Nov 29 '11 at 13:36
• I don't understand what you mean. 3d textures may be useful in other uses than as simply textures, though; if some data a shader needs conveniently maps into a 3d array, that would be one use. But as a "normal" texture, they're generally just a waste of memory. – Jari Komppa Nov 29 '11 at 17:57
• One application of a 3D texture is Distance Mapping, though as Jari says, they're very uncommon in a game due to their complexity. [link]http.developer.nvidia.com/GPUGems2/gpugems2_chapter08.html – Jackalope May 20 '15 at 14:05

Think origami.

A UV map is like a flattened (unwrapped) 2D skin of your 3D mesh (shell).

If you were to cut out the map and fold it along the mesh lines, the result would be your 3d model.

The (U,V) floating point values range from (0,0) to (1,1) The upper left corner of the UV map is (0,0) The lower right corner is (1,1)

Each vertex in a mesh of polygons (tris/quads) has a (U,V) value that tells the renderer which part of the map to use.

In the GPU pipeline, vertex shaders calculate the 2D projections of each pixel in these 3D polygons, and then fragment shaders colour them in using the UV map.

This cannot be fully appreciated without a picture like this which makes it all obvious:

As the other commentators mentioned, the W component is used by the renderer for fancier effects like shadow mapping, but the UV map is the base for understanding.

Note that a vertex shader must call a fragment shader at least once for each and every pixel that needs to be coloured in. That is why GPUs are parallel processors with dozens of cores - the shader pipeline is very demanding.

Please also note that CPU-integrated GPUs are designed for mobile devices and are limited to less than one tenth the number cores than that of similar generation external GPUs. This because of mobile power and cooling constraints. Moore's Law seems to have slowed down, but performance is still improving (with the odd melt-down here and there ..)

UV-mapping billions of pixels at 240+ frames per second can cause a real hot mess!

A uv map maps a point (x,y,z) in the mesh to a point (u,v) in the texture image. Since an image maps (u,v) to a color, the two maps can be chained, yielding a map from the mesh space to the color space.

         uv map       color map
(x,y,z)   ->   (u,v)    ->     color
mesh           Texture
space          space