Cellular autamata in pixel bender m0ose.com

This is a Cellular autamata built in Pixel Bender.
It is based on a JS!K project by Phil McCarthy .
a good description of the algorithm is here.
This source code can be found here .

To view this page ensure that Adobe Flash Player version 10.0.0 or greater is installed.



Here is the pixel bender source :




kernel DisplacementMap
<
namespace : "dood";
vendor : "m0ose";
version : 1;
description : "Displacement by bitmap map 16 bit. X is alpha and red. Y is green and blue";
>
{

parameter   float       dampening
    <
        minValue        :  0.0;
        maxValue        :  1.0;
        defaultValue    :  0.9;
    >;
    
input image4 src;
input image4 src2;

output pixel4 dst;


void evaluatePixel()
{
float2 pos = outCoord();
float2 pos2 = outCoord();

float4 centernew = sampleLinear(src2, pos);
float4 center = sampleLinear(src, pos);
pos2.x = pos.x - 1.0 ;
float4 left = sampleLinear( src, pos2 );
pos2.x = pos.x + 1.0 ;
float4 right = sampleLinear( src, pos2 );
pos2.x = pos.x ; 
pos2.y =  pos.y -1.0;
float4 up = sampleLinear( src, pos2 );
pos2.y = pos.y + 1.0 ;
float4 down = sampleLinear( src, pos2 );

float4 outP = sampleLinear(src, pos);

float velocity = ((left.b + right.b + up.b + down.b ) / 4.0) - centernew.b ;
velocity = velocity * dampening ; 
outP.b = center.b + velocity ;

dst = outP ;
}
}

and here is the as3 source source minus some small flex mxml stuff that were not working in the html.



			import flash.filters.ShaderFilter;
			
			import org.osmf.events.TimeEvent;
			
			public var w:int = 320	;
			public var h:int = 240 ;
			public var dampening:Number = 0.99;
			
			public var b0:BitmapData; 
			public var b1:BitmapData;
			public var b2:BitmapData;
			
			public var shader:Shader;
			public var filter:ShaderFilter;
			
			//load the pixel bender kernel
			[Embed(source="pb/waterAutomata3.pbj" , mimeType="application/octet-stream")]
			protected var displacer:Class;
			
			public var t:Timer ;
			public function init():void
			{
				b0 = new BitmapData( w,h,false, 0x0000ff/2) ;
				b1 = new BitmapData( w,h,false, 0x0000ff/2) ;
				b2 = new BitmapData( w,h,false, 0x0000ff/2) ;
				
				
				b0.fillRect( new Rectangle(19,10,20,40) , 0xff0000);
				b0.fillRect( new Rectangle(50,50,20,40) , 0xff00ff);

				shader = new Shader( new displacer() );
		
				//loop();
				t = new Timer(1);
				t.addEventListener(TimerEvent.TIMER, loop);
				t.start();
				
				_img.source = new Bitmap( b2);
			}
			
			public function loop(e:TimerEvent = null):void
			{
				
				shader.data.src.input = b1;
				shader.data.src2.input = b0;
				shader.data.dampening.value = [ dampening ];
				var sj:ShaderJob = new ShaderJob( shader, b2, b2.width, b2.height );
				sj.start( true);
				
				
				//b0 = b1.clone();//might not need clone?
				//b1 = b2.clone();
				b0 = b1;
				b1= b2.clone();
			}
			public function mouseOver(e:MouseEvent):void
			{
				perturb( e.localX, e.localY);
			}
			public function perturb(x:int,y:int):void
			{
				b0.fillRect( new Rectangle( x, y, 4,4), Math.random() * 0xff );
			}
			
			public function changeDampeningSlider():void
			{
				dampening = _dampSlide.value;
			}
			
			
			
		



    

cody smith ( m0ose2 @ yahoo dot com )