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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

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

Wh<strong>en</strong> you create a Shader instance and link it to a Pixel B<strong>en</strong>der shader, a ShaderData object containing data about the<br />

shader is created and stored in the Shader object’s data property. The ShaderData class doesn’t define any properties<br />

of its own. However, at run time a property is dynamically added to the ShaderData object for each metadata value<br />

defined in the shader source code. The name giv<strong>en</strong> to each property is the same as the name specified in the metadata.<br />

For example, suppose the source code of a Pixel B<strong>en</strong>der shader includes the following metadata definition:<br />

namespace : "Adobe::Example";<br />

v<strong>en</strong>dor : "Bob Jones";<br />

version : 1;<br />

description : "Creates a version of the specified image with the specified brightness.";<br />

The ShaderData object created for that shader is created with the following properties and values:<br />

namespace (String): "Adobe::Example"<br />

v<strong>en</strong>dor (String): "Bob Jones"<br />

version (String): "1"<br />

description (String): "Creates a version of the specified image with the specified brightness"<br />

Because metadata properties are dynamically added to the ShaderData object, you can use a for..in loop to examine<br />

the ShaderData object. Using this technique you can find out whether the shader has any metadata and what the<br />

metadata values are. In addition to metadata properties, a ShaderData object can have properties repres<strong>en</strong>ting inputs<br />

and parameters that are defined in the shader. Wh<strong>en</strong> you use a for..in loop to examine a ShaderData object, check<br />

the data type of each property to determine whether the property is an input (a ShaderInput instance), a parameter (a<br />

ShaderParameter instance), or a metadata value (a String instance). The following example shows how to use a<br />

for..in loop to examine the dynamic properties of a shader’s data property. Each metadata value is added to a Vector<br />

instance named metadata. Note that this example assumes a Shader instance named myShader is already created:<br />

var shaderData:ShaderData = myShader.data;<br />

var metadata:Vector. = new Vector.();<br />

for (var prop:String in shaderData)<br />

{<br />

if (!(shaderData[prop] is ShaderInput) && !(shaderData[prop] is ShaderParameter))<br />

{<br />

metadata[metadata.l<strong>en</strong>gth] = shaderData[prop];<br />

}<br />

}<br />

// do something with the metadata<br />

For a version of this example that also extracts shader inputs and parameters, see “Id<strong>en</strong>tifying shader inputs and<br />

parameters” on page 305. For more information about input and parameter properties, see “Specifying shader input<br />

and parameter values” on page 305.<br />

Last updated 6/6/2012<br />

304

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

Saved successfully!

Ooh no, something went wrong!