topical media & game development

talk show tell print

#graphic-flex-image-effects-05-Flex-ShaderJobTest.ax

#graphic-flex-image-effects-05-Flex-ShaderJobTest.ax [swf] [flash] flex


  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 @ax-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.
parameter: 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();
                  }
  
          }
  
  }
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.