This project is an implementation of the paper Positon based fluids and a simple screen space fluid render based on WebGL.
View the project online at https://xuxmin.github.io/pbf
50k particles:
- Incompressible fluid simulation based on PBF method(Position based fluids), including surface tension, XSPH viscosity, and vorticity confinement.
- Use texture to store and share data. The calculation of each particles is carried out in parallel in the vertex shader, and the fragment shader is responsible for transferring data between textures.
- A four-step rendering method to search for neighbor particles, which is described in detail in GPU Gems.
- A simple screen space fliud rendering method, including depth/thick calculation, bilinear filtering smoothing, normal reconstruction, and shading.
The Performance was evaluated on my laptop with Intel Iris(R) Xe96EU Graphics, Intel Core i5-11320H @ 3.2GHz, no discrete graphics.
Under the configuration of 4 incompressibility confinement iterations each frame, the 50K particle runs at about 50 fps, 100K particles are about 25 fps.
View the project online in https://xuxmin.github.io/pbf. You can also download the code to run locally. If you want to run locally, you need to build a simple local server.
Method 1. Use the plugin of Visual Studio Code.
- In Visual Studio Code, install the plugin Live Server
- Ctrl+Shift+P,enter “Live Server: Open with Live Server”
- Then the browser automatically opens the local server with the specified address
Method 2. Use Node.js
-
Install node.js and npm.
-
Install a lightweight http server based on Node.js.
npm install http-server -g
-
Enter the project root directory and start the local server.
cd pbf http-server . -p 8000
-
Access
http://127.0.0.1:8000/
in the browser.
The web page is shown below:
The frame rate is displayed in the top left corner. Click "Run" button to start simulation, click again to stop. Press "Reset" button to reset the status. You can also enter the frame number in the text box next to the "Step" button, and then press "Step" button to simulate the specific frame number.
The parameters that can be adjusted:
-
particleSize: the size of particle to render, only useful when SSFR is off.
-
deltaTime: the time step of simulation.
-
resolution: the resolution to display the particles, you must click "Reset" button after adjust it.
-
particlesNum: the number of particles. Note that adjust the resolution at the same time to achieve the best display effect, finally click "Reset" button again.
-
solverIterations: the number of confinement iterations.
-
viscosity: the viscosity of the fluid.
-
vorticity: vorticity confinement.
-
SSFR: Screen space fluid rendering.
-
SSFR: whether to open the screen space fluid rendering.
-
tint_color_(r/g/b): the tint color.
-
max_attenuate, attenuate_k: color attenuation from thickness,
attenuate = max(exp(-attenuate_k*thickness), max_attenuate)
.
-
-
Surface tensile:
-
correction: whether surface tension correction is enabled.
-
tensileK: the parameter of surface tension correction.
-
-
Acceleration: the acceleration of the fliud (usually only gravity).
-
Obstacle: Add an obstacle。obstacleX, obstacleY, obstacleZ control the position。sizeX, sizeY, sizeZ control the size.
-
Macklin M , Müller, Matthias. Position based fluids[J]. Acm Transactions on Graphics, 2013, 32(4):1-12.
-
W. J. van der Laan, S. Green, and M. Sainz, “Screen space fluid rendering with curvature flow,” in Proceedings of the 2009 symposium on Interactive 3D graphics and games - I3D ’09, 2009, p. 91.
-
Chapter 29. Real-Time Rigid Body Simulation on GPUs | NVIDIA Developer
-
Screen Space Fluid Rendering for Games slices. Simon Green, NVIDIA