Modeling a bench in Blender - Unity in Action: Multiplatform game development in C# with Unity 5 (2015)

Unity in Action: Multiplatform game development in C# with Unity 5 (2015)

Appendix C. Modeling a bench in Blender

In chapters 2 and 4 we looked at creating levels with large flat walls and floors. But what about more detailed objects? What if you want, say, interesting furniture in the room? You can accomplish that by building 3D models in external 3D art apps. Recall the definition from the introduction to chapter 4: 3D models are the mesh objects in the game (that is, the three-dimensional shapes). In this appendix I’ll show you how to create a mesh object of a simple bench (see figure C.1).

Figure C.1. Diagram of the simple bench you’re going to model

While appendix B lists a number of 3D art tools, we’ll use Blender for this exercise because it’s open source and thus accessible to all readers. You’ll create a mesh object in Blender and export that out to an art asset that works with Unity.

Tip

Modeling is a huge topic, but we’ll cover only a handful of modeling functions that will allow you to create the bench. If you want to keep learning more about modeling after this chapter, look to some of the many books and tutorials on the subject (to start with, look at the learning resources on www.blender.org).

Warning

I used Blender 2.67, so the explanations and screenshots come from that version of the software. Newer versions of Blender are released frequently, and there may be slight changes to the placement of buttons or names of commands.

C.1. Building the mesh geometry

Launch Blender; the initial default screen looks like figure C.2, with a cube in the middle of the scene. Use the middle-mouse button to manipulate the camera view: click and drag to tumble, Shift with click-drag to pan, and Control with click-drag to zoom.

Figure C.2. The initial default screen in Blender

Blender starts out in Object mode, which, as the name implies, is when you manipulate entire objects, moving them around the scene. To edit a single object in detail, you must switch to Edit mode; figure C.3 shows the menu you use (Edit appears in this menu only when an object is selected, and Blender starts out with the object selected). Similarly, when you first switch to Edit mode, Blender is set to Vertex Selection mode, but there are buttons (refer to figure C.4) that let you switch between Vertex, Edge, and Face Selection modes. The various selection modes allow you to select different mesh elements.

Figure C.3. Menu for switching from Object to Edit mode

Figure C.4. Controls along the bottom of the viewport

Definition

Mesh elements are the vertices, edges, and faces that comprise the geometry of the mesh—in other words, the individual corner points, the lines connecting the points, and the shapes filled in between connected lines.

Fundamental mouse and keyboard shortcuts in Blender

Also depicted in figure C.4 are the various transform tools. As in Unity, the transforms are Translate, Rotate, and Scale. The first button toggles the Transform Gizmo (the arrows in the scene) on and off; I recommend leaving that gizmo on, because otherwise you can only access the transform tools via keyboard shortcuts. The keyboard shortcuts in Blender are often unexpected, as is the mouse functionality.

For example, though the use of the middle-mouse to manipulate the camera makes intuitive sense, selecting elements in the scene is done with the right mouse button (in most applications the left mouse button selects things). Even weirder, a box selection is done by pressing B and then left-clicking-and-dragging. You add to the selection (as opposed to replace the selection) by holding Shift while clicking on elements, and you clear the selection by pressing A.

These are the basic controls for using Blender, so now we’ll see some functions for editing the model. To start with, scale the cube into a long, thin plank. Select every vertex of the model (be sure to also select vertices on the side of the object facing away) and then switch to the Scale tool. Click-drag the blue arrow for the Z-axis to scale down vertically, and then click-drag the green arrow for Y to scale out sideways (see figure C.5).

Figure C.5. Mesh scaled into a long, thin plank

Switch to Face Selection mode (use the button indicated in figure C.4) and select both small ends of the plank. Now click on the Mesh menu at the bottom of the viewport and select Extrude Individual (see figure C.6). As you move the mouse you’ll see additional sections added to the ends of the plank; move them out slightly and then left-click to confirm. This additional section is only the width of the bench legs, giving you a little additional geometry to work with.

Figure C.6. In the Mesh menu use Extrude Individual to pull out extra sections.

Definition

Extrude is when you push out new geometry with a cross-section in the shape of the selected faces. The two different extrude commands define what to do when multiple elements are selected: Extrude Individual treats each element as a separate piece to extrude, whereas Extrude Region treats the entire selection as a single piece.

Now look at the bottom of the plank and select the two thin faces on each end. Use the Extrude Individual command again to pull down legs for the bench (refer to figure C.7).

Figure C.7. Select the thin faces underneath the bench and pull down legs.

