Docs / Fabric simulation

Fabric simulation: placement and pins

Where the sheet hangs, in which plane and at what angle, the starting ripple, pin presets and pins of your own, pinning and letting go on the live sheet, long-range attachments, and moving pins.

Color legend plain property method

A sheet is laid out flat when it is built, in a plane and at an angle you choose, around a point you choose. From there gravity takes over, and what holds the sheet up, or holds it at an angle, is its pins: points that never move. Where it hangs and what holds it are the Fabric’s, read at every build. Names are colored by what they are: a property or a method; values and fields keep the plain color.

Placement

Origin is the middle of the sheet, on both axes, in either orientation, so where a sheet hangs doesn’t depend on its size: change the resolution or the spacing and it stays where you put it. Orientation picks its plane: Vertical spans X and Y, like a curtain, and Horizontal spans X and Z, like a tablecloth. Its columns run along +X either way, and its rows up +Y or along +Z.

A sheet is (points − 1) × RestLength along each side, so a 96 × 64 sheet at 6 cm, upright, with Origin at (0, 3, 0), spans x −2.85 to 2.85 and y 1.11 to 4.89. To place a sheet by its top edge instead, add half its Height, which the settings’ figures give before the build:

// the top edge at y = 4.5, wherever the grid's size puts the bottom
var height = (_settings.HeightInParticles - 1) * _settings.RestLength;

_fabric.Origin = new Vector3(0.0f, 4.5f - height * 0.5f, 0.0f);

Rotation then turns the sheet to any angle, about Origin, so it turns in place:

_fabric.Rotation = Quaternion.CreateFromYawPitchRoll(yaw, pitch, roll);
_fabric.Rebuild();

That is the angle it starts at, nothing more. Gravity, the wind and the colliders stay in world space, so a tilted sheet still falls down Y and still lands on the ground. What holds a tilt is the pins, which turn with the rest of the sheet and then never move. Any angle is safe: a turn keeps every distance and every fold angle, so the thread lengths, the pins’ reach and the folds’ rest angles measured from the sheet as built all still hold, and the first step has nothing to put right. The texture coordinates follow the grid, so neither the orientation nor the rotation changes them. The rotation is normalized at the build, and one of no length, or not finite, throws.

Fabric placement properties

Property Description
Origin Vector3. The sheet’s middle, in world space. (0, 3, 0) by default.
Orientation FabricOrientation. The plane the sheet is built in, Vertical by default.
Rotation Quaternion. The angle it starts at, about Origin. Quaternion.Identity by default.

All three are read at a build: change them, then call Rebuild, or let AutoRebuild do it. The sheet can’t be moved without starting over: to carry cloth along, move its pins, see moving pins.

FabricOrientation values

Value Description
Vertical Spans X and Y, like a curtain or a flag: rows up +Y, facing +Z.
Horizontal Spans X and Z, like a tablecloth: rows along +Z, facing −Y.

The starting ripple

A sheet laid out perfectly flat has no reason to buckle one way rather than the other, and a flat sheet dropped flat would stay flat as it fell. So a build lays it out with a gentle wave across its plane, SeedRipple of the spacing high: 0.05 by default, raised for a sheet dropped flat, at most MaxSeedRipple, 0.5. A wave of the same shape at any resolution, since it is measured in the spacing.

The wave costs no stretch. The points are laid out closer in the plane, so the threads that climb the wave’s steepest part are exactly their length, and the flatter ones start a little short, which cloth allows: a thread doesn’t push back until it is folded shorter than CompressionSlack of its length. A wavy sheet is narrower, not stretched, by a few percent at most: BuiltWidth and BuiltHeight give the size it was laid out at, and Width and Height the flat sheet’s, which cuts are measured on. The folds rest at the wave’s angles, so a hanging sheet keeps a little of the wave until something bends it.

Pin presets

PinMode names a layout of pins, LeftEdge unless you set another. The top is the last row, HeightInParticles − 1, in either orientation: the top of an upright sheet, the +Z edge of one lying flat, and wherever that row ends up after a rotation.

FabricPinMode values

Value Description
None Nothing pinned: the sheet drops.
TopEdge The whole top row, like cloth tacked along a batten.
TopCorners The two top corners, like washing on a line.
LeftEdge The whole first column, like a flag on its pole. The default.
FourCorners All four corners, like a sheet stretched over a frame.

Custom pins

The pins are the preset’s and PinnedIndices’s together: a set of grid indices you fill. With None as the preset, the set is all of them:

