Earlier this year, I wrote a Belousov-Zhabotinsky simulation in ActionScript. Although very pretty, this sort of code is fairly slow in AS and deserved to be ported to Pixel Bender. So last night, I downloaded the Pixel Bender Toolkit and set about writing my first ever Pixel Bender shader.
The BZ reaction code I used was originally written for Processing by Alasdair Turner and is pretty simple - consisting of three lines of code to determine the values for each pixel in three 'channels' which refer to the three chemicals in the reaction.
Rather than manually looping over each pixel as the AS (and Processing) code, Pixel Bender has an evaluatePixel() method with is invoked for each pixel in parallel (or as parallel as possible). I sample each surrounding pixel using and average those values using a box blur. Finally, I set the output pixel based on the BZ algorithm.
The output is far nippier that the AS version and, because Pixel Bender filters execute on the GPU, the UI is far more responsive. I've added some quick-and-dirty drawing tools: the swatches in the left hand toolbar allow the user to select a colour draw on the screen.
I've included my Pixel Bender Kernel along with the compiled filter under the assets directory in the source code.
The application lives here and the source code is available here.
I just have to say thank you, your programs are inspiring, i was stagnating in my Actionscript programming and feel reinvigorated by your work
ReplyDeleteWow! Thanks Andy.
DeleteCheers,
Simon