The shape is complete! But before you export the model over to Unity, you want to take care of texturing the model.

C.2. Texture-mapping the model

3D models can have 2D images (referred to as textures) displayed on their surface. How exactly the 2D images relate to the 3D surface is straightforward for a large flat surface like a wall; simply stretch the image across the flat surface. But what about an oddly shaped surface, like the sides of the bench? This is where it becomes important to understand the concept of texture coordinates.

Texture coordinates define how parts of the texture relate to parts of the mesh. These coordinates assign mesh elements to areas of the texture. Think about it like wrapping paper (see figure C.8); the 3D model is the box being wrapped, the texture is the wrapping paper, and the texture coordinates represent where on the wrapping paper each side of the box will go. The texture coordinates define points and shapes on the 2D image; those shapes correlate to polygons on the mesh, and that part of the image appears on that part of the mesh.

Figure C.8. Wrapping paper makes a good analogy for how texture coordinates work.

Tip

Another name for texture coordinates is UV coordinates. This name comes from the fact that texture coordinates are defined using the letters U and V, like coordinates on the 3D model are defined using X, Y, and Z.

The technical term for correlating part of one thing to part of another is mapping—hence the term texture mapping for the process of creating texture coordinates. Coming from the wrapping paper analogy, another name for the process is unwrapping. And then there are terms created by mashing up the other terminology, like UV unwrapping; there are a lot of essentially synonymous terms surrounding texture mapping, so try not to get confused.

Traditionally the process of texture mapping has been wickedly complicated, but fortunately Blender provides tools to make the process fairly simple. First you define seams on the model; if you think further about wrapping around a box (or better yet, think about the other direction, unfolding a box) you’ll realize that not every part of a 3D shape can remain seamless when unfolded into two dimensions. There will have to be seams in the 3D form where the sides come apart. Blender enables you to select edges and declare them as seams.

Switch to Edge Selection mode (see the buttons in figure C.4) and select edges along the outside of the bottom of the bench. Now select Mesh > Edges > Mark Seam (see figure C.9). This tells Blender to separate the bottom of the bench for purposes of texture mapping. Do the same thing for the sides of the bench, but don’t separate the sides entirely. Instead, only seam edges running up the legs of the bench; this way, the sides will remain connected to the bench while spreading out like wings.

Figure C.9. Seam edges along the bottom of the bench and along the legs

Once all the seams are marked, run the Texture Unwrap command. First select the entire mesh (don’t forget the side of the object facing away). Next, choose Mesh > UV Unwrap > Unwrap to create the texture coordinates. But you can’t see the texture coordinates in this view; Blender defaults to a 3D view of the scene. To see the texture coordinates you must switch from 3D View to UV Editor, using the Viewports menu located on the far left of the toolbar (not the word View but the little icon; see figure C.10).

Figure C.10. Switch from 3D View to UV Editor, where the texture coordinates are displayed.

Now you can see the texture coordinates. You can see the polygons of the bench laid out flat, separated and unfolded according to the seams you marked. To paint a texture, you have to see these UV coordinates in your image-editing program. Referring again to figure C.10, under the UVs menu choose Export UV Layout; save the image as bench.png (this name will also be used later when importing into Unity).

Open this image in your image editor and paint colors for the various parts of your texture. Painting different colors for different UVs will put different colors on those faces. For example, figure C.11 shows darker blue where the bottom of the bench was unfolded on the top of the UV layout, and red was painted on the sides of the bench. Now the image can be brought back into Blender to texture the model; select Image > Open Image.

Figure C.11. Paint colors over the exported UVs and then bring the texture into Blender.

At this point you can return to the 3D view (using the same menu you used to switch to UV Editor). You still can’t see the texture on the model, but that only requires a couple more steps. You need to delete the default light and then turn on textures in the viewport (see figure C.12).

Figure C.12. Delete the default light and view the texture on the model.

To delete the light, first switch back to Object mode in order to select it (using the same menu you used to switch to Edit mode). Press X to delete a selected object; delete the camera while you’re at it. Finally, go to the Viewport Shading menu to switch to Texture. Now you can see the finished bench, with texture applied!

Go ahead and save the model now. Blender will save the file with the .blend extension, using the native file format for Blender. Use the native file format to work in so that all the features of Blender will be preserved correctly, but later you’ll have to export the model to a different file format for importing into Unity. Note that the texture image isn’t actually saved in the model file; what’s saved is a reference to the image, but you still need the image file that’s being referenced.