Lighting

Alexey Tukalo
Arction Ltd
Published in
6 min readDec 19, 2017

The combination of shadows and lights is essential for the visualization of a photorealistic 3D object on 2D screens. Shadows are nothing but the lack of light, therefore discussion of a light source appears to be a perfect start for our journey.

Light Sources

Almost any 3D scene contains one or more independent light source. A pixel brightness is calculated for every light source separately and summed together to get an amount of light reflected towards the camera. There are five light source types which we are going to discuss:

  • Ambient light fills the entire space uniformly. Surface position and direction don’t have any influence on the final result. There are no shadows if there is only ambient light on a scene, thereby everything looks flat. A small portion of this light type can be used as a fill light, to add some details in the deep shadows.
Scene with directional light rendered by LightningChart
  • Directional light is the simplest type of light able to produce shadows. It only takes into account the directions of the light source and exposed surface. There is no analogy for this type of light in real life, but in many cases, it can be used for simple and cheap simulation of certain lighting effects. It is a right choice for simulation of a very powerful and remotely located light source. The sunlight is a perfect example of directional light.
Scene with point light rendered by LightningChart
  • Point light is emitted in all directions from its center. In addition to directions, it also takes into consideration the distance between a surface and a light source. The Sun at a scale of the solar system is the closest real-life example of the point light.
  • Spotlight is a limited version of the point light. It shines only at a certain angle. A lamp is a perfect real-life example of this light source type. It is most commonly used for the creation of photorealistic images.
  • Area light is an advanced version of the point light. In this case, light is emitted by an arbitrary shape instead of a point. The lightsaber is the most famous example of area light, while a good real-life example of the area light would be the neon light.

Light and surface interactions

To produce the final image, light emitted from the light sources has to be correctly reflected from surfaces. There are several different ways of how the interaction can happen.

  • Emission Lighting is the simplest type of lighting. It ignores all light sources and defines the lowest brightness of the object. To some extent, it is similar to the Ambient light which is applied to a single object. An advanced rendering engine can treat the object with Emission Lighting as the Area light source. Emission lighting is just what the doctor ordered for a visualization of lightsaber. It can also be used for a nimbus and many other mystical shiny objects.
  • Diffuse Lighting is a part of light which would be reflected from the ideal matte surfaces. It is the most important type of lighting for accurate shape perception. It is calculated as a dot product between surface normal vector and the vector of the light ray. Vector normal is a vector perpendicular to the surface at a certain point. This type of reflection is not influenced by the relationship between positions of a camera and a surface. The picture below demonstrates that the ray produced by a light source is equally reflected towards both cameras in case of diffuse reflection.
Schematic drawing of diffuse reflection
  • Specular Lighting represents a mirror-like reflection of light sources. It lets the viewer feel the smoothness of a surface. Specular lighting tends to be essential for the rendering of glossy objects. In addition to the surface’s vector normal and vector of the light ray, calculation of specular lighting requires information about the camera position. The specular highlight happens if the vector of a light ray at the hit point is directly reflected towards the camera. This type of reflection is strongly influenced by the relationship between positions of a camera and a surface. The picture relies on the well-known fact that in mirror-like reflection an incidence angle of a ray is equal to a reflection angle of the ray from a surface. So the ray produced by the light source is reflected only to one of the cameras in case of specular light.
Schematic drawing of specular reflection

In real-life different reflection types are mixed on the same surface. The level of influence of this interaction type on the shading of particular objects is defined by coefficients. Together with the texture, they form a material which is used to describe the appearance and reflective characteristics of the surface.

Computation on GPU

Lighting calculation is a very expensive part of rendering, thereby it is important to understand how can it be done in an efficient way. It is also important to discuss aggressive approaches for an optimization of the process and trade-offs related to them.

There are two main choices for GPU accelerated computation. It can be executed at fragment/pixel shader or at vertex shader. You can read more about shaders here.

Due to inaccuracy occurring at the rasterization stage, the brightness calculated at vertex shader can be inaccurate. It is especially bad for calculation of specular lighting because the approach is not able to perform a precise transition of the highlight’s shape. Moreover, small highlights located in a center of polygons are completely lost in case of per-vertex lighting. That is why per-fragment lighting is the most common solution for modern 3D engines. It provides more accurate and smooth results. Nevertheless, calculations located at fragment/pixel shader are executed much more frequently, which makes the approach relatively slow. Therefore, per-vertex lighting is preferred for computationally intensive engineering and scientific visualizations, because performance is significantly more important than photorealism there.

Summary

There are several different types of light sources and ways how the light can be reflected from a 3D object. A wise combination of them allows creating photorealistic images of any kind. Lighting calculation is a computationally intensive process which can be efficiently accelerated by parallel execution on GPU. More advanced techniques for acceleration of the process, especially in the case of scenes with a huge number of light sources, will be discussed in the next articles.

--

--

Alexey Tukalo
Arction Ltd

Young language agnostic software developer interested in functional programming, software design, web development and computer graphics.