Provides a deferred shading solution with support for shadow mapping and fullscreen post effects.
At draw time all the visible renderables are classified and sorted to maximize rendering performance, minimizing shader technique changes whilst taking advantage of hardware Z-buffers.
Renderable objects are classified into 3 main categories:
The application can draw any additional renderables by a set of callback functions given to the DeferredRendering object.
This object creates a G-buffer to store the following properties for each visible pixel in the screen:
Once the information is filled by the opaque renderables then the DeferredRendering object proceeds to the lighting of the scene depending on the active lights affecting the visible scene. These are the light types currently supported:
For point, spot and fog lights the lighting attenuation is calculated using two textures, the falloff texture provides attenuation depending on the Z coordinate on light space and the projection texture provides attenuation and color based on the X and Y coordinates on light space.
Occlusion queries are used to determine the contribution of the light to the scene and features are enabled or disabled accordingly to improve performance and picture quality.
If shadows are enabled, multiple shadow maps will be created and reused for each light that casts shadows. Different resolutions will be used depending on the size of the light volume. The implementation is using exponential shadow maps which allows the application of a dual pass Gaussian blur to the shadow maps in order to produce smoother shadows.
Non-opaque renderables are rendered as a separate pass after the deferred lighting is done.
Once all the composition passes have finished, the shaded scene will be transferred to the back buffer and at this point fullscreen post effect can be applied.
The DeferredRendering object will request the following shaders to the ShaderManager:
These are the effects supported by this renderer:
The following meta material properties are supported for the scene renderables:
The renderable will not cast shadows.
Non-opaque material that will blend with the background. The renderable will be rendered back to front and will be ignored during lighting calculations. Materials using effects with alpha test enabled but without blending enabled do not need this flag.
Non-opaque material that will blend with the surface immediately below. The renderable will be ignored during lighting calculations. Materials using effects with alpha test enabled but without blending enabled do not need this flag.
Scale for the flare dimensions in world units. Only required if effect is flare.
The renderable will be rendered last if opaque or first if transparent and will be ignored during lighting calculations.
The following TechniqueParameters properties are supported for the scene renderables:
Array with 4 numbers providing the material color
Array of 6 numbers forming a 3x2 transformation matrix applied to the uv coordinates
Texture that will provide the diffuse color
Texture that will provide the specular color
Texture that will provide the per pixel normal
Texture that will provide the emissive color
Cubemap texture that will provide the skybox color
Texture that will provide the alpha component
The following TechniqueParameters properties are supported for the scene lights:
Texture that will modulate the light color on the XY plane
Texture that will modulate the light color on the Z plane
The renderables are rendered in the following passes:
Required scripts
The DefaultRendering object requires:
/*{{ javascript("jslib/renderingcommon.js") }}*/
/*{{ javascript("jslib/deferredrendering.js") }}*/
Summary
Syntax
var settings = {
shadowRendering: true,
shadowSizeLow: 512,
shadowSizeHigh: 1024
};
var renderer = DeferredRendering.create(graphicsDevice, mathDevice, shaderManager, effectsManager, settings);
The shadowRendering option enables shadow mapping. The shadowSizeLow and shadowSizeHigh set the sizes for the ShadowMapping object shadow textures.
Summary
Prepares the render buffers required for the deferred shading.
Syntax
if (!renderer.updateBuffers(graphicsDevice, width, height))
{
errorCallback("Failed to initialize deferred renderer");
}
If the function returns false the creation of the render buffers failed, possibly because of the lack of video memory.
This method does nothing if the internal buffers are already of the required dimensions.
Do not call this method inside the beginFrame / endFrame rendering block.
Summary
Updates the shaders used for the deferred shading.
Syntax
renderer.updateShader(shaderManager);
If the required shaders were not ready when the renderer was created this method can be used to update them.
Summary
Updates light and material information to prepare for the deferred shading.
Syntax
renderer.update(graphicsDevice, camera, scene, currentTime);
Call this function after the scene and the camera have been updated for the current frame.
Summary
Renders the scene.
Syntax
renderer.draw(graphicsDevice,
clearColor,
drawDecalsFn,
drawTransparentFn,
drawDebugFn,
postFXsetupFn);
The callback executed to draw extra visible decals, it can be set to null. For example:
function drawDecalsFn()
{
fxm.drawDecals(graphicsDevice);
}
The callback executed to draw extra visible transparent objects, it can be set to null. For example:
function drawTransparentFn()
{
fxm.drawTransparent(graphicsDevice);
}
The callback executed to allow the application draw any debugging information required, it can be set to null. For example:
function drawDebugFn()
{
scene.drawNodesExtents(graphicsDevice, camera);
}
The callback executed to set the GraphicsDevice to the state required for the transfer of the shaded scene to the backbuffer, can also be used to apply fullscreen effects. It is only required to set the active technique and set the required TechniqueParameters parameter, the renderer is responsible for drawing the fullscreen quad. For example:
function copyPostFXSetupFn(graphicsDevice, finalTexture)
{
graphicsDevice.setTechnique(copyTechnique);
copyTechniqueParameters.colorTexture = finalTexture;
graphicsDevice.setTechniqueParameters(copyTechniqueParameters);
};
You can find some examples in the PostEffects object.
Summary
Sets the lighting scale factor applied to the lighting calculations. By default this values is set to 2.0.
Syntax
renderer.setLightingScale(1.0);
Get the default size of the buffer used by skinning. This will be undefined until the shaders are loaded.
See also GPUSkinController.
For example:
GPUSkinController.setDefaultBufferSize(renderer.getDefaultSkinBufferSize());
Summary
Releases the DeferredRendering object and all the resources it allocated.
Syntax
renderer.destroy();
Summary
The version number of the DeferredRendering implementation.
Syntax
var versionNumber = DeferredRendering.version;
Summary
A dictionary of passes to passIndex used by DrawParameters to specify the pass they are rendered in.
Valid values are:
Syntax
drawParameters.userData.passIndex = renderer.passIndex.transparent;