package { import flash.display.Shader; import flash.filters.ShaderFilter; import flash.events.Event; import flash.events.MouseEvent; [SWF(width=500, height=500, backgroundColor=0x000000)] /** * Demonstrates use of a custom shader as a bitmap filter, showing how to use custom ShaderWrapper * to either load or embed shader bytecode. In addition, this class responds to user interaction * in order to update and reapply the shader with new settings. */ public class graphic_flex_image_effects_05_Flex_DesaturateTest extends graphic_flex_image_effects_05_Flex_AbstractImageLoader { // [Embed(source='/source/desaturate.pbj', mimeType='application/octet-stream')] // private static const DesaturateKernel:Class; private var _shaderWrapper:graphic_flex_image_effects_05_Flex_ShaderWrapper; /** * Constructor. Passes path of image to load to super class. */ public function graphic_flex_image_effects_05_Flex_DesaturateTest() { super("graphic-flex-image-effects-05-assets-butterflies.jpg"); } /** * Method that is called once image loads in super class. * This sets up loading of shader, or applies shader if bytes have been embedded. */ override protected function runPostImageLoad():void { // If you are using the Flex SDK, you can uncomment the Embed metatag above // and the following line, removing the other ShaderWrapper instantiation that follows, // in order to draw with the shader immediately and bypass loading //_shaderWrapper = new graphic_flex_image_effects_05_Flex_ShaderWrapper(DesaturateKernel); _shaderWrapper = new graphic_flex_image_effects_05_Flex_ShaderWrapper("graphic-flex-image-effects-05-assets-desaturate.pbj"); if (_shaderWrapper.shader == null) { _shaderWrapper.addEventListener(Event.COMPLETE, onShaderLoaded); } else { applyShader(); } } /** * Applies shader through call to setDesaturationFilter() based on current mouse position, * and adds loaded image to stage. A listener is set up for when the mouse moves. */ private function applyShader():void { addChild(_loadedBitmap); setDesaturationFilter(); stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove); } /** * Sets properties on shader based on current mouse position * and applies it to the loaded image. */ private function setDesaturationFilter():void { // full desaturation is when mouse is at right of stage var percent:Number = stage.mouseX/stage.stageWidth; // only update the shader if percent value has changed if (_shaderWrapper.getParameter("percent") != percent) { _shaderWrapper.setParameter("percent", percent); var shader:Shader = _shaderWrapper.shader; // reapply filter var filter:ShaderFilter = new ShaderFilter(shader); _loadedBitmap.filters = [filter]; } } /** * Handler for when the mouse moves. Calls setDesaturationFilter() to reapply shader. * * @param event Event dispatched by Stage. */ private function onStageMouseMove(event:MouseEvent):void { setDesaturationFilter(); } /** * Handler for when the shader completes loading. Calls applyShader(). * * @param event Event dispatched by ShaderProxy. */ private function onShaderLoaded(event:Event):void { applyShader(); } } }