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.

Wednesday, 22 April 2015

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

By the end of Part II of this series we had learned how to make our very first UV map of a simple rectangular box and we'd learned how to create a material and assign an external texture to it. We're going to continue playing with that box for a little longer before moving on to talk about UV mapping more complex objects.

It occurred to me that while I did show how the UV map relates to the mesh object I may perhaps have failed to emphasize (or at least directly state) how that in turn relates to the object in world.

I uploaded the exact brick texture we're working with in this
tutorial so the screen shot here is using the identical brick.png


At first glance in-world you would think there's no real difference between our uploaded box and a prim cube set to the same dimensions. There is, and this is what I should have made a little clearer at the end of part II:
  • A prim box has 6 SL-faces (one per side) and each SL-face can be selected independently, then given a different texture which you might then scale, rotate or tint using the controls in the Texture tab of your viewer's editor. Each side of the box is "texture-independent" because each is a separate SL-face.
  • Our mesh box has only 1 SL-face because we've only assigned 1 material to it and each material is imported as a SL-face. When we apply a texture that face in-world, the texture is mapped onto the object based precisely on the UV map we've supplied. If we scale or rotate the in-world texture, it affects all sides of the box equally because as far as it's concerned the entire box is just a single SL-face. This takes a little time to get used to working with.
There is an -in-Blender way of setting up the material to allow us to scale the texture on the surface of our mesh to allow us to preview it on the screen, but that's a bit beyond the scope of this tutorial.

As an exercise (that also helps illustrate a number of other points), let's make our box into something that would behave identically in-world to the way a prim cube does. Since this means that we are going to want 6 SL-faces in-world, and each SL-face or a mesh is a different material, this means we're going to need a total of 6 different materials assigned to our mesh box -- one to each side. What is often difficult to grasp as a beginner is that this does not mean that we need to break up our UV map! The map is independent of the material.

To add an additional material to a mesh object, click the small "+" sign on the right-hand side of the list of materials in the Properties pane's Materials tab. This will then add a new material slot to the list for this object and everything else we do is the same as we did when setting up our first material. This time, though, we're just going to give it a different viewport colour but not bother to assign an image texture to it. If you need a detailed reminder of the steps, they're in the picture.


We've added the new material, but nothing has changed on our model. That's because when we assign the very first material to an object Blender automatically assigns it to all faces (Blender-faces) of the object. For any extra materials you add to the object, you need to explicitly tell Blender which faces should use that newly-defined material.

Select all of the faces on the front side of our box. I used face-select mode and the Circle select tool but you can use any method you like (see the Baby Steps series of tutorials). You'll notice that if you're in face select mode the selected material in the list in the Material properties will automatically highlight our previous brick texture because that's the material currently assigned to the faces you're selecting.

Select our "SL-face 2" material from the list, then click the "Assign" button to tell Blender to assign the selected faces to this material instead. As soon as you do so, your selected faces will lose their brick texture and show the purple colour we picked for the viewport display of 2nd material.


Now we need to repeat the above steps to create materials called SL-face 3, 4, and 5. Then add one more called SL-face 1 because in SL the first face actually has the number assignment  "0". Our brick texture can be our SL-face 0 material. You can create all of these extra materials before you assign them since the materials are independent entities.

When you create a material, even through you're doing it this way with an object selected, what you're actually doing is creating an available material for the scene. Because of the (in my opinion very convenient) method we're using, this newly-created material is being added automatically to the select object. That identical material is available to any and all other scene objects and any change you make to the material's properties is automatically reflected in all objects that have that material assigned to it.

To make our box exactly like an in-world prim we'd have to figure out which number to assign to which side of our box but for our purposes let's not take the time and effort to do that. Once you've finished, you could upload two copies of your box -- the one with only 1 material assigned to it and the new one with 6 materials -- and rez them both to the ground then play with textures on them. Your second box will be almost identical to working with a prim cube.

One useful thing to discover at this point is a handy Edit mode selection method that we didn't talk about in the Baby Steps III tutorial. Try selecting none of the faces, then choose (select) one of the materials in the list of materials assigned to the box, then click the "Select" button. It will immediately select just those faces/edges/vertices (depending on which selection mode you're in). You can add to the selection by picking another material and clicking the "Select" button again since this is "add to the current selection" rather than "replace the selection."

