Monday, 20 April 2015

Blender: Introduction to Textures, UV Maps, and Materials - Part II

Today we continue our look the subject of texturing by looking at the actual "how to" mechanics of adding textures, setting up very basic materials using the Cycles Render engine, and take a brief glimpse at the subject of UV mapping.

If you haven't already read Part I of this tutorial I strongly suggest you do so before proceeding. The materials portion of this tutorial assumes that you have set Blender to use the Cycles Render engine rather than the default Blender Render and will be useless to you if you haven't. Lastly, in the Baby Steps I tutorial I recommended switching Blender's assignment for the bottom pane from the default Timeline pane to the (for our purposes) far more useful UV/Image Editor pane. This isn't essential -- you can just switch from Default view to UV Editing view -- but I find it far more convenient to have it at my fingertips, so to speak. If you've done all of this and then add a simple mesh cube to your scene your screen should look something like mine.

Before we begin I should emphasize that we're only going to be touching the proverbial tip of the iceberg in this tutorial. Both UV mapping and material set-up are very complex subjects that can take a considerable amount of time and practice to master. We're only going to touch on a few of the most basic starting points. Even so, brace yourself: this will be a long one.

Getting Ourselves Ready

For today's tutorial we'll only look at a basic cube. In Part III of this series we'll expand our horizons a little to look at cylinders, spheres, and other more complex shapes.

