Lighting in Unity
- santosh nalla
- Aug 22, 2020
- 6 min read
Lighting in Game engines adds life to you game and makes them look beautiful but before applying lighting and making your scene beautiful its very important to understand lighting from performance point of view. Lighting is the main culprit for you low frame rates and crashes in your game especially if its a mobile game.
So lighting in unity is of 2 types
Real Time Direct Lighting - This is super familiar type where point light, Direction light, spot light come under this. Here all the lighting related calculations(shadows, illumination of pixels) is calculated dynamically(Usually we don't prefer this for mobile games as it takes up a good amount of processing power).
Pre-Computed Indirect Lighting - In games where we don't have dynamic day and night cycles, this technique is used.Here all the lighting calculations are done on pixels and stored as textures. So NO Dynamic lighting calculations are done here but still it gives the same effect when they are static. Its like all objects are already illuminated, their shadows calculated but all these are pre computed and not dynamic so lot of processing power is saved here.
Firstly lets discuss about this Real Time Direct Lighting.
Point light, Direction light and spot light come under this. To create one, go to create-> Light -> Select one. As i already told here all the lighting calculations are done dynamically. There are options for each type of light. Most of the things like intensity, Shadow type, Color etc are self explanatory.
In these Real time Direct lighting, if you explore the inspector tab for each light, you will find an interesting property called "Cookie". What exactly is this Cookie?
Cookie are textures that you can use as specialized patterns. In theater and film, lighting effects have long been used to create an impression of objects that don’t really exist in the set. Jungle explorers may appear to be covered in shadows from an imaginary tree canopy.
Its like faking light shadows, using just a texture as cookie.
A cookie is just an ordinary texture but only the alpha/transparency channel is relevant. its just a patters that can be projected and create a custom shadow.

The most important part of lighting system in unity is global illumination.
Global Illumination: Until now we have learned about direction light, spot light and point light. To define global illumination in a very simple terms is - it defines how light bounces of the surface which direction, point and spot alone CANT. Its like other objects affect each others appearances. For example, if you have a red board and a while cube in front of it, the red color is not projected by simply using this direction, point or spot. However if you check "Global Illumination", the light which falls on the plane gets reflected on to the cube as red shade and makes it look more realistic.
Example of using just Direction light and Using Direction light along with global illumination.
With option global illumination checked:

Global Illumination unchecked:

The most important point to remember is(we can say this as limitation) - All objects where you love to see this realistic global illumination is on STATIC OBJECTS only. So make sure all objects are static and "Generate" light. So once you generate light, this global illumination effect is seen only on static objects. Once they move, they still have those pre calculated global illumination values only( unless and until you click on "Generate" light button.
So yes , if you want to see Global illumination effect-
Make sure you check "Global Illumination" check box in Lighting settings tab
Make sure all objects are static. (GL doesn't work on non-static objects)
Make sure to Generate light or put it in Auto Generate mode at least.
Moving static objects DOESN'T recalculate this GL effect automatically. It shows you pre-computed value only.
Any light point , Direction and spot can get you this global illumination effect.( this might get deprecated in future. Because in future they are planning to give this effect only to direction light) .
Clicking on any light, if you see in inspector tab - you see 2 options - intensity and intensity multiplier

Intensity as you already know, effects the intensity of the light(be it point,direction or spot).
Intensity Multiplier tells us how strong the light bounces. Its like the more the intensity multiplier value, the more the global illumination effect for that particular light source. So its purely related to Global Illumination effect.
So now lets put down all limitations of Global Illumination:
This is only for static/non-moving objects
Lighting data needs to be calculated and Re-calculated every time we move a static objects out of its original place.
Quality of the shadows are really unrealistic. Need some work around here.
Now that we have learnt about "Real Time Global Illumination", lets discus "Baked Global Illumination" now.

Baked Global illumination wraps objects in special light map textures. All that lighting effects are stores on textures(which can increase the size of your game obviously but saved crazy processing power issues). Yes "Real time" and "Baked" doesn't give you the same effect- yes they are different, yes they have their own pros and cons.
But before learning, lets look into steps to generate Baked Global Illumination light maps-
The first obvious step is to check "Baked Global Illumination" check box.
After checking this and clicking on generate button, you surprisingly see no effect and probably be wondering why. See here is the answer - before generating light maps make sure your light source has Mode set to "Baked" or "Mixed".
Also make sure all objects where you want to bake this global illumination are set to STATIC (at this stage this is the most obvious step :P)
Click on generate button and wait for it to bake you new light map textures( Yes yes i know it takes super long but PATIENCE :P)
One more thing here is - For any model to generate lightmaps you have to make sure they have 2nd UV channel, else you are gonna mess up the texture. For this you have to make sure you check "Generate Lightmap UV" in model tab for all imported models

A light map is a data structure used in light mapping, a form of surface caching in which the brightness of surface in a virtual scene is pre-calculated and stored in texture maps for later use. This is done is order to provide a low computational cost. They almost look like textures , but instead of texture they contain lighting information. Once baking is done, in folder named under scene, you can see the light maps generated. So it generates lighting mapping data(this once is also created in Real time GL). In addition to this lighting mapping data, here in baked GL, 2 images are also generated(light maps).
Intensity maps - which gives lighting details- dark and bright spots like
Directionality maps - This is used for normal maps only.
Together they form light maps.
Here is super important point to note: If your game is not having any normal maps applied, you can simply ignore the generation of directionality maps. This is because these maps consume memory. Yes they provide low computational speed but at a cost of some memory(not in all cases )
So then how do i only bake and generate intensity maps and ignore directionality maps.
In lighting tab -> when you go to lightmaps settings -> You see an option "Directional Mode". Set this to "Non- Directional" to ignore Directionality maps and save some memory for your bake.

See can have much smoother shadows buy increasing light maps resolution in light map settings.
If you see the lightmaps settings(in the above image), you can see the type of light mapper we are using by default is "Progressive".
Unknowingly all this time we have been using Progressive lightmapper technique.
Progressive Lightmapper is a fast path-tracing-based lightmapper system that provides baked lightmaps and Light Probes with progressive updates in the Editor. You get an output of the lightmap as soon as it’s available, and even though it’s incomplete, you still have a rough idea of what the scene will look like.
The Direct Samples and Indirect Samples controls how many randoms rays are fired of in PATH TRACING process.Try to read about path tracing to understand more about these options but in a very simple terms let me explain path tracing.
A color/brighness of a pixel is determined by 2 rays. Rays reaching the pixel directly and Rays reaching the pixel indirectly(bouncing from an other surface). So direct samples decide how many sample rays we take to determine the direct rays effect on a pixel.
One final option there is ambient occlusion. Ambient Occlusion is a sophisticated ray-tracing calculation which simulates soft global illumination shadows by faking darkness perceived in corners and at mesh intersections, creases, and cracks, where ambient light is occluded, or blocked.
Comments