Similarly, you could select the entire box then select the name of a material and click the "Deselect" button to deselect those faces or edges or vertices. The selection method you're using when you deselect a material is important because vertices and edges will have more than 1 material assigned to it if it's at the boundary between two materials. If you're in vertex select mode and deselect the SL-face 1 material, you'll also be deselecting some vertices that belong to some of the other textures which will affect which faces are selected. If you do the same thing in in face select mode only the faces assigned to that specific material are deselected.

As a general rule of thumb to avoid mistakes, it's not a bad idea to switch to face select mode before using either button, then switch back to vertex or edge select mode if you need to do an operation with a vertex or edge context.

This also illustrates that it might often be convenient to assign materials to faces even if you haven't yet UV-unwrapped your object because then you can use them to speed up your workflow if you need to rapidly select or deselect parts of the object while working with it. For a complex object you could conceivably create dozens of different "working materials" to make selection easy; then delete them later to get yourself back within SL limits.

Speaking of deleting a material...you need to be aware that there are two "contexts" and therefore two methods for deleting a material.
  • delete a material from being assigned to an object
  • delete a material from the scene
To do either of these you must be in Object mode with the object selected.

If you just want to delete a material from this object you select the material in its list and click the "-" sign. This well remove it only from this object and any faces that were assigned to this material will now be automatically assigned to the next material above it in the list. If you delete the very first material in the list, they'll be assigned to the material that now becomes the 1st material. If there are no materials left in the list, your object will be back to having no materials assigned to it and look the way it does immediately after you've added the mesh object to the scene. This has absolutely no impact at all on the object's UV map because that's a completely separate, independent entity.

If you want to completely delete a material from the entire scene, select that material in any object that uses it and click the "X" symbol to the right of the location that you rename it. Be careful though! This deletes the material from every single object in the scene that uses it so all faces that used that material will then be reassigned to whatever material was above it  in that object's list (which might not be the same as the material above it in this object's list).

Generally speaking it's a lot safer to only delete a material from an object rather than the scene, even if no other objects are using it, just to avoid accidents. Blender does some "smart culling" of materials when you save a file. If there's a material that isn't being used by any object in the scene it won't bother saving it so the next time you load the file it will disappear. In the meantime, the material will still be held in memory and could be used for some other object.

You can even persist an unused material in the file by flagging it using the little "F" button beside its name. This tells Blender to store it even though it has no current users. If you've flagged a material, you need to hold down the shift key when you press the "X" button if you want to completely delete it from the scene and have Blender stop saving it.


While all of this is great for showing you how to create new materials, delete materials, and so on, it's not showing you any of the power of using mesh even for something as simple as a mesh box. If our goal was a box that's identical to a prim cube we'd just rez a prim cube. Let's do one very simple example of something that we can only do with a mesh box that will also introduce you to a concept about UV maps that is a little mind-bending when you first encounter it.