A mesh cube is the simplest of all mesh objects: it has 8 vertices that combine to give us 12 edges and 6 quad faces. If wanted a mesh cube in-world for some reason, this would be the object I'd export. When I did so, each of those simple quad faces would be converted into a pair of triangular faces instead because SL/Opensim only allows tris so the in-world mesh cube would end up with 12 tri-faces and a total of 18 edges. To give you a proper taste of this subject, though, our cube is actually too simple so let's switch into Edit mode (hotkey: Tab) and make a few changes to it to make it a little more "complex" (even though it's still going to be a very basic shape.

In edit mode and with the entire object selected (hotkey A toggles between selecting none and all) let's begin by scaling it along only the x-axis by a factor of 2. That's most easily done with the hotkey combination S X 2. Let's also scale it to half its default height on the z-axis using the hotkey combination S Z 0.5. Then, finally, let's so something called "Subdividing" its faces to give us a number of faces on each side rather than just 1 face per side. On the Toolshelf's "Tools" tab you'll see a button called "Subdivide". Click it and then in the Most Recent Action section of the Toolshelf change the number of cuts to 3. You should now have a box that looks like this:

This is perfect for today's tutorial since it allows me to show you a number of interesting things that are impossible to do with a single prim cube. We'll stay in Edit mode, too, since this is where the bulk of our work is done when working with materials and UV Maps.

We have 3 main tasks to do before our "fancy" rectangular box is ready for export:
  • Assign 1 or more materials to it
  • Create a UV map for it
  • Decide what texture to use for each material
In actual fact, we don't need to do that 3rd task at all if all of the textures we intend to use are already ones we've uploaded into Opensim but often it's quite handy to have them assigned and visible in Blender so we can get a more accurate preview of what the object will look like in-world.

The order that we do these three things doesn't matter; just like the order that you build and texture things in-world doesn't matter.

In Opensim, at some point you upload a texture or use one of the ones in the default texture library. You could have uploaded that texture months ago and it doesn't matter as long as it's in your inventory and ready to use. You can change your mind tomorrow and upload a different one, using it to replace whatever you've already put on the prims' surfaces.

You can build an entire house using prims and their default pine textures (or "blank" texture if you prefer) then go around later to do your SL-face texture assignments (the approximate equivalent of assigning materials).

In-world you don't have to think about creating UV maps because you don't have any control over that with prims. In Blender you do, so you need to take care of that part too but you can do it any time you like and change it later if a different mapping is better for what you want to achieve.

As a general rule of thumb, though, in most cases you'll start by modeling an object. At some point during that process you'll at least do an initial UV map for it although you may later go back and adjust that map or even completely remap it all over again. At some later point, you'll begin assigning materials and deciding what textures to use on them. In essence, this roughly mirrors the typical in-world building approach except your UV mapping is already done for you the instant you rez an object.

Following this general rule of thumb, then, we've made our "model" -- our rectangular box -- so our next step is usually to create a UV map for it.

Our First UV Map

As you'll recall from Part I of this tutorial, our UV map is a bit like figuring out how to gift-wrap something without using any more wrapping paper than necessary or like turning the Earth into a flat map. For our object, each face (Blender-face) needs to have a corresponding bit of paper assigned to it on our flat UV map.

Our object in the scene seems to look fine though. Blender is showing us a nice even grey surface covering the entire thing. That's only because Blender has been programmed to show us that when we haven't yet gotten around to doing it properly ourselves. If you export the box exactly as it is now and then put the texture "brick1_256" on it form the Opensim texture library, it will look like this:

The left side of the picture shows it with Opensim's "Default" texture mapping and looks like a complete disaster with the texture all broken up into pieces and at weird angles; but we can change that to use "Planar" mapping instead and suddenly it all looks fine again. Sadly, this is probably the first thing many people learn and that's where they're very tempted to stop. They'll skip learning about UV mapping and simple switch all of their mesh imports to planar mapping and hope it looks good -- and for extremely simple object it will.

The reason for this huge difference in appearance is very simple: the "Default" mapping setting in Opensim says "use whatever UV map was supplied with this object" and we haven't made one for it yet in Blender so very weird things happen. The "Planar" mapping setting in Opensim says "ignore whatever UV map might have come with this object and try to texture it based on surface area instead" which in the case of a very simple rectangular box will look great because it's such as simple shape. Try this with anything more complex, though, and it will start to look extremely distorted and barely better than the default mapping.

Our goal when UV mapping an object in Blender is to get it to look right using the Default mapping setting in Opensim, no matter what shape it is. To do this, we need to "UV unwrap" our object just like unwrapping a gift box, peeling off the surface to make it flat with as little stretching as possible. For convenience in texturing it later, we usually also want to minimize how much tearing we do too.

If you think about this for a moment, your first thought is probably going to be "I need a pair of scissors" and Blender has a very handy pair for you: it's on the "Shading/UVs" tab of the Toolshelf. It isn't called "UV scissors" since we're actually not making any cuts. It's called "Mark Seam" where we "mark" a seam to tell Blender that's where to cut with its imaginary scissors when it's unwrapping the box. If we make a mistake and change our mind about where to make a cut, we have the "Clear Seam" button immediately below it to remove a selected seam (or part of a seam) that we've already marked.

Your next thought might be something like "there's a lot more than just one way to do this" and you'd be right. There are hundreds -- if not thousands -- of ways to do it even for an incredibly simple object like this box. To list just a few...
  • Do it like a child's paper box toy where we end up with one piece of paper that we fold back into the box shape.
  • Cut each side of the box into a separate pieces of paper, giving us 6 pieces that get taped together when we wrap it back up
  • Cut each of the ends off, then make one cut length-wise across the remaining middle piece. That would leave us with three pieces of paper to fold and reassemble.
  • Cut each and every individual face of the box into its own little piece of paper, then take a quick trip to the corner store to buy more tape because we're going to need it when reassembling our jigsaw puzzle of 96 little pieces of paper.
There are lots more ways...and even more when you remember that each of those quad faces needs to be turned into a tri when we import it to Opensim so we could do that in advance and use those triangular faces' edges as somewhere to make a cut.

So I'm not going to show you "the right" way to unwrap this box because, depending on what you intend to do with it and how you intend to texture it, there are many "right ways" to do it. I'm just going to start by showing you the "child's toy" way.

If you've done this as a kid you'll already know roughly where we want to make our cuts ("mark" our seams) but let's do it a step at a time. The easiest way to select a group of joined edges on a box like this is to switch to edge select mode, then hold down your ALT key and click on one of the edges in the line that you want to select (if you swapped your mouse buttons as I suggested in the Virgins and Mice tutorial that means a left-click; if you didn't it's a right-click). To add more to your selection, hold down the shift key too. With three quick clicks you'll be able to make a selection like mine, then click the "Mark Seam" button in the toolshelf.

When you mark the seam you'll see each of those edges now displays a red line along it (it's easier to see after you de-select them since the orange selection indicator will be on top of the red seam line). If for any reason you don't see that red marking go to the 3D View's property panel on the right, look for the section called "Mesh Display" and put a check in the box for showing overlays for seams. By default it should already be checked.

