Live code: http://iamnop.com/particles-mrt/
Click to set the gravity point. Hold Alt for Maya camera controls.
(Requires webgl_draw_buffers extension. More info on how to enable.)
Video: https://www.youtube.com/watch?v=IyM0YxizdnY
GitHub: https://github.com/nopjia/particles-mrt
Technical Details
This demo can be broken into two parts: 1) the compute and 2) the view.
The compute part is where most of the work happens to simulate the particles. There are three textures representing each of the particles states: position, velocity, and color. Each particle is then represented by a UV coordinate into these textures, essentially a pixel, where its states are stored. A point to note here is that the position and velocity textures have to be float textures in order to represent the large range of numbers. At first I tried scaling up the range of a regular byte texture but there just wasn't enough resolution for the simulation.
The heavy lifting is done through a GLSL shader which I use as a compute shader. It reads in the three state textures (nearest filtering of course, wouldn't make sense otherwise) and performs a simulation step. Then it writes the results back to the corresponding framebuffers, all at once thanks to the webgl_draw_buffers extension. Because OpenGL doesn't allow writing to the same textures that you are reading from, I needed a duplicate set of framebuffers/textures to swap between every frame.
The view part is responsible for the actual drawing of the particles onto the screen. The number of particles is determined by the size of the compute textures (e.g., 1024x1024 textures would give us ~1 million particles!) As mentioned earlier, each particle is represented only by a pixel location in the state textures, so all I had to do for each vertex is to submit a UV coordinate. Then in the vertex shader, I simply use the UV to look up its position from the position texture and its color from the color texture. (The velocity texture is only used by the compute shader to run the simulation and isn't used here.)
In order to ensure smoothness, the simulation runs on a different loop than the rendering. The simulation step has to run at a fixed time step, independent of the frame rate, otherwise a variable time step would introduce irregularities in the simulation, which becomes quite apparent when there are a million particles. Putting the simulation on a fixed update step not only produces smooth results but also allows for consistent results no matter the frame rate.
WIP!
This is a work in progress, and I'm planning to add more features. Most important is to add support for not having the webgl_draw_buffers extension so any up-to-date browser can run this. Then I'm planning to add a few post-processing effects like ghost trails and bloom to make it look even more awesome. Finally, I'll need better UI to expose more settings to play with!
Note on Live Demo!
It is not guaranteed to work on all machines! The demo is using a draft extension which might not be supported on your hardware. Here's how to try to get it to work:
- Get the latest version of Chrome
- Go to chrome://flags/
- Enable "WebGL Draft Extensions" (chrome://flags/#enable-webgl-draft-extensions)
- For Windows, you might also need to enable D3D11 (chrome://flags/#enable-d3d11)
If it doesn't work, try again with Firefox? I'm sorry but I promise I will add support for no extension soon!




Nice! but actually the link to the live demo is broken right now.
ReplyDeleteWhat about flexibility of the system? What if I would like to create another effect? Then probably whole shader would have to be rewritten?
Just updated the post with how to get the demo to run. Make sure you have "WebGL Draft Extensions" flag enabled.
DeleteThe system is pretty flexible. The particle behavior is completely defined in particleCompute.fs. Just modify the shader and recompile it and you'd have a new effect. I am in fact thinking about implementing a live shader editor for this as well. It is definitely possible.
Works perfectly on XP Sp3 chromium 33 d3d9. Very beautiful.
ReplyDeletethis demo doesn't work on Firefox even I enable the extension config. It seems that we can't get the position from the compute buffer. Do u know why or if firefox doesn't support the extensions
ReplyDeleteHello there, is this Blog stil alive? Is there a possibility to trigger a click via script on a random position in the webgl-container? Thanks in advance
ReplyDeleteAt 24/7 Mobile Tyre Service Melbourne, we provide prompt and professional tyre solutions for drivers across Melbourne, no matter the time of day or night. Whether you have a flat tyre, require urgent roadside assistance, or need a complete tyre replacement, our experienced team is just a call away. We travel directly to your location—be it your home, workplace, or the side of the road—so you don't have to worry about towing or lengthy delays. With quick response times, quality tyres, and expert workmanship, we help you get back on the road safely and efficiently, 24 hours a day, 7 days a week.
ReplyDeletehttps://247mobiletyreservicemelbourne.com.au/
At Balgowlah Village Medical Practice, we are committed to providing high-quality medical care tailored to the needs of individuals and families across the Northern Beaches. Our experienced GPs offer a wide range of healthcare services, including general family medicine, preventive health checks, chronic disease management, skin cancer assessments, women's health, men's health, children's healthcare, immunisations, and mental health support. By focusing on compassionate, patient-centred care, we aim to build lasting relationships and provide reliable medical guidance to help patients maintain their health and wellbeing through every stage of life.
ReplyDeletehttps://balgowlahvillagemedicalpractice.com.au/
Boujee By Ashtons brings creativity and elegance to special occasions with beautifully curated party supplies and bespoke event styling services across Melbourne. From birthdays and baby showers to weddings, engagements, and corporate gatherings, our talented team works closely with clients to design celebrations that reflect their vision and personality. We offer everything from premium decorations and custom balloon arrangements to stylish backdrops and fully personalised event setups. With a passion for detail and a commitment to delivering exceptional experiences, we help turn every occasion into a memorable and visually stunning celebration.
ReplyDeletehttps://boujeebyashtons.com.au/
At Adelaide Aqua Boys, we provide expert pool care services to help residential and commercial pool owners enjoy clean, safe, and well-maintained swimming pools all year round. Our skilled team delivers comprehensive solutions, including regular pool servicing, water testing, chemical balancing, equipment checks, and preventative maintenance tailored to your pool's needs. With a commitment to quality workmanship and customer satisfaction, we ensure your pool remains crystal clear, efficient, and ready for use, giving you more time to relax and enjoy the water without the stress of ongoing upkeep.
ReplyDeletehttps://adelaideaquaboys.com.au/
At ProDental Clinic, we are dedicated to delivering exceptional dental care for patients of all ages in a welcoming and supportive environment. Our experienced dentists offer a comprehensive range of services, including regular dental examinations, preventive treatments, cosmetic dentistry, professional teeth whitening, dental implants, and clear aligner therapy. Using modern techniques and advanced technology, we create personalised treatment plans designed to improve both oral health and smile aesthetics. Our goal is to help every patient achieve a healthy, confident smile while enjoying comfortable and stress-free dental care at every visit.
ReplyDeletehttps://prodentalclinic.com.au/
At Kandid Loans, we simplify the lending process by providing personalised mortgage solutions tailored to your financial goals. Whether you're buying your first home, moving to a new property, refinancing an existing loan, or expanding your investment portfolio, our knowledgeable mortgage brokers are here to support you every step of the way. With access to a wide range of trusted lenders, we compare options, provide transparent advice, and manage the application process efficiently to help secure suitable loan products. Our goal is to make financing your property ambitions easier, more convenient, and aligned with your unique circumstances.
ReplyDeletehttps://kandidloans.com.au/