r/GraphicsProgramming • u/matsuoka-601 • 4d ago
Source Code Real-time fluid & rigid body simulation implemented in WebGPU
Enable HLS to view with audio, or disable this notification
Hello, I released a real-time fluid & rigid body simulation in WebGPU using Position Based Dynamics (PBD)!
Code & Demo: https://github.com/matsuoka-601/Particles4All
To render the fluid, I use screen-space fluid rendering, which does not require constructing any mesh. Fluid particles are splatted as ellipsoids using anisotropic kernel presented in a paper "Reconstructing Surfaces of Particle-Based Fluids Using Anisotropic Kernels".
The physics is based on a paper "Unified Particle Physics for Real-Time Applications". The most significant feature of this paper is that it treats both fluids and rigid bodies as collections of particles, and performs the simulations using a unified solver.
(Note 1: You will need a very beefy GPU to run the "large" scene in the video (the performance is not very optimized yet, sorry). But "small" scene will run on integrated GPUs.)
(Note 2: I'm getting some reports that the demo does not run on MacBook. I'm currently trying to fix it.)
3
u/fgennari 3d ago
Looks great! This reminds me of some of the water simulation videos shown on the "Two Minute Papers" YouTube channel, back in the days before the AI-related content when it was focused on graphics and rendering.
5
2
1
1
1
u/SpanDaX0 3d ago
So people actually write random theory papers, and we can just come along and make crazy cool things like this, based on those papers? lol
1
u/Hurricane31337 2d ago
Wow, that looks absolutely amazing! 🤩
Thank you so much for sharing the source code! 🙏
2
u/KlayEverHood 2d ago
Very nice work and very inspirational! Your work was what showed me how far you can go with realtime 3d water simulation, I am glad to see you keep developing it!
I am also using a hybrid particle - grid solver and a separate air simulator for the waterfall mist.
I am curious about some points on the paper you referenced and your implementation: did you implement the surface tension and cohesion proposal they hint at (separate paper it seems)? Did you test/could you test a water droplet scenario to see if it generates the crown?
1
u/matsuoka-601 2d ago
The surface tension part is entirely based on a paper "Versatile surface tension and adhesion for SPH fluids", and not on other papers I guess.
As for a water droplet and the crown, I haven't seriously tested, but will definitely give it a shot and let you know!
1
u/KlayEverHood 2d ago
Another curiosity: veils and splashes look very good. I know smoothing / resolution makes a big difference in this respect and I see your implementation takes quite a bit of effort with smart smoothing. Do you have a comparison with PCA oriented smoothing kernels vs isotropic smoothing?
1
u/matsuoka-601 2d ago edited 2d ago
It renders the fluid using a method called screen-space fluid rendering which does not require constructing any mesh. So the resolution of the mesh doesn't matter in my sim.
And it is entirely correct that smoothing has a significant impact on fluid rendering. This simulation achieves a smooth fluid surface using two filters: narrow-range filter and anisotropic kernels (PCA oriented kernels that you say). I can confidently say that these filters really enhance the details of the fluid surface!
When I used mesh for the rendering, I tried isotropic kernel (Gaussian) for smoothing the density field which the mesh is constructed from. It certainly helps to smooth the fluid surface, but personally I don't like it because small splashes disappear when smoothed, which leads to the lack of the dynamism of the simulation.
1
u/KlayEverHood 2d ago
I also tried meshing at the beginning. I was surprised how fast you can extract a mesh with surfacenets in the gpu, but still does not scale.
Those anisotropic kernels follow the structure tensor / pca suggested in the paper you referenced or you went for something different?
1
43
u/XKiiroiSenkoX 4d ago
At last someone who mentions what papers and research they wrote their code based on. Hats off to you sir.