Working With Lighting In Unity – Theory and Practice (2016 by Volodymyr Sapaiev)

Beautiful real-time illumination in video games greatly impacts the rendering, which can be critical for mobile devices. Lightmapping is a technology, that stores the lighting information in a texture, which saves a lot of computing resources. In this article I will explain how lighting works in games, describe the process of creating a lightmap in Unity 5, and share a few tips on lighting.

Small teamswith less time to create unique graphics for game locations can leverage lighting to help spice the game up!

WORKING WITH UNITY3D

GENERAL INFORMATION:

Unity3D provides us with 5 light source types:

  • Directional Light: The simplest one, it imitates the sun light. Basically it’s the infinite count of rays, that are parallel to one another.
  • Point Light: It can be thought of as a point in 3D space from which light is emitted in all directions. A good example of point light would be a light bulb.
  • Area Light: Imagine a rectangle from which light is emitted in all directions. This kind of light sources is commonly used in offices, malls and other untenantable buildings, where a lot of space needs to be lit up.
  • Ambient Light: This can be thought of as a global light source affecting objects in the scene from every direction. Use cases: lighting up too dark shadows, spicing up a dungeon by adding idle light of bioluminescent plants.
  • Light Probes: Special type of light source, that only affects dynamic objects. Technically it’s not a light source, but let’s call it that way to keep things simple.

All objects in Unity are either DYNAMICor STATIC. Static objects don’t move during the game, and we can use light baking for this type of objects. Dynamic objects move during the game, like heroes, monsters, a waving flag, a rolling stone, etc. You can’t use lightmapping for this kind of objects, instead they are illuminated by either real-time light sources or using light probes (more details on this subject in the end).

NOTES: Unity caches all the intermediate results of calculating. This can load 10 GBon your system disc. Customize caching (Edit > Preferences GI Cache) to set the saving path and define max cache size. Try turningCompression off; it will take up more disk space but the process will speed up. You can also clean cache, but be aware that if you have a scene with already calculated lightmaps, the lightmaps will be erased too.

When rendering lights in Unity, in the bottom right corner you can see what the engine is currently doing. At the very end of rendering process it’ll read “Compositing”.

LIGHT SOURCE PROPERTIES:(some light sources have more or fewer properties).

  • Baking: This allows you to choose the light source processing method. For lightmapping choose “Baked”.
  • Color: The light source color.
  • Range:How far light emits from the light center (Point and Spot lights only).
  • Intensity: Brightness of the light.
  • Bounce Intensity: Defines brightnessof indirect rays, cast from current light source.
  • Shadow Type: Determines whether Hard, Soft, or No Shadows will be cast. Soft Shadows gives beautiful results buttake more time for rendering.
  • Baked Shadow Angle (Available for Soft Shadows Only): In a long shadow, the farther away from the object it gets, the more blurry it becomes. If you set this parameter to 0, the shadow won’t blur. Set to more than 0, for a blur effect.

SCENE LIGHTING PROPERTIES:

The Lighting Window (Window > Lighting) consists of 3 tabs: OBJECT, SCENE, LIGHTMAPS. To run the rendering process, click the “Build” button at the window bottom. Note the “Auto” check, which runs rendering automatically every time you make changes to the scene (only check it if you have a very powerful computer).Once the lightmapping process is finished, see info about count and size of lightmaps.

OBJECT TAB: Select an environmental object in the scene, to see a list of properties and parameters. If the object is static, “Lightmap Static” is checked. The field you will use most often is “Scale in Lightmap”. Every object gets its lighting baked into a separate little texture, which is later placed into a big lightmap-atlas. By changing this value, you can alter the amount of space the texture will take. Note: the less space taken, the lowerthe baked lighting quality. The default value is 1.0, but for objects rarely seen or located far awaydecrease this value to 0.1 or even less.

SCENE: The Scene tab contains all the key lighting settings. NOTE: As an introductory tutorial on Unity lighting and lightmapping for mobile platforms, I am excluding Real-time Global Illumination (GI, Indirect Lighting) and HDR-textures (High Dynamic Range).

Ambient Source:customize Ambient Light (light present all around the scene, doesn’t come from any specific source object). Choose Skybox (sky is a texture), Color (uses a flat color for all ambient light), or Gradient (gradient based skies).Consider Gradient option toset 3 colors: sky, horizon and ground, which will create a pretty complex and interesting ambient light effect. Ambient Intensity sets the brightness of ambient light. A possible artistic look: a really bright scene, only illuminated by Ambient Light itself.