Delete all of the additional materials we just created except for our original brick material one one other (it doesn't matter which other one you keep) and rename that extra one "picture." If you kept SL-face 2 and made yours purple like mine is, you'll have something like this:


Now switch back into edit mode. select all faces and use the "Assign" button to assign them all back to the brick texture (leaving no faces currently assigned to the "picture" texture. Now deselect all faces and then select only the four faces in the center of the top of the box and assign only those four faces to the "picture" texture instead.


Let's think for a moment what would happen if we import this in-world and texture it. If we pick the mesh "prim" and apply a texture to it, it will look exactly the same as it did when it only had the single material assigned to it because the UV mapping is keeping everything nicely aligned and we're telling it to use the same texture in-world for both SL-faces. But what if I switch to the viewer's "Select (SL-)Face" mode and pick just that small rectangular part that we've assigned to our "picture" material and apply this picture to it instead:

Picture I took of my Hedonism dance club in Refugegrid
You are free to save a copy to work with for this tutorial

The following screenshot from in-world shows exactly what happens, with both being the identical mesh box. The one on the left is assigned only the brick texture so it's being assigned to both of the SL-faces of the mesh. Because they use the identical UV map which defines the entire mesh surface they're perfectly and seamlessly aligned. If I change the scale or rotation or any other aspect of the texture, they'll both be affected identically.

The Box on the right is something that's impossible to do with a single prim unless you make a special texture for the one side of it that has our Hedonism picture embedded into it, then apply that texture to that face. But if I later change my mind and want to use a different surrounding texture I'd have to go back and create a whole new special texture for that side. The same would be true if I changed my mind about what picture to display...I'd have to go and make a special texture for the whole side again. And then if I decided I wanted to scale my bricks to make them very small, I'd be back into PhotoShop again to make yet another special texture and be jumping through hoops to get everything nicely aligned.

I could achieve something like this using two prims -- the one with the picture raised above the surface of the other as is commonly done with picture frames in-world -- but this is just an extremely simple example to illustrate the point so the difficulty of replicating the effect using only prims isn't hard at all. Also, any time I have to use 2 or more prims to make something I'm immediately presented with the issues of adjusting and possibly scripting a linkset later rather than a single prim.

"But wait," I hear you thinking, "we're only seen a small part of the Hedonism picture in that space." Yes, that's true and let's demonstrate why. Take a copy of my Hedonism picture (or use some other picture you already have stored on your computer instead, if you prefer) and use the same method we used in Part I of this tutorial to assign it to our picture material. Then in the UV/Image Editor pane select it from the drop-down list of loaded images so it's displaying in the pane, and select the four faces you assigned to the "picture" material on your mesh.


This explains what we're seeing in-world when our viewer's texture is still set to scale with x and y at once per dimension. This maps the entire image texture to fit the horizontal and vertical dimensions of the entire UV map instead of just this small section of it.

One solution to this is to adjust the in-world scaling (and vertical and horizontal offsets) of that SL-face until the image displays correctly. It would take a bit of fiddling around with it until you got it just right, but it's possible to do. Wouldn't it be handy if there was an easier way? Happily, there is, but first we need to take care of a second issue that would be a problem no matter whether we were working with prims or mesh: the "aspect ratio" of the image.

If we want to fill our material area with this perfectly square image we can't do it without distorting it. If we keep the rectangular shape our material, our Hedonism picture would have to be stretched (quite a lot!) left-to-right to make it fill that width. That means we need to fix our material area to make it square.

We're going to move our existing edges to do this and I'll just give you a fast "step by step do this" set of instructions rather than explain exactly what we're doing or why since that's more in the subject of modeling techniques.
  • In the 3D View pane, switch to edge selection mode
  • Hold down the ALT key and select one of the two left edges of our picture material. This will select the entire "edge loop" that runs all the way around the box.
  • Hold down the SHIFT key and ALT key and select one of the two right edges of our picture material. This will add that edge loop to our selection.
  • Use the hotkey combination S X 0.5 to scale our selection along only the x-axis by a factor of 50% which will result in the picture area now being square

Now we have a nice square area for our picture to go into (although it's a bit small for the overall size of the object but for this tutorial that's fine) but you'll notice that something odd happened to our materials displayed on the mesh. The bricks along those outer faces are now stetched quick badly, and the bricks along the inner faces and in our picture are are now squished left-to-right. That's because changes to our mesh do not change their corresponding locations on the UV map that we've already made. If you select all you'll see down in the UV map that they're all evenly distributed exactly as before so when our textures are mapped the get stretched as required on the mesh to match the mapping.

To resolve that, the easiest thing to do in this case is simply repeat the same method with our map that we just used on our mesh.
  • In the 3D Pane select all (hotkey A) so we can see our entire UV map
  • In the UV/Image Editor pane switch to edge UV selection mode
  • ALT + select one of the inner left edges to select that entire loop
  • SHIFT + ALT + select one of the inner right edges to add that entire loop to the selection
  • Scale them using the hotkey combination S X 0.5 (we have to use x, not u for the hotkey even though we're technically scaling the u-axis not the x-axis)

This resolves our brick-stretching issue and gives us a nice square area for our picture material. There are other methods we could have used achieve this but doing it the way we did allowed me to show you the effect of editing parts of the mesh after you've already mapped it.

Now we get to the part I referred to earlier as being a little "mind-bending" about UV maps. While our picture material is now nice and square, it is still mapped to only a very small portion of the total image area. To make our in-world texturing job incredibly easy, it would be nice if we could just cut that little part out and map it differently than the rest of the mesh to make it fill the whole space. We can!

In the 3D View pane switch to face select mode and select only our four picture material faces. Then use the UV Unwrap drop-down and pick "Unwrap" again. If you recall, in Part II of this tutorial I was very careful to explicitly say "Blender's unwrap functions only unwrap and optimize the unwrapping for the currently selected faces" which means when we do this second unwrapping of just these four faces it tries to fill the image area with it as best possible while minimizing the stretching. Since the image area is perfectly square and so are our combined 4 faces, they fit perfectly in the entire map.


You'll now see the entire Hedonism picture filling the area of the picture material because we've now remapped them to do so. The mind-bending part for a novice Blender user is that this in no way affects our existing UV map for all of the other faces. I won't show a picture of it here because it would look a little confusing, but try selecting the entire object again in the 3D view pane and then look closely at the UV map. The square we just cut out is now missing from the original map portion and is super-imposed on the entire image area, but all of the other faces on the map are unchanged.

This means our vertices around the outer edges of the image which belong to the picture material are also the same vertices as the ones outlining the hold we cut into our original map and belong to the brick material -- they're in two places at once! Although confusing, this doesn't cause any issues at all and is not at all uncommon to see on some UV maps where you're mixing multiple materials on the same map and maximizing the resolution and ability to seamlessly scale textures on their surfaces. Here's what our box looks like in world after this remapping.


I should point out that this overlapping UV practice is generally discouraged for "real" Blender artists where there are none of the constraints we face with mesh uploads for SL/Opensim. For render output directly from Blender they would normally prefer to keep their maps much cleaner (for ease of editing) and approach this sort of thing in a different way that works perfectly in Blender but would result in a fairly horrible appearance if uploaded in-world.

The important point to take from this exercise is how little Blender cares about the relationship between the shape of the mesh and the way it's UV mapped. The Opensim viewer shares that feeling. All they really care about is that each Blender-face has a valid UV mapping assignment. You could, in fact, individually map all 96 faces of our box separately even if they were all assigned to the same material. In Blender, you could even create 96 different materials and make each face of the mesh into a unique one. For upload in-world, you're limited to 8.

Although I'd hoped to show a little bit about unwrapping a couple other objects (a cylinder and sphere) in this part of the tutorial, I realize that it's already getting pretty lengthy so let's leave that until Part IV and end this one with one more, different, approach to unwrapping our box just to show you another aspect of even a simple object's unwrapping.

Use the tab hotkey to switch to Object mode and delete the picture material (using the "-" sign). Notice that even though we've deleted that material, the mapping hasn't been lost and your brick texture will appear on the surface of the entire box looking like it does on the left-hand box in my in-world picture. Now tab back into Edit mode.

Deselect all and switch to edge selection mode, then select the 6 box side edges that you haven't already marked with seams and then mark them


Now select all and unwrap the entire box again using the same unwrap method we've been using for all of our previous unwrapping. Now look at the results in your UV/Image editor pane (you'll want to switch back to displaying the brick texture here since that's the one being used by our material). You may also want do select none with the mouse pointer in this pane, then switch to island selection mode so you can click on each one individually to see it a little more clearly. You can also select one side of the box in the 3D Image pane to see its corresponding location on the UV map.


This is quite a marked different mapping of a box and is extremely common for Blender artists. It's also an approach you would typically take if you're making a single custom texture to go on the object instead of using multiple in-world textures or tiling seamless textures.

When you isolate part of a mesh by marking seams around it, Blender approaches its "unwrap" method of unwrapping it a little differently. Each part of the mesh that has been separated from the rest by being enclosed by seams is now unwrapped and optimized separately. Then once all pieces are done, Blender scales them all to match one another and then arranges them to try to best fit them in the overall available space of the image area of the map. This will often result in individual pieces being rotated in unexpected ways and (as far as I can tell) there's a very convoluted logic in the positioning and rotation of the individual pieces.

You'll often find that if you unwrap the same (complex) object several times in a row, it might arrange them a little differently. Further, immediately after unwrapping you can change the Method in the Most Recent Action section of the Toolshelf to see if there's any benefit to using the Conformal rather than Angle Based method. To be perfectly honest I'm not entirely clear on the difference between the two although my understanding is that it affects the logic Blender uses to determine the "least stretching" and "best arrangement" and "equal scaling" goals of the the Unwrap method.

All of this means that your result could easily be different than my picture above, and your result might even vary a little if you unwrap again. The maps will be "right" but the rotations and arrangements might be different.

As a part of your workflow, expect that you'll have to do some work on individual islands after unwrapping; moving and rotating them as necessary to get them into a useful arrangement for import in-world. Again, these are concerns that Blender artists don't have...they are working directly with it in Blender that issue simply doesn't exist for them.

I'll conclude this part of the tutorial by urging you to play around some more with the things we touched on in Part III. UV mapping and materials assignments are a critical part of what makes mesh so appealing to work with in-world but so difficult to master. There's an art to it that, quite frankly, has a long slow learning curve. I'm still learning new things or approaches with almost every new project I undertake.

In Part IV we'll shift our attention to things that aren't as nice and convenient a shape as a simple box. Brace yourselves, it gets even more entertaining...

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
Licence

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.