Gamasutra is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

October 20, 2020
October 20, 2020
Press Releases
October 20, 2020
Games Press

If you enjoy reading this site, you might also want to check out these UBM Tech sites:

# Creating Stencil Shadows on iPhone

June 16, 2010 Page 2 of 3

The third pass of the stencil buildup process. Here the complex shadows become more evident: the statue has different values in its shadow areas.

One of the problems is that if we have an alpha of 1 in our destination buffer, and an alpha of 1 for our rendered object, the value in the destination buffer will saturate at 1.

This can happen if a shadow volume is complex and has multiple front faces for the same pixel. A Torus, for instance, has two front facing walls and two back facing walls. The algorithm needs to add up to two before it can subtract in order to render into the buffer properly.

Stencil buffers can have a range of 256 values all the way up into the millions depending on how many bits the stencil buffer has. This allows the stencil to be added to repeatedly before the subtraction phase of the algorithm.

In order to resolve this with the alpha buffer, a value of 1/256 can be used for the alpha value of the shadow volume when it is rendered. This allows for additional complexity of geometry to accumulate in the buffer and avoid saturation.

The fourth pass of the stencil buildup process. The shadow stencil darkens more, and the complex shadows stand out.

However, this also requires more work later when it comes time to apply the shadows, so it is more efficient to use something on the order of 4/256 or 8/256 depending on the complexity of the objects in the scene and the fidelity desired. If too high a value is used for the complexity of the scene, the buffer will saturate resulting in shadowing artifacts.

The fifth pass of the stencil buildup process. Here the shadow stencil is nearly 0 everywhere there are shadows.

Once the saturation problem is solved, another problem presents itself. The resulting alpha mask is very light. As a result, when the full screen quad is rendered, the shadow will not be visible. The mask is also not consistently a single value, which will result in shadows of varying darkness when cast by complex objects. The solution is to perform a number of normalization passes on the buffer using a full screen quad.

The first pass of the stencil normalization process. Here the shadows are pulled down to 0, and the unshadowed areas become 1 - desired alpha value.

The initial pass is rendered with the quad at an alpha of 1/256 (fewer passes are needed if the alpha of the quad is larger as discussed earlier) with the Subtract blend operation, 1 - Destination Alpha as the source blend and One as the destination blend.

Each subsequent pass is performed with the Reverse Subtract blend operation, 1 - Destination Alpha as the source blend, and Destination Alpha as the destination blend. This builds up the alpha buffer such that we have a clear stencil of 1 where we have no shadows and a small alpha where we want shadows.

 Pass Alpha (unshadowed) Alpha (shadowed) Initial 0.0000 0.0040 Pass 1 1.0000 0.9922 Pass 2 1.0000 0.9767 Pass 3 1.0000 0.9306 Pass 4 1.0000 0.7966 Pass 5 1.0000 0.4312

Page 2 of 3

### Related Jobs

Remedy Entertainment — Espoo, Finland
[10.20.20]

Senior Material Artist
innogames — Hamburg, Germany
[10.20.20]

Mobile Software Developer (C++) - Video Game: Forge of Empires
Camouflaj — Bellevue, Washington, United States
[10.17.20]

Senior Graphics Engineer
Camouflaj — Bellevue, Washington, United States
[10.17.20]

Animation Engineer