Monday 30 March 2015

Update on Scene Creation and Batching

After adding more of our assets into the game I can really appreciate the effect that static batching can have. I am experiencing big savings of draw calls now with more objects in game. More the triple the amount of draw calls i'm actually having to make which is great! So here are just a view progress screen shots of some more assets in game.







Saturday 28 March 2015

Importing Assets and Batching In Unity

Ok now I have the models unwrapped, textured and all using the texture atlas. Time to get them into the game to see if it was all worth it!

Importing the Models


When importing models into unity there are a few things to watch out for; for us it was the automatic smoothing that is applied when you first import your model. As we are working in a poly art style the last thing we want are our polys getting smoothed out. We want the amount of light that hits each face to be calculated from the normal and used across the entire poly. To fix this switch the normal and tangent options to calculate. Then bring the smoothing angle down to zero. I also want all my models to share the same material so I unchecked the import materials box. This looks much better and brings out the shape of our models.



Importing the Texture Atlas

When bringing any texture into unity you should check its settings after import. Unity will automatically set it as a low resolution so you should set its max size back to be closest to the actual size of the image. This is why I chose to make the texture map 2048x 2048; the options listed for size are all powers of two so I can set the texture to render at its true resolution. When theses settings have been changed the texture should appear in game at its proper resolution.


Applying the Materials

The way I apply the atlas is to select a model which will be using it. Drag and drop the texture onto the components list and Unity will automatically create a material. Now for every other model you want to texture simply add the Texture Atlas material to the material section on the mesh renderer component. Now these models are all sharing the same material.

Batching


So far the batching is looking very promising, as this is still a developing game there are far more assets to be imported. The team has also just upgraded to unity 5 so I want to make sure that working out the % savings is still the same procedure as unity 4.6. For the moment we appear to be saving roughly half the draw calls we would otherwise make without batching. I will update with full figure once more assets have been placed in game.



Texture Atlas and UV Remapping

So now I have all our models textured and unwrapped and ready to go.  What I want to do first is make up the texture atlas that will be applied to all models in game to allow for batching of draw calls to occur. This will be explained in more detail in a later post. I have elected to create the atlas manually as being a small game I am certain I can include all textures needed onto the same map.

So what is a Texture Atlas?

Example taken from gamasutra, referenced below

Essentially it is a large image containing many smaller images (2D or 3D textures) arranged in a collage or regular indexing pattern. Instead of loading in many textures to a game and applying each separately to your assets or models, a complete texture atlas or a few atlases containing all of your textures are loaded in instead. These atlases are shared between different models in your game. For example in a larger game you might have one Texture atlas containing the textures for all the environmental assets, one for character textures, etc. And atlas can usually hold anywhere between 16 to 126 textures.

Why use a Texture Atlas?

For games a texture atlas has performance benefits that cannot be ignored. Firstly they save on physical memory. When you bring a texture into your game engine it will be automatically re-sized and to a power of 2 number. For instance if I bring in a texture that is 400 x 400 pixels, it will be automatically padded up with white space to 512x512. This space is simply wasted memory. If you use a texture atlas you can pack your textures together in such a way as to minimize any wasted memory.


The other major advantage to using a texture atlas is that it increases batching in your game. When you are playing a game you are rendering scene after scene very quickly. Each object or model in the scene needs to be drawn. A draw call goes to the GPU or CPU to draw that object, the texture for the object is loaded into memory and the object is drawn, then it moves on to the next draw call. It must load in whatever texture that object is using, draw it and then move on again. However if the GPU did not need to stop an load in a new texture for each object before drawing could begin then the whole process would be a lot quicker. This is what batching does. If all the objects in game are being drawn from the same texture then the draw calls for those objects can be batched together. Calls to the GPU are very expensive in game terms so lightening this load is great for your games performance!

Making the Texture Atlas

I have opted to make my texture atlas by hand in Photoshop. There are many programs out there that can be used to do this for you, but has I said before, our game is small and I want maximum control over the result. To make the atlas I made a 2048 x 2048, 300 dpi blank image in Photoshop to form the base. Our textures are mostly small and simply colored so this size will be plenty big enough for our needs. I then started adding and arranging the different textures to the sheet and it’s slowly started filling out. I saved the image and this will form the material for each model in game. We are developing our game in an agile iterative environment so there atlas is still being added to as more models are made and textured.


Re-mapping UVs to Atlas

Again I have opted to take the manual approach, this is also said to be the more reliable approach when working in Unity. I could remap each models UV’s in unity to a section of the texture atlas using some specialized piece of code or plugin but I have instead opted to remap the UV’s by hand in 3DsMax. 

The approach I took was to:
  • Open a model in 3DsMax whose texture is contained in the atlas
  • Apply the UVW unwrap modifier, the seams created earlier should be visible again
  • Open the UV editor and view your wire frame unwrap
  • Click the drop down menu on the right and select pick texture, find the atlas
  • Select your entire unwrap wire-frame and uniform scale and move it until it is back over its section of the texture atlas.
  • Save the model with these new UV coordinates and repeat for other models.


Now all of these models will know exactly where their texture data is on the texture atlas when it is applied to them.

References :

Books

  • Bly, P. D., 2012. Holistic Game Development with Unity.
  • Nvidia, 2004. SDK White Paper - Improve Batching using Texture Atlasing .
  • McDermot, W., 2011. Creating 3D Game Art for the iPhone with Unity. 

