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 />

var loader:URLLoader = new URLLoader();<br />

loader.dataFormat = URLLoaderDataFormat.BINARY;<br />

loader.addEv<strong>en</strong>tList<strong>en</strong>er(Ev<strong>en</strong>t.COMPLETE, onLoadComplete);<br />

loader.load(new URLRequest("myShader.pbj"));<br />

var shader:Shader;<br />

function onLoadComplete(ev<strong>en</strong>t:Ev<strong>en</strong>t):void {<br />

// Create a new shader and set the loaded data as its bytecode<br />

shader = new Shader();<br />

shader.byteCode = loader.data;<br />

}<br />

// You can also pass the bytecode to the Shader() constructor like this:<br />

// shader = new Shader(loader.data);<br />

// do something with the shader<br />

Embedded in the SWF file: the shader file can be embedded in the SWF file at compile time using the [Embed]<br />

metadata tag. The [Embed] metadata tag is only available if you use the Flex SDK to compile the SWF file. The<br />

[Embed] tag’s source parameter points to the shader file, and its mimeType parameter is "application/octetstream",<br />

as in this example:<br />

[Embed(source="myShader.pbj", mimeType="application/octet-stream")]<br />

var MyShaderClass:Class;<br />

// ...<br />

// create a shader and set the embedded shader as its bytecode<br />

var shader:Shader = new Shader();<br />

shader.byteCode = new MyShaderClass();<br />

// You can also pass the bytecode to the Shader() constructor like this:<br />

// var shader:Shader = new Shader(new MyShaderClass());<br />

// do something with the shader<br />

In either case, you link the raw shader bytecode (the URLLoader.data property or an instance of the [Embed] data<br />

class) to the Shader instance. As the previous examples demonstrate, you can assign the bytecode to the Shader<br />

instance in two ways. You can pass the shader bytecode as an argum<strong>en</strong>t to the Shader() constructor. Alternatively,<br />

you can set it as the Shader instance's byteCode property.<br />

Once a Pixel B<strong>en</strong>der shader has be<strong>en</strong> created and linked to a Shader object, you can use the shader to create effects in<br />

several ways. You can use it as a filter, a bl<strong>en</strong>d mode, a bitmap fill, or for stand-alone processing of bitmap or other<br />

data. You can also use the Shader object’s data property to access the shader’s metadata, specify input images, and set<br />

parameter values.<br />

Accessing shader metadata<br />

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

While creating a Pixel B<strong>en</strong>der shader kernel, the author can specify metadata about the shader in the Pixel B<strong>en</strong>der<br />

source code. While using a shader in ActionScript, you can examine the shader and extract its metadata.<br />

Last updated 6/6/2012<br />

303

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

Saved successfully!

Ooh no, something went wrong!