Thursday, 30 April 2015

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

I have been struggling for about a week now with multiple different drafts for part IV of this series in which I intended to cover two other other common shapes -- cylinders and spheres --and then finish up by showing how I would tackle a more complex object like the Suzanne monkey mesh. Sadly, I've come to realize that writing this at a novice level is probably impossible for any object other than a cylinder so for this last installment of the series we'll take a very brief look at that, and then at some point in the future I'll write a tutorial for the others that is geared towards people with a little more Blender experience.

Cylinders

In most cases we approach unwrapping a cylinder using much the same approach as unwrapping a box -- mostly because from a modeling standpoint they're much the same thing except their "end caps" are a little different. You could even think of a box as being a 4-sided cylinder where the ends are more efficiently modeled. I'll show you what I mean.

Start Blender and add a new mesh Cylinder object to the scene, then look at the most recent actions part of the Toolshelf. By default a newly-created cylinder will have a vertices setting of 32, meaning that its circular shape is defined by 32 vertices at each end, resulting in 32 faces. We can never have a perfectly circular cylinder since that would require an infinite number of vertices, so any cylinder is always a compromise between how smoothly round we want it to seem -- requiring a large number of vertices -- versus how detailed we want our model to be. The 32-vertex default is quite good when the faces are set to have smooth normals, though we often can -- and should! -- use even fewer to help keep our face count as low as possible.

Just for fun, take that to the extreme by setting the vertices value to 4. Suddenly, even though we've added a cylinder, it looks like a box. From a modeling perspective, they're identical. That's why our approach to a cylinder will be quite similar to the one we used for the box.