Online
  • Ivanov, I.-A., 2015. Gamasutra. [Online] Available at: http://www.gamasutra.com/view/feature/2530/practical_texture_atlases.php?print=1
  • Nividia, n.d. Texture Atlas Tools. [Online] Available at: http://www.nvidia.com/object/texture_atlas_tools.html
  • Rayolsen, D., 2012. Texture Atlas Tutorial. [Online] Available at: http://forum.unity3d.com/threads/texture-atlas-tutorial.149939/
  • Wolfire Blog Games, 2014. Using Texture Atlases [Online] Available at: http://blog.wolfire.com/2010/03/Using-texture-atlases
  • Paladine Studios, 2012. 4 Ways To Increase Performance of your Unity Game [Online]Available at: http://www.paladinstudios.com/2012/07/30/4-ways-to-increase-performance-of-your-unity-game/

Texturing and Unwrapping


Unwrapping

As part of my advanced 3D concepts model I took on the task of getting all of our models textured and into our final project game “Haven”. I was given model meshes from both the environmental modeler and character molder to unwrap and paint on the textures. As our game is being made in a poly art style we had decided to go with a flat color pallet and really let the lighting in game do the work so the textures applied were purposefully very simple as you will see below.

My first task was to unwrap the environmental models and texture these using Photoshop. The images below show the texture map, and the texture applied to some of the models.



For each model I followed the general approach:
  • Remove any pre-existing seams * Tip
  • Applied UVW Unwrap modifier
  • Draw seams point to point
  • Unwrap each sub-section of the model
  • Arrange and pack unwrapped segments 
  • Render UVW Template to file * Tip
  • Collapse the stack (convert back to edible poly) and save file

*Clearing a of model seams

The way I do this is to first convert my model to an editable mesh. Then I go to utilities tab (little image of the hammer), click the more… tab and select UVW remove. Click ok and you should see a parameters box appear on the right panel. Click to remove the UVW and materials. (Sometimes this doesn't work straight away for me so I click each a few times for good measure and it always works) Now remember to convert you mesh back to an editable poly and now when you apply the unwrap modifier there should not be any annoying seams. This is also great if you have made a mess of your unwrap and want to start with a clean slate.

*Rendering UVW Template
I would recommend rendering the template as white wire map on black background as its will make it easy to extract the wire fame into its own layer in Photoshop later. From the UV editor window I select tools, render UVW template. I use the settings; fill: gray, mode: none, edges: white, visible edges and seam edges checked.

Texturing

Once I had the texture map I brought this into Photoshop and I could begin painting on the texture using the map as a guide. Depending on the complexity of the map and art style this can take a varying amount of time.  When the map is painted I save it as an image, can be jpeg, png, bmp etc. Then when I apply this to my model the texture will wrap around it as it was set out in the unwrap.



 Tip

To make the wire frame from the UVW template its own layer, open the image in Photoshop;
  • Convert background to a layer
  • Click image > adjustments >invert > to invert the colors.
  • Click select > Color range > and click anywhere that is white. This will select the entire white background and now you simply hit delete and you are left with the black wire frame.
And that's it! Texturing and coloring a model really brings it to life. Take the little blue guy above as an example, its hard to see how anyone could be drawn to or empathize with him before the big yellow eyes where painted in. But now when people see him in the game we get the "awww" response we where looking for!

Wednesday 18 March 2015

Introductions


Welcome to my first ever blog… and Post!

For a computing student you’d think I’d be well up on this sort of thing but I never managed to get around to it. I have however had the pleasure to read some extremely useful and interesting Blogs by fellow games developers and I have wanted to add to the pool for a while now so here it goes!

Who is this unknown person?

I’m currently in my 4th and final year of study in Games Development at the Dundalk Institute of Technology, Ireland. As part of my Advanced 3D concepts module my lecturer Michael Connelly asked us all to set up a blog to show our project work. Being a budding socialite I of course was thrilled with this idea… However! This is actually a really good idea and a smart way for him to drag the remainder of us shy folk out of our shells before we are unleashed onto the world in a months’ time.

I am team lead, AI Programmer and Level Designer for my final year project team. Our game is called Haven and it is a VR therapy game designed to gauge emotional and empathetic responses in children from and 8+. We decided to go with PolyArt style graphics and a fun friendly color pallet.

A few screen shots of the Project, we are currently working towards our Alpha Build.



And what are you doing here?

Our game is designed for play with the Oculus Rift; this means that when it is displaying the game for the player it must render the entire scene twice, once for each camera. We are all doing our part to optimize this process and it is what I was considering when choosing my project. I decided I would look into texture atlases which if used would allow for batching of draw calls in our game, improving rendering efficiency.

For me this meant taking the models produced by Conor Pendlebury (the team’s environmental modeler) and unwrapping, texturing, and combining them into a larger texture atlas. I predicted that a small game like ours could use a single texture atlas for all the assets in the level. Loading this into Unity and applying it to our models will allow is to save a significant amount of draw calls through batching at runtime.


Ok I’m listening…

I will be splitting my procedure for doing this into at least 3 posts showing my approach;

  1. Unwrapping and Texturing
  2. Creating a Texture Atlas & Manual UV Remapping
  3. Achieving Batching in Unity

I must state that I by no means am saying mine is the best or most correct way of doing this, it is simply an approach to be taken or left in part or in whole. Who knows might help someone with some little problem they are having!