What we've just done is tell Blender that's one of the cuts we want to make when unwrapping our box, so we've essentially just identified a fold-down flap. Now let's do the same thing again with the two ends of the box. This time we only need to select the top line of edges and one side of edges because there other side has already been marked as a seem.

Once we've done that, we've marked enough seams for our box to be unfolded just like a child's paper toy. We've told Blender where we want to cut, and Blender's UV-unwrapping logic will have no trouble at all figuring out the rest of the folding process. Before we can do our actual unwrapping, though, we now need to tell Blender which faces we want to unwrap because Blender's unwrap functions only unwrap and optimize the unwrapping for the currently selected faces. We want to unwrap the entire thing so press the "A" hotkey twice -- the first time will deselect the edges you just selected, then the second time will select the entire model since that's what the A hotkey does.

Now click the "Unwrap" drop-down box that's immediately above the Mark Seam button in the Toolshelf and you'll see that the first of the nine (yes, 9!) different unwrapping methods is simply called "Unwrap." Pick this one. It tells Blender to pay attention to the seams we've marked and to do the best-guess job it can to unwrap our selected faces into a nice flat UV map with as little distortion of the faces as possible. If you make the UV/Image Editor pane at the bottom of your screen larger you'll see the result a little better. If you chose to leave that as the Timeline pane, you'll need to switch your viewing mode to UV Editing to see it.

You might notice that there are some options in the Most Recent Actions area after you unwrap. You can leave these at their default values for what we're doing today and, in fact, for an object mapping as simple as this one they wouldn't have any effect anyway.

Congratulations, you've now just UV-mapped your first mesh object. You'll notice in the UV/Image Editor pane that it occupies the entire large, square UV map grid area, with the edges of the map stretching from side to side and top to bottom right to the edges of the area. That's because I happened to (unintentionally) pick scaling ratios for the box that makes it fit perfectly like this in a square area. If we'd done this to our original cube it would look like this:

When Blender unwraps something using the "Unwrap" method it will make whichever dimension is the largest fill either the height or the width of the area. When we talk about UV-maps we don't use the terms x-axis and y-axis. Instead, we call them the u-axis (left-right) and v-axis (up-down) which helps to clarify things. It's a lot easier to say "u-axis" than "UV-map-x-axis."

Now that we've unwrapped our box, we could upload it to Opensim and here's what it would look like in-world with that identical brick texture and default mapping.

We've fixed the whole "jumbled mess of complete mayhem" that we saw previously in our unmapped box but the size of the brick texture is enormous on our box. We can "fix" that by changing the horizontal and vertical scale values in our viewer's texture settings (it will be the same size if we set the horizontal scale to 16 and the vertical scale to 8); but if you look closely you'll see that the ends aren't rotated around to have the same alignment as the sides. When we used planar mapping, before, they were. To explain the reason for this difference we need to understand how Opensim applies a texture to a mesh and how this relates to the mesh's UV map. The easiest way to do this is show you directly in Blender.

Setting Up Our First Material

Because I want to show you this with a brick texture, we need a brick texture that we can use in Blender. I don't have a license to supply you with a copy of the exact Opensim one (even though it's opensource), nor do I want to spend time telling you how to get one, so let's use an opensource brick texture that we do have license to use and is pretty close to the one in Opensim:

Source: Texturelib

