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

You use a ShaderJob object to execute a shader in stand-alone mode. First you create the ShaderJob object and link it<br />

to the Shader object repres<strong>en</strong>ting the shader to execute:<br />

var job:ShaderJob = new ShaderJob(myShader);<br />

Next, you set any input or parameter values that the shader expects. If you are executing the shader in the background,<br />

you also register a list<strong>en</strong>er for the ShaderJob object’s complete ev<strong>en</strong>t. Your list<strong>en</strong>er is called wh<strong>en</strong> the shader finishes<br />

its work:<br />

function completeHandler(ev<strong>en</strong>t:ShaderEv<strong>en</strong>t):void<br />

{<br />

// do something with the shader result<br />

}<br />

job.addEv<strong>en</strong>tList<strong>en</strong>er(ShaderEv<strong>en</strong>t.COMPLETE, completeHandler);<br />

Next, you create an object into which the shader operation result is writt<strong>en</strong> wh<strong>en</strong> the operation finishes. You assign<br />

that object to the ShaderJob object’s target property:<br />

var jobResult:BitmapData = new BitmapData(100, 75);<br />

job.target = jobResult;<br />

Assign a BitmapData instance to the target property if you are using the ShaderJob to perform image processing. If<br />

you are processing binary or number data, assign a ByteArray object or Vector. instance to the target<br />

property. In that case, you must set the ShaderJob object’s width and height properties to specify the amount of data<br />

to output to the target object.<br />

Note: You can set the ShaderJob object’s shader, target,width, and height properties in one step by passing argum<strong>en</strong>ts<br />

to the ShaderJob() constructor, like this:var job:ShaderJob = new ShaderJob(myShader, myTarget, myWidth,<br />

myHeight);<br />

Wh<strong>en</strong> you are ready to execute the shader, you call the ShaderJob object’s start() method:<br />

job.start();<br />

By default calling start() causes the ShaderJob to execute asynchronously. In that case program execution continues<br />

immediately with the next line of code rather than waiting for the shader to finish. Wh<strong>en</strong> the shader operation finishes,<br />

the ShaderJob object calls its complete ev<strong>en</strong>t list<strong>en</strong>ers, notifying them that it is done. At that point (that is, in the body<br />

of your complete ev<strong>en</strong>t list<strong>en</strong>er) the target object contains the shader operation result.<br />

Note: Instead of using the target property object, you can retrieve the shader result directly from the ev<strong>en</strong>t object that’s<br />

passed to your list<strong>en</strong>er method. The ev<strong>en</strong>t object is a ShaderEv<strong>en</strong>t instance. The ShaderEv<strong>en</strong>t object has three properties<br />

that can be used to access the result, dep<strong>en</strong>ding on the data type of the object you set as the target property:<br />

ShaderEv<strong>en</strong>t.bitmapData, ShaderEv<strong>en</strong>t.byteArray, and ShaderEv<strong>en</strong>t.vector.<br />

Alternatively, you can pass a true argum<strong>en</strong>t to the start() method. In that case the shader operation executes<br />

synchronously. All code (including interaction with the user interface and any other ev<strong>en</strong>ts) pauses while the shader<br />

executes. Wh<strong>en</strong> the shader finishes, the target object contains the shader result and the program continues with the<br />

next line of code.<br />

job.start(true);<br />

Last updated 6/6/2012<br />

321

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

Saved successfully!

Ooh no, something went wrong!