Shadow Volumes in Alpha

Author: Aras Pranckevicius

Description
This package implements very basic shadows using shadow volume algorithm. Like all shadow volume based shadows, it's very fillrate hungry, and this script only implements very basic functionality, but on the plus side it should work on pretty much all graphics cards and performance might be enough for simple cases. Requires Unity Pro as it uses GL class. (note: as of Unity 4.0, it works with Unity Basic as well)

Note that in Unity 2.0 and up there are easier to use built-in shadows based on shadow mapping algorithm. But this technique still works if you want shadow volumes.

Usage


The provided unity package contains the example scene inside. In general, the usage is this:
 * Use closed and shadow volume friendly meshes for shadow casters.
 * Meshes should be completely closed and each edge should be between exactly two triangles.
 * Meshes generally should not have hard shading creases, i.e. be smooth. Unless you implement the following point, meshes should be fairly highly tessellated as well.
 * Meshes should be highly tesselated, or each hard edge should actually have two zero-area triangles that join both sides of the hard edge. Imagine that if you move one face away, no holes should appear - the invisible triangles should stretch out and fill the gap.
 * Attach RenderShadowVolume.js to a Camera. Setup shadow casting Objects, shadow casting light, volume extrusion distance and the shader for extrusion (ShadowVolumeExtrusion) in Inspector.
 * Using no ambient lighting is probably a good idea as well.
 * Hit play and enjoy.

This script uses GL class and therefore requires Unity Pro. (note: as of Unity 4.0, it works with Unity Basic as well) It should run on about any hardware.

The package
Zipped Unity package for Unity 2.x: [[Media:ShadowVolumeAlpha.zip]]

Behind the scenes
The script implements shadow volumes without using the stencil buffer, as described in "Shadow Volumes Revisited" paper by Roettger, Irion and Ertl; 2002.

Shadow is rendered by darkening the screen twice in shadowed regions. The paper describes alternative ways of applying the shadow, this is just one of them.

Shadow volume extrusion is done in a vertex shader, by just moving away vertices that have normals facing away from the light source. This is not 100% correct way of doing the extrusion, but it often works fine for highly tesselated objects. Even with them, sometimes it introduces small 1 pixel shadow artifacts. The extrusion can be improved in two ways:
 * 1) Do extrusion on the CPU, extruding away faces (not vertices!) that face away from the light, and fill in shadow volume side faces. Or:
 * 2) Preprocess shadow casting meshes so that all vertices actually contain face normals, and between each non-planar triangle pair there are degenerate triangles included. Pretty much as described in "Shadow Volume Extrusion using a Vertex Shader" article on ShaderX by Brennan; 2002 (can be found on AMD developer site).

As this script uses alpha channel of the screen itself, it does not play nicely with Glow and other effects that also use alpha channel of the screen. A minor change could be made to make it render volumes into alpha channel of a screen-sized render texture.

History

 * 2008 Mar 11 Initial version