In the far distant past of last October I showed a little Stage3D accelerated particle system. Displaying a fantastic rate of progress I’ve made the same thing again, but this time it’s better, and uses HaXe.
Select a number of particles to create (50,000 or 100,000 is liable to take a while to load up and might even exhaust Flash’s available memory on lower spec machines). Once the particles have been generated you can drag with the left mouse button to change your view.
In terms of feature difference to the old version all that’s new is that any number of particles can be created (oh, also this version is in 3D). As there are limits to how many vertices and indices you can load into Stage3D for a single draw call, the larger particle counts make use of a simple batching system I’ve been building to spread themselves between multiple draw calls. My batch manager is still a little slow, and this example rather foolishly adds new particles one at a time, so you have to wait a little while it constructs the batches at the start.
15th April Update:
I’ve since rewritten how I handle memory in my nascent 3D framework to use the soon to be a “premium” feature of direct memory access. End result is that time to generate 100,000 particles went from 30 seconds to 5. To celebrate I added an option to generate 500,000 particles.
I should probably also mention that if you really did want huge numbers of particles active at once in a game you can save substantial memory use by creating just one batch’s worth of vertex data and drawing it several times each frame with varied vertex constants. In this example I’m quite deliberately wasting memory to test out my framework.
From a development point of view the big difference is that this is built using HaXe rather than AS3. Explaining HaXe is outside the scope of this little post, but it’s really pretty great and you should check it out. Its major feature is the ability to compile to a variety of languages, but for now I’m just using it as a straight up replacement for AS3 in Flash development as I enjoy the additional features of the language.
For shader work HaXe really stands out thanks to HxSL, which is a quite simple but very effective high level shader language, similar to GLSL for OpenGL or HLSL for DirectX. This means that I can write Stage3D shaders without directly using AGAL.
My HxSL shaders are still converted into AGAL-equivalent bytecode when the project is compiled so I’m still limited by the same restrictions as AGAL itself is: no loops (although HxSL can ‘unwind’ for loops between constant values) and a limit of 200 commands per shader being the main ones. But writing and especially maintaining the shader is vastly easier.
The HxSL version actually does slightly more, as it applies a simple billboarding effect to the particles so that they’re always facing the camera. Notice how the HxSL version is at least as readable as the AGAL even without any comments at all and how the comments for the AGAL dwarf the actual code.