Table of Contents

Cloth you can tear

A sheet of 128 by 80 points in the wind, held together by 20,352 springs that tear when stretched to three times their length. Each WebAssembly instruction solves four springs.

Starting the module...
Drag pull the cloth Right-drag cut it Scroll nearer or farther

A cut stays cut, and a hard pull tears the cloth by itself. Dropped on the ball, the cloth drapes over it and settles on the floor around it.

The kernel

Each frame is eight substeps. A Verlet step moves every point by its velocity (where it is minus where it was), plus gravity and a wind that varies along the rows and across the columns, so the sheet billows instead of tilting. Three passes over the springs follow. Each pass pulls every pair of neighbours toward its rest distance, shared by the two points' inverse masses. A pinned point has an inverse mass of 0 and stays put.

The springs are solved in four sets whose members share no point: horizontal springs from even columns, from odd columns, vertical springs from even rows, from odd rows. So a whole set can be solved at once, four springs per instruction. A horizontal spring's two points sit side by side in memory, so i8x16.shuffle splits them into lanes and weaves them back after. A torn spring keeps its lane with a factor of 0, so no lane branches. On the ball, every point that falls inside it is pushed out to the surface, and every point below the floor is set on it, four points at a time through lane masks.

The module draws the cloth itself: perspective, a z-buffer and Gouraud shading from each point's normal, with the front teal and the back amber. Each cell whose four springs still hold is two triangles.

This is the start of SOLVE4, the macro that solves four springs (its arguments name the locals that hold the two points' coordinates, their inverse masses, and whether the spring holds):

    local.get \bx
    local.get \ax
    f32x4.sub
    local.set \dx
    local.get \by
    local.get \ay
    f32x4.sub
    local.set \dy
    local.get \bz
    local.get \az
    f32x4.sub
    local.set \dz
    local.get \dx
    local.get \dx
    f32x4.mul
    local.get \dy
    local.get \dy
    f32x4.mul
    f32x4.add
    local.get \dz
    local.get \dz
    f32x4.mul
    f32x4.add
    f32x4.sqrt
    local.set \len
    local.get \al
    local.get \len
    SPLAT 0x1.83060cp-5          // 3 rest: the tearing length
    f32x4.le
    v128.and
    local.set \al
    local.get \al
    local.get \len
    SPLAT 0x1.020408p-6          // rest: 2 / 127
    f32x4.sub
    local.get \len
    SPLAT 0x1.12e0bep-30         // 1e-9: no 0 / 0 where two points meet
    f32x4.add
    f32x4.div
    f32x4.mul
    local.set \k

The whole kernel, with the rasterizer and the controls, is cloth.wasm.s. Under Node on an i9-13900K, a frame takes about 5 ms. The physics (eight substeps, 24 passes over the 20,352 springs and the collisions) takes about 1.3 ms, and the rasterizer most of the rest. The figures under the demo are your browser's.

Build one yourself

dotnet new asmwasm -n Kernel      # a WebAssembly library in LLVM's WebAssembly assembly
dotnet build Kernel               # Kernel/bin/Debug/freestanding/browser-wasm/Kernel.wasm
node Kernel/run.mjs               # calls it from Node; a page loads it the same way

The WebAssembly guide covers the language, the FUNCTION macros and the traps. One trap this kernel met: v128.bitselect(a, b, mask) keeps a where the mask is set and b elsewhere. x86's blendvps takes its second operand where the mask is set, so the operands are the other way round.