Gamasutra: The Art & Business of Making Gamesspacer
Creating Stencil Shadows on iPhone
arrowPress Releases
March 25, 2019
Games Press
View All     RSS







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 Article Start Previous Page 2 of 3 Next
 


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


Article Start Previous Page 2 of 3 Next

Related Jobs

Phosphor Studios
Phosphor Studios — Chicago, Illinois, United States
[03.22.19]

UI Artist
Pixel Pool
Pixel Pool — Portland, Oregon, United States
[03.22.19]

Software Developer (Unreal Engine 4, Blueprint, C++)
Skybox Labs
Skybox Labs — Vancouver, British Columbia, Canada
[03.22.19]

Senior Gameplay Animator
Phosphor Studios
Phosphor Studios — Chicago, Illinois, United States
[03.22.19]

Senior Gameplay Programmer





Loading Comments

loader image