Thursday, May 7, 2009

Dynamically changing Pixel Blender data at runtime

In my previous post, i had demonstrated the use of Pixel blender filters in Flex. In this post, we will be modifying the file that we did last time so that the user can change the red,blue and green channels and the alpha values at runtime.So if you haven’t done so , finish the previous tutorial. We can access the data of the Pixel Blender filter by the data property of the Shader class.The data property is an object of type ShaderData and it contains all the variables and parameters of the filter. The parameters are available as variables of type ShaderParameter. Here, we are going to use the same filter we used last time and we are going to allow the user to change the values of the color parameter at runtime with a Slider. Open the mxml file that you used for the previous tutorial and make the changes as :

<?xml version="1.0" encoding="utf-8"?>
<Application xmlns="" xmlns:gumbo="library:adobe/flex/gumbo" xmlns:mx="library:adobe/flex/halo" applicationComplete="init()">
import flash.display.*;
import flash.filters.ShaderFilter;
[Bindable]private var shader:Shader;
private var shaderFilter:ShaderFilter;
[Embed(source="assets/RGBAEdit.pbj", mimeType="application/octet-stream")]
private var ImgFilter:Class;

private function init():void
shader = new Shader(new ImgFilter());
shaderFilter = new ShaderFilter(shader);
img.filters = [shaderFilter];

private function apply(e:Event):void
{[0] = rVal.value;[1] = gVal.value;[2] = bVal.value;[3] = aVal.value;
shaderFilter = new ShaderFilter(shader);
img.filters = [shaderFilter];

<mx:Panel id="panel" title="Pixel Blender Test"> <mx:Image id="img" source="@Embed('assets/test.png')"/>


<mx:Label text="Red"/>

<mx:HSlider id="rVal" liveDragging="true" minimum="0" maximum="1" value="1" change="apply(event)"/>

<mx:Label text="Green"/>

<mx:HSlider id="gVal" liveDragging="true" minimum="0" maximum="1" value="1" change="apply(event)"/>



<mx:Label text="Blue"/>

<mx:HSlider id="bVal" liveDragging="true" minimum="0" maximum="1" value="0" change="apply(event)"/>

<mx:Label text="Alpha"/>

<mx:HSlider id="aVal" liveDragging="true" minimum="0" maximum="1" value="1" change="apply(event)"/>




No comments:

Post a Comment