Provides loading and managing of shaders.
This object keeps a map of requested shaders by path in order to avoid duplicates. It also provides a default shader for those cases where the required one is missing or still not yet loaded.
Loading of shaders is an asynchronous operation, so the application should periodically check for the manager to finish all the pending requests before updating all its shader references.
Remapping functionality is also supported by the manager.
Required scripts
The ShaderManager object requires:
/*{{ javascript("jslib/shadermanager.js") }}*/
/*{{ javascript("jslib/observer.js") }}*/
Summary
Syntax
var shaderManager = ShaderManager.create(graphicsDevice, requestHandler, defaultShader, errorCallback);
Summary
Requests loading of a shader by path.
Syntax
var onload = function onloadFn(shader) {};
var shader = shaderManager.load(path, onload);
Returns the requested shader if already loaded, the default shader otherwise. The callback function is called with the loaded shader as an argument.
Summary
Returns the loaded shader stored with the given path.
Syntax
var shader = shaderManager.get(path);
Returns the default shader if the required one is missing or not yet loaded.
Summary
Alias one shader to another name.
Syntax
shaderManager.map(alias, name);
Summary
Deletes the shader stored with the given path.
Syntax
shaderManager.remove(path);
Summary
Reloads the shader stored with the given path. Useful when the shader has been updated on the server and the application wants to retrieve the new one. The reloading of the shader will be done asynchronously.
Syntax
shaderManager.reload(path, callback);
The callback function is called with the loaded shader as an argument.
Summary
Reloads all the stored shaders. Useful when many shaders have been updated on the server and the application wants to retrieve the new ones.
This operation may take a long time to complete, the reloading of the shader will be done asynchronously.
Syntax
shaderManager.reloadAll();
Summary
Returns the number of shaders requested but still to be loaded.
Syntax
var numPendingShaders = shaderManager.getNumPendingShaders();
if (numPendingShaders)
{
keepWaiting();
}
Summary
Returns true if a shader is not pending.
Syntax
if (shaderManager.isShaderLoaded(path))
{
noMoreWaiting();
}
Summary
Returns true if a shader is missing.
Syntax
if (shaderManager.isShaderMissing(path))
{
errorWhilstLoading();
}
Summary
Enables remapping of loading paths.
The remapping only affects the loading URLs. The shader will be stored under the original path it was requested with.
Syntax
shaderManager.setPathRemapping(mappingDictionary, prefix);
// example usage:
var mappingTableReceived = function mappingTableReceivedFn(mappingTable)
{
shaderManager.setPathRemapping(mappingTable.urlMapping, mappingTable.assetPrefix);
};
mappingTable = TurbulenzServices.createMappingTable(gameSession,
mappingTableReceived);
If a remapping is required to find the shaders then this must be called before the renderer is created. If the remapping is done afterwards then some shaders may not load correctly.
Both arguments for setPathRemapping are properties on the MappingTable object.
Summary
Enables automatic resizing of a parameter arrays.
The resizing is done during loading, existing shaders will not be updated.
Ideally developers should modify the source shader code instead of patching it at runtime but this method is provided for convenience for those cases where the shader code may be used for separate applications with different requirements.
Syntax
shaderManager.setAutomaticParameterResize(name, size);
// example usage:
// The model used for this sample only has 20 bones so we optimize for it.
// Each bone has 3 V4s.
shaderManager.setAutomaticParameterResize("skinBones", 20 * 3);
Summary
Releases the ShaderManager object and all the resources it allocated; the object and the shaders it referenced will be invalid after the method is called.
Syntax
shaderManager.destroy();