It's actually a bit nicer than the Opensim one because it's a bit higher resolution (600x600 for the smaller of the two version on that site). That's not the ideal size for Opensim since it will be scaled on import to 512x512, but it's fine for our purposes and Blender is quite happy to use any image size that isn't so large that it makes your graphics card melt. Save a copy of this seamless brick texture to an easy-to-find-again location on your hard drive. Ideally, place it in the same folder that you're saving your box model in (you are remembering to regularly save your work, right?) because then we won't have to go hunting for it. I saved my copy of it using the name "brick.png"

There are a number of different ways to approach using a texture we made or downloaded from somewhere else, but let's look at the easiest and most typical way to do it for the Cycles Render engine. If you've decided to stick with the old Blender Render engine you're on your own now -- the rest of this tutorial only applies to the Cycles Render engine.

Switch to the Materials tab of the Properties pane and the entire area below it will change. At the top is a list of materials that are currently assigned to the selected object. Since we haven't assigned any yet, the list will be empty and immediately below it will be a "New" button. Click this and a new material will be created and given the default name "material." The area below this will now fill with settings for the material as well (you may need to expand or hide some sections to have it resemble my screenshot below). If you click in the text part of the new box that replaced the "New" button, you can change the name of this material to something more meaningful, like "brick" and I'd suggest getting into the habit of doing so.

We're going to ignore most of the materials properties settings for a moment and only change two things in the "Settings" section at the very bottom. By default a new material is given a viewport colour and specular colour of white. Let's click in the white box for the colour which will bring up Blender's colour-picker -- it's similar to your viewer's -- where we can choose a colour that's a sort of orange-red similar to the brick. Don't try to make it exact...just something that's very roughly in the same range. Also do the same thing and make the specular setting black which will eliminate the shininess of the surface in Blender's view. These do not change the object at all in-world -- this only changes what the object looks like in Blender's 3D View pane when using Solid view mode which is what we're using by default. (Side note: if you actually export it like this without doing anything else, the colour will be applied to the object by setting its texture to blank and then applying this colour in the viewer.

Because it's the first material assigned to this object, it's automatically assigned to all the object's faces as well, saving us the trouble of explicitly doing so.

Blender considers any texture it isn't creating itself as being an "external" image texture. Blender also has the capability to generate textures internally using one if its render engines, with Cycles being by far the most powerful of these but well beyond the scope of this tutorial. We're going to use our downloaded brick texture instead so let's use the quick method for assigning an external image texture for this material to use.

First, we need to tell Cycles that we want to use an external image. In the Surface section of the Material properties look for the setting for "Color." If we click in the large colour-picker portion of this line we could pick a colour to use for the actual texture -- identical to using a blank texture in-world and then giving it a colour -- but, instead, we want to click on the small dark grey box immediately to the right of it which brings up a huge list of options. The one we're looking for is in the Texture column of the list and called "Image Texture." (The eagle-eyed will notice that one of the other options there is "brick texture" but that isn't what you might suspect, believe me!)

Now that we've told Cycles that we're going to use a texture created elsewhere, we need to tell it where to find that texture so it can load it into memory. You'll see that after selecting Image Texture as our "color" it's now replaced the colour-picker and a new option has appeared allowing us to pick an image texture. Click "Open" and navigate to wherever you stored the brick texture you downloaded above.

You'll notice that I also switched the preview object in the window to a cube instead of the default sphere. This previewer has no effect at all on anything -- it's simply a convenience for quickly previewing what a texture looks like on an object of that shape. For the most part I actually keep the preview section of the settings pane collapsed.

When you pick a texture it will be loaded into Blender's memory and you'll see it appear on your preview object. The large "Open" button will be replaced with the name of the texture you picked although there's still a small file folder icon beside it that you can click to re-open the file picker if you accidentally loaded the wrong image.

The rest of the settings in this area will have default values that are fine for our purposes but are your gateway to highly advanced options for texturing; although unfortunately many of them aren't applicable to Opensim.

The one thing that hasn't yet happened is probably the thing you were hoping to see: the actual texture on your object in the main 3D View pane. Instead, we're seeing the blank texture with whatever viewport colour we assigned to it a few moment ago. That's because by default we're in Blender's "Solid" view mode which takes its values from the Settings section. To see what we really want to see we need to switch to Texture view mode. It's also a little easier to see when we don't have all our faces selected so press hotkey A to deselect everything for now.

