Showing posts with label game engine. Show all posts
Showing posts with label game engine. Show all posts

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!

Wednesday, November 20, 2013

Unity Engine - Building Games for Facebook, Playstation, XBox 360, etc. (Overview)

I know I'm not the only one intrigued by gaming, which is one reason I like to write about building games. So far I have written articles that include information on building games for Android and Kindle Fire. This article provides an overview to the basics of Unity 4.

Unity is a game engine that provides the ability to build games that can run on a variety of platforms including XBox, PlayStation, Windows Phone, Android , Blackberry, Webplayer, PC/Unix/Mac standalone and more, as shown in the following picture.


I think it's exciting to be able to build a game once and distribute it to multiple platforms because that means developers can distribute a game more widely, which equals a much larger audience and, ultimately, more money.

Unity Development Environment



Unity's core user interface includes the Project, Assets, Inspector, Hierarchy, Scene and Game windows. The following paragraphs discuss these windows in greater detail.

Project


In Unity you create a project to build a game. When a new project is created, you select a package to import. When you import a package the assets associated with the package are automatically included as part of the project when Unity opens. Sharing assets, such as audio files, 3D buildings, player avatars (humanoids) and other files decrease development time since some GameObjects are already completed. Developers then place and use the GameObjects the way they want to within their game.  For example, if you create a new project and import the Stealth package; all of the assets associated with the Stealth project will import into your new project including the 3D buildings, a 3D bus, humanoids, and more . Developers can then create a new game using one or more of the existing Stealth game assets; or even modify some of the assets before using them. Developer can then create new assets and incorporate them into the game.




Assets


In Unity, assets are files included in a project. Assets are available from the Project Browser. Unity has an Assets window that provides a way to view the assets within a folder displayed by the Project Browser window.


Note that assets may or may not be physically added to a scene. Although GameObjects are usually added to a scene. For example, audio files are assets and scripts are assets. Scripts reference GameObjects but you would not drop a script onto a game scene and expect to see the script do something.

Assets can be shared across multiple games because they are included in the game from a file that resides on your computer. Developers can buy assets, sell assets or even download free assets. Assets are available from the Asset Store at https://www.assetstore.unity3d.com/. And, developers can learn more about selling Unity assets by navigating to http://unity3d.com/asset-store/sell-assets.

GameObjects, Components & the Inspector Window


The Inspector Window provides a way to manipulate GameObjects by providing access to the GameObject's components. Notice the scene window below shows a GameObject selected  (a red arrow points to the GameObject). Notice the Inspector window, which is highlighted with a red box, shown in the picture below.


The Inspector window shows the components and related properties that can be manipulated to achieve a desired result. The Inspector window includes a Transform component used to indicate the position of the GameObject. The Transform component of the GameObject can also be used to scale or rotate the GameObject, as can be seen by viewing the Transform properties shown in the above picture. The following picture shows script components associated with the GameObject. Also notice a sound clip (named weapon_sciFiGun_fire) is part of the Done Enemy Shooting component and is used to bring shooting to life in the game.



In the example below, the script components were written using C#. However, developers can also use JavaScript to program games in Unity. To learn more about GameObjects read the documentation titled "GameObject" at http://docs.unity3d.com/Documentation/Manual/GameObjects.html.

Hierarchy


The Hierarchy window includes a list of gameobjects used to create a scene. For example, let's say you downloaded the Stealth game for Unity (https://www.assetstore.unity3d.com/#/content/7677). If you add the completed Stealth scene (called DoneStealth as shown in the following picture) to the Scene View, the Hierarchy shows all of the GameObjects used to complete the scene. It also shows parent/child relationships (as applicable) between objects. You can then click on each GameObject to view its related components in the Inspector window.


In addition, the Hierarchy window also includes a Create option. Developers use the Create menu to create GameObjects that display in a Scene. For example, a camera is added to a scene by selecting Create -> Camera.The camera is the GameObject through which players view the world built by the developer. One or more camera's are added to a scene so that players may view the entire world that makes up a scene.

In addition to Cameras additional GameObjects can be added such as spotlights, area lights, terrain, trees, or even cubes (that may be manipulated to serve as the ground a player walks on). The following picture shows the Create menu on the Hierarchy window.


Scenes


When a player plays a game, the game environment is delivered as a Scene. In Unity the Scene tab provides access to the Scene View. Through the Scene View developers can build what the user will see and the GameObjects with which the user will interact.

The toolbar (shown above) provides the tools developers use to manipulate and navigate to GameObjects. The hand tool enables developers to move around in the scene. The Translate tool (second from the left) provides a way to select GameObjects and drag them or enter values to place the object in a specific location. Developers can rotate GameObjects using the Rotate tool (third from the left); and, they can resize GameObject using the Scale tool (fourth from the left).

If you have downloaded the Stealth game for Unity you can see the Scene View in action by creating a new project (File -> New Project from within Unity) that includes the Stealth assets you downloaded.


To view the GameObjects and even the scripts used to build the game, navigate to the Done folder and locate the DoneScenes folder. Drag and drop the completed Stealth scene onto the Unity Scene view, as shown in the following picture. You can then play the game to see how it works.



Game 


You can then use the Game View to run and test games in Unity. To begin you must click on the Game tab, as shown by a red circle below. You then click the Play button, as highlighted by the red arrow in the following picture, to run the game.


When the game is running it behaves as it would behave if it were built and distributed to a platform. As a developer, you can use the Game View to perform all of the actions you have programmed the game to perform. You can learn more about the Game View by reading the article located at http://docs.unity3d.com/Documentation/Manual/GameView40.html.


Using Unity to Build Facebook Games


Developers can download the Facebook SDK to use Unity to create games for Facebook. For more information about the Facebook SDK for Unity view the Unity - Facebook Developers article at https://developers.facebook.com/docs/unity/

To learn more about the Unity game engine visit http://unity3d.com/unity.