Jason Lynch , 4th Year Individual Portfolio (The Thinking City) - Level Optimization

Here you will find a step by step layout of the development behind the level optimization in "The Thinking City". We use techniques such as occlusion culling, lightmap baking, and frustum culling.

Back To Homepage

Relevance of Performance in our game:

 

Performance is a massive part of every game. Without performance optimization a lot of PCs would not have the power to run games, especially big ones. The same is true for our game. At the start we didn't need to worry about performance as the level was not very big and we were getting about 120+ frames per second. However, as the game developed and we started to add objects that required a lot of performance power we found the frame rate dropping to as low as 10-20 frames per second which made the game unplayable. Another reason performance is so important to us is that we are developing a VR game and to have a VR game run comfortably on a person and not trigger motion sickness it should run at about 90 frames per second. This value can differ depending on the VR device but is a nice number to aim for. To combat low performance I have currently implemented occlusion culling and lightmap baking to drastically bring our performance back up to a level that is playable in VR.

Technical Challenges

Optimizing The Thinking City was a bit of a challenge for a number of reasons. I had never done any optimization for a game before so all of this was new to me. We were also several months into the project when performance became a noticeable issue so I had to do a lot of setting changes on objects to get them ready to be optimized. Below is a summary of the technical challenges I had to overcome to get occlusion culling and lightmap baking working in our game.

  1. I had not tackled occlusion culling before and had no idea where to start.
  2. I had only heard of the phrase lightmap baking and didn't know how to generate them.
  3. It got a bit messy changing the settings of objects already in the game.
  4. I had to sort out what objects were valid for these kind of optimizations and which were not.

To address these issues I managed to scrape together a couple of tutorials from YouTube as well as get access to some of the Unity developers example to see how they generated occlusion culling and lightmaps and looked up the Unity game engine documentation for further info. 

References

I find myself to be a lot more of a piratical learner than a person who picks things up by reading or seeing them especially when it comes to things like developing in unity or programming. To this end I have amassed a pool of video resources/online courses that I can use to help me learn the engine as well as help optimize the level for the best framerate possible. Below you can find a link to the resources that I am using throughout development.

 

Keywords searched: Unity, Occlusion Culling, Lightmaps

Level Optimization Process

Occlusion Culling 

Preparing Objects for occlusion culling

There are two main types of occlusion culling, static occlusion culling, and non-static occlusion culling. Static occlusion culling is used on objects that never change throughout the game such as walls and floors for example. These objects are likely to never move within the game or swap materials. Static occlusion culling is used to save on frames per second by not rendering any static objects that appear outside of the cameras field of view. 

To prepare an object for static occlusion culling it must first be marked as static. To mark an object as static you must first select the game object in the Unity editor. On the top right of the screen you can see a static checkbox as shown in the image below. Once you select that checkbox the object is ready to be included in the occlusion culling process.

You can also see a video below on preparing an object to be included for occlusion culling.

Marking object as Static

Details

Setting Object to Static

Download SettingStaticObject.mp4 [0.67MB]
Details

Baking the Occlusion Map into the Game

Once all objects in the scene that are meant to be static are set up we then need to bake in the Occlusion Map. This process is done by selecting Window > Rendering > Oclussion Culling. This will bring up a window that will allow you to bake the Occlusion Map into the game. Unity will then automatically bake the Occlusion map into the level. Once that is done when you play the game and observe the scene view you can see that only objects within the cameras field of view (the green zone) are the only objects being rendered. This results in a big save on frame rate which is really needed for our game.

You can see a video below of the baking process and the result.

Occlusion Baking and Result

Download BakingOcclusion.mp4 [6.21MB]
Details

Lightmap Baking

Light Types & Modes

Lights have a massive impact on a games performance. There are several types of lights all with their own unique purpose.

 

Point Light

This is a type of light that shines equally in all directions. It's intensity falls as it approaches the end of it's range until it reaches zero. These are generally used when simulating lamp light. They can also be used along side explosion and spark effects to convincingly light up an area.

 

Spot Light 

Spot lights work similar to point lights in that they have a range which light falls off over time. The spotlight however is constrained to an angle which results in a cone shaped field of light. 

 

Directional Light

Directional lights constantly point in one direction and throw light in that direction. They are used to simulate sunlight. Unlike the other two light types above this light does not diminish over time.

 

Area Light

Area lights are very processor heavy so can only be baked into scenes. They cast light in only one direction and come in several shapes. The light falls off after a certain distance just like the point light and spotlight. These lights are usually used for internal lights such as house lights.

 

Light Modes

The majority of the lights bar the area light can be set to three different modes depending on how you want to use them.

 

Realtime

Realtime lights calculate and update their illumination every frame during the games runtime. Unity does not precompute any calculations for realtime lighting. This results in realtime lights being very heavy on the CPUs processing so should be used sparsely in games.

 

Mixed

Mixed lights are a mixture of both realtime and baked lighting. These lights have pre-calculations done by Unity at runtime and update every frame like realtime lights do.

 

Baked

Baked light are all pre-calculated by Unity before runtime. They are not included in any runtime calculations at all. They are used when lights will not change in game.

Preparing Objects for Lightmap Baking

There are a few steps to take before baking the lights into a scene. The first thing to do when importing a model that you have not made a custom lightmap for is to turn on the lightmap generator in the model settings in Unity. This will prepare the object so that light will bake on to it properly. Once that is done you can then drag the model in to the scene where you want to place it. When you are happy with the objects placement select it again and in the objects "Mesh Renderer" you open the "Lighting" tab and turn on the "Contribute Global Illumination". This will allow it to have light baked on to it. Also make sure that the "Recieve Global Illumination" is set to the "Lightmaps" option. Once this is complete the object is ready to have light baked on to it. 

You can see images below of the steps taken to set up a light map.

 

Generating a Lightmap

Details

Allowing Object to Receive Global Illumination

Details

Baking Scene Lights

Once the objects within the scene have their light setting set up they can they contribute to the lightmap baking. To bake lights into a scene you need to go to Window > Rendering > Lighting Settings. In here you can alter the lighting settings as needed . I left these settings on default as they worked just fine. When you are happy with the settings you can then select the "Generate Lighting" button which will then proceed to generate the lights for your scene. This process can take several hours to complete and will use a lot of your CPU processing power. The end result will be lights and shadows that will not move and be set in place until you decide to bale again.

You can see a video below of the whole process of baking lights into the scene.

Baking Scene Lights

Download BakingLights.mp4 [2.83MB]
Details