Saturday, October 28, 2017

The Art of Optimization

Disclaimer:

Most of the projects that I will be doing are for a game that I am developing on my own code named Project Ninjio. In short, the game is a story action driven role playing game (common referred to as an action RPG). It features 2 uniquely styled areas, one in which the cities are built to flow along with nature, and the other where the cites are hyper futuristic. The game is set in a futuristic China, where after a large war, the country is split into the two settings described above. The game is built in the Unity 3D game engine.

The Art of Optimization

Introduction

It has been my goal to insure that Ninjio is the best experience that I can offer to its players. I had many smaller goals that I have for Ninjio to insure this high quality experience, however one of them (and the most important one to the gamer inside me) is making sure that the game will run at a constant 60 frames per second (FPS) or higher at all times on all sorts of hardware.

60 whats?

Without diving too far into the technology that we all use on a nearly daily basis, a frame in terms of a computer is the image that the computer is outputting to your monitor or display. It is well documented that the human eye can only see around 30 to 60 of these images per second, any lower number and the image would look choppy while any higher number would result in a similar looking image while wasting computing power, which is not ideal. Gamers, like myself, can definitely "feel" the difference between 60 fps and it's lower counterparts. Most games these days strive to run at this 60 fps threshold, however most fail (especially when running on consoles) and run at around 30 fps. 

So what are the benefits of this higher frame rate?

Image result for frames per second
Look at the difference, the 30 FPS image looks smeared
compared to the 60 FPS one.
In short, the higher the frame rate (and therefore higher fps) the faster you see the result of your action, say moving your mouse or pressing a button. This is generally what you want in games where there is a lot of action going on at a time, like Ninjio. Another benefit to this higher frame rate is that images do not look blurry and smeared when you pause the game, as there are more images being outputted to you monitor.

The challenges of achieving higher frame rates

In short, there are 2 ways to achieve higher frame rates, by either optimizing your code or optimizing your art. While optimizing your code could yield a better result on a per optimization basis, it is generally harder to do. Art, on the other hand is very easy to do. It is important to optimize your art as best as you can, even while you are creating it.

How to optimize your art

Related image
Not a too much changes visually here
One easy way of optimizing art in video games is to make sure you are using the least amount of vertices (or points in 3D space) as you can. By doing so and asking questions like "Do I really need a ton vertices to make a sphere when I could do it in much less vertices and have it look the same", you can optimize different pieces of art very easily. Another method is to remove any faces (3 or more vertices put together to make a side of an object) that you know that players are not supposed to see. This could be like the back of a building facade that players cannot see in game or the bottom of a building that would normally be buried under the ground.  These methods are very simple and easy to do, however sometimes you do need a high polygon count on an object or sometimes you do need all of the faces to be filled in, it is situations like this where different programing tricks come in handy.
Image result for level of detail
Level of detail (LOD) in Team Fortress 2

One programming trick is level of detail, or LOD for short. It is the process of making it so that the game engine gradually reduces the amount of detail in the object as object gets farther away from the player. This method has to be programmed, but the results of it can vastly improve the performance of the game.
Image result for draw call batching
An example of draw call, look at how the
 trees are rendered together in a single
graphics call, as shown by the grey box.
Another programing trick, one that is fairly easy to do inside of the Unity3D engine, is draw call batching. This trick involves telling the computer that instead of treating each object as its own separate entity upon the rendering of the scene, instead the engine tells the computer that a certain group of objects should be rendered at the same time, thus saving processing time on the computer's side. This trick only works well when you have objects that are static, like building and pieces of the environment that do not move during gameplay.
Occlusion culling in Horizon: Zero Dawn
One last programming trick is called occlusion culling. In short, it makes it so that the computer only renders what the camera inside of the game can see. Normally the computer will render every single object in the scene regardless of if it is onscreen or not. With occlusion culling, the game doesn't render anything that the player cannot see through the camera, so objects behind the player are not even processed and rendered at all. Occlusion culling is used heavily in some of the earlier 3D games like the original Crash Bandicoot. However this method has become popular in modern games like Horizon: Zero Dawn as the method allows for a higher visual fidelity, especially in games where the worlds are large and expansive. 

No comments:

Post a Comment