Identifying shader inputs and parameters

The first step in specifying shader input and parameter values is to find out whether the particular shader you’re using expects any input images or parameters. Each Shader instance has a data property containing a ShaderData object. If the shader defines any inputs or parameters, they are accessed as properties of that ShaderData object. The properties’ names match the names specified for the inputs and parameters in the shader source code. For example, if a shader defines an input named src , the ShaderData object has a property named src representing that input. Each property that represents an input is a ShaderInput instance, and each property that represents a parameter is a ShaderParameter instance.

Ideally, the author of the shader provides documentation for the shader, indicating what input image values and parameters the shader expects, what they represent, the appropriate values, and so forth.

However, if the shader isn’t documented (and you don’t have its source code) you can inspect the shader data to identify the inputs and parameters. The properties representing inputs and parameters are dynamically added to the ShaderData object. Consequently, you can use a for..in loop to examine the ShaderData object to find out whether its associated shader defines any inputs or parameters. As described in Accessing shader metadata, any metadata value defined for a shader is also accessed as a dynamic property added to the Shader.data property. When you use this technique to identify shader inputs and parameters, check the data type of the dynamic properties. If a property is a ShaderInput instance it represents an input. If it is a ShaderParameter instance it represents a parameter. Otherwise, it is a metadata value. The following example shows how to use a for..in loop to examine the dynamic properties of a shader’s data property. Each input (ShaderInput object) is added to a Vector instance named inputs . Each parameter (ShaderParameter object) is added to a Vector instance named parameters . Finally, any metadata properties are added to a Vector instance named metadata . Note that this example assumes a Shader instance named myShader is already created:

var shaderData:ShaderData = myShader.data; 
var inputs:Vector.<ShaderInput> = new Vector.<ShaderInput>(); 
var parameters:Vector.<ShaderParameter> = new Vector.<ShaderParameter>(); 
var metadata:Vector.<String> = new Vector.<String>(); 
 
for (var prop:String in shaderData) 
{ 
    if (shaderData[prop] is ShaderInput) 
    { 
        inputs[inputs.length] = shaderData[prop]; 
    } 
    else if (shaderData[prop] is ShaderParameter) 
    { 
        parameters[parameters.length] = shaderData[prop]; 
    } 
    else 
    { 
        metadata[metadata.length] = shaderData[prop]; 
    } 
} 
 
// do something with the inputs or properties