Shading

Overview

Shading is the material reaction to incoming light. It is not necessary to simulate light with all its physical properties as most shading effects can be achieved with simple models. For performance it is best to use the simplest shading model that is sufficient. In CryENGINE we put most of the effort into the Illum shader that is used by most materials. Illum is an übershader which means it is not a final shader itself but a description of how to create hardware shaders, depending on some customization flags (see ).


High-quality shading as implemented in CryENGINE.

Phong and Others

Illum uses the Phong model which is the most known shading model. The model can represent materials like plastic (even translucent), wood and paper very well. Other materials like for vegetation, metal, skin, hair and cloth often require more sophisticated shading models and use other special übershaders. The different material look is rarely visible in the diffuse shading component. Special shaders often only differ in the specular behavior (anisotropic highlights, color shifting for car paint) and how translucency is handled (subsurface scattering, back-lighting).

In most real-time renderers you can specify a diffuse/albedo texture with diffuse/albedo material color. Unlike in some offline renderers, we simply multiply both colors as this is efficient and enables more interesting effects. The same applies for the specular texture and the specular material color. In the alpha channel of the specular texture we can store a multiplier (0 .. 1) to modify the specular power factor (1=dull to >50=polished). To avoid lighting artifacts with bad textures, we clamp the alpha channel (specular preset needs to be used) during Resource Compiler processing.


Different diffuse colors

The specular material color can be used to create some special effects but often it should be chosen between white (a red snooker ball for example has a white specular) and the diffuse color (blue metal for example reflects the scene and lights in blue). In rare cases a different specular color is useful. Imagine a reflective red ball coated with some dull yellow finish. White light travels into the finish which absorbs the blue parts. The diffuse reflection is red but the specular reflection is yellow. Complex car paints play with that effect but that is beyond the simple light model we explain here.


Different specular colors (left) and different specular levels


Different glossiness

The specular effects on materials are actually reflections of bright objects (e.g. sun, point light, glowing objects) and only in very reflective materials (e.g. mirror, metal) darker parts of the scene are visible (ordinary reflections). The material can have sharp reflections (high specular power value, e.g. >50) or only dull reflections (e.g. 1 .. 8). Both affect reflections as well as the specular computations with point lights or the sun.

The Fresnel effect is visible when light is reflected (specular). The effect depends on the angle between the eye and the material surface. The Fresnel term defines how much light is reflected and how much is left for other light interaction (more specular contribution means less diffuse). This feature is currently connected to material environment mapping. Actually the effect would be visible for any kind of lighting (point lights, directional, light probes) but much more subtle. For performance reasons we only have it as a special feature.


Different specular colors and reflection amount (left) and different Fresnel bias and Fresnel scale (right)

Many material systems allow to tweak the material ambient color independently from the diffuse (albedo) and specular material color. This allows more control but is likely to cause more problems. Materials reacts on light the same way, no matter if it's coming from a light source directly or through some diffuse or specular bounce from the scene.

Sub-surface scattering (SSS) can be seen with materials that are partly transparent. Light is traveling in the medium, scatters and gets filtered by the inner color and might finally exit at some other point of the surface. This has an interesting effect on shadows as the SSS part appears softer. This gives shadows on skin a subtle red border. The SSS contribution can be split into a view dependent and a non view dependent part (similar to diffuse and specular reflection). The view dependent part can be seen when looking at some bright light source with a scattering material in front of it (e.g. a leave). The light shape appears as some blurry image on the other material side. The non view dependent part can be seen no matter where the light source is. It can even be behind the viewer. That effect is usually much more blurry. When light travels through some medium, it can be filtered by the medium inner color. When using non primary colors for the medium, the result can have some interesting shades depending on the travel distance within the medium (e.g. as it can be seen on ocean water waves).

Transparency

Proper transparency handling is one of the open problems in real-time rendering. The problem can be solved in various ways but none of them has satisfying quality and performance properties.


Material properties: opacity, 2-sided, no shadow and additive

In CryENGINE shadows are currently not affected by the opacity. It is possible to extend shadow maps with another channel or three channels to enable colored shadows but the features is barely required and costs quite a lot of memory and bandwidth.

Projected shadows give almost the same effect and work much faster without the mentioned complications.


Light source with projector texture.

World Space Shading

Shading can be computed in any space as long as all values are transformed into the same space. For quality it is best to pick a space that is orthogonal, otherwise specular highlights will appear elliptical instead of circular. Since CryENGINE 2 we do world space shading for better quality and better performance with many lights. It is cheaper to transform the normal for tangent space normal mapping from tangent space into world space than transforming multiple lights into tangent space. In some cases (single light) it can be faster to do tangent space lighting so you might find both in the shaders.

Note that our world space shading is actually offset by some point near the camera. It can be the camera center but that might not be true when considering reflections. The benefit of this offset is higher precision in the floating point math. We avoid precision problems by moving them far away from the viewer.

Energy Preserving Specular

The Phong specular power factor is a value that is used to adjust how polished specular appears. As the brightness of the specular is usually independent from that, the energy reflected by the material changes as well.


Decreasing specular power using Phong model (left) and decreasing polishing degree which appears more natural (right).

This would be wrong if the value would define how polished the material is but it is correct as it is just a value in the model. To keep backward compatibility with the many existing materials, we did not change that. Both behaviors can be implemented with the same shader performance unless a specular texture is adjusting the power factor (alpha channel of the specular texture). Then some math cannot be done in the material setup but needs to be done later, in the pixel shader. Here it would also change the behavior as artists already darkened the specular texture (red, green, glue) to compensate for the problem.

Image based lighting doesn't have this problem as there is no specular power factor and the mip adjustment factor automatically produces the proper energy conserving results. A solution for the problem would be a shader flag but for the usual reasons we try to avoid the additional shader combinations. To make Image Based Lighting consistent with the Phong model, we can introduce an artificial brightening factor so that materials behave similar for both lighting approaches.