Thursday 20 October 2011
GPU State Preserving Particle Systems with WebGL & HaXe
Well this is the post I didnt think was going to happen. I have been struggling for weeks with this little bit of tech, ill explain more about why it has been so difficult in another post. For now however, ill just talk about this sample.
So the idea was to build upon what I had been working with previously with my stateless particles systems with WebGL and HaXe. The intention from the start was to replicate some of my very early work (from 2007) on state preserving particle systems in WebGL.
Before I go any further, you can check it out in action here:
http://mikecann.co.uk/projects/HaxeWebGLParticles/
First a quick reminder. The difference between a stateless and state-preserving particle simulation is that in the latter we store and update the positions, velocities and other properties of each particle per frame, allowing us to interact and control the simulation. This differs from the stateless particle simulation (detailed in my previous post), where the position for each particle is calculated each frame based on a fixed algorithm.
A fairly reccent addition to WebGL made this possible, namely texture lookups in the vertex shader (aka Vertex Texture Fetch). I wont go over the exact details how this makes state preserving particle systems possible as I have already documented it in my earlier work. A brief explanation is that it allows you to use the fragment shader to perform the updates on particle state stored in textures then use the vertex shader to map those states to a point-sprite vertex buffer.
Basically what this means is that the entire particle simulation can be contained and updated on the GPU, which means no read-back. This allows us to achieve simulations of millions of particles without too much difficulty (depending on GPU ofcourse).
I have uploaded the source for people to perouse at their leisure:
https://github.com/mikecann/HaxeWebGLParticles
As usual it was written using the JS target of HaXe so it should be fairly easy to understand whats going on if you have written any Ecma-script-like-language. Im going to detail this in my next post, but the code isnt the best I have ever written as its a result of a mish-mash of various samples and examples I have found on the web. If anyone has any comments on things that are wrong or could be done better I would be very happy to hear about them.
Labels:
complex,
GLSL,
gpu,
Hardware,
haxe,
HTML5,
Javascript,
Particles,
Personal Projects,
Programming,
WebGL
Subscribe to:
Post Comments (Atom)
Nice - I've been considering (trying) to build a SPH particle demo on the GPU. It needs to check local particles to calculate pressure values, thus is pretty complex. I've seen some broadphase algos implements on the GPU, but I dont know if the work on OpenGL ES2
ReplyDeleteCool!
ReplyDeleteYe, well as mentioned in this post and the next (more rantish post) I actually started from some N-Body webGL code that I found on the net (cant find the link now). Im sure that would be a good starting point for you.
[...] a continuation of my earlier work on GPU particles using WebGL and HaXE. Im trying to emulate some work I did years ago in XNA, on the LieroXNA [...]
ReplyDeleteThis looks great, definitely will try to implement this myself! Thanks for the share
ReplyDeletegreat demos, Mike! I've been working through the learningwebgl.com lessons with haXe using your Mat4.hx file. I noticed when using the rotate method that my cube was stretching when rotating around more than one axis at the same time. Adding a bit of code to normalize the passed vector fixed it like:
ReplyDeletepublic function rotate(theta, x:Float, y:Float, z:Float)
{
var len:Float = Math.sqrt(x * x + y * y + z * z);
if (len != 1) {
len = 1.0 / len;
x *= len;
y *= len;
z *= len;
}
...
so just thought I'd let you know in case you use it for some 3D stuff ; )