Shadowmap - thothbot/parallax GitHub Wiki
Shadow map plugin
This is internal plugin which adds shadow into scene.
This plugin is available in the core
package.
How to use
You should initialize shadow map plugin in the AnimatedScene
instance
@Override
protected void onStart()
{
ShadowMap shadowMap = new ShadowMap(getRenderer(), getScene());
}
Configuration methods
Hard vs Soft shadows
On the image is soft shadow.
Using the same lights, you can switch Shadow to Hard Shadows. In this case the transition from lit to shadowed regions is "hard": either something is 100% in shadow, or 100% lit. Hard shadows are faster to render but often they look less realistic.
// By default it is true
shadowMap.setSoft(false);
Shadow Cascade
Cascaded shadow maps are the best way to combat one of the most prevalent errors with shadowing: perspective aliasing.
images/shadow_hard_nocascade.jpg
Cascaded shadows work by dividing viewing area into progressively larger portions and using the same size shadow map on each. The result is that objects close to the viewer get more shadow map pixels than objects far away.
// By default it is false
shadowMap.setCascade(true);
Debug shadow map
For debug purpose you may want to see the field where scene objects can cast or receive shadow.
You can enable debugging by the following method:
// By default it is false
shadowMap.setDebugEnabled(true);
With visible shadow camera (see below) the image will be the following:
images/shadow_debug_camera.jpg
How to Make a shadow
To make shadow you should use Directional or Spot lights.
Those classes are implementations of the abstract ShadowLight
class.
Common methods
To enable shadow casting, do the following:
light.setCastShadow( true );
If you wish to cast shadow only, without lighting:
light.setOnlyShadow( true );
The following are shadow properties:
light.setShadowBias( -0.005 );
light.setShadowDarkness( 0.35 );
Also it is important to define shadow map width and height:
// By default they are 512
light.setShadowMapWidth( 1024 );
light.setShadowMapHeight( 1024 );
For debug purpose you may want to see shadow camera
light.setShadowCameraVisible( true );
Shadow from Directional light
This light uses Orthographic camera to generate shadow.
directionalLight.setShadowCameraNear( 200 );
directionalLight.setShadowCameraFar( 1500 );
directionalLight.setShadowCameraLeft( -500 );
directionalLight.setShadowCameraRight( 500 );
directionalLight.setShadowCameraTop( 500 );
directionalLight.setShadowCameraBottom( -500 );
Shadow from Spot light
This light uses Perspective camera to generate shadow.
light.setShadowCameraNear( 200 );
light.setShadowCameraFar( 1000 );
light.setShadowCameraFov( 50 );
How to Cast and Receive shadow by objects
All objects can cast and receive shadow.
To cast shadow, just enable it in object properties:
mesh.setCastShadow(true);
To receive shadow, as well as shadow casting, do the following:
mesh.setReceiveShadow(true);