Sunday, November 9, 2014

Zity - Lighting Revisited

If you ever played a game with really good lighting you probably noticed it. The way the shadows were cast and the illumination of objects helps to set the mood of the player and even can fuel the gameplay in certain genres. Horror games use lighting to limit the player, making him afraid of what he can't see. Action games use lighting to heighten the thrill and intensity. Strategy games can use light to convey time of day or weather. In any genre lighting can be a tool well worth polishing in order to get the most out of the game.

So it's no surprise that Zity will be heavily investing in a good lighting technique. Zity is all about tension and suspense and even sometimes the peace of being away from the tension. To help convey that feeling I will use lighting.

There's several different lighting techniques and methods to choose from when creating a 2D game. It's easy to see in games like Terraria where you can alter the lighting in the settings and see the various effects. But before even diving into techniques I'll give a brief overview of some options.


The first option for lighting is a simple FOV (field of view) method. In FOV lighting, only parts of the screen that are in the players vision are lit up, everything else is darkened. So for instance if my player was standing in the middle of the screen looking to the right, everything behind him would be dark since those areas are outside his FOV. In the picture to the right you can see how the zombie behind the wall is darkened while the zombie in front is bright.


The advantages to FOV lighting are that the player gets more immersed in the game. By limiting the players useful sight it adds tension and forces the player to constantly look around to make sure they're safe. It also makes going around corners and exploring new areas more uncertain. FOV lighting is used mostly in action/arcade games and some notable mentions are Monaco and Teleglitch. In Monaco the lighting is used to force players into thinking and strategizing and for general incoherence and hilarity. In Teleglitch the FOV lighting is used a little differently then described above but also lends to the feeling that you are in an enclosed area and enemies could be anywhere.

The next lighting option is basic diffuse lighting. This type of lighting is what you see in real life when looking around in a darkened room with a single light-bulb. The objects in the room are illuminated and as a result you can see them. Some objects however will be darker or lighter and shadows will be cast.

In a 2D game, full on diffuse lighting can be used to add color and atmosphere to a game. Here's how it works: First you define light sources. Objects like lamps and flashlights can be light sources, and even things like the sun can be made into a light source. The light sources have all the information about the light; things like intenstiy, distance, color, and shape are all properties that need to be set in a light-source.


Once there are light sources the lighting can begin. In order to light a scene it must first be darkened*. So without getting into specific code, what you would do is draw all the objects to the screen like normal, and then draw on top of them either the ambient light (from the sun) or just a pre-defined level of darkness. Our example scene is now barely light enough to see.

Now that the scene has been darkened we can get to work lightening it. To do this you would have to go through each light-source you've defined (In this case it would be the campfire) and you draw it's light. The light image itself is just a gradient created from the properties you defined earlier. You can see how this plays out in the picture to the left, where the light from the campfire illuminates the scene.

After the scene has been illuminated there is still a problem: there are no shadows. In order to add shadows we would need to define objects that can block light. In real life these objects are pretty much anything you can see, but in a game you often want light to pass over objects and illuminate them without getting in the way. In the example we've been using the red wall would be an object that blocks, or occludes, light.

Now that you've got your light-source AND occluder we can finish the scene. To create the shadows we need to first get the region of the scene that is in shadow. To do this we start at the light-source (campfire) and make our way to every point on the occluder (the corners) and we stretch past those points as far as the light would go.

These points define the area of the scene that needs to be returned to it's darkened state. To do that requires splitting up the area into triangles and rendering those triangles on the GPU with the darkness level used previously. That information is beyond the scope of this blog post but there are other posts out on the internet that explain it in more detail.

The result is a lighted scene in whatever color the lights were, with shadows casting out from occluders. This kind of lighting is really cool and can be used to great effect. For instance having bullets lighten the scene or flickering hallway lights give a creepy green aura.

On to the last option for lighting: Tile lighting. This kind of lighting is featured prominently in both Terraria, and Minecraft (though both have now moved on to smooth lighting). It consists of lighting the scene in chunks (tiles) based on their proximity to a light source. This is very similar to diffuse lighting but how it is done is very different.

Tile lighting has it's place in games. It is a cool effect if nothing else but could be used for other things like actual gameplay. Say you wanted to know if a tile had enough light for a tree to grow on it, you could easily find that out with tile lighting whereas with diffuse lighting it would be a much more tricky affair probably involving sampling pixels. Tile lighting is also well used in situations where diffuse is too complicated. One example is if I wanted light to come in from the hole in the roof of a building and cascade down through the holes in each floor until it is too weak to continue. With tile lighting this would be just a matter of continuously pushing the light outward from it's source and falling through the holes ot the next level down. With diffuse you would have to create a new light source at each hole making sure the values matched up.

So there are some examples and explanations of lighting options. In Zity I intend to use both Diffuse and FOV. Diffuse will cast light from the various light-sources and be used for the ambient light of the sun. FOV will be used to add a little more tension and insecurity to the gameplay. I may even get around to using tile lighting if Zity expands to multilevel buildings like I hope it will. At any right Zity will have lighting, and it will look cool.

This is the first of my weekly blog posts keeping you updated on Zity. Check back next week for more info!

No comments:

Post a Comment