Now gradually increase the number of vertices (there's a little arrow on either side of value's box that you can use to step through values, or you can click and drag inside the box to use it like a slider, or you can manually type in numbers. As you increase the vertex count, the cylinder starts to look a lot more like what we'd expect to see in world. For this exercise let's use a vertex count of 16.

Let's also change the radius of the object to make it only 0.25m (giving us a cylinder with a diameter of 0.5m). You'll notice that at this smaller radius the surfaces looks smoother than it did when the radius was larger. That's a side-lesson: you can use far fewer faces when your object will be small in world.

We'll leave its depth (or you can think of it as its height or its length) at the default 2.0m but we want to change the "Cap Fill Type" from Blender's default Ngon fill to an Opensim-friendly "Triangle Fan" instead. If we used ngon the ends of our cylinder would be 16-sided faces which Blender is perfectly happy to work with, but Opensim requires us to import all-triangles anyway so a triangular fan is more useful to us. The triangle fan adds a vertex at the exact center of each end cap's face, then connects it to each of the (in our case 16) vertices, making a triangle face of each. The other option is "None" which would remove the faces from either end instead. You'll often use "none" when the cylinder is just the starting point for a mesh you'll be modeling. Let's give it ends, though, with triangle fan.


Our UV mapping is always done in Edit mode so press tab to switch into that. Now we need to mark our seams and from our experience with the box you'll probably not be surprised to learn that the first two we'll mark at the seams to separate the ends from the main part. If you switch to edge select mode, you can then ALT+click on one of the bottom loop's edges to select the entire loop, then use SHIFT+ALT+click to add the other end. If you did that in vertex select mode it would also select all of the faces of the main art which we don't want to do. Now mark those as seams.

If you think of our earlier wrapping paper and scissors analogy, you'll realize we now have two separated end pieces and then one remaining piece of paper that's wrapped around the "tube" part of the cylinder so we only need to mark one more seam: any one of the 16 vertical edges connecting the top to the bottom. We're working with a perfectly cylindrical object so it doesn't matter which one we pick so let's just arbitrarily select the one closest to the "front" of our view and mark it.Now select the entire mesh and unwrap it using the same "Unwrap" method we've used for the box.

As you can see, the whole operation is the same one we'd we'd use for a box if we didn't mind separating our ends from the main part. If you were planning to make your own custom texture for this object the map we have now would be perfect. We could create a texture (in Gimp or Photoshop) that has the textures we want in the appropriate parts of the image.

Exporting a UV Map to Use as a Template

If you're familiar with creating textures in either (or both) of those applications, you might be wondering how you could get a reference our of Blender to use as your template -- just like the templates you may have used for making SL skins or SL clothing. That's easy!

Simply export the UV map as an image by going to the UV/Image Editor's pane and use the menu UVs > Export UV Layout. This will bring up a file saving screen where you choose which directory to save your new file to, name it, and then at the lower left hand side there are some options for the image format to export to, how large to scale the map, and how opaque to make the background.

Normally I'll export as a 1024x1024 png file with 0.0 fill opacity which will give me a completely transparent image (at maximum Opensim import size) with black lines marking the vertices. You can drop this as a layer into Gimp or PhotoShop to use as your reference. If you decide to use a smaller texture, simply scale down to 512x512 or 256x256 later in those programs.

Mapping the Cylinder to Make it More Like a Prim


If you'd like your cylinder to accept textures exactly the way a prim cylinder does -- which is far more convenient if you plan to use a seamless tiled texture -- you'll either need to make some changes to the map or remap it in a slightly different way. A prim cylinder is mapped to:
  • have the map island of the main body be exactly the full image area so when your Opensim texture is set to tile 1:1 it completely fills the main body exactly
  • have each of the end caps also occupy the full portion of the image area that they're able to occupy so an Opensim tiling of 1:1 will map the texture fully to the surface.
  • all three parts are separate SL-faces which means they have the equivalent of 3 different Blender materials assigned to them -- one for the top cap, one for the bottom cap, and one for the main body.
We can do this by scaling and moving the 3 islands or our existing UV map around but that would take a lot of time (and be somewhat imprecise) so let's remap our cylinder a different way.

In the 3D View pane switch to face select and ALT+click on one of the face of the main body of the cylinder to select the entire loop of faces (and this will simultaneously deselect any of the end-cap faces that might already have been selected).

Use the "Unwrap" unwrapping method with just these faces selected and Blender will attempt to have the map fill the image area to the greatest degree possible while minimizing stretching. In the most recent actions area change the value for Margin to 0.0 (this tells it not to put any "padding" space around an island) to avoid having it leave a very small gap between the edge of the map and the edge of the image. This results in the map "fitting" perfectly from top to bottom of the image (meaning in-world it is now mapped perfectly to a scale of 1 on the texture's y-direction) but it is still not fitting fully from left to right.


If we used a tiled texture with this map and wanted our in-world texture to precisely fit it, we'd have to scale and offet our texture setting for the x-axis in world until we got it right (and it would be extremely difficult to get it perfect) so we want to scale our UV map to fill the width instead. We could do some math to figure out exactly how much to scale and then move the map, but it's much easier simply to do it the following way, using a few handy tricks.

We know our cylinder is  perfectly round, so we also know that if the entire map of it is centered in the image area and we scale it, the outer edges will reach the edges of the image at the same time. We can move the map to exact center quite easily by selecting it (you probably already have the map fully selected) and then in the properties side-panel on the right switch the Display Coordinates setting to show the Normalized value by checking that box. When the box is unchecked the UV Vertex values are displayed in pixels vs when it's checked the values are in percent of the to total image area. You'll see that the normalized y value is already 0.5 because the map is exactly centered top-to-bottom. Change the x value to 0.5 as well and it will move the map to position it at the perfect center left-to-right.

Now that the map is centered, if we knew exactly how much to scale it by to fill the image area we could simply do that by manually typing the value after our hotkey combination S X. Unfortunately we don't; and it's not an easy value to estimate (2*pi*r of the cylinder). We could also just "SX" and manually scale it while eyeballing it which, if zooomed in, would probably be pretty good. But there's a quick and easy way to make it perfect.

First, we need to change a UV/Image Editor pane settting by clicking on "UVs" in the pane's menu bar and selecting the "Constrain to Image Bounds" option near the bottom. You'll notice that this acts as a toggle so this adds a little check mark to the left of the option's name to tell you it's now active and if you selected it again it would disable the constraint. We want to constrain our maps to the boundaries of the image so now, when we scale, if we try to scale beyond the image area Blender won't let us.


Now you can simply use the hotkey "S" to initiate scaling and scale it larger. Any scaling that would cause the map to leave the image area is constrained so it will simply ignore any attempts you make to scale it any larger than the full image area. Once scaled, the map now perfectly fills the space meaning that in-world a texture on this map will scale perfectly at 1:1.

Now we need to do our end caps which is extremely easy. Simple select all of the face of only one of the caps (in the 3D View pane) and then unwrap it. Because it's circular Blender will automatically scale it to fill the image area. (Remember that the  margin needs to be set to 0.0 so there's no padding, but Blender will remember this from when you unwrapped the body so it will already be zero in this case.) Then select the other end cap and repeat the process. If we'd selected both end caps at the same time and unwrapped them, Blender would have scaled them to allow both to fit together inside the image area which isn't what we want.

Once you've done that, select the entire model in the 3D View pane. Everything will be superimposed on one another and each of our "islands" is filling the entire image area. This is the way an in-world prim cylinder has been mapped.

Remember that even though our mapping now matches a prim's we haven't assigned a separate material to each of those pieces so if you imported this in-world like this, any texture you applied would go on all three surfaces and if you scale the texture in-world it will scale on all three surfaces as well. If you want to be able to scale the texture of your end-caps differently (or use a different texture) you'll need to create and assign them to a different material; and if you want to be able to make the end caps different from one another you'd need to assign each to a different material. It all depends on how you intend to use your model in-world as to how many materials you assign.

A Different Method

So far we've only been using the "Unwrap" method of unwrapping because it's the one you're fairly likely to be using quite often for many models. To try to describe all of the other unwrapping methods is (considerably!) beyond the scope of this tutorial but I would like to show you that there's a completely different approach we could have used to achieve exactly the same results. I'll give you more of a "step-by-step do-this" list but I'll leave explaining the details to a more advanced level of tutorial.

Let's clear our mapping so we're starting again from scratch. In the 3D View pane make sure your entire model is selected, then from the UV Mapping drop-down select "Reset". This tells Blender to forget any maps for the selected faces and, instead, map each face to individually fill the entire image area.

Now select only the faces for the main part of the cylinder body (in face select mode, ALT+click on one of those faces to select the loop). When we're unwrapping by selecting only parts at a time we don't actually need to bother marking seams at all but it's still good practice to do so.

Now from the UV Mapping drop-down select "Cylinder Projection" and then in the most recent actions area make sure you have the following settings:
  • Direction: Align to Object
  • Align: Polar ZX
  • Radius 1.0
  • Check the "Correct Aspect" box
  • Uncheck the "Clip to Bounds" box
  • Check the "Scale to Bounds" box
The resulting map will be exactly what we wanted and the only appreciable difference is that cylinder projection automatically chooses its own place to make the seam to cut the continuous round surface and will even ignore the seam you've already marked there, so it's unlikely that it will have picked the same place to "wrap around" as you did. For using a seamless texture this isn't a problem, though.

Now with your mouse pointer somewhere in the main part of the 3D View pane use the hotkey Ctrl+I to invert your selection. This will select both end caps and deselect the main body faces. If you aren't in User Ortho view, press the numpad5 key to toggle to it. Then press numpad7 key to view the cylinder from the exact top -- don't adjust your view at all to be able to see any sides...it should look like a flat circle.

Now from the UV Mapping drop-down select "Project From View (Bounds)" -- make sure you pick the one with "(Bounds)" at the end, not the simple project from view. This will map both endcaps to fill the image space based on your current viewpoint so as long as you're looking from exactly above the object and in ortho view, this results in them being exactly the way we want them.

Now if you select the entire object you'll see that we have the identical map to the one we had before which might make you wonder why I didn't just show you this easier method first. There are a number of reasons:
  • you were already a little bit familiar with the "Unwrap" method
  • I hadn't yet indicated the meaning of the margin setting for it but now I have
  • there are some other considerations involved in cylinder projection that I simply can't explain in sufficient detail for them to be meaningful to you without getting into more advanced material
  • I want to now show you a major difference between those two approaches of unwrapping that will demonstrate why it's worth your time and effort experiment and learn how to use both,because each has its advantages and disadvantages.
 To be able to show you this last point, though, we need to take a slight detour...

Blender Image Generation

Blender is not just a program for modeling. It has a huge array of additional features that are additional aspects of working with 3D objects. This includes animation, physics simulations, lighting environments, texture creation, and probably a lot more things that I'm only vaguely aware of (and probably even more things that I don't even know about at all!). The vast majority of Blender artists will be using Blender to generate/create their entire finished output as either a single very high resolution image or as an animation. The whole idea of "model it here and export to use it somewhere else" just isn't part of the Blender artist's thought process so what we're doing as Opensim content creators is waaaaaaaaaaaaaaaaaaaaaaay out in left field as far as they're concerned.