Success! Finally we see our brick texture displayed on the object. The reason Blender defaults to solid view instead of texture view is in consideration of the typical user who would have serious issues using texture view when working. A typical user will have multiple objects in a scene, each with potentially multiple textures, and the normal image resolution for those textures would be at least 2048x2048 (although 4000x4000 or more is preferred). This rapidly gobbles up huge amounts of graphics card power resulting in what is best described as "lag" when working. Go ahead an try it some time...put 20 objects in a scene, each with a 4096x4096 texture on it and then try panning around. With solid view Blender can eliminate all of that overhead and deliver extremely rapid response but you have to work with just plain, blank colours and wait until the end, when you're ready to render, to see what it really looks like.

Relating a Material to a UV Map
The brick texture we're using here is more than four times the resolution of the "brick1_256" texture in the Opensim texture library and has more bricks in it so it doesn't look quite as bad on our box as my earlier in-world screen shot; but it will serve nicely to end this part of the tutorial series: showing how our material is related to our UV Map.

To do this we have one more simple thing to do: display our brick texture in the UV/Image Editor pane. Select everything again (with your mouse pointer in the main 3D View window press hotkey A again to select all) and you'll see the mapped faces again in your UV/Image Editor pane. In that pane, click the small icon to the left of the "New" button which will bring up a list of any textures we've already loaded into Blender while working with this file. There's only the one: our brick texture.

As soon as you do this the image will load into the UV/Image Editor pane and you'll now see your UV map superimposed over the image. This is showing you exactly how the image will map onto your object. Each face on the map is a face on the object, and the part of the texture under it will be mapped directly on to that face exactly as you see it here. If you switch to face select mode in the 3D View pane and then select only one face, you'll only see the corresponding face show in the UV map. Try doing this to get a feel for what part of the brick texture each face is getting the texture it's displaying. Once you've experimented a bit, select all faces again in the 3D View pane.

We have covered a lot of ground already in this tutorial so let's leave you with one last thing to experiment with before moving on to Part III.

You'll find that selecting and deselecting faces, edge and vertices works more or less the same way in the UV/Image Editor pane as it does in the 3D View pane. You can also select "island" which for this particular UV mapping would select the entire map. The "A" hotkey also does its usual select all/none job as long as your mouse pointer is somewhere in the UV/Image Editor pane.

Try selecting all, then with your mouse still in that pane use the hotkey "G" to grab and move the UV map. You'll see the texture moving on the surface of your 3D View pane's object to correctly map the texture under each face on the UV map to the corresponding face of the object. Also try "R" to rotate the UV Map. You'll see the texture rotating on the object in the 3d View pane because mapping is now putting a different part of the image "under" the map too. Similarly, try hotkey "S" to scale, then scale your UV map to make it occupy a much smaller overall area of the brick texture. Again, the texture on our object in the 3D View pane will show the effect of this change of mapping, making the bricks become huge on its surface. Also try scaling the UV map to be much larger than the image. The bricks are now much smaller in the 3D View pane.

As you'll see as you experiment with these changes to the UV map, any faces on the map that are outside of the image area of the texture are still being covered by brick. Blender automatically tiles the image texture an infinite distance in all directions so it's impossible to run out of image.

What We've Done So Far...

Just to give you a quick summary of what we've done during part II of this tutorial:
  • we marked some seams to tell Blender how to peel the surface off our box
  • we used UV Unwrap to do the actual creation of our UV map
  • we created and applied (simultaneously a material that we called "brick"
  • we then loaded an external texture to use for our brick texture
  • and finally, we began playing around a little with position, rotation and scaling of the UV map to see its impact on the way the texture appears on the object
We'll talk a little more about this in Part III, and look some more complicated objects to unwrap. If you want some "bonus things to play with" try selecting a single vertex or edge or face in the UV map and move it (hotkey "G") while watching what happens to the texture at the corresponding location of your object in the 3D View pane.

No comments:

Post a Comment