โ† Back to Home

WebGPU Particle Fluid Simulation

Version CI Pages License: MIT

WebGPU TypeScript Vite

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 Physics

All 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 Independent

Physics uses delta time calculations, so the simulation runs at the same speed whether you're getting 30 FPS or 144 FPS.

๐ŸŽฏ Adaptive Quality

Automatically detects your device capabilities and adjusts particle count from 2,500 to 10,000 for optimal performance.

๐ŸŽจ Persistent Trails

Beautiful 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

๐ŸŽฎ 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:

  1. Apply Gravity โ†’ velocity += gravity * deltaTime
  2. Mouse Repulsion โ†’ Push away if within radius
  3. Clamp Velocity โ†’ Limit to MAX_SPEED
  4. Update Position โ†’ position += velocity * deltaTime
  5. 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

๐Ÿ› ๏ธ 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:

  1. Read AGENTS.md for Spec-Driven Development workflow
  2. Follow the Contributing Guide
  3. Update specs before implementing features
  4. Test your changes with npm test
  5. 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 = true in about:config

๐Ÿ™ Acknowledgments

Built with modern web technologies:

๐Ÿ“„ License

MIT License ยฉ 2026 LessUp

See LICENSE for details.

๐Ÿ”— Links

๐Ÿ”ฎ Live Demo ยท ๐Ÿ’ป Repository ยท ๐Ÿ› Issues ยท ๐Ÿ’ฌ Discussions


Made with ๐Ÿ’œ and WebGPU