Hour 3. Models, Materials, and Textures


What You’ll Learn in This Hour:

• The fundamentals of Models

• How to import custom and premade models

• How to work with Materials and Shaders


In this hour you will be learning all about models and how they are used in Unity. You will start by looking at the fundamental principles of meshes and 3D objects. From there you will learn how to import your own models or use ones acquired from the asset store. You will finish the chapter by examining Unity’s material and shader functionality.

The Basics of Models

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 3rd axis, objects need to have volume in order to fool the eye. As games employ 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. Later in this hour you will be examining just how triangles can make up various shapes in the Unity scene view.


Note: Why Triangles?

You might be asking yourself why 3D objects are made up entirely of triangles. The answer is very simple. Computers process graphics as a series of 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 only need one more vertex to make another. While one triangle takes 3 vertices to make, two triangles takes only four and three triangles only requires five. This makes them very efficient. The second is that using this practice of making strips of triangles, any 3D object can be modeled. No other shape affords you that level of flexibility and performance.



Note: Model or Mesh?

The terms “model” and “mesh” are very similar and can often be used interchangeably. There is a difference, however. A mesh contains all of the vertex information that defines the 3D shape of an object. When someone is referring to the shape or form of a model, they are really referring to a mesh. A model then 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.


Built in 3D Objects

Unity comes with a few basic built in meshes for you 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 you what built in meshes are available. You have worked with the cube and sphere in the previous hours of this book.

Figure 3.1. The Built in Meshes in Unity

Image

Tip: Modeling with Simple Meshes

Do you need a complex object in your game but you can’t find the right type of model to use? Nesting objects in Unity allows you to easily make simple models using the built in meshes. Simply place the meshes near each other so that they form the rough look you are going for. Then nest all of the objects under one central object. This way, when you move the parent all the children move as well. This may not be the prettiest way to make models for your game, but it will do in a pinch!


Importing Models

While having built in models is nice, most of the time your games will require art assets that are a little more complex. Thankfully, Unity makes is rather easy to bring your own 3D models into your projects. Simply 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 will build a game object around it. Natively, Unity supports .fbx, .dae, .3ds, .dxf, and .obj files. This gives you the ability to work with just about any 3D modeling tool.


Caution: Default Scaling of Meshes

Most of the inspector view options for meshes are advanced and will not be covered right now. The property you are interested is the “Scale Factor”. By default, Unity imports meshes scaled down. By changing the value of the scale factor from 0.01 to 1, you are telling Unity to allow the model to enter the scene as the same size as it was created.


Models and the Asset Store

You don’t have to be an expert modeler in order to make games with Unity. The asset store provides a simple and effective way to find premade models and import then 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.

Textures, Shaders, and Materials

Applying graphical assets to 3D models can be very daunting if you are not familiar with it. Unity employs a very 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 will be covered individually in their own sections, but Figure 3.5 shows you how they fit together. You will 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 requiring a lot of work.

Figure 3.5. The Model Asset Workflow

Image

Textures

Textures are flat images that get applied to 3D objects. They are responsible for models being colorful and interesting instead of white 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. Once 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!


Note: That’s an Unwrap!

Imaging 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 something 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 will 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 will not be covered with this text. You should be able to suffice at this level with a preliminary knowledge of how it works.



Caution: Weird Textures

Later in this hour you will be applying some textures to models. You may 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 is a problem that 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, you need to use textures specifically designed for (unwrapped for) the model that you are using.


Shaders

If the texture of a model determines what is drawn on its surface, then the shader is what determines how it is drawn. Another way to look at it is while a material contains properties and textures, shaders dictate what properties and textures a material can have. This may seem nonsensical right now, but later when we create materials you will begin to understand how they work. Much of the information about shaders will be covered later this hour as 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.


Tip: Thought Exercise

If you are having trouble understanding how a shader works, consider this scenario: Imagine you have a piece of wood. The physicality of the wood is its mesh, while the color, texture, and visible element is its texture. Now take that piece of wood and pour water on it. The wood still has the same mesh. It still is made of the same substance (wood). It looks different, however. It is slightly darker and shiny. The water in this example is the shader. The shader took something and made it look a little different without actually changing it.


Materials

As mentioned above, 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, though all shaders have some common functionality.

To create a new material, start by making a “Materials” folder. Then, right click on the folder and select “Create->Material”. Give your material some descriptive name and you are done. Figure 3.6 shows two materials with different shaders selected. Notice how they each have a base texture, main color, tilling and offsets, and a preview of the material (blank now because there is no texture). The “Shiny” material, however, uses a specular shader and comes with properties for specular color and shininess. All of these properties will be covered below.

Figure 3.6. Two Materials with Different Shaders

Image

Shaders, Revisited

Now that you have an understanding of textures, models, and shaders it is time to look at how it all comes together. Unity has a lot of built in shaders, but this book will be concerned only with a few of the “Normal” family of shaders. These are the shaders that are the most basic and should be useful for everyone. Table 3.1 lists some of the basic shaders and describes them.

Table 3.1. Basic Normal Family Shaders

Image

Now that you are familiar with a few of the built in shaders, it is time to look at some of the common shader properties that you will be working with. Table 3.2 lists and describes the common shader properties.

Table 3.2. Common Shader Properties

Image

This may seem like a lot of information to take in, but once you become more familiar with the few basics of textures, shaders, and materials, this will become a very smooth process.

Summary

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&A

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 all manner of art assets to put in your games.

Q. Will I need to know how to use all of the built in shaders?

A. Not necessarily. There are many shaders that are very situational. Start with the shaders 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, then you can certainly sell them in the store.

Workshop

Take some time to work through the questions here to ensure you have a firm grasp on the material.

Quiz

1. True or False: Due to their simple nature, squares make up meshes in models.

2. What file formats does Unity support for 3D models?

3. True or False: Only paid models are able to be downloaded from the Unity asset store.

4. Explain the relationship between textures, shaders, and materials.

Answers

1. False, meshes are made up of triangles

2. .fbx, .dae, .3ds, .dxf, and .obj files

3. False, there are several free models as well

4. Materials contain textures and shaders. Shaders dictate the properties that can be set by the material and how the material gets rendered.

Exercise

Let’s experiment with the effects shaders have on the way models look. You will be using the same mesh and texture for each model and only the shaders will be different. The project created by this exercise is named “Hour3_Exercise” and is available in the Hour 3 book files.

1. Create a new scene or project.

2. Add a “Materials” and “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 the “Brick_Texture”. In the inspector view, change the “Aniso Level” from 1 to 3 to increase the texture quality for curves. Hit “Apply”.

4. In the project view, select the “Brick_Normal”. In the inspector view, change the “Texture Type” to “Normal Map”. Hit “Apply”.

5. Add a directional light to your project (click “GameObject->Create Other->Directional Light”) and give it a position of (0, 10, -10) with a rotation of (30, 40, 0).

6. Add four spheres to your project. Scale them each to (2, 2, 2). Spread them out by giving them positions of (2, 0, -5), (-2, 0, -5), (-2, 2, -5), and (2, 2, -5).

7. Create four new materials in the “Materials” folder. Name them “DiffuseBrick”, “SpecularBrick”, “BumpedBrick”, and “BumpedSpecularBrick”. Figure 3.9 contains all of the properties of the four materials. Go ahead and set their values.

Figure 3.9. Material Properties

Image

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.249.252