WebGPU Particle Fluid Simulation
English | ็ฎไฝไธญๆ
๐ฎ Live Demo ยท ๐ Documentation ยท ๐ Specifications
A high-performance particle fluid simulation built with WebGPU compute shaders. Watch 10,000 particles interact with realistic physics, all running on your GPU.
๐ก Try it: Open the Live Demo and move your mouse over the simulation to push particles around!
โจ Highlights
โก GPU-Accelerated PhysicsAll physics calculations run on the GPU via WebGPU compute shaders. Gravity, repulsion, velocity clamping, and boundary bounce happen in parallel for 10,000 particles. |
๐ Frame-Rate IndependentPhysics uses delta time calculations, so the simulation runs at the same speed whether you're getting 30 FPS or 144 FPS. |
๐ฏ Adaptive QualityAutomatically detects your device capabilities and adjusts particle count from 2,500 to 10,000 for optimal performance. |
๐จ Persistent TrailsBeautiful motion trails via dedicated offscreen texture with velocity-based color mapping (cyan for slow, purple for fast). |
๐ Quick Start
Get the simulation running in under a minute:
# 1. Clone the repository
git clone https://github.com/LessUp/particle-fluid-sim.git
cd particle-fluid-sim
# 2. Install dependencies
npm install
# 3. Start development server
npm run dev
Then open http://localhost:5173 in a WebGPU-enabled browser.
Requirements
- Node.js 18+ (Download)
- Browser: Chrome 113+, Edge 113+, or Safari 17+ (Check WebGPU Support)
๐ฎ Controls
| Action | Effect |
|---|---|
| Move Mouse | Push particles away from cursor |
| Touch (Mobile) | Same as mouse interaction |
| Resize Window | Automatically adjusts HiDPI scaling |
๐ Project Structure
Built with OpenSpec for specification-driven development:
particle-fluid-sim/
โโโ openspec/ # ๐ OpenSpec framework
โ โโโ specs/ # Specifications (Single Source of Truth)
โ โ โโโ product/ # Product requirements & acceptance criteria
โ โ โโโ rfc/ # Technical architecture decisions
โ โ โโโ api/ # API contracts & type definitions
โ โ โโโ testing/ # BDD test specifications
โ โโโ changes/ # Active change proposals
โ โโโ config.yaml # OpenSpec configuration
โโโ docs/ # ๐ Developer & user documentation
โ โโโ API.md # Complete API reference
โ โโโ PERFORMANCE.md # Optimization guide
โ โโโ TROUBLESHOOTING.md # Common issues & solutions
โ โโโ architecture/ # System architecture
โ โโโ setup/ # Environment and toolchain setup
โ โโโ maintenance.md # Closeout workflow and maintenance guide
โโโ src/ # ๐ป Source code
โ โโโ config/ # Simulation constants & configuration
โ โโโ core/ # Core modules (WebGPU, physics, rendering)
โ โโโ shaders/ # WGSL compute & render shaders
โโโ scripts/ # ๐ง Build & release automation
โโโ .github/ # ๐ค CI/CD workflows & templates
๐๏ธ Architecture
CPU-GPU Heterogeneous Computing
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ CPU (TypeScript) โ
โ โข WebGPU initialization โ
โ โข Quality detection & scaling โ
โ โข Input handling (mouse/touch) โ
โ โข Frame loop orchestration โ
โโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ writeBuffer / commandEncoder
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ GPU (WGSL Shaders) โ
โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโโโโโ โ
โ โ 1.Compute โโโถโ 2.Trail โโโถโ 3. Render โ โ
โ โ Physics โ โ Effects โ โ Particles โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโฌโโโโโโโโ โ
โ โ โ
โ โผ โ
โ โโโโโโโโโโโโโโโโ โ
โ โ 4. Present โ โ
โ โ To Screen โ โ
โ โโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Physics Pipeline
Each particle updates every frame with these steps:
- Apply Gravity โ
velocity += gravity * deltaTime - Mouse Repulsion โ Push away if within radius
- Clamp Velocity โ Limit to
MAX_SPEED - Update Position โ
position += velocity * deltaTime - Boundary Bounce โ Elastic collision with damping
Performance Characteristics
| Metric | Value | Notes |
|---|---|---|
| Particles | 2,500 - 10,000 | Adaptive based on device |
| Particle Size | 16 bytes | 4 ร float32 (x, y, vx, vy) |
| Compute Workgroup | 64 threads | Optimized for GPU architecture |
| Frame Budget | < 16ms | Targets 60 FPS |
โ๏ธ Configuration
All constants in src/config/sim.ts:
| Parameter | Default | Description |
|---|---|---|
PARTICLE_COUNT |
10,000 | Base particle count (scaled by quality) |
GRAVITY |
{x: 0, y: 600} |
Gravity acceleration (px/sยฒ) |
MAX_SPEED |
800 | Velocity ceiling (px/s) |
REPULSION_RADIUS |
200 | Mouse influence radius (px) |
REPULSION_STRENGTH |
3,000 | Mouse repulsion force (px/s) |
DAMPING |
0.9 | Bounce energy retention (90%) |
TRAIL_FADE_ALPHA |
0.05 | Trail persistence per frame |
๐งช Testing
Property-based testing with Vitest + fast-check:
# Run all tests
npm test
# Watch mode (TDD)
npm run test:watch
# With coverage report
npm run test:coverage
Test Coverage
- โ Particle initialization bounds
- โ Physics integration correctness
- โ Boundary bounce behavior
- โ Color interpolation accuracy
- โ Quality heuristic calculations
- โ HiDPI coordinate mapping
๐ ๏ธ Development
Available Scripts
| Command | Description |
|---|---|
npm run dev |
Start development server with HMR |
npm run build |
TypeScript check + production build |
npm run preview |
Preview production build locally |
npm run lint |
Run ESLint |
npm run typecheck |
Run TypeScript type checking |
npm run format |
Format code with Prettier |
Contributing
We welcome contributions! Here's how to get started:
- Read AGENTS.md for Spec-Driven Development workflow
- Follow the Contributing Guide
- Update specs before implementing features
- Test your changes with
npm test - Submit a Pull Request
Quick start for contributors:
# 1. Fork and clone
git clone https://github.com/LessUp/particle-fluid-sim.git
cd particle-fluid-sim
# 2. Install dependencies
npm install
# 3. Create a feature branch
git checkout -b feature/your-feature-name
# 4. Make changes and run tests
npm test
# 5. Commit and push
git commit -m "feat: add your feature"
git push origin feature/your-feature-name
# 6. Open a Pull Request
๐ Documentation
Specifications (Source of Truth)
| Document | Description |
|---|---|
| ๐ Product Requirements | Functional & non-functional requirements |
| ๐ RFC 0001: Core Architecture | System architecture & design decisions |
| ๐ RFC 0002: Implementation Tasks | Implementation task tracking |
| ๐งช Testing Specification | BDD test specifications |
Guides & Tutorials
| Document | Description |
|---|---|
| ๐ API Reference | Complete API documentation |
| โก Performance Guide | Benchmarks and optimization |
| ๐ง Troubleshooting | Common issues and solutions |
| ๐ Docs Index | Curated map of the durable documentation set |
| ๐๏ธ Architecture | System architecture overview |
| ๐ Setup Guide | Environment setup, LSP, and local tooling |
| ๐ ๏ธ Workflow Guide | OpenSpec-first closeout workflow and review gates |
Additional Resources
๐ Browser Support
WebGPU is required. Check compatibility:
| Browser | Version | Status |
|---|---|---|
| Chrome | 113+ | โ Recommended |
| Edge | 113+ | โ Recommended |
| Safari | 17+ | โ macOS 14+ |
| Firefox | Nightly | โ ๏ธ Enable flag |
| Opera | 99+ | โ Chromium-based |
Firefox Users: Enable WebGPU by setting
dom.webgpu.enabled = trueinabout:config
๐ Acknowledgments
Built with modern web technologies:
- WebGPU - Next-generation graphics API
- TypeScript - Type-safe JavaScript
- Vite - Lightning-fast build tool
- Vitest - Blazing fast testing framework
- fast-check - Property-based testing
๐ License
MIT License ยฉ 2026 LessUp
See LICENSE for details.
๐ Links
๐ฎ Live Demo ยท ๐ป Repository ยท ๐ Issues ยท ๐ฌ Discussions
Made with ๐ and WebGPU