10.5 - Texture Mapping - Texture Coordinates

The most challenging part of texture mapping is the assignment of texture coordinates to the vertices of a model. Blender has a rich set of tools for this task. In fact, it has more tools and techniques than we could possibly cover in a single lesson. We will discuss the basics and you can pursue more advanced techniques on your own.

The typical goal of texture mapping is to wrap an image around an object in a way that covers the entire model and the individual triangles of the model are not noticeable. But other scenarios are possible. You can texture individual faces or groups of faces if you want to. However, keep in mind that rendering individual faces with different texture maps typically requires that you render each face separately, which can greatly slow down rendering speeds.

Texture Mapping Basic Primitives

Blender has built-in texture mapping functionality for all of the basic mesh primitives. Therefore, if all you want to do it wrap an image around a triangular mesh, then the process is:

  • Select a model.
  • Enter “edit mode”. (Tab key)
  • Hit the ‘a’ key until all faces are selected.
  • In the properties panel, select the “Materials” panel.
  • Create a new material and give it an appropriate name.
  • “Assign” the material to the model’s faces. (All faces should be selected when you do this!)
  • With the material still the “active material”, select the texture properties panel.
  • Create a new texture and give it an appropriate name.
  • In the “Type” drop-down box, select “Image or Movie”.
  • In the “Image” section, select “Open”, which brings up a file browser. Navigate to the file you want to use and select it. Always save your texture map files in the same folder as the models that use them. This allows Blender to find the file without a file path.
  • In the “Mapping” section:
    • For the “Coordinates:” drop-down menu select the “Generated” option. This means Blender will automatically generate the texture coordinates.
    • For the “Projection:” drop-down menu select the type of primitive you are texture mapping (e.g., flat, cube, tube, or sphere).
      • flat: The entire image is used for the quad(s) or triangle(s). If the aspect ratio of the image is different than the aspect ratio of the 3D surface, then the image will be distorted.
      • cube: The entire image is used for each of the 6 sides of the cube.
      • tube: The entire image is wrapped around the model (assuming it is a torus).
      • sphere: The entire image is wrapped around the model (assuming it is a sphere).
    • Note that in the case of “flat” and “cube” generated texture coordinates, the image is reused for each “side”, while the “tube” and “sphere” generated texture coordinates use a single copy of the image and wrap it around the entire model.

You can verify that a model has texture coordinates by rendering it in Blender or by exporting an OBJ file and looking for lines that start with “vt” (vertex texture coordinates).

The steps above are demonstrated in the following 5 minute video.

UV Texture Mapping

In many cases you want more control over the assignment of texture coordinates than the “generated” values described above. This is done using “UV texture mapping” tools. The basic idea is that you take a 3D model and select edges that can be “cut” to unfold the model onto a flat surface. Then you lay the unfolded faces on top of the texture map image and arrange them as desired. This gives you total control over the texture coordinates for every vertex.

The steps for performing UV texture mapping starts with the same steps as above:

  • Select a model.
  • Enter “edit mode”. (Tab key)
  • Hit the ‘a’ key until all faces are selected.
  • In the properties panel, select the “Materials” panel.
  • Create a new material and give it an appropriate name.
  • “Assign” the material to the model’s faces. (Make sure all faces are selected.)
  • With the material still the “active material”, select the texture properties panel.
  • Create a new texture and give it an appropriate name.
  • In the “Type” drop-down box, select “Image or Movie”.
  • In the “Image” section, select “Open” which brings up a file browser. Then select your image.
  • In the “Mapping” section, for the “Coordinates:” drop-down menu, select the “UV” option. This means you will be setting the texture coordinates using the “UV/Image editor”.

You now have texture coordinates assigned to every vertex, but not the texture coordinates you probably want. You need to transform the faces of your model so that they are over the correct portions of the texture image. To setup for this task, do this:

  • Change the window panel configuration to the “UV editing” mode. This will allow you to see both your 3D model and the “UV/Image Editor”.
  • In the “Browse Image to be linked” drop-down menu select the texture map image. This will display the image in the “UV/Image Editor”.
  • Also in the menu options of the “UV/Image Editor”, enable the “Keep UV and edit mode mesh selection in sync” option. This allows you to select vertices, edges, or faces in either the “3D View” or the “UV/Image Editor” window and the corresponding element(s) will be selected in the other window.
  • In the “3D View” window change your display mode to “texture”. This allows you to see the texture image on the faces of your model.

