This project integrates a GPU-accelerated fluid simulation into a Next.js application using WebGL. It provides interactive, visually appealing fluid dynamics rendered directly in the browser.
- 💧 Real-time fluid simulation leveraging WebGL shaders
- 🖱️ Interactive fluid dynamics responding to user input
- 🧩 Modular and maintainable code structure with clear separation of concerns
- 🎨 Beautiful visual effects with customizable parameters
src/components/fluid: Contains the main fluid simulation componentsFluidComponentCorePart1.tsx: Core fluid simulation React component part 1FluidComponentCorePart2.tsx: Core fluid simulation React component part 2 (WebGL rendering and simulation)FluidComponent.tsx: Higher-level React component integrating fluid simulationconfig/: Configuration settingsfluidConfig.js: Default configuration for the fluid simulation
webgl/: WebGL utilities and shader managementBlitManager.js: WebGL rendering utilitiesFluidKernels.js: Simulation step functionsFramebufferManager.js: Framebuffer creation and managementRenderManager.js: Rendering pipelineShaderManager.js: Shader compilation and program managementSplatManager.js: Splat creation functionalityWebGLContextManager.js: WebGL context initializationshaders.js: Shader source code
utils/: Helper utilities and logginglogger.js: Logging utilities
npm installRun the development server:
npm run devOpen http://localhost:3000 (or the next available port) to view the application.
Build the application for production:
npm run buildStart the production server:
npm startfluid-nextjs/
├── public/
├── src/
│ ├── app/
│ └── components/
│ └── fluid/
│ ├── FluidComponentCorePart1.tsx
│ ├── FluidComponentCorePart2.tsx
│ ├── FluidComponent.tsx
│ ├── FluidSimulation.js
│ ├── config/
│ │ └── fluidConfig.js
│ ├── utils/
│ │ └── logger.js
│ └── webgl/
│ ├── BlitManager.js
│ ├── FluidKernels.js
│ ├── FramebufferManager.js
│ ├── RenderManager.js
│ ├── ShaderManager.js
│ ├── SplatManager.js
│ ├── WebGLContextManager.js
│ └── shaders.js
├── package.json
└── README.md
The fluid simulation uses the Navier-Stokes equations to create realistic fluid dynamics. WebGL shaders perform the complex calculations on the GPU, allowing for smooth, real-time performance even with high-resolution simulations.
The simulation is broken down into several steps:
- 🔄 Advection: Moving quantities (dye and velocity) through the velocity field
- 🌀 Curl Calculation: Computing the vorticity (curl) of the velocity field
- 🔄 Vorticity Confinement: Enhancing small-scale vortices for more interesting flow
- 📊 Divergence Calculation: Computing the divergence of the velocity field
- 📈 Pressure Solve: Solving the pressure equation using Jacobi iteration
- 🔄 Gradient Subtraction: Making the velocity field divergence-free
- 🎨 Rendering: Visualizing the dye and velocity fields
Each of these steps is implemented in a separate module for better maintainability and clarity.
- 🖱️ Click and drag to create fluid splats
- 💻 Watch as the fluid dynamically responds to your movements
This project is licensed under the MIT License. See LICENSE for details.
- Inspired by various WebGL fluid simulation techniques
- Built with Next.js, React, and WebGL
The app is deployed on GitHub Pages at:
https://mrballistic.github.io/fluid-nextjs/
To build and deploy:
- Configure
next.config.tswith the correctbasePathandassetPrefixfor your repo. - Run
npm run buildandnpm run exportto generate static files. - Deploy the
outdirectory to thegh-pagesbranch of your repository.
You can automate deployment using GitHub Actions or other CI/CD tools.