Graphics Programming  
Lecture 03 Light, Shadows, and Global  
Illumination  
Edirlei Soares de Lima  
<edirlei.lima@universidadeeuropeia.pt>  
The Physics of Light  
Light is electromagnetic radiation of a frequency that can be  
detected by the human eye (visible light).  
From the optics view, light can be seen as electromagnetic  
rays that travel in a straight line from its source.  
The light source emits rays of light.  
When the light hits an object, some of the light bounces off the object.  
If the reflected light hits our eyes (or the camera lens) then we see  
the object.  
The Physics of Light  
Regular Reflection occurs when the angle at which light  
initially hits a surface is equal to the angle at which light  
bounces off the same surface.  
It occurs only when the rays fall on a highly smooth surface, such as a  
mirror.  
Irregular Reflection occurs when the rays fall on an irregular  
surface and are scattered in different directions.  
The Physics of Light  
The Physics of Light  
Surface Normals  
When we simulate light in computer graphics, the object's  
surface plays an important role. The surface normal vectors  
define how light interacts with the surface.  
To a surface at a point P, the normal is a vector that is  
perpendicular to the tangent plane to that surface at P.  
P
Diffuse Shading  
Many objects in the world have a surface appearance that is  
not at all shiny (e.g. newspaper, unfinished wood, and dry,  
unpolished stones).  
Such objects do not have a color change with a change in viewpoint.  
These objects can be considered as behaving as Lambertian  
objects.  
Specular Shading  
Some surfaces have highlights (e.g. polished tile floors, gloss  
paint, whiteboards). These highlights have the color of the  
light and move across the surface as the viewpoint moves.  
Physically Based Shading  
Physically Based Shading is a model that seeks to render  
computer graphics in a way that more accurately simulates  
the flow of light of the real world.  
Phong has been used by the game industry for a long time, but  
nowadays is being replaced by physically-based shading.  
Unity introduced Physically Based Shading in Unity 5 (2015)  
Light in Unity  
Direct light:  
Light that is emitted, hits a surface once, and is  
then reflected directly into a sensor (a camera).  
Indirect light:  
All other light that is reflected into a sensor,  
including light that hits surfaces several times.  
Real-time lighting:  
Light calculated at runtime.  
Baked lighting:  
Light calculations that are performed in advance,  
saved, and then then applied at runtime.  
Global illumination:  
A group of techniques that model both direct and  
indirect lighting to provide realistic lighting results.  
Types of Light in Unity  
Point Light:  
A Light source that is located at a point in the scene and emits light in  
all directions equally.  
Spot Light:  
A light source that is located at a point in the scene and emits light in a  
cone shape.  
Directional Light:  
A light source that is located infinitely far away and emits light in one  
direction only.  
Area Light:  
A light source that is defined by a rectangle in the scene, and emits  
light all directions uniformly across its surface area.  
Point Light  
Spot Light  
Directional Light  
Area Light  
Light in Unity  
Directional Light:  
Often used to represent the sun .  
Has a significant effect on the look o
scene.  
Spot Light, Point Light and Area Light:  
Usually represent artificial light sources .  
Their positions are usually determined by  
scene objects.  
Shadows  
Shadows appear when light rays are blocked: when an object  
is between a light source and another object. It prevents the  
light rays from reaching the other object.  
The fist object casts a shadow on the second one.  
Shadows in Computer Graphics  
In ray tracing rendering, shadows can be added very easily:  
Light comes from some direction l;  
When computing the color of a point p on a surface, the point is in  
shadow if we cast a ray in direction l and it hits an object. Otherwise,  
the object is not in a shadow.  
How to implement shadows in  
rasterized renderings?  
Shadow Mapping!  
Shadow Mapping  
Basic idea: if we looked our scene from the view point of the  
light source, all of the objects that we can see would appear  
in light. Anything behind those objects, however, would be in  
shadow.  
Shadow Mapping  
Algorithm:  
Step 1: render the scene from the light's point of view (without  
calculating light, color or any other shading process).  
Step 2: extract and save (usually in a texture) the depth buffer from  
the rendering (z-buffer generated by the projection process).  
This texture is called shadow map. If there are multiple lights, a separate  
depth map must be used for each light.  
Step 3: perform a normal rendering pass, and when evaluating  
whether a fragment is visible to the light source, project its location in  
the shadow map and compare the looked-up value dmap with the  
actual distance d to the light source.  
If the distances are the same, the fragment’s point is illuminated; if the d > dmap,  
that implies there is a different surface closer to the source, so it is shadowed.  
Shadow Mapping  
Screen-Space Shadow Map  
Shadow Map  
Rendered Scene  
Frame Debugger  
Window->Frame Debugger  
Shadow Mapping Frame Debbuger  
Shadow Mapping Frame Debbuger  
Shadow Map Light 1  
Shadow Map Light 2  
Unity Shadows Settings  
Shadow quality settings: Edit -> Project Settings -> Quality  
Shadow settings per light:  
Unity Shadow Maps  
By default, Unity renders the scene four times per light (cascade  
technique). The shadow maps are split into four quadrants,  
each being rendered from a different point of view.  
Shadow Quality  
Hard vs. Soft Shadows:  
Hard Shadows  
Soft Shadows  
Shadow Quality  
Shadow problems:  
Numerical precision limitations (shadow acne):  
No bias  
With bias  
Shadow map resolution:  
No blur  
With Blur  
Shadow Quality  
Cascades Bands:  
Global Illumination  
Many surfaces in the real world receive most or all of their  
incident light from other reflective surfaces. This is often  
called indirect lighting or mutual illumination.  
Global Illumination  
Although accounting for the interreflection of light between  
surfaces is straightforward, it is potentially costly because all  
surfaces may reflect any given surface.  
Global Illumination  
Particle tracing process:  
Global Illumination in Games  
Traditionally, video games have been limited to direct lighting.  
The calculations required for indirect lighting were too slow so they  
could only be used in non-realtime situations (such as CG animated  
films).  
A solution to this limitation is to calculate indirect light in a  
pre-processing phase (only for static objects and surfaces):  
pre-calculated global illumination.  
Lightmapping!  
Lightmapping  
Lightmapping is a technique used to store lighting information  
into textures.  
Lightmaps allows global illumination at a relatively low computational  
cost.  
Instead of lighting the entire scene in  
real time, it pre-calculates all lighting  
information offline, render it into  
textures, and them use the textures  
in real-time.  
Lightmapping in Unity  
To create a lightmap, Unity computes the entire light path  
offline:  
Lightmapping in Unity  
Test scene:  
Lightmapping in Unity  
Baked Light configuration:  
Static Geometry  
The objects of the scene must be static:  
Lightmapping in Unity  
Real-Time Lightning vs. Lightmapped Scene:  
There is no specular lighting in the lightmapped scene!  
Real-Time Lightning  
Lightmapped Scene  
Lightmapping in Unity  
Lightmapping Settings  
Default resolution  
Higher resolution  
Indirect Light  
With lightmapping we have indirect lighting.  
Real-Time Lightning without Environment Lightning  
Lightmapped Scene without Environment Lightning  
Indirect Light  
With lightmapping we have indirect lighting.  
Real-Time Lightning without Environment Lightning  
Lightmapped Scene without Environment Lightning  
Indirect Light  
With lightmapping we have indirect lighting.  
Real-Time Lightning Emissive Surface  
Lightmapped Scene Emissive Surface  
Light Probes  
Lightmaps only work for static objects. In order to apply the  
baked lighting to dynamic objects we can use light probes.  
A light probe is a point in space that stores information about  
the lighting at that location.  
While lightmaps store information about light hitting the surfaces,  
light probes store information about light passing through a empty  
space.  
Placing Light Probes  
Add a new group of light probes:  
GameObject -> Light -> Light Probe Group  
Unlike lightmaps, the resolution of the light probe information  
is entirely defined by how closely packed the probes are  
positioned.  
Usually you begins by involving the area that will contain dynamic  
objects with the probes:  
Placing Light Probes  
Then add more probes depending on how the lighting  
conditions change.  
Place as few light probes as possible. It is essential that you do not  
place them inside static geometry.  
Place then in a more condensed pattern around areas that have  
complex or contrasting light.  
Placing Light Probes  
Even if the gameplay takes place on a 2D plane (for example,  
cars driving around on a road surface), the light probes must  
form a 3D volume.  
Bad choice of light probe positions  
Good choice of light probe positions  
Placing Light Probes  
Light probe positions must take into account that the lighting  
will be interpolated between sets of probes.  
Mixed Lighting  
Lightmaps allow us to compute light offline.  
Limitations: no specular lighting, no real-time shadows, and we need  
light probes to influence dynamic objects.  
Indirect light is the one thing that baked lighting has that real-  
time lighting lacks, because it requires a lightmap.  
Mixed lights allow indirect light to be combine it with real-  
time lighting.  
Of course this means that shading becomes more expensive.  
Mixed Lighting  
To use mixed lighting, the light’s mode  
has to be set to Mixed.  
The lightmap will only store indirect light  
information.  
Dynamic objects use the light probes to  
compute indirect light.  
Full Lightmap  
Only indirect light  
Mixed Lighting  
With lightmapping we have indirect lighting.  
Dynamic Object  
Lightmapped Scene  
Mixed Lighting Scene  
Mixed Lighting  
Mixed lighting is as much expensive as real-time lighting (it  
adds lightmaps for indirect light).  
We can improve this by including shadows into lightmaps:  
Shadowmask Mode.  
In this mode, both the indirect lighting and the shadow  
attenuation for mixed lights are stored in lightmaps.  
Mixed Lighting  
Mixed lighting with shadowmask is not as cheap as fully baked  
lighting.  
Subtractive Mode provides the fast and lowest-quality results: it  
renders shadows of dynamic object in real time for only one light, and  
composites them with baked direct and indirect lighting.  
Only recommended for platforms that are unable to use any  
of the other modes.  
Limitations:  
No specular lighting;  
Dynamic shadows only for one Directional Light  
Realtime Global Illumination  
Lightmapping works very well for static geometry, but it  
cannot deal with dynamic lights (like the sun traveling across  
the sky).  
To support real-time global illumination, Unity uses  
the Enlighten system (developed by SiliconStudio).  
It still requires a precomputation phase similar to the  
lightmapping process, and it is still limited to static  
objects.  
It precomputes all possible light bounces in the scene  
and encodes this information for use at runtime.  
Realtime Global Illumination  
Enlighten System:  
Realtime Global Illumination  
Realtime global illumination can be enabled independent of  
baked lighting. In addition, the Light Mode must be set to  
Realtime.  
Realtime Global Illumination  
Realtime Global Illumination can also be used for static  
objects that emit light. This makes it possible to vary their  
emission with matching realtime indirect light.  
Realtime Global Illumination  
Precomputed Realtime Global Illumination also has the  
limitation that only static objects can be included in the  
precomputation (moving objects cannot bounce light onto  
other objects).  
However, they can still pick up bounce light from static objects using  
Light Probes.  
Moving ambulance without indirect light  
Moving ambulance with indirect light from light probes  
Exercise 1  
1
) Add lights, shadows, and configure global illumination for the  
morgue room scene.  
The scene must have:  
Emissive ceiling lights;  
Indirect lighting;  
Shadows;  
Base Scene: https://edirlei.com/aulas/cg/morgue_room.zip  
Further Reading  
Hughes, J. F., et al. (2013). Computer Graphics: Principles  
and Practice (3rd ed.). Upper Saddle River, NJ: Addison-  
Wesley Professional. ISBN: 978-0-321-39952-6.  
Chapter 15: Ray Casting and Rasterization  
Chapter 26: Light  
Chapter 27: Materials and Scattering  
Marschner, S., et al. (2015). Fundamentals of Computer  
Graphics (4th ed.). A K Peters/CRC Press. ISBN: 978-  
1
482229394.  
Chapter 10: Surface Shading  
Chapter 11: Texture Mapping  
Chapter 18: Light  
Chapter 33: Global Illumination