A real-time cloth physics simulation using WebGPU, converted from the original C++ OpenGL implementation.
https://jayhuggie-cloth.netlify.app/
- A modern browser with WebGPU support:
- Chrome 113+ or Edge 113+
- Safari 18+ (with WebGPU enabled)
- Firefox also works too!
- Real-time cloth physics simulation with particle-based system
- Spring-damper connections for realistic cloth behavior
- Aerodynamic forces (wind simulation)
- Ground collision detection
- Interactive camera controls
- Real-time parameter adjustment via UI
- Left Click + Drag: Rotate camera
- Right Click + Drag: Zoom in/out
- R: Reset camera
- Z: Zoom in
- X: Zoom out
- ESC: Exit
The right panel provides controls for:
- Fixed Points: Translate and rotate the fixed attachment points
- Cloth Coefficients: Adjust mass, gravity, and ground level
- Spring-Damper: Modify spring and damping constants
- Aerodynamics: Control wind velocity, fluid density, and drag coefficient
web/
├── src/
│ ├── physics/ # Physics simulation classes
│ │ ├── Particle.ts
│ │ ├── SpringDamper.ts
│ │ └── Triangle.ts
│ ├── shaders/ # WGSL shaders
│ │ ├── cloth.vert.wgsl
│ │ └── cloth.frag.wgsl
│ ├── utils/ # Utility functions
│ │ ├── math.ts
│ │ └── webgpu.ts
│ ├── Camera.ts # Camera controller
│ ├── Cloth.ts # Main cloth simulation
│ ├── Ground.ts # Ground plane
│ ├── Renderer.ts # WebGPU rendering
│ └── main.ts # Application entry point
├── index.html # HTML entry point
├── package.json
└── vite.config.ts
- Particle-based mass-spring system
- Verlet integration with oversampling for stability
- Spring-damper connections (structural, shear, and bend)
- Aerodynamic forces based on triangle surface area
- Ground collision with position correction
- WebGPU rendering pipeline
- Phong lighting model with two light sources
- Real-time vertex buffer updates
- Depth testing for proper occlusion
- Language: TypeScript/JavaScript instead of C++
- Graphics API: WebGPU instead of OpenGL
- Shaders: WGSL instead of GLSL
- UI: HTML/CSS instead of ImGui
- Math Library: gl-matrix instead of GLM
WebGPU is a relatively new API. If you encounter issues:
- Make sure you're using a supported browser version
- Check that WebGPU is enabled (Chrome:
chrome://flags→ "Unsafe WebGPU") - Some browsers may require HTTPS for WebGPU (use
npm run previewwith HTTPS)
"WebGPU is not supported" error:
- Update your browser to the latest version
- Enable WebGPU in browser flags if needed
- Check browser console for more details
Performance issues:
- Reduce the number of particles in
Cloth.tsconstructor - Lower the oversampling count
- Close other browser tabs