In this hour, you learn all about models and how they are used in Unity. You start by looking at the fundamental principles of meshes and 3D objects. From there, you learn how to import your own models or use ones acquired from the Asset Store. You finish this hour by examining Unity’s material and shader functionality.
Note: Why Triangles?
You might be asking yourself why 3D objects are made up entirely of triangles. The answer is simple. Computers process graphics as a series of points, otherwise known as vertices. The fewer vertices an object has, the faster it can be drawn. Triangles have two properties that make them desirable. The first is that whenever you have a single triangle, you need only one more vertex to make another. To make one triangle, you need three vertices, two triangles take only four, and three triangles require only five. This makes them very efficient. The second is that by using this practice of making strips of triangles, you can model any 3D object. No other shape affords you that level of flexibility and performance.
Video games wouldn’t be very video without the graphical components. In 2D games, the graphics consist of flat images called sprites. All you needed to do was change the x and y positions of these sprites and flip several of them in sequence and the viewer’s eye was fooled into believing that it saw true motion and animation. In 3D games, however, things aren’t so simple. In worlds with a third axis, objects need to have volume to fool the eye. Because games use a large number of objects, the need to process things quickly was very important. Enter the mesh. A mesh, at its most simple, is a series of interconnected triangles. These triangles build off of each other in strips to form basic to very complex objects. These strips provide the 3D definitions of a model and can be processed very quickly. Don’t worry, though; Unity handles all of this for you so that you don’t have to manage it yourself. Later in this hour, you’ll see just how triangles can make up various shapes in the Unity Scene view.
Note: Model or Mesh?
The terms model and mesh are similar, and you can often use them interchangeably. There is a difference, however. A mesh contains all the points and lines that defines the 3D shape of an object. When you refer to the shape or form of a model, you are really referring to a mesh. A model, therefore, is an object that contains a mesh.
A model has a mesh to define its dimensions, but it can also contain animations, textures, materials, shaders, and other meshes. A good general rule is this: If the item in question contains anything other than vertex information, it is a model; otherwise, it is a mesh.
Unity comes with a few basic built-in meshes (or primitives) for you to work with. These tend to be simple shapes that serve simple utilities or can be combined to make more complex objects. Figure 3.1 shows the available built-in meshes. (You worked with the cube and sphere in the previous hours.)
Having built-in models is nice, but most of the time your games will require art assets that are a little more complex. Thankfully, Unity makes it rather easy to bring your own 3D models into your projects. Just placing the file containing the 3D model in your Assets folder is enough to bring it into the project. From there, dragging it into the scene or hierarchy builds a game object around it. Natively, Unity supports .fbx, .dae, .3ds, .dxf, and .obj files. This enables you to work with just about any 3D modeling tool.
You don’t have to be an expert modeler to make games with Unity. The Asset Store provides a simple and effective way to find premade models and import them into your project. Generally speaking, models on the Asset Store are either free or paid and come alone or in a collection of similar models. Some of the models come with their own textures, and some of them are simply the mesh data.
Applying graphical assets to 3D models can be daunting for beginners, if you are not familiar with it. Unity uses a simple and specific workflow that gives you a lot of power when determining exactly how you want things to look. Graphical assets are broken down into textures, shaders, and materials. Each of these is covered individually in its own section, but Figure 3.5 shows you how they fit together. Notice that textures are not applied directly to models. Instead, textures and shaders are applied to materials. Those materials are in turn applied to the models. This way, the look of a model can be swapped or modified quickly and cleanly without a lot of work.
Note: That’s an Unwrap!
Imagining how textures wrap around cans is fine, but what about more complex objects? When creating an intricate model, it is common to generate something called an unwrap. The unwrap is somewhat akin to a map that shows you exactly how a flat texture will wrap back around a model. If you look in the Robot Kyle > Textures folder from earlier this hour, you notice the Robot_Color texture. It looks strange, but that is the unwrapped texture for the model. The generation of unwraps, models, and textures is an art form to itself and is not covered in this text. A preliminary knowledge of how it works should suffice at this level.
Caution: Weird Textures
Later in this hour, you will apply some textures to models. You might notice that the textures warp a bit or get flipped in the wrong direction. Just know that this is not a mistake or an error. This problem occurs when you take a basic rectangular 2D texture and apply it to a model. The model has no idea which way is correct, so it applies the texture however it can. If you want to avoid this issue, use textures specifically designed for (unwrapped for) the model that you are using.
Textures are flat images that get applied to 3D objects. They are responsible for models being colorful and interesting instead of blank and boring. It can be strange to think that a 2D image can be applied to a 3D model, but it is a fairly straightforward process once you are familiar with it. Think about a soup can for a moment. If you were to take the label off of the can, you would see that it is a flat piece of paper. That label is like a texture. After the label was printed, it was then wrapped around the 3D can to provide a more pleasing look.
Just like all other assets, adding textures to a Unity project is easy. Start by creating a folder for your textures; a good name would be Textures. Then drag any textures you want in your project into the Textures folder you just created. That’s it!
If the texture of a model determines what is drawn on its surface, the shader is what determines how it is drawn. Here’s another way to look at this: A material contains properties and textures, and shaders dictate what properties and textures a material can have. This might seem nonsensical right now, but later when we create materials you will begin to understand how they work. Much of the information about shaders is covered later this hour, because you cannot create a shader without a material. In fact, much of the information to be learned about materials is actually about the material’s shader.
As mentioned earlier, materials are not much more than containers for shaders and textures that can be applied to models. Most of the customization of materials depends on which shader is chosen for it, although all shaders have some common functionality.
To create a new material, start by making a Materials folder. Then right-click the folder and select Create > Material. Give your material some descriptive name and you are done. Figure 3.6 shows two materials with different shader settings. Notice how they both use the same Standard shader. Each has a base Albedo color of white. A Smoothness setting of zero is a rough surface, and the lighting looks very flat as the light bounces in a lot of directions. A higher setting leads to a shinier look. There is also a preview of the material (blank now because there is no texture).
Now that you understand textures, models, and shaders, it is time to look at how it all comes together. Unity 5 has a very powerful Standard shader, which we will be focusing on in this book. Table 3.1 describes the common shader properties.
This might seem like a lot of information to take in, but once you become more familiar with the few basics of textures, shaders, and materials, you’ll find this a smooth process.
Unity has several other shaders which we won’t cover in this book. The Standard Shader is very flexible, and will cover most of your basic needs.
In this hour, you learned all about models in Unity. You started by learning about how models are built with collections of vertices called meshes. Then, you discovered how to use the built-in models, import your own models, and download models from the Asset Store. You then learned about the model art workflow in Unity. You experimented with textures, shaders, and materials. You finished by creating a textured brick wall.
Q. Will I still be able to make games if I’m not an artist?
A. Absolutely. Using free online resources and the Unity Asset Store, you can find various art assets to put in your games.
Q. Will I need to know how to use all the built-in shaders?
A. Not necessarily. Many shaders are very situational. Start with the shader covered in this chapter and learn more if a game project requires it.
Q. If there are paid art assets in the Unity Asset Store, does that mean I can sell my own art assets?
A. Yes, it does. In fact, it is not limited to only art assets. If you can create high-quality assets, you can certainly sell them in the store.
Let’s experiment with the effects shaders have on the way models look. You will use the same mesh and texture for each model; only the shaders will be different. The project created in this exercise is named Hour 3_Exercise and is available in the Hour 3 book files.
1. Create a new scene or project.
2. Add a Materials and a Textures folder to your project. Locate the files Brick_Normal.png and Brick_Texture.png in the Hour 3 book files and drag them into the Textures folder.
3. In the Project view, select Brick_Texture. In the Inspector view, change the aniso level to 3 to increase the texture quality for curves. Click Apply.
4. In the Project view, select Brick_Normal. In the Inspector view, change the texture type to Normal Map. Click Apply.
5. Select the Directional Light in your Hierarchy, and give it a position of (0, 10, –10) with a rotation of (30, –180, 0).
6. Add four spheres to your project. Scale them each to (2, 2, 2). Spread them out by giving them positions of (1, 2, –5), (–1, 0, –5), (1, 0, –5), and (–1, 2, –5).
7. Create four new materials in the Materials folder. Name them DiffuseBrick, SpecularBrick, BumpedBrick, and BumpedSpecularBrick. Figure 3.9 contains all the properties of the four materials. Go ahead and set their values.
8. Click and drag each of the materials onto one of the four spheres. Notice how the light and the curvature of the spheres interact with the different shaders. Remember that you can move about the Scene view to see the spheres at different angles.