In the Baked GI section you can find basic lighting quality settings for both direct and indirect light, including Global Illumination and Final Gather (see Maya notes at end).

TIP:Work iteratively!:
1) Set lighting quality to minimum, so the rendering process runs quickly.

2) Render and View the result.

3) Make changes and restart the rendering process. When you’re close to being satisfied, increase the render quality and turn on the Final Gather. It will take a while, so run when you want totake a break.

All Baked GI settings operate with a unit called a “texel”. A texel is a pixel, but not on the screen, rather in the texture space of a 3D model. Experiment with Baked Resolution (try setting it to 25 to start).

NOTE: A rendered Lightmap is made of layers: separate textures includingDiffuse (direct light), Global Illumination, Final Gather, andAmbient Occlusion. After that the engine merges the maps together (laying them on top of one another: Diffuse is the base map, then on top the FG layer using Lighten blend mode like in Photoshop, thenthe AO layer using Multiply, etc). The layers merge into the final lightmap.

Baked GI

  • Baked Resolution:General lightmap quality (Diffuse). The higher the value, the better image quality. After a certain threshold you won’t see any difference; find a balance between baking quality and rendering time.
  • Baked Padding:The distance between contiguous textures in the texture atlas. If the compression is on and the atlas becomes more blurry, some of the textures may overlap. Set between 1 and 3.
  • Compressed:Usually checked (Unity compresses lightmap-altases, to avoid humongous sizes).
  • Indirect Resolution:The quality or detailing of the GI, FG and AO layers. Do not set a big value: You won’t see huge difference. If you set Baked Resolution to 40, set Indirect Resolution between 3 and 8.
  • Ambient Occlusion: AO shades corners, cracks etc. This parameter defines how fast energy is dispersed. It isan artistic trick;not real, physically correct lighting.
  • Max Distance: The distance of darkening for Ambient Occlusion. If Max Distance is high, the shading effect will start far from a corner, if Max Distance is set low, only deep corners will be shaded.
  • Final Gather: Turns on the FG indirect lighting method (by default only Global Illumination is rendered). Managed by Indirect Resolution parameter
  • Ray Count: Number of rays emitted by every FG point. Try 32 or 64 rays.

GENERAL GI:

  • Directional Mode:Bakes a second atlas for each lightmap. Straight lightmap is a plain texture, which is later set on top of objects, imitating their illumination. The second atlas, which is referred to as Directional, holds information about the light rays direction. This mode can only be used for objects with Normal Maps, and is not for a mobile game. Otherwise – pick “Non Directional.”
  • Indirect Intensity: The brightness of indirect lighting layers, including FG. Remember: Lightmaps arebuilt from layers placed on top of one another.
  • Bounce Boost: Adjust brightness of the reflected photons. Infois stored in GI layer.
  • Atlas Size: The size of the full Lightmap (atlas) with baked lighting. Set to 2048. The smaller the atlas size, the more atlases will be made, increasing draw calls, impactingprocessing.

FOG:Has nothing to do with lighting rendering. Can be turned on and off at any time. Experiment! Fog is a pretty powerful artistic tool to create visual layers/depth. You can drastically change the scene by using fog, making a sunny scene sunnier and a dark scene darker.

Fog can also be used as a tool to improve processing. You may switch off textures for the far objects and hide them with the fog (think old versions of World of Warcraft)..

LIGHTMAPS:Where the rendered Lightmaps are shown. If you have a lot of atlases, you won’t be able to see all of them in the top part of the section, because you can’t use scrolling in that window. Drag the mouse to the bottom of the window and then scroll.

Note to what extent the last Lightmap-atlas filled with textures. If it is almost empty, increase the Lightmap quality to fill the entire atlas, or decrease the quality to get rid of the last (almost empty) atlas. The following image has 2 “good” atlases and one atlas, that is half empty (less efficient).

Note: Lightmaps are stored in EXR files, that have 32-bit color depth.

LIGHT PROBES:Probes are an efficient and cheap way of imitating baked lighting for dynamic objects.Light Probes are a network of spheres that absorb the lighting info in the scene. If their quantity is great enough, they create a map of your level or location illumination, andcan then be used to illuminate dynamic objects (which can’t use regularstatic baking).

