Wednesday, January 1, 2014

Unity Game Engine's Mesh Renderer, Mesh Filter, Shaders and Materials Overview

Unity Game Engine provides a way for game developers to apply shaders, textures and colors to a polygon mesh. From the user avatar to the get-a-way car or even the abandoned science lab with broken furniture, rabid dogs and glass vials--the mesh filter and mesh renderer turn polygon meshes into recognizable objects. This article provides an overview and examples related to Unity's mesh filter and mesh renderer. All of the examples included in this article are taken from the Unity Stealth Project, which can be downloaded at https://www.assetstore.unity3d.com/#/content/7677.

Meshes


A mesh is a collection of triangles used to create an impersonation of an object in a 3D world. The following picture shows a partial mesh.


Meshes are used to define a 3D world because they render more quickly than images.



Each triangle, used to makeup the mesh has three corner points referred to as vertices. In programming, the mesh class (the interface to an object's mesh geometry) uses not only vertex arrays; but also triangle arrays.  The vertices of the triangles that make up the mesh are stored as integers in a single vertex array. The vertices are assigned to the mesh. The developer can then build the triangle indices--3 indices in the vertex array for each triangle. The triangles are then assigned to the mesh and the vertex normals can automatically be calculated using RecalculateNormals().


While the triangles give the mesh shape; the vertex normal values assist in providing lighting from the appropriate angles. This lighting is required so the mesh can be seen. For more information about normal vectors see http://docs.unity3d.com/Documentation/Manual/ComputingNormalPerpendicularVector.html.

In Unity, if an object is textured its wireframe can be seen (while using the Scene View) by selecting the Wireframe option, as shown in the picture below. Alternatively, you can select Textured Wireframe to see the materials associated with the mesh and the mesh as an overlay.



For more information on meshes see the Unity Script Reference Documentation at http://docs.unity3d.com/Documentation/ScriptReference/Mesh.html and the Unity Mesh documentation at http://docs.unity3d.com/Documentation/Manual/AnatomyofaMesh.html.

Selecting a Mesh Filter


You can drag a mesh onto the Scene View in Unity to specify the mesh you want to render. (For more information on the Scene View you can read my previous article titled Unity Engine - Building Games for Facebook, PlayStation, XBox 360, etc. (Overview). If you want to change a mesh, you can do so by selecting the Mesh Filter option on the Inspector window, which appears after you select a mesh or gameobject.


The above approach works well if you have a gameobject associated with materials; but want to change the selected mesh. To view the currently selected mesh, from the Inspector, select the gameobject using the Scene View. When the Inspector displays click the Select Mesh button (shown below). The Select Mesh window (shown in the picture above) displays. You can then view the currently selected mesh: or select a different mesh from the list of available meshes.


If you have imported a mesh from an application and want to apply shaders and textures you will need to use Unity's mesh renderer, which is discussed below.

Note: Although Unity does not include modeling tools; you can import meshes and materials from other tools (such as Autodesk SoftImage) into Unity. But keep in mind that the Unity Asset store does include Editor Extensions/Modeling plugins that provide a way to extend the Unity Engine to perform some 3D modeling tasks.

Mesh Renderer


Within Unity a mesh is rendered by applying a shader, which is a program that executes inside the graphics processing unit (GPU). The shader defines how data (associated with a graphic) is processed. For example, the shader defines how the graphic is rendered such as whether or not to cast shadows, lighting, color, etc. The following picture shows the properties associated with Unity's Bumped Specular Shader, which executes the commonly used vertex shader and fragment shader.


The shader also determines the render queue to which an object belongs. This is done so objects are drawn in the proper sequence. Unity ships with four pre-defined render queues as follows:
  • Background queue, which renders its objects first.
  • Geometry queue, which renders opaque geometry objects before the Alpha Test, Transparent and Overlay objects are rendered.
  • Alpha Test queue, which renders alpha-tested objects after the opaque geometry objects have been rendered.
  • Transparent queue, which renders objects such as glass and all other alpha-blended objects after the opaque geometry and alpha-tested objects are rendered.
  • Overlay queue, which renders lens flares and any other overlay effects after the Transparent objects are rendered.

The Stealth Project includes the prop_battlebus mesh, which uses 16 materials to apply shaders, textures and color to achieve the look shown below.


To display a gameobject the mesh filter passes the mesh to the mesh renderer, which applies the shaders, textures and color values.

To add shaders and textures you must first add a material element that will enable you to select the a material that includes the desired shader and texture files. The Stealth Project Battle Bus uses the following 16 materials, which can be applied by adding a material element for each material.
  1. prop_battlebus_body_mat
  2. prop_battleBus_wheels_mat
  3. prop_battleBus_lights_mat
  4. prop_battleBus_tv_mat
  5. prop_battleBus_sacks_mat
  6. prop_battleBus_canister_mat
  7. prop_battleBus_carpet_mat
  8. prop_battleBus_barbedWire_mat
  9. prop_battleBus_crates_mat
  10. prop_battleBus_appendages_mat
  11. prop_battleBus_tubes_mat
  12. prop_battleBus_baggageFrame_mat
  13. prop_battleBus_whiteMetal_mat
  14. prop_battleBus_blackMetal_mat
  15. prop_battleBus_boxes_mat
  16. prop_battleBus_canvas_mat

The following picture shows the prop_battlebus mesh displayed in the Scene View. Notice there is only 1 default material element (Element 0) added to a mesh. Also, the Mesh Filter is the prop_battleBus.


Click the Materials Size option to change the Size value from the default 1 to 16. Press enter to execute the change. Unity adds 16 materials so a material can be associated with each element until all materials have been added to the Battle Bus.


We can use the Select Material button to select the desired material for Element 0. I selected the prop_battlebus_body_mat material for Element 0, as shown in the following picture.



Once the material is associated with the prop_battleBus I can view the properties associated with the material, as shown in the following picture.


The shader associated with a material drives the properties available to the material. For example, the prop_battlebus_body_mat uses the Reflective Bumped Specular shader (as shown in the following picture). The Reflective Bumped Specular shader is a member of the Reflective Shader Family and includes reflective properties used to simulate metal objects.


You can learn more about shaders and related properties by viewing Unity's Built-in Shader Guide at http://docs.unity3d.com/Documentation/Components/Built-inShaderGuide.html.

If you click the Select button associated with the Base (RGB) Gloss (A) property I can see or change the 2D Texture .tif image associated with the prop_battlebus_body_mat material. You can also click the Select button associated with the Normalmap to see the normal map image used by normal map shaders to make low-polygon models appear to be more detailed.



To add the remaining materials you click the Select Material button associated with the remaining elements (1 through 15) and select the applicable material from the list presented earlier in this article. Once you have added all of the materials the Battle Bus will look like the picture shown below.


If you are new to Unity and/or 3D game development it helps to look at existing games to see how they are guilt and coded to get a better handle on the assets and capabilities your game can have. Unity's Asset Store provides a number of example projects that can be downloaded and used to learn more about Unity and 3D/2D games.

Happy New Year Everyone!