Future to the fantasy ★ ★
X3D Editor

Cobweb 3.1

WebGL X3D Browser

Something special, something more!

Dynamic Shadows

Shadows are a result of the absence of light due to occlusion; when a light source's light rays do not hit an object because it gets occluded by some other object the object is in shadow. Shadows add a great deal of realism to a lighted scene and make it easier for a viewer to observe spatial relationships between objects. They give a greater sense of depth to our scene and objects. For example, take a look at the following scene with and without shadows:

Note: This is an experimental feature and has not yet been standardized.


Turning on Shadows

To be able to use shadows, you first have to enable shadow rendering via Script node, second you need to enable Phong shading, third a light source. What kind of light source does not matter – shadows can be used with directional lights and spot lights as well as point lights. Shadow rendering is turned on, when the shadowIntensity field of a light node is set to a value greater than zero. The higher the setting, the more intensive the shadows will be. However, be advised that shadow computations are somehow expensive and can have noticable impact on the performance of your application. The performance can be optimized if a StaticGroup node is used for scene elements like floors, trees, rocks, buildings, and other static objects. If local lights are used, the scene can be optimized further and it is always a good idea to combine similar objects to one geometry.

Script to enable shadow rendering:

Script {
url "ecmascript:
function initialize ()
Browser .setBrowserOption ('Shading', 'PHONG');
Browser .setBrowserOption ('Shadows', true);

Cobweb and Titania add four important fields to every light node:

X3DLightNode : X3DChildNode {
SFColor [in,out] shadowColor 0 0 0 [0,1]
SFFloat [in,out] shadowIntensity 0 [0,1]
SFFloat [in,out] shadowDiffusion 0 [0,inf)
SFInt32 [] shadowMapSize 1024 (0,inf)

All light sources contain an shadowIntensity, a shadowColor, a shadowDiffusion, and a shadowMapSize field. The shadowIntensity field specifies the darkness of the shadow. Shadow intensity may range from 0.0 (no shadow) to 1.0 (full intensity). The shadowColor field specifies the spectral colour properties of the shadow as an RGB value. The shadowDiffusion field specifies an blur over the shadow. Shadow diffusion may range from 0.0 length units (no blur) to infinity. For every light there must be rendered an image of the shadow, the shadow map. The shadowMapSize field specifies the size of this image in pixels.