Skip to content
Yinliang Chen
All notes

Realistic 3D Cloth Simulation in Real-Time with WebGL

May 8, 2025
project lapse time: 2024-02-13 - 2024-05-07 This project aims to create a simulation of 3D cloth that exhibits visually plausible behavior in real-time, all rendered within a web browser using WebGL. The core ambition was to bring the complex dynamics of fabric to life, focusing on several key visual objectives:
  • Realistic Draping: Achieving a natural look as the cloth settles over objects like spheres and planes, clearly showing the effects of gravity.
  • Dynamic Folding and Wrinkling: Generating distinct and believable folds and wrinkles as the cloth interacts with objects and itself during movement.
  • Physically Consistent Interactions: Ensuring the cloth interacts with both static and dynamic objects without unrealistic interpenetration, conforming to surfaces in a believable manner.
The simulation's underlying methodology was inspired by the work of Baraff and Witkin on using implicit integration techniques to ensure stability even with large time steps in the simulation.
Visual Goal - Realistic Draping Example
Visual Goal - Dynamic Folding and Wrinkling
Visual Goal - Physically Consistent Interactions
The project successfully met its primary visual goals, delivering a real-time cloth simulation within a web browser environment.
  • Draping Achieved: The simulated cloth demonstrated stable and natural-looking draping when placed over a static sphere, forming gentle curves and folds that are consistent with gravitational effects.
  • Folding and Wrinkling: In tests involving self-collision, such as when the cloth hangs from pinned corners, the simulation produced noticeable and characteristic folds and wrinkles as the cloth collapsed under its own weight. While finer details could be enhanced with higher mesh resolutions, the fundamental buckling behavior was clearly present.
  • Consistent Interactions: The cloth interacted convincingly with the ground plane and a rolling test sphere, avoiding significant interpenetration thanks to effective collision response mechanisms.
A crucial factor in achieving these visual results with reasonable stability was the combination of explicit collision handling steps and iterative position correction using Provot's Dynamic Inverse method. Although the core integration step was a simplified version compared to a full implicit solver, these post-step corrections were vital for preventing excessive stretching and managing penetrations, thus maintaining visual plausibility at interactive rates. The use of a fixed physics timestep also played a significant role in ensuring consistency. The most significant challenge encountered during development was the implementation of robust and stable collision handling, particularly for cloth-sphere and cloth-self interactions. Initial approaches often resulted in visible interpenetration or numerical instability, such as jittering or "exploding" vertices, especially when the cloth was under tension or moving rapidly near an obstacle. Ensuring particles were correctly projected out of objects and calculating appropriate response velocities (including friction and restitution) without introducing excessive energy or instability proved difficult. While spatial hashing helped manage self-collision check complexity, stably resolving the actual penetrations was the core difficulty.
Cloth Draped Over a Static Sphere
Cloth draped over a static sphere.
Cloth Folding During Self-Collision Test
Cloth folding during self-collision test.
Cloth Interacting with Ground Plane and Sphere
Cloth interacting with ground plane.
Here is a video demo:
The deployed web application can be explored here: https://yinliang-cpsc510-cylqqqcyls-projects.vercel.app/ The cloth is modeled as a grid of particles interconnected by three types of springs: structural, shear, and bend springs. Particle positions and velocities are updated using a fixed timestep (1/60th of a second). The core physics update employs a simplified integration scheme. It applies gravity and basic damping, updates velocity and position, and then heavily relies on subsequent constraint steps for stability and collision handling. Key implementation aspects include:
  • Collision Detection and Response: Handled explicitly after the main position update. For sphere and ground collisions, particle velocities are adjusted directly, considering friction and restitution. Padding is used for early collision detection.
  • Self-Collision: A spatial hash grid accelerates neighbor finding for self-collision checks. Penetrating non-adjacent particles trigger position corrections.
  • Stretch Limiting: Provot's Dynamic Inverse method is iterated multiple times per step to correct particle positions and prevent springs from exceeding a defined stretch limit, significantly enhancing stability.
  • Pinned Constraints: Specific particles can be "pinned" by skipping their updates.
  • Rendering: Handled by WebGL, dynamically updating vertex buffers and using GLSL shaders for lighting. Camera controls (orbit, pan, zoom) and test case switching are implemented via event listeners.
While the project achieved its primary goals, several avenues exist for future enhancements:
  • Full Implicit Integration: Implementing the complete Baraff & Witkin implicit integration method to achieve greater stability with stiffer springs and potentially larger time steps.
  • Improved Collision Response: Refining collision handling, especially for self-collision resolution, possibly by incorporating position corrections directly into an implicit solve or exploring continuous collision detection methods.
  • Advanced Material Properties: Introducing parameters for anisotropic behavior (direction-dependent properties), plasticity (permanent deformation), or tearing.
  • Rendering Enhancements: Adding texture mapping and more sophisticated lighting and shading models for improved visual fidelity.
  • Optimization: Profiling the simulation loop and WebGL calls to identify and address performance bottlenecks.
This project provides a solid foundation in real-time cloth simulation, with many exciting possibilities for further development and refinement.
On this page