Now you are ready to edit the texture coordinates. Follow these steps:

  • In the “3D View” window, while in edit mode, change to “edge selection” mode.
  • Select the edges that can be “cut” to unfold your model onto a flat surface.
  • Bring up the edge menu (CRTL-E) and select “Mark seams”.
  • With your cursor in the “3D View” window and with all of the faces selected, hit the “u” key. In the pop-up menu select the “unwrap” command. This “cuts” the model faces at the seams you have chosen and lays the model’s faces out over the texture image. If you don’t see the faces displayed in the “UV/Image Editor” the most likely reason is that the model can’t be unfolded using the edges you selected for the seams. Note that some of the faces in the “UV/Image Editor” may be on top of each other. This happens when multiple faces use the same part of the texture image.
  • Use the normal editing commands, such as “g” for grab, “s” for scale, “r” for rotate, “b” for box select, etc., to move the face vertices over the desired portion of the texture image. If you distort the shape of any of the triangles (or quads), the resulting texture map will be distorted as well.

If you don’t like any of your decisions, you can always restart the process by selecting all of the model’s edges, clearing the edge seams (CRTL-E, “clear seam”), selecting new edges to cut to “unfold” the model, and then “unwrap” the model again.

The steps above are demonstrated in the following 7 minute video.

Texture Mapping Data

The data needed for texture mapping is stored in your OBJ and MTL model files. The following example shows the data for a cube. The texture coordinates are on the lines that begin with “vt”. The values implicitly define an array of texture coordinates that are indexed starting at one. When the faces are defined later in the file each vertex has geometry coordinates, (x,y,z), from the defined vertices, and a (s,t) value from the defined texture coordinates. The syntax 5/2 defines a vertex what uses the 5th (x,y,z) value and the 2nd (s,t) value. Notice that individual geometric and texture coordinates are used for multiple faces.

# Blender v2.73 (sub 0) OBJ File: 'textured_cube_faces.blend'
# www.blender.org
mtllib textured_cube_faces.mtl
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 1.000000 1.000000
vt 0.000000 1.000000
usemtl Material
s off
f 1/1 2/2 3/3 4/4
f 5/1 8/2 7/3 6/4
f 1/1 5/2 6/3 2/4
f 2/1 6/2 7/3 3/4
f 3/1 7/2 8/3 4/4
f 5/1 1/2 4/3 8/4

The “material definition” file, MTL, stores a filename for a texture map in a map_Kd property of a material. Here is an example MTL file:

# Blender MTL File: 'textured_cube.blend'
# Material Count: 1

newmtl cube_material
Ns 96.078431
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
map_Kd texture1.png

Other Ways to Create/Calculate Texture Coordinates

A texture coordinate specifies a location from which a color can be retrieved. For image based texture mapping the location is a position in a 2D image. For procedural based texture mapping, which you will study in the next lesson, a texture coordinate specifies a location within a calculated pattern. It is possible to use the geometry, (x,y,z) vertices, of a model as texture coordinates. Consider the following scenarios:

  • Define a model inside a unit cube in the range (0,0,0) to (1,1,1). (The model can always be scaled to whatever size you need for rendering purposes.) For every vertex, (x,y,z), use the (x,y) value as your (s,t) texture coordinates. Note that if x or y is not changing across a surface this does not work well.
  • Define a model inside a unit cube in the range (0,0,0) to (1,1,1). For every vertex, (x,y,z), use (x,y), (x,z), or (y,z) as your (s,t) texture coordinates depending on the direction of the vertex normal vector. For example, if the normal vector is pointing in the Z or -Z direction, use (x,y), if the normal vector is pointing in the X or -X direction, use (y,z), or if the normal vector is pointing in the Y or -Y direction, use (x,z). (Or you could differentiate and use (x,z) for +Y and (z,x) for -Y.)
  • Define your model using whatever units are appropriate, but scale the vertices to the range (0.0, 1.0) in your fragment shader before using them for (s,t) coordinates. Depending on how you define your model you might have to pass the fragment shader the minimum and maximum limits on the model’s geometry.

Summary

We have only discussed a few of Blender’s texture coordinate editing tools. If you are interested in more advanced techniques, search for related video tutorials on the web. The Blender documentation for texture mapping is a good reference as well. Please note that Blender has three different rendering algorithms (Blender Render, Blender Game, and Cycles Render) and each algorithm uses different types of material properties. Therefore the way you create and modify textures is different for the three different rendering algorithms! The instructions above work with the default Blender Render algorithm.

Glossary

texture coordinate
A location from which a color can be retrieved.
image texture mapping
A process that maps pixel locations in a 2D image to fragments that compose a 3D triangle.
UV editing
Visual tools that allow a texture designer to assign texture coordinates to model vertices.
Next Section - 10.6 - Texture Mapping Using Procedures