Shadow volume
Shadow volume is a shadow rendering technique proposed by F. Crow in 1977. It makes use of the stencil and depth buffers to perform boolean operations on the extruded silhouette of a given mesh, and extracts shadows with little rendering effort.
Depth (z-) pass
The advantage of the z-pass approach is that it does not need back or front caps. It is, however, unsuitable as it does not handle properly the case where the camera dwells inside a shadow volume (it then inverts the shadows).
Depth (z-) fail
The z-fail approach proposed by Bilodeau, Songy and Carmack is more suitable as it correctly handles such cases (although it still yields a very crisp sort of shadow).
Issue
The problem with the z-fail approach is that it needs front- and back- caps for the volume mesh, which are not trivial to extract. This is the part that is missing here.
Code
(This is an experimental feature!)
In client/app/engine/graphics/render/renderer.js
, shadow volumes may be turned on by setting the flag this.shadowVolumes = true;
. (Note: it is only visible when the sun is sufficiently high.)
The mesh that needs its shadow volume computed is the terrain mesh. It is in the form of a strip of faces (2 tris each) which are (xz), (yz) or (xy)-aligned, and of edge length 1 (and sqrt(2) for the diagonal).
In client/app/engine/graphics/terrain/shadow.js
, the getDynamicShadowVolumeGeometry
function generates an incomplete shadow volume. It is (almost) real-time for the default Perlin-generated strip of faces, so performance should be kept almost-real time. It simply clones the original strip and closes non-diagonal edges with a quad; the back-facing side of this mesh is later projected with a vertex shader (graphics/shaders/shadow
) to produce the final shadow volume.
So the mesh has front caps but it needs back caps.
At the end of this getDynamicShadowVolumeGeometry
function, the edgeHash
map has all the boundary edges.
Proposal: for each boundary edge (v1v2), add two vertices v3 and v4, with normals facing downward, and form a quad (v1v2v3v4). Then the vertex shader should project (v3v4) to infinity and emulate a back cap.
Other enhancements
Fine-tune the vertex shader to reduce possible precision artifacts (blinking faces) due to the volume shadow front-cap overlapping with the actual terrain geometry.