Greetings!

Welcome to Scifi-Meshes.com! Click one of these buttons to join in on the fun.

Local TutorialBlender For the Faint Hearted - 08 : UV Texture Mapping and Texture Baking

SphynxSphynx195 Posts: 461Member
edited July 2009 in Tutorials #1
Blending for the faint hearted - Sphynx's guide to Blending

Tutorial 8: UV Texture Mapping and Texture Baking



UV texture mapping is the one area of 3D applications that, when friends ask me about it, tends to generate the most excitement and yet the most consternation because they simply can't get it right.

In retrospect, after you know how to do it, it is a relatively straightforward and painless process - as long as you obey the rules, and do not make assumptions on what you are doing.

What is UV texture mapping?
Well, the first thing to ask is, 'what is UV?' To the uninformed, it is very easy to look at those letters and think of ultra-violet - which, considering that materials reflect light, might not actually be too bad a guess.

In actual fact, the U and the V represent the two dimensions of a 'flat' surface (even if that surface happens to be ultimately bent over a three-dimensional mesh. Saying U and V in the context of computer graphics, is therefore synonimous with saying X, Y and Z.

How do you map a flat surface to 3D shape?
We can't of course map a flat image directly onto a strangely shaped 3D mesh. We need to do something first - and contrary to what some people will tell you, it is nothing new. Cartographers have been doing it for thousands of years. We need to unwrap the mesh and lie it flat so that we can draw on it.

Let me ask you a question - how do you unwrap a cube? If you don't actually know anything about UV texture mapping, but were still able to come up with an answer straight away then you just did something that I said you mustn't do - you made an assumption. You probably jumped from A to C and got the card-board box style 'cross' shape in your head (or something similar), missing out B altogether.

sb008_001.jpg
Image: Unfolding a box

What's the 'B' and why is in important? Well, the B is establishing where joins are going to be in the box - the seams that we can break apart to allow us to lie all the pieces out flat. As a human, with a brain capable of arbitrary thought, you probably just chose some side that seemed to make sense, or otherwise thought 'who cares' and chose some at random. Blender can't do that. And if it is that difficult for a cube, think what it would be like for a complex mesh.

So what do we need to do?
UV texture mapping in Blender will typically follow a strict set of operations:
  1. Build the mesh;
  2. Unwrap the mesh;
  3. Copy the 'flat' image and 'index' it;
  4. Re-apply the image to the mesh to see how the 'index' fits;
  5. Draw the final image on the mesh, based upon the 'index' orientation;
  6. Apply the final image(s) to the mesh.

You will see that we talk about 'indexes' a lot. These are not really anything special, are not Blender terminology, and could be any shape or pattern that will allow you to make sense of where the various parts of the image appear on the mesh once the image is mapped. This is important,as once the mesh is unwrapped into a flat surface, the various parts of that surface can be in some strange orientations - some parts can be at angles that simply don't make sense when compared to the mesh itself. With a cube for example, your 'index' may simply boil down to you writing a number of each face of the image.

So lets UV map a cube...

Building the cube
OK, do we really need to go through this again? Just start up an empty blender scene and add a cube, or better yet open up one of your older tutorial files that has a cube, camera and lights already set up - that way you can render it when we are finished without further hassle. Save the scene down as Tutorial8_00.blend

sb008_002.jpg
Image: Back to the cube...

Unwrapping the mesh
We are going to probably need two 3D view windows, so if you are only using one split it vertically in the middle so that you have a window on either side of the screen. If you went through Tutorial 2, then you may have 4 setup so we will just use the top two.

Select the cube in the left window, and place the window into local mode using the [numpad /]. Rotate the cube into a three-quarter view to make selection easier. We are also going to use a new editing mode, so from the editing modes drop-down menu in the window menu bar, select [UV Face Select]. By default, you will be in face selection mode, and the cube will turn totally white.

sb008_003.jpg
Image: The cube in UV face select mode

In the right hand window, select the [Window Type] drop-down menu on the far left of the menu bar, and switch from [3D View] to [UV/Image Editor]. You will get a black background with a grey square on it. Do not be confused by the square - it is not any form of representation of your cube. The square shape is purely coincidence, and it is actually just a work surface. What we are going to do next will appear on this surface.

sb008_004.jpg
Image: An image editing window

The menus on this new window type will be different - in the [Uvs] menu, select the option [LSCM Unwrap] and Blender will ask you to confirm. When it's finished we get... nothing. The window is still empty. Naturally - as we have already discussed, Blender has absolutely no idea of how to unwrap your cube. We need to give it some pointers - we need to tell it where to split the cube so that it can be unwrapped.

sb008_005.jpg
Image: The UV menu

I have seen tutorials explain this process by selecting vertices. I don't hold with this policy however, as first of all the difference between a pink spot and yellow spot is not that great, and secondly trying to visualise the cut is a nightmare. I much prefer to see the edge that I am cutting.

Hit [a] until nothing is currently selected.

Move your mouse into the window with the cube in it and hit [Tab] to enter [Edit Mode] (because we started in UV Mode, [Tab] will now toggle back and forth between these two modes), then select the [Edge select mode] icon in the window menu bar. This will now allow us to select individual edges. Hit [a] until nothing is currently selected.

sb008_006.jpg
Image: Cutting with edges

Select an edge of the cube (any edge) and you will see that it turns orange - perfect for visualising where our cuts are going to be made (in Blender, we actually refer to these as 'seams').

Deselect the edge that you selected, and select each of the edges in the diagram below - after selecting each edge, hit [Space] and use the menu [Edit] > [Edges] > [Mark Seam], or just hit [ctrl-e] to jump straight to the seam menu and select [Mark Seam] from there.

sb008_007.jpg
Image: Marking the seams

Once all of them are made into seams, stop, and try to take in why we selected those edges (there are other patterns that we could have used - a cube is a pretty straightforward shape, afterall). Imagine cutting the cube at those points and laying all of the faces flat on ground. It is important at this point to understand why we cut these edges. When you do this with your own meshes, this tutorial is not going to help you (unless of course, your mesh is a cube...)

If you are having problems, start in the front face then let your eyes wander along each attached face, but not stepping over the seams. You'll start at the front, go underneath, up the back and then onto the top, then you could go either left or right at the T-junction. See how this could be un-folded into a flat shape? That's the aim when creating your seams.

sb008_008.jpg
Image: Following the path of the seams

When you are finished, press [Tab] to return to [UV Face Select] mode. Now move your mouse over into the [UV/Image Editor] window on the right, hit [a] to select everything (you need to tell Blender which UV faces are going to be involved - remember that when you do this for real, you will probably map different images to different parts of your mesh), then select [LSCM Unwrap] again (or just press [e]). Hopefully, you should now get a nice 'T' shaped set of unwrapped faces in the form of a mesh.

sb008_009.jpg
Image: The unwrapped faces

I should point out at this stage, that there are other methods of unwrapping your mesh, other than [LSCM Unwrap], but for what we want to do here, it is by far the most useful. The rest of the UV buttons can be found on the [Editing] menu (F9) when the 3D view is in [UV Face Select] mode, on a pane labelled [UV Calculation]. There are methods with names like [Cube], [Sphere] and [Cylinder] and different size methods, but we won't be looking at them here as their uses are a little more advanced. Don't be fooled by their names - they are little more complicated to use than you would imagine.

You can now either leave the layout as Blender has arranged it, or you can use the standard [r] (rotate), (scale) and [g] (grab) keys to manoever the mesh. Remember however, that you will need to select the mesh first. I'd recomend always using [a] for this to ensure that you get everything - the last thing that you want now is to start deforming the arrangement that you've created! I generally manoever the mesh (if required) so that it fills as much of the display area as possible.

It is possible to deform the mesh my moving individual points, as well as weld points together, and mirror faces. For the cube however, none of this is necessary. When using those transform methods (they are all found on the [Transform] sub-menu of the [UV] menu, if you don't want to use the key shortrcuts), they are really only useful when you have unwrapped a complicated mesh and Blender has had to unwrap them into multiple pieces.

Once you have the unwrapped mesh as you want it, you can either PrintScreen or ScreenGrab the image, or (as I would recommend) choose [Uvs] > [Save UV Face Layout] to save the images as a Targa (.tga) file. Do this latter option now and save the unwrapped face image. This save option is a Python plugin however, so if you have not installed Python it may not be available to you and you may need to use the Printscreen or ScreenGrab option instead.

Indexing the image
The python script to export the mesh as a targa allows you to define several parameters. The [Wrap], [All Faces] and [Ob] buttons will all be on as default, and I'd leave them that way. In particular, the [Ob] button will automatically name the targa file using the object name. You just need to put the path into the filename box, not the full filename.

The [Size] setting allows you to set the file size (ie. width of the square image file) that you want in pixels, while the [WireSize] setting sets the size of the wire pattern, also in pixels. Now, just make sure that your mesh is the one selected, and choose [Export] to create the targa file. One slight oddity, is that the script does not tell you that it is finished and does not switch the window back to what it was before. Once you confirmed that the targa file exists therefore, just return the window back to what it was (usually, the buttons window is used to display pythoin script interaction).

Once we have the image, however it was generated, load it into your favorite image editor application - like Photoshop, Painter, Gimp or MSPaint (he, he, he...). To be quite honest, it does not matter which as long as it can cope with targa files. At present, we just need to add an acceptable indexing system. I loaded mine into Gimp and just added hand-written letters to each face.

sb008_010.jpg
Image: Indexing the image

Once you've done that, save the file down again. Remember that you can scale the image to get a higher resolution (a tip - just don't change the aspect), and even save it to a different file type. Both of these are best done however, for the final image - it does not really matter for the index image.

Applying the indexed image
Now go back into Blender. In the [UV/Image Editor] window, use [Image] > [Open] to select and load the image that you edited. Once you load it, it will appear in on screen.

sb008_011.jpg
Image: Applying the indexed image

If you used the [Save UV Face Layout] script to save the image (so it was square), chances are that it loaded back perfectly in place over the mesh. Sometimes, this can be hard to see however, so hit and move your mouse a little (slowly...!) - you'll soon see if the mesh sits perfectly placed on top of your image. If it does, press [Esc] to leave scale mode without really changing the mesh.

If it does not fit perfectly, use the [r], and [g] keys again to manipulate the layout so that the mesh fits the image.

Tip: Sometimes, your image will load in and the window will suddenly jump to huge dimensions, giving the impression that the image has loaded in far too large and you need to re-scale it. Don't be fooled by this - the initial unwrapped set of faces may be displayed in a smaller resolution to the image being loaded, and the image being loaded is therefore too large for the current zoom level. The unwrapped face will automatically accomodate the larger image (as long as it is the same aspect ratio), so all you should need to do is zoom the window to see it all - not scale it.

sb008_012.jpg
Image: The indexed image applied to the cube

Actually - that's it. The image is loaded and available for the mesh. Move your mouse into the 3D view with the cube, and switch to a fully textured display mode (alt-z is the shortcut for this). You should see your indexed image mapped correctly onto the cube.

Updating and applying the final image

Now that you can see where each of the indexed areas actually go on your mesh, you can take the original exported image and start to illustrate it properly. A tip however, is that if your image editor has layers, do so on a new layer - not the one containing the mesh grid. If you lose track of where you are in the image editing (trust me, you will...), you can always bring the mesh layer to front temporarily and see where you are.

When you are finished, save you final image (for safety) in a format that preserves the layers, export the image (without the mesh grid, of course) and load that image into Blender in place of the indexed version.

Now that you have actually got an image in Blender, the [Image] menu expands to contain a few more options than just [Open]. Here, you can now [Reload], [Replace] and [Save] the image being used , amongst other things. The key however, is that now that you have an image attached and registered correctly using your indexing method, that you shoud not under any circumstances change the geometry of the unwrapped mesh being displayed. This mesh contains all of the vertex data that tells Blender how to map the image to the mesh. Change that vertex data, and your images will no longer map correctly.

Multiple applications
One of the things that you may have noticed by now, is that the application of this image is singular. There is not much control over how the image is applied. You've attached it, and that's it. Does it affect colour, specularity, the normal... or what? In actual fact, its pretty much only the colour.

Well, the application of this one image is just the start. In most cases, I don't actually apply my own UV textures this way. It is pretty limiting at the end of the day. However, by this time two important things have happened. Select the cube that you applied your UV texture to, and hit [F9] to go to the editing buttons. Remember in the early tutorials that we looked at four buttons marked [Edges], [VertCol], [TextFa] and [Sticky]? Take a look at them now - you'll notice that [Edges] and [TextFa] now have something in them - they both give the option to [Delete], rather than [Make].

sb008_013.jpg
Image: The object now has more information...

In the case of the [Edges] data, this is where our seams are being stored. In the case of the [TextFa], this is where the information is stored regarding our UV co-ordinates and UV mapping information. Even if we don't actually attach a final UV map to the object, if this data is available then we can start to attach UV images in a different way.

Attaching UV images as a texture
Once you have a correctly oriented indexing image that you can re-use time and time again, you can build image maps for multiple different purposes - say a reflection map, a specularity map, a colour-map and a bump-map (of course, you may not need all of these). How do we then apply these images to get the effects, when the standard attachment UV attachment system does not allow us to specify what it is going to be used for?

To do this, we need to follow the entire process of unwrapping, attaching our index image and orienting ourselves. Once that is done, the mesh contains Edge and UV texture face data, so we can use it elsewhere.

First of all, we need to get rid of the existing index map as we don't really need it anymore. Make sure that the left window is still in [UV Face Select] mode, and that the right window specifies the required datablock in the drop-down menu beneath the image (we only have one, so it should be the correct one - but you may be doing this with several attached to your own meshes). When you are sure, hit the black 'X' next to the drop-down menu to delete the link to the image.

The Texture Face data however, is still present. Take a look on the [Editing] menu (F9) and it still says [Delete].

If you have used this cube before and it has two materials, select the second material index in the list (in the [Link and Materials] pane and delete it so that the entire cube is using the same material. Switch to the Materials menu using [F5], and clear any textures that are already attached to the material (if any) and add a new one. Rename it 'Cube Color Map'.

Jump across to the [Texture Buttons] (F6) and change the texture type to [Image]. Select [Load Image], and select one of your mesh images (either the index or final version - it's up to you). No go back to the [Material Buttons] panel and select the [Map Input] pane. Hit the [UV] button to use texture co-ordinates - didn't understand this co-ordinate mapping method in the last Tutorial? Well, you do now.

For a standard colour map, that's it. If you are using one of your earlier scenes that already has lights, render it now and take a look. One cube with a UV mapped texture - this time, one that you control and tell what you want it to affect.

sb008_014.jpg
Image: Our rendered cube

All you need to do now, is make different images for your different texture needs (if required, some maps work well in multiple capacities), and add them as different textures - modifying each to indicate what aspect of the material that you want them to influence.

Texture Baking
One of the overlooked features of Blender's UV menu is its ability to bake textures using a plugin. The term 'baking' comes from the pottery or ceramic world, were the final form of the outside surface of the object is 'baked'. The various layers, normally ending in a layer of glaze, become permanent and can no longer be modified.

The process of texture baking in a 3D application can be useful when the final state of the material may be very complex. Once the various procedural textures, image maps, and the effects of reflectivity, transparency and lighting have been added to the scene, the rendering time may be quite significant - especially if the meshes in question are going to be used in animations where a saving in render time would be extremely useful.

The process of texture baking in Blender, follows a four stage process:
  1. Prepare your mesh, add all of the materials and textures required and set up the normal environmental conditions of your scene - such as lighting and objects that will be reflected.
  2. Go through the UV unwrapping process to the point at which you have the mesh unwrapped in the window, but do not export the images for indexing - there is no need.
  3. Perform the texture-baking process to generate a UV mapped image containing all of the aspects of the materials.
  4. Remove all of the existing textures from the mesh, and re-apply the single UV mapped image.

Let's take the following scene for example. This mesh contains the indexing UV image that we built in the last part of this tutorial, re-applied as a normal texture. On to of that, I have applied a second texture and made it the 'magic' procedural texture. I have changed the [Map To] settings so that it modifies the [RayMirror] (set to a base value of 0.10), and the Normal. This second texture therefore, will not actually modify the colour of the cube, but will modify the shape of each face and its reflectivity. I rendered the cube and got the following image:

sb008_015.jpg
Image: A cube that we want to texture bake

Imagine however, that this cube had a few more textures - possibly three of four procedural textures. Coupled with the reflectivity that needs to be calculated in a complex scene, and the per-image render time might start to suffer. As a result, I followed the UV mapping process up to the point of getting an unwrapped mesh, then selected [Texture Baker] from the UV menu.

There are two possible questions that the [Texture Baker] can ask. If you already have an image in place, it may ask whether you want to replace the current image or not. If you reply not, then the texture will be rendered but not immediately attached to the UV co-ordinates - you can do that youself manually later. The second question will ask for the default pixel width of the generated image. There are four standard settings - [256], [512], [768] and [1024].

Baking the textures from this mesh, gave us the following image:

sb008_016.jpg
Image: Our unwrapped texture bake

There are a few things to remember in regards to texture baking. First, while texture baking will reduce the rendering times of meshes by combining the results of multiple textures into one final image, it is just an image at the end of the day - dynamic changes to the surface of a mesh possible during an animation are therefore removed.

Next, texture baking will not incorporate the effects of other objects that could change their influence in an animation. This is the reason why you will notice that the red-sphere is not reflected in the final texture - reflections like this can only be generated based upon where the sphere happens to be at the time of rendering.

Texture baking produces a single image - if some of the textures involved physically deform the material, then this will naturally not incorporated into the final image, other than in the form of the optical illusion formed by the original deformation at the time of render. Move the mesh or the camera angle therefore, and such illusion of deformations incorporated into the image will at best be lost and at worst clash with the new viewpoint.

Texture baking is best used when the textures and materials in use on the mesh are complex, but where their image and position of the mesh will rarely change during the lifetime of a scene.

There are plenty more in the UV and Image Editing areas of Blender, but we are not going to look at them now. We know the basics of building materials, textures and UV mapped textures - so its about time that we put it all into practice.

What's in Tutorial 9?
We were going to dip into tips on building textures in this tutorial, but I think that now we will dedicate that to a full blown general turorial instead.

In tutorial 9, we are suspending the general learning of Blender and going for some consolidation. For the next few tutorials, we are going to use what we have learned in Tutorials 1 to 8 and take a brief for a design from the building of the models, through texturing to a complete scene. Along the way, we'll look at some tricks, tips and minor functions that are useful, but really don't deserve a full tutorial in themlseves.

After that, we'll come back to the learning and work through what could be called intermediate and advanced features of Blender. We'll use those techniques to continue work on the final scene and work up to an animated sequence.

Copyright (c) 2006, Craig Robinson ("Sphynx")
Permission has been granted for inclusion only on www.scifi-meshes.com and www.industrial-meshes.com
Post edited by Sphynx on

Posts

  • tetsujintetsujin0 Posts: 0Member
    Finally took the time to go through this tutorial and made myself a simple texture. Hooray!

    One thing I think would make the tutorial better is if it textured a somewhat more ambitious shape than a cube. Working with a cube's faces is pretty easy, conceptually - but I feel like unwrapping and texturing a more complex shape would be more informative. I ran through the tutorial and textured a curved, subsurf'ed mesh from my Zaku model rather than a cube.
  • SphynxSphynx195 Posts: 461Member
    Well, this is a pretty simple tutorial - texturing anything more would introduce the possibility of problems occuring because of other areas, and not because a member got the basic principle wrong.

    It was always planned to do a much more complex example within the 'Gun' tutorial sequence that never got finished. One day...
  • eracooneracoon171 Posts: 0Member
    boh... Why did I miss this?
    great tutorial.
  • I.G.88I.G.880 Posts: 0Member
    Just wondering how you would do this on a angled texture as i would like to do a paint job type thing on my cruiser and cant figure it out!
  • SphynxSphynx195 Posts: 461Member
    By 'angled texture', do you mean angled polygons? In any case, the process is all the same - you just have to ensure that what you paint into the graphic is appropriate for the alignment of the object.
  • I.G.88I.G.880 Posts: 0Member
    I attempted... i will have to play with this more later on still learning blender right now so.. but as i have no feedback on the cruiser i guess i have some time to screw with UV stuff
  • [Deleted User][Deleted User]2 Posts: 3Member
    can you do this in blender 2.49 for me . this No. of Blender is irrelevant to my current app .(please help)
  • citizencitizen171 Posts: 0Member
    attackmesh wrote: »
    can you do this in blender 2.49 for me . this No. of Blender is irrelevant to my current app .(please help)

    It's almost identical, except now you have to mark the seams in edit mode rather than UV. Go into edit mode, select the edges to use as seams, then hit ctrl-e and select 'mark seam'. When you've created all your seams hit u, then select 'unwrap' from the menu.
Sign In or Register to comment.