_fabric.PinMode = FabricPinMode.None;
_fabric.PinnedIndices.Add(_fabric.ParticleIndex(column: 0, row: 47));
_fabric.PinnedIndices.Add(_fabric.ParticleIndex(column: 32, row: 24));   // a point inside the sheet is fine
_fabric.Rebuild();

Any point can be pinned, inside the sheet as well as on its edge. ParticleIndex works the index out, row * WidthInParticles + column, and checks it: swapped, a column and a row still make a valid index almost every time, and pin the wrong point. Before the first build it counts in the settings’ grid. A build throws ArgumentOutOfRangeException for an index in the set that is outside the grid.

Fabric pin members

Member Description
PinMode FabricPinMode. The preset, read at a build. LeftEdge by default.
PinnedIndices Read-only ISet<int>, a set you fill: more points to pin at a build, by index.
ParticleIndex(int column, int row) Returns int: the index of the point at that column and row. Throws ArgumentOutOfRangeException outside the grid.
SetPinned(int index, bool pinned) Pins a point of the live sheet where it is, or lets it go, with no kick. Takes effect at the next step.
IsPinned(int index) Returns bool: whether a point of the live sheet is pinned now.
RefreshAnchors() Measures the pins’ reach along the weave again now, rather than before the next step.

Pins on the live sheet

SetPinned pins a point where it hangs now, or lets one go, and IsPinned reads it back. Nothing jumps: a pin carries no motion, so a point pinned mid-swing stops there, and one let go falls from where it is, at rest. The preset and the set still describe the next build, which puts the pins back as they say.

// let the top edge go, all but its corners: it drops from where it hangs
var top = _fabric.HeightInParticles - 1;

for (var column = 1; column < _fabric.WidthInParticles - 1; column++)
    _fabric.SetPinned(_fabric.ParticleIndex(column, top), false);

A change takes effect at the next step, when the pins’ reach is measured again, once for however many points changed. That runs on the CPU over the weave, with nothing read back from the GPU, so a pin change is an event, not work for every frame. The one case that reads back is a sheet whose records you have uploaded yourself since, see moving pins. Both throw ArgumentOutOfRangeException for an index outside the sheet.

Long-range attachments

Threads pull a point towards its neighbors, a few rounds a step, and a wide, heavy sheet hanging from its top settles a little stretched, like rubber, since every thread between the pins and the hem gives a little and the rounds never quite catch up. UseLongRangeAttachments, on by default, ties every point to its nearest pin along the weave: a point is never let further from that pin, in a straight line, than the shortest path to it through the threads. A cloth that hangs straight sits right at that reach; one that stretches meets it, and stops.

The reach is measured once per change of pins or damage, from wherever the pins are, as a shortest path through the threads that are whole: with shearing on, the diagonals count, at √2 of the spacing. Each point keeps the index of its pin and the length of that path, AnchorIndex and MaxAnchorDistance in its record. A point no pin reaches, a piece cut free of every pin, isn’t held at all, and falls. With nothing pinned the attachments do nothing, and cost nothing.

FabricSettings attachment property

Property Description
UseLongRangeAttachments bool. Holds every point within reach of its pin along the weave, so a big, heavy sheet doesn’t stretch like rubber. true by default.

Moving pins

A point keeps the index of its pin, not the pin’s position, and the length behind it is a length through the weave, not through space: so a pin can move as far as you like, and the attachments follow it with nothing to measure again. What moves a pin is the record itself. Read the sheet back, move the pins, and upload it:

// a host carrying a banner along: the top row's pins move with the pole
var particles = _solver.ReadBack();
var top = _fabric.HeightInParticles - 1;

for (var column = 0; column < _fabric.WidthInParticles; column++)
{
    // only the position: a pin carries no motion, and the solver sets its previous position to match
    particles[_fabric.ParticleIndex(column, top)].Position += offset;
}

_solver.UpdateParticles(particles);

ReadBack waits for the GPU to catch up, which is a stall every time, and each upload needs a fresh readback: an older array would put the whole sheet back where it was. So pins that move every frame cost a stall every frame, which a few small sheets afford and a large one doesn’t.

Moving pins needs nothing else. Changing which points are pinned is another matter, since the reach is measured from them. SetPinned measures it again by itself. A change made through UpdateParticles, by a record’s IsPinned, isn’t measured until you call RefreshAnchors, which reads the pins back from the GPU once and measures the reach from them; until then the records keep the reach you uploaded. The Fabric’s next SetPinned or IsPinned after such an upload reads the pins back too, once.