
October 8, 2008

I don’t usually post about two lines of code snippets, or about workarounds to that damn bug or about basic tutorials for beginners. I honestly haven’t enough time and will to post on a daily basis. Usually I use this blog to mantain me focused on a single project I find interesting and take it to the end (not a really working however :-P).
Logic comes first, code is the last step
So what about the title? What’s the the perfect beginner but somehow satisfying tutorial?
Answer snow/rain/starfield and you got it, and here we have a basic starfield class tutorial.
However the main reason I am posting this is not about the 30-40 code lines involved in rendering a random starfield but make you stop a moment and think about a possible easier way to do what you are doing.
I’ll try to be clearer: this morning I was working on a flash animation and just wanted to place a background starfield on my bucholic landscape view. So I quickly created the class but was missing something… off course a starfield is not cool if stars don’t twinkle (winkle, scintillate
shine… vary in light intensity etc.). So I started to recall the milions of experiments like this I did years ago in Actionscript 1 and ok… the first answer was something like:
- Generate every star as a sprite/movieclip (possibily in a main container)
- list the instance in some array, dictionary (whatever you prefer)
- to make ‘em twinkle, at every frame, do a for loop on every instance and vary its alpha with some some increment/decrement/sine calculation (o simply randomize it if you are lazy)
or you can have a Star class togheter with the Starfield class and let the Star class think about everything, so it creates its EnterFrame event to start twinkling when added (but it’s always a bad idea to have hundres of EnterFrame events, when you can have only one in the main class.
But…
Flash teach me that there are always many options to get to the same results (that is what I really like most about flash developing), AND… that bitmap filters and blend modes are a gift to hardcore developers with tremendous possibilities.
So.. do we really want a subtle background effect to keep 50% of the CPU? I don’t.
Continue Reading »

September 27, 2008
Updated on 04/27/2009: Class with some improvements to skip transparent pixels. The two images should however share the same amount (or similar) of visible pixels.
Lot of space for improvements here, if someone want to contribute very appreciated.
Ok, lot of time passed since I posted my PixelMorphing class experiment.
Seemed to me that post passed quite unobserved but then I received a lot of feedback and e-mail asking to share the source code.
Well, the first time I wanted to speed up posting and do further experimentation/optimization so I din’t prepare the source but since the main believe of this blog is that if someone ask for code I release it, here we are. Optimization still not done… I am a lazy one, so if someone want to help improve it is really welcome.
Now the steps to play with it:
- Download the PixelMorphing class
- Use it with this code:
import com.oaxoa.fx.PixelMorphing;
var pm:PixelMorphing=new PixelMorphing(pic1, pic2, 200);
addChild(pm);
pm.start();
//pm.reset();
or certainly you can place some buttons or some combo and wait for some event for it to start or reset. The main code of the previous post is this:
Continue Reading »

April 2, 2008
Hi there, some time since I last posted a technical post so here we are.
Brief
I was experimenting with some simple 3d formulas and this is the result…
1200 particles morphing in 3D space.
Added an interactive camera to start having fun.
Usage
Simply move your mouse up/down to move camera forward/backward and left/right for strafing.
Perss SPACE bar to cycle particle conformation between four different shapes.
For better experience move forward and backward while particles are morphing…
Following the morph from behind while moving very fast feel very gratifing for me
Performances
On my old CPU it performs @31fps and neither takes the cpu at 100%
No z-sorting here, just fun. I am not trying to build a real 3d engine… and no class for now, just some procedural code, but I am sure my cool readers will not have problems wrapping it inside a class definition if needed
Caurina Tweener needed.

Experience the demo
And here come the code! Simply past it in the first frame actions of a new fla.
Continue Reading »

February 24, 2008
I just edited the Fountain equalizer post adding the source code.
You’ll just need to download the two particles classes and copy the main code.
I received really a lot of requests about the code in the comments and by mail, and I am very pleased about this… means you liked it.
Expect more equalizing fun.

February 15, 2008
Hi there,
I am back after some pause with this interesting effect. This class takes every pixel from two pictures, sort them by averaged luminosity e reconstruct the second picture animating pixels from the first one.
The effect is a very nice pixel morphing (particles morphing) and the final result resambles some color ramp recolor tecnique.

Start demo
This is the second algorythm I wrote. The first one was build to do the calculation about moving pixels and then draw the full picture parsing the full array of pixels. The bigger problem in the first one was speed of execution.
The setPixel method of the BitmapData class is far too slow for this kind of effect and even slower was the parsing every frame of the full array containing all the pixel of the first image.
So I come with completely new algorythm that uses three bitmaps/bitmapdatas where the first only displays original image and set to trasparent only pixels that have moved away.
The scond one redraws every frame, but only displays moving pixels which are stored in a much shorter array and the last one shows only pixels which reached the final position.
With this second approach only pixels effectively changing are redrawn and a lot of speed can be gained.
This first post is only for showcase, during next days I will write a technical post about class structure, speed optimisations etc. off course with source code.
Don’t forget to leave a comment if you like it

December 27, 2007
Here’s a new experiment.
It uses the computeSpectrum method together with a simple particles class i wrote.
I think the effect is graphically very satisfing and personally cannot stop to look at it.
The song is “Aria regina della notte” from Mozart’s “Flauto Magico”.
Tomorrow I’ll post code and sources of this.
Hope you’ll be hypnothized from this as I am.

The picture is not well explaining, you must see this in action.
EDIT:
Ok, sorry for the late but here we are with the source code:
Download the SimpleParticleGenerator and SimpleParticle class zipped togheter, then just use this code to build the equalizer.
I release the code just now because I wanted to wrap the main code in a class too, and maybe add some methods to scale it like a component, but I realized it’s totally unuseful for few lines of code, and I don’t wanna loose the fun part in the experiment.
I promise that from now I will be faster to release the source code
// Author: Pierluigi Pesenti
// Web: http://blog.oaxoa.com
import flash.media.Sound;
import flash.net.URLRequest;
import flash.media.SoundMixer;
import flash.utils.ByteArray;
import flash.events.*;
import flash.display.Sprite;
import flash.filters.GlowFilter;
import com.oaxoa.fx.SimpleParticleGenerator;
import com.oaxoa.components.FrameRater;
var req:URLRequest=new URLRequest("mozart.mp3");
var sound:Sound=new Sound(req);
var ba:ByteArray=new ByteArray();
var samples:uint=32;
var offset:uint=256/samples;
// create a sprite and add 1*samples istances of the SimpleParticleGenerator class
var _holder:Sprite=new Sprite();
for(var i:uint=0; i<samples; i++) {
var pg:SimpleParticleGenerator=new SimpleParticleGenerator();
pg.y=252;
pg.x=15+i*offset*2;
pg.angleDeg=-90;
_holder.addChild(pg);
}
// add the holder sprite and start music
addChild(_holder);
sound.play();
// listener for frame based animation
addEventListener(Event.ENTER_FRAME, onframe);
// some fancy glow filter
// the glow filter is added to the holder sprite
// and not to the single particle for two reasons
// a lot of speed gain, and visually better blend result
var glow:GlowFilter=new GlowFilter();
glow.color=0xFFAA00;
_holder.filters=[glow];
// at every frame
function onframe(event:Event):void {
// compoute sound specturm
SoundMixer.computeSpectrum(ba, true);
var count:uint=0;
//for every generator
// with a 0-256 range only the left channel is sampled
// you can easily goto from 256 to 512 for the right or sample both and average value
for (var i:uint=0; i<256; i+=offset) {
var t:Number=ba.readFloat();
var n:Number=t*20;
var g=_holder.getChildAt(count);
// set the particles speed at a value of n
// n = the sound peak * 20 to raise the effect
g.speed=n;
count++;
}
}
//add frame rate meter
var fr:FrameRater=new FrameRater(0xffFFFF, false, true, 0x0099ff);
addChild(fr);
Expect more equalizing fun.