CS 184 Sp23 Project 4: Cloth Sim

Website Link: https://saltyminty.github.io/cs184-sp23-websites/proj4/index.html

Irene Geng | i-geng

Mingyang Wang | saltyminty

Overview:

In this project, we built a physics-based cloth simulation. We model a cloth as a grid of point masses connected by springs that provide structural, shearing, and bending constraints. We simulate the cloth in real-time by using Verlet numerical integration to approximate its shape and movement. In the last section of the project, we also implement several CGL shaders, for diffuse shading, Blinn-Phong shading, texture mapping, displacement and bump mapping, and environment-mapped reflections.

Part 1: Masses and springs

​​Part 2: Simulation via numerical integration

Part 3: Collisions with other objects

Part 4: Self-collisions

Part 5: Shaders


Part 1: Masses and springs

Overview: We simulate a cloth as a grid of point masses connected by several types of springs that enforce structural, shearing, and bending constraints.

Here are some screenshots of scene/pinned2.json from a viewing angle where we can clearly see the cloth wireframe; this shows the structure of our point masses and springs.

Here is what the wireframe looks like with different spring constraints enabled.

without shearing constraints

with only shearing constraints

with all constraints

with no point masses or constraints


Part 2: Simulation via numerical integration

Overview: At each timestep of the simulation, we calculate the total force acting on each point mass by applying the external force and spring correction forces. Then, we use Verlet numerical integration to update the positions of point masses. We also constrain position updates to prevent unreasonable deformation of springs.

At a very low ks, the cloth does not hold its starting shape well, and looks loose and stretchy after reaching its resting state. In contrast, at high ks, the cloth behaves stiffer and holds its shape more; the resting position’s shape is similar to its shape at the start of the simulation. This is as expected, since a higher ks corresponds to higher internal forces holding the cloth/system together. The final resting position of the simulations are shown below.

ks = 5

ks = 5000
(default)

ks = 10000


Density determines the density (and thus weight) of each individual point mass in our mass-spring system. This affects how much gravitational force affects our system and thus the way the cloth deforms when resting. A lower density results in lower gravitational force so the cloth deforms downwards less, while a higher density results in higher gravitational force and more downward deformation.

density = 0.5 g/cm^2

density = 15 g/cm^2
(default)

density = 15000 g/cm^2

Damping in the simulation represents the loss of energy due to friction and heat loss. When the damping term is small, the cloth continuously swings back and forth because the simulated system does not lose any energy at each timestep; the cloth will not slow to a stop. When the damping term is moderate, the cloth falls down at a natural rate and comes to a stop. When the damping term is large, the cloth falls down very slowly, before coming to a complete stop.

damping = 0

damping = 0.2
(default)

damping = 1

Here is a screenshot of our shaded cloth from scene/pinned4.json in its final resting state, with default parameters.


Part 3: Collisions with other objects

Overview: We support cloth collisions with spheres and planes. When a point mass in the cloth intersects or goes inside of a sphere/plane, we adjust the point mass’s position so that it is bumped up to the surface of the object.

Here are screenshots of our shaded cloth from scene/sphere.json in its final resting state on the sphere, with different ks values. At low ks, the cloth looks very loose and droops over the sphere, forming many small folds. At higher ks, the cloth behaves stiffer and holds its shape more, forming fewer, larger folds. This is as expected, since a higher ks corresponds to higher internal forces holding the cloth/system together. The final resting position of the simulations are shown below.

ks = 500

ks = 5000
(default)

ks = 50000


Here are screenshots of our shaded cloth lying peacefully violently at rest on the plane, with different shaders.

normal shading

mirror shader

Campanile texture shader

Displacement mapping


Part 4: Self-collisions

Overview: We implement self-collisions so that the cloth does not pass through itself. When two point masses in the cloth are too close to each other, we apply a repulsive collision force to move them apart. To improve efficiency, we spatially hash each point mass into a 3D box volume; at each timestep, we only check pairs of points that lie in the same 3D box volume.

Here are screenshots documenting how our cloth falls and folds in on itself, starting with an early, initial self-collision and ending with the cloth at a more restful state.


As we decrease density, the cloth gets stiffer and has less folds. A higher density causes more folds while while collapsing on itself, which is expected (as it corresponds to a lighter cloth)

As we increase ks, the cloth gets stiffer and has less folds. A lower ks causes more folds while while collapsing on itself, which is expected (as it corresponds to a stretchier cloth)

density = 1.5 g/cm^2

density = 15 g/cm^2
(default)

density = 150 g/cm^2

ks = 500

ks = 5000
(default)

ks = 50000


Part 5: Shaders

A CGL shader is a program that takes as input attributes of a vertex and the scene, and outputs a 4-dimensional vector that contains RGB and alpha information. Shader programs can run in parallel on a GPU to improve the efficiency and speed of rendering.

A typical CGL shader is composed of a vertex shader and a fragment shader. A vertex shader first applies various transformations to a single vertex; afterwards, these per-vertex values can be interpolated across the polygon faces of a mesh. The output of a vertex shader is used as input to a fragment shader, which computes an out color and determines the color of each pixel. Different geometric transformations specific to the vertex/fragment shaders will produce different visual effects, such as diffuse surfaces or surfaces with a Blinn-Phong reflection.

This is the equation for Blinn-Phong shading. It has three components:

The first term is  , which models reflected ambient light, which is shading that adds constant color to fill in black shadows and approximates any additional illumination not captured by the other terms.

The second term is , which models Lambertian/diffuse shading. The diffusely reflected light only depends on distance from the light source, and not view direction.

The third term is , which models specularly reflected light. This is the light that is reflected off the surface in the near-mirror direction.

Adding these three terms together produces the Blinn-Phong reflection.


Here are variations of our Blinn-Phong shader, where we output only a specific component.

only ambient component

ka = 0.05

only diffuse component

kd = 1.0

only specular component

ks = 1.0

entire Blinn-Phong model


Here is a screenshot of our texture mapping shader using the project writeup deliverable instructions as our texture.


Here are screenshots of bump mapping on the cloth and the sphere, as well as screenshots of displacement mapping on the sphere. We used texture_4.png, which looks like a checkerboard pattern.

sphere bump mapping

sphere displacement mapping

cloth bump mapping

cloth displacement mapping


Here are screenshots of bump and displacement mapping on the sphere with different mesh coarseness levels. In bump mapping, the higher resolution sphere -o 128 -a 128 has a smoother and rounder surface compared to the lower resolution sphere, which has visible corners upon close inspection. In displacement mapping, higher resolutions lead to sharper displacements and a less smooth look to the sphere, while in lower resolutions, the displacements are more smoothed out.

bump -o 16 -a 16

displacement -o 16 -a 16

bump -o 128 -a 128

displacement -o 128 -a 128


Here is a screenshot of our mirror shader on the cloth and on the sphere.