package { import flash.display.BitmapData; import flash.display.ShaderJob; import flash.events.ShaderEvent; [SWF(width=300, height=300, backgroundColor=0x000000)] /** * Builds on SquareGradientTest to show how a shader can be run as a backgroudn process. */ public class graphic_flex_image_effects_05_Flex_ShaderJobTest extends graphic_flex_image_effects_05_Flex_SquareGradientTest { private var _shaderJob:ShaderJob; /** * Draws with shader initially. This override stops super class's drawing with the shader * as a synchronous process and instead sets up a ShaderJob to run the process in the background. * When the job fires its COMPLETE event, the shader will be drawn into the shape. */ override protected function drawShader():void { var width:Number = stage.stageWidth; var height:Number = stage.stageHeight; // the bitmap the shader will write its data into var bitmapData:BitmapData = new BitmapData(width, height, true); _shaderProxy.center = [width/2, height/2]; _shaderProxy.gradient = getGradient(); // new job set up with shader and bitmap data to write shader result into _shaderJob = new ShaderJob(_shaderProxy.shader, bitmapData); _shaderJob.addEventListener(ShaderEvent.COMPLETE, onShaderComplete); _shaderJob.start(); } /** * Handler for when the shader job completes. This draws with the shader. * * @param event Event dispatched by ShaderJob. */ private function onShaderComplete(event:ShaderEvent):void { graphics.beginBitmapFill(event.bitmapData); graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight); graphics.endFill(); } } }