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.

9 Comments

Richard

about 2 years ago

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

Reply

about 2 years ago

Cool! Ye, 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.

Reply

Kamen Dimitrov

about 2 years ago

This looks great, definitely will try to implement this myself! Thanks for the share

Reply

Mike Cann

about 2 weeks ago

My pleasue!

Reply

about 2 years ago

great 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:  public 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 ; )

Reply

Mike Cann

about 2 weeks ago

Thanks mate :) Good to know should I ever go back to it!

Reply

fenbf

about 2 weeks ago

What about CPU particle systems? are they doomed? But it is great that we can do such advanced system also in the browser or on mobile (for instance with GL ES 3.0/3.1)

Reply

Mike Cann

about 2 weeks ago

I dont think they are doomed if you want small simulations but big simulations GPU is the way to go.

Reply

Leave a Comment

Leave a Reply