About Shadows
From Multiverse
| Client Scripting |
|
Overview • Coordinated Effects • Fireball Example • Terrain Decals • Compositors • About Shadows • Client Animation System • Creating Your Own Animations • Scripting Avatar Appearance |
| Reference |
Contents |
Introduction
The Multiverse Client can render shadows. This document describes the shadow techniques the client can use, how to configure shadow rendering, performance and other impacts of using shadows, and changes required to game assets to use shadows.
Terminology
- Caster
- An object that casts shadows.
- Receiver
- An object that receives shadows, meaning that shadows can be cast onto it.
- Shadow texture map
- The texture map that is used internally by the shadow rendering system to determine which areas are in shadow and which are not. There will be one shadow texture map per light that casts shadows.
Shadow techniques
The Multiverse Client uses texture modulative shadow rendering. In addition to the basic rendering pass, this mechanism performs two further rendering passes per light: a caster pass and a receiver pass. So, for example, a scene with one light requires three rendering passes to render.
During the caster pass, the client renders each shadow caster object from the point of view of the light; this pass produces the shadow texture map. During the receiver pass, the client renders each shadow receiver from the point of view of the main camera and applies the shadow it, using the shadow texture map to determine which pixels are shadowed and which are not.
There are three possible technique settings for rendering shadows:
- None - disables shadows (the default).
- Simple - uses the fixed function graphics pipeline, so it can run on older hardware that does not support pixel shaders. This technique has some limitations, described below.
- Depth - provides the best results, but requires a graphics card that supports pixel shader 2.0. If the graphics card doesn't support pixel shader 2.0, then the technique downgrades to simple.
Simple technique
The simple technique has several limitations:
- Objects cannot be both shadow casters and shadow receivers with this technique, and therefore does not support self-shadowing.
- Terrain is a receiver and all other objects are shadow casters.
- Parts of objects that stick below the terrain cause reverse shadows (pointing back towards the light).
Depth technique
With the depth technique, all objects can be both casters and receivers, and therefore it supports self-shadowing. The Depth technique does not have the "reverse shadows" problem that occurs with the Simple technique.
Configuring shadow properties
Use the ShadowConfig object of the Client Scripting API to configure display of shadows. This object has properties that configure shadow display.
Shadow technique
Set the ClientAPI.ShadowConfig.ShadowTechnique property to determine the shadowing technique the client uses. Possible values are: Simple, Depth, and None (the default).
For example, the Scripts/Startup.py file in the Multiverse sample asset repository includes the following lines:
# This function is an event handler that runs when the world has been initialized.
def WorldInitHandler(sender, args):
pass
#Uncomment the line below to turn on shadows in your world.
#ClientAPI.ShadowConfig.ShadowTechnique = ClientAPI.ShadowTechnique.Depth
# Register an event handler that will run when the world has been initialized.
ClientAPI.RegisterEventHandler('WorldInitialized', WorldInitHandler)
This code registers an event handler for the WorldInitialized event. To enable shadows, remove the comment (#) character from the beginning of the line that sets the ShadowTechnique. Note that this code uses an event handler because you cannot reference the ShadowConfig object until after the world has been initialized.
Shadow color
Set the ShadowColor property to modulate (multiply) the pixel color of objects that are in shadow.
Number of shadow textures
Use the ShadowTextureCount property to set the number of shadow textures the system allocates. If there are more shadow-casting lights than shadow textures, then the number of lights that can cast shadows will be limited by the number of shadow textures. The default value is one (1).
Shadow texture size
Use the ShadowTextureSize property to set the size of the shadow textures. Increasing this size reduces the blockiness of the shadows, but uses more texture memory. The default value is 1024.
The following table shows the approximate amount of texture memory required by different shadow texture sizes:
| Value of ShadowTextureSize | Texture Memory Required |
|---|---|
| 512 | 2 Megabytes |
| 1024 | 8 Megabytes |
| 2048 | 32 Megabytes |
| 4096 | 128 Megabytes |
Area in front of the camera that displays shadows
Use the ShadowFarDistance property to set the size of the area in front of the camera that displays shadows. Near the far edge of this distance the shadows will fade out. Increasing this distance will cause a larger area to be affected by shadows, but will result in blockier shadows, since the shadow texture is being stretched across a larger area.
Performance considerations
Each light that casts a shadow requires two extra rendering passes. This can have a large effect on the frame rate if you are using several shadow-casting lights in a scene. If you want your game to run on a wide variety of machines, limit the number of lights that cast shadows and the size of the shadow textures.
You can also write scripts that choose shadow settings based on the client machine's hardware capabilities. See Client Scripting API - HardwareCaps.
Material changes to support shadows
Objects that use vertex shaders to do animation must provide special versions of the vertex shaders for use during the shadow caster and receiver passes. If you use the shaders provided by Multiverse to do skeletal animation, then you can use the provided shadow caster and receiver vertex shaders by adding the following lines to your material:
shadow_caster_vertex_program_ref CompoundVS4_Shadow_Skinned
{
}
shadow_receiver_vertex_program_ref CompoundVS4_ShadowReceiver_Skinned
{
}
See Material Scripts for general information on the Multiverse Client's material scripting capabilities.
The example below is a material used by the human female model in the Multiverse Sampleworld asset repository. It illustrates how to include these lines in the context of a material definition.
material human_female.head_a_material
{
technique
{
pass
{
// Vertex program reference
vertex_program_ref CompoundVS4_Basic_Skinned
{
}
shadow_caster_vertex_program_ref CompoundVS4_Shadow_Skinned
{
}
shadow_receiver_vertex_program_ref CompoundVS4_ShadowReceiver_Skinned
{
}
fragment_program_ref CompoundPS_Basic
{
param_named spec_level float 0.1
param_named shininess float 1
}
texture_unit
{
texture human_female_face.dds
tex_coord_set 0
}
}
}
technique
{
pass
{
shading phong
ambient 1.00000 1.00000 1.00000 1.00000
diffuse 1.00000 1.00000 1.00000 1.00000
specular 0.00000 0.00000 0.00000 1.00000 0.00000
emissive 0.00000 0.00000 0.00000 1.00000
texture_unit
{
texture human_female_face.dds
tex_coord_set 0
}
}
}
}