For example. You have one Point Light in your scene, that is used for baking (Baking parameter is set to Baked). You don’t set it to Real-time, because it’s expensive in terms of processing. You Lightmap the scene and see your Point Light illuminate the space around it. A monster enemywalking next to the light will remain dark, because the enemy is a dynamic object, and your Point Light only affects static objects.

Next you set up a network of Light Probes around the light source: choose Game Object > Light > Light Probe Group. A cube of 4 spheres appears. [Ctrl] + LeftClick to choose a sphere. In the Inspector add or delete spheres, or[Ctrl] + [D] to duplicate spheres

Bake again. Now when the monster walks around the Point Light itis illuminated by it.

Set up the Light Probes neatly and thoughtfully. To avoid graphic artefacts and flickering light make sure Light Probes surround actual light sources.

Below you can see a screenshot of a correctly set-up network. The Light Probes are not just randomly scattered. They surround light sources trying to “catch” the transition between light and shadow.

FINAL THOUGHTS: A tip about creating environmental models for Lightmapping:In Maya it is a common practice top delete polygons on objects which you know will never be seen, like the backs of furniture that are yup against a wall, to reduce polygon size and render time. In Unity this can sometimes create strange artifacts from the Lightmap rendering: next to the objects with the “holes” (i.e. deleted polygons) you may see bad shadows. Delete these polygons with caution.

THEORY OF LIGHTING (in Maya)

Lighting in computer graphics divides into two categories:

  • Direct Illumination. Light rays strike the surface directly from actual light sources.
  • Indirect Illumination.Rays bounce off surface & scatter for soft, ambient light.

Indirect Illumination: The two main ways to simulate indirect lighting Maya are Global Illumination (GI) and Final Gather (FG). They can be used separately, but are best when used together. They are processor-intensive, however: rendering (the process of calculating and visualizing complex lighting) takes up huge amount of time.

Global Illumination (GI) simulates the complex behavior of the light as it bounces and interacts with the world. A light source emits photons (particles) carrying info aboutlight color and brightness. When striking a surface photons light it upand losing part of their energy, which alters the data (about the color & brightness) they carry. The photons then bounce off and strike another surface, losing more of the energy. The render settings determine how many times the light bounces.

Final Gather (FG) This method scatters points (final gather points) around the scene, then the points cast around light rays (instead of photons). Rays sample the surrounding area and bring the brightness and color values back to the point. Picture this: late evening, the sun has almost set over the horizon; it’s getting dark, but a small part of the room is still lit by orange sunset light. A final gather point on the floor casts around a few rays, some of which reach the lit part of the room and bring the information back to the point, at the same time slightly illuminating the floor with the indirect orange light. It’s not as robust compared to GI, but it gives good results, and is widely used to set up beautiful soft light for the scene.

For the following image I cranked up the indirect light parameters so you could see how light particles bounce off the objects.

Ambient Occlusion: Ambient Occlusion (AO) is the effect of shading in the corners and cracks. Imagine a light ray striking a room corner: it will repeatedly reflect from both walls, gradually fading away. The farther away a corner is, the less light reaches there.

Usually Ambient Occlusion is used to artistically highlight the shading effect. In reality light rays don’t lose their energy so fast for room corners to be as dark as we see in games. In physically correct lighting the engine calculates the rate of energy loss, so you don’t need to render an AO map unless you want it for an artistic concept.

By the way, some 2D-artists like imitating this effect when drawing. Their work process is generally similar to creating 3D-graphics.

TEXTURE ATLASES: Texture Atlasis a big texture that holds smaller textures. Texture atlases are most often used to save computing resources. For example, there’s a tavern in your game. It has 20 wooden objects inside (tables, chairs, spoons etc.). Each of them has its own texture. As a result your CPU will request the GPU to render each object separately (20 times). If we assign one sole material to all 20 wooden objects, querying one texture atlas, that holds all 20 textures, the GPU will be able to render objects using only1 draw call.

In case of lightmapping there’s a small texture created for every object in the scene. The lighting information is baked into each texture. After that thousands of small textures for different objects are put intobigger texture atlases by the engine. Like with the wooden objects, the productivity increases.

1