13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with Pixel B<strong>en</strong>der shaders<br />

Specifying shader input and parameter values<br />

Flash Player 10 and later, Adobe AIR 1.5 and later<br />

Many Pixel B<strong>en</strong>der shaders are defined to use one or more input images that are used in the shader processing. For<br />

example, it’s common for a shader to accept a source image and output that image with a particular effect applied to<br />

it. Dep<strong>en</strong>ding on how the shader is used the input value may be specified automatically or you may need to explicitly<br />

provide a value. Similarly, many shaders specify parameters that are used to customize the output of the shader. You<br />

must also explicitly set a value for each parameter before using the shader.<br />

You use the Shader object’s data property to set shader inputs and parameters and to determine whether a particular<br />

shader expects inputs or parameters. The data property is a ShaderData instance.<br />

Id<strong>en</strong>tifying shader inputs and parameters<br />

Flash Player 10 and later, Adobe AIR 1.5 and later<br />

The first step in specifying shader input and parameter values is to find out whether the particular shader you’re using<br />

expects any input images or parameters. Each Shader instance has a data property containing a ShaderData object. If<br />

the shader defines any inputs or parameters, they are accessed as properties of that ShaderData object. The properties’<br />

names match the names specified for the inputs and parameters in the shader source code. For example, if a shader<br />

defines an input named src, the ShaderData object has a property named src repres<strong>en</strong>ting that input. Each property<br />

that repres<strong>en</strong>ts an input is a ShaderInput instance, and each property that repres<strong>en</strong>ts a parameter is a ShaderParameter<br />

instance.<br />

Ideally, the author of the shader provides docum<strong>en</strong>tation for the shader, indicating what input image values and<br />

parameters the shader expects, what they repres<strong>en</strong>t, the appropriate values, and so forth.<br />

However, if the shader isn’t docum<strong>en</strong>ted (and you don’t have its source code) you can inspect the shader data to<br />

id<strong>en</strong>tify the inputs and parameters. The properties repres<strong>en</strong>ting inputs and parameters are dynamically added to the<br />

ShaderData object. Consequ<strong>en</strong>tly, you can use a for..in loop to examine the ShaderData object to find out whether<br />

its associated shader defines any inputs or parameters. As described in “Accessing shader metadata” on page 303, any<br />

metadata value defined for a shader is also accessed as a dynamic property added to the Shader.data property. Wh<strong>en</strong><br />

you use this technique to id<strong>en</strong>tify shader inputs and parameters, check the data type of the dynamic properties. If a<br />

property is a ShaderInput instance it repres<strong>en</strong>ts an input. If it is a ShaderParameter instance it repres<strong>en</strong>ts a parameter.<br />

Otherwise, it is a metadata value. The following example shows how to use a for..in loop to examine the dynamic<br />

properties of a shader’s data property. Each input (ShaderInput object) is added to a Vector instance named inputs.<br />

Each parameter (ShaderParameter object) is added to a Vector instance named parameters. Finally, any metadata<br />

properties are added to a Vector instance named metadata. Note that this example assumes a Shader instance named<br />

myShader is already created:<br />

Last updated 6/6/2012<br />

305

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!