One happy little benefit of this, though, is that Blender artists tend to need some in-program aids to help the make sure that what they're doing is going to look right when they render their final result. A single render can take hours and hours to run, so these aids let them do quick and easy checks before committing to all of that (potentially wasted if the made a mistake somewhere) time.

One of these handy aids is also extremely useful for us: Blender's tool to quickly generate a very useful texture for previewing the way that textures will map and tile on objects' surfaces. There are actually two useful textures it can generate: a colour grid and a UV grid. My personal preference is for the colour grid but you might want to experiment with both and might find you prefer the UV grid instead. They're created the same way.

In the UV/Image pane click the "New" button which will bring up a little box for creating a new texture. Give it a name (I'll call mine "reference grid") and decide what size you'd like it to be. Normally I just use the default 1024x1024 size since almost all of the seamless textures I use in-world are prefectly square and using this highest (for Opensim) resolution isn't at all taxing for Blender to display (Blender artists typically work with textures that are 4000x4000 or larger). You'll see a drop-down box that has three options:
  • Blank: creates a blank, white texture which isn't helpful for what we want
  • UV Grid: creates a special map that is useful for viewing UV Map layouts but (in my opinion) a bit harder to view at a glance so I prefer the 3rd option
  • Colour Grid: which creates a checkerboard texture of colours and labels that makes it really easy to see where parts of an image will map on a model.
For this exercise pick "Color Grid" but you might want to come back and generate a UV grid as well, later, just to see what it looks like. When you then press "OK" Blender will generate the texture for you and automatically load it into the UV-Image Editor pane.


You'll notice that a couple things happened...the texture appeared and it's much larger than the default 256x256 UV map area so you'll have to zoom out to see the whole thing. It also automatically scaled your existing UV map to fit the new much larger image area. That's because a UV map doesn't care what size image is being used but it will adjust the way it displays in this pane to fit whatever image you load into it. If you'd created a 1024x512 texture it would stretch itself in the display to fit that exactly the same way. I need to stress that this does not in any way change the map itself, it only changes how the map is being displayed in the pane.

All we've done, though, is create a texture which is now loaded in Blender's memory and available to use. If you switch the 3D Pane's view from "solid" to "texture" you still won't see this texture on your cylinder because the object doesn't yet have a material assigned to it -- we need to create one.

We learned how to do that earlier in this series so I won't go into details but the steps are:
  • In the Properties pane's Materials tab, create a new material for our cylinder (which automatically assigns it to all faces of our object because it's the first material we've defined)
  • Give it a useful name ("colour grid" is what I called it)
  • click the small box at the end of the surface colour to bring up the menu allowing us to pick "image texture" as the type of surface
  • this time instead of loading one from an external file we're going to use the texture that we already generated and is loaded in Blender's memory so you can just click the small icon at the left side of the box to pick it from a list of all currently loaded textures.
  • we'll use all of the other default property values and won't even bother to give it a colour down in the settings area.
  • and then, finally, switch to texture view mode for the 3D View pane

Now we have the handy colour grid displayed on our cylinder making it extremely easy to see how textures are being mapped to each face. If you wanted to use separate materials for your end caps (or even a separate one for each) you'd repeat creating the materials again and assign them just to those faces. You don't need to keep generating new colour grids since you can assign the same texture to as many different materials as you want.

Now that we have this handy reference texture, let's look at one of the key differences between the "Unwrap" and "Cylinder Projection" methods of unwrapping an object.

Unwrap vs Cylinder Projection

In many cases you're not going to be modeling perfect cylinders using mesh -- there's not much point in doing so when you can just as easily rez a cylinder prim in-world to work with instead. Instead, let's give ourselves something that's a bit more typical of something we'd actually be modeling. I won't explain what we're doing here because these are modeling techniques so they don't apply to this tutorial and would take too much addition tutorial to discuss in any detail. Just follow along with the steps.

We'll start by selecting our two end caps and deleting them so we're only working with the main body part.
  • In the 3D View pane using face select, select all of the faces for our two end caps and ensure that none of the faces for the main part of the cylinder are selected.
  • Then press the hotkey "X" (or the Delete button on your keyboard will do the same thing) which will bring up a dialog asking what you want to delete.
  • Pick the "Faces" option which will get rid of the end caps but won't delete the vertices that also belong to the main body of the cylinder.
Now we're going to give ourselves a little more geometry to work with by loop-cutting the cylinder's body.
  • Press hotkey Ctrl+R to initiate a loopcut and you'll see that if you position your mouse pointer over the cylinder's body there's a purple line displayed on it that loops around the body of the cylinder.
  • With your mouse's scroll wheel, scroll "up" to increase the number of cuts to 3 and you'll now see three purple lines dividing the cylinder into equal parts.
  • Now press the enter button on your keyboard twice without moving your mouse at all between the 1st and 2nd press or, if you prefer, you can click twice with your mouse to confirm the two parts of the action: part 1 is setting the number of cuts, part to is positioning them which is why we don't want our mouse to move, keeping them positioned centrally and evenly spaced.
After doing the loop cut operation make sure your values in the most recent actions area match mine. You should now also have all three of these newly cut loops automatically selected for you (and anything else that was previously selected will have been deselected).
  • With the three new loops selected, use the hotkey combination S 1.2 to scale them by a factor of 1.2. This will "push" the two outer loops closer to the endcaps which is fine for this example but when you're doing this in a "real life" modeling case you might want to set the z scaling manually back to 1 in the most recent actions.
  • Now select only the center loop of edges that run around the cylinder
  • Scale these a little further using S 1.1

If you followed each step correctly you should now have a bowed-out cylinder shape like the one above which is impossible to do with a single prim in-world. Irregularly shaped cylinders are extremely common in the world...tree trunks, arms, legs, torsos, ropes...the list list is almost endless. It's actually extremely rare to find perfect cylinders so this "deformed" one that we're working with now is representative of something you'll likely be trying to make using mesh.

Now let's compare our two methods of unwrapping it. We deleted our end caps because they would have remained the same as they were for both methods since we haven't in any way changed them. It's only our body that has changed.

Select the entire object and let's do the "Unwrap" method of unwrapping it first. Pick "Unwrap" from the UV Mapping drop-down box. Look closely at your map and at your "deformed" cylinder and you'll see that all of the mapping is now oddly stretched. Only the central "equator" line and one of the 16 vertical edges is still straight. All of the other edges are now a little bit curved. If you try scaling the UV map to fit the entire image area (while still constrained to bounds) you'll find that you can't do it. You'll never be able to arrange for it to have a nice, smooth, even, invisible seam at the place you marked as your seam.

This is because, as I've mentioned in the previous two parts of this series, the Unwrap method attempts to minimize the amount of stretching of each face. The checkerboard texture will be distorted on the surface of the cylinder when you look at the entire thing, but the actual stretching of the texture on each individual face of the mesh is the absolute minimum it can be.

If you're making a custom texture for this in Gimp or Photoshop (or directly in Blender) this will often be just fine, but for using any seamless textures or ones you get from shopping in-world it's likely to be objectionable. You'd rather have your old map back where the map is nice and straight and fills the entire image area even if that means a bit more stretching of the individual faces' texture.

Let's do our cylinder projection unwrap instead by selecting "Cylinder Projection" from the drop-down of UV Mapping methods and then ensure that it's still using the same settings as before in the most recent actions area:
  • Direction: Align to Object
  • Align: Polar ZX
  • Radiius: 1.0
  • and both "Correct Aspect" and "Scale to Bounds" checked and "Clip to Bounds" unchecked

When you look closely you'll see that the cylinder projection method reproduced the exact same type of result that we had when unwrapping our "perfect" cylinder. All the edges are perfectly straight on the UV map and the map fills the entire image area. On the model itself, the distortion of the texture on individual faces is a little greater -- although barely visible with this small of a "bow" to the shape -- but our seams are perfect and the vertical lines and horizontal lines of our texture remain that way on the model.

Cylinder projection mapping is by far the more useful mapping approach for any seamless texture because we'd usually prefer to keep the alignments and seamlessness even though we're suffering with a little more texture stretching. For anything that we'd make a custom texture for -- like the parts of a body or pieces of our mesh monkey Suzanne -- it's often more convenient to use the Unwrap method since we'll be making the textures to fit it anyway.

Where Do We Go From Here

This has been a very long -- and probably difficult for a new user to digest -- series and yet it would be an extreme understatement to say that we've only scratched the surface of the subject of UV mapping. Blender currently offers nine different methods of unwrapping and each of those methods has fairly wide variety of options. Further, we've only looked at the two -- by far! -- simplest objects to unwrap. Even something geometrically simple as a sphere is relatively challenging to unwrap for a beginner, and more complex shape are even more so.

To delve any deeper into this, however, would require either extremely lengthy instructions just explain how to create the models to demonstrate them with, and would require other bits of familiarity with Blender and the way objects import in-world and take those textures so they're simply too far beyond the scope to tackle at this level.

My best advice is play! Experiment with the methods. Read about them. Try them. Import them and see what happens when you try to use them in-world. This will begin to show you how things work and will do so in a much more meaningful way than having me write a lengthy textbook-length series of tutorials trying to describe it.

In my final (I think...unless I remember something else that's appropriate) tutorial for the novice level user I will talk about the last step in the process: exporting a model from Blender and importing it in-world. That should give you a somewhat decent foundation to begin to learn from and, as you practice and gain experience, make my more advanced tutorials accessible to you.