Color legend plain property method event
A simulated sheet of cloth: it builds the grid of particles, keeps the pins and the damage, and drives the solver every frame. It draws nothing.
A sealed class, in the namespace BorrowedTorch.FabricSimulator.Simulation. It takes a FabricEffect, which owns the GPU buffers and runs the steps, and a FabricSettings, which it applies before every step. The sheet itself is built by Rebuild, or by the first call that needs it. The quick start guide shows it in context, with code. Most members upload to the GPU or read from it: call them from the thread that owns the graphics device.
Names are colored by what they are: a property, a method or an event; values and fields keep the plain color.
Properties
BuiltHeight →
float · Read-only. The sheet’s height as built, in meters: Height less what the starting wave of SeedRipple takes up, a few percent at most. The wave’s rows start at their rest length on its steepest rise, so wavy cloth is narrower rather than stretched. 0 until the sheet is built.
BuiltWidth →
float · Read-only. The sheet’s width as built, in meters: Width less what the starting wave takes up, as for BuiltHeight. 0 until the sheet is built.
Effect →
FabricEffect · Read-only. The solver driving this sheet, given to the constructor. A renderer reads its ParticleBuffer, and its diagnostics are read from it.
HasTears →
bool · Read-only. Whether the sheet has any damage: a thread severed by Cut or Tear. Free to ask. false again after ClearTears and Rebuild.
Height →
float · Read-only. The flat sheet’s height, in meters, or its depth when it lies Horizontal: (HeightInParticles - 1) × RestLength. The range of a cut’s v coordinate, from 0 at the first row to Height at the last. BuiltHeight is the as-built figure.
HeightInParticles →
int · Read-only. Points from the first row to the last, as built; until the sheet is built, the settings’ HeightInParticles. Bind this one, not the settings’, to a shader’s FabricHeightInParticles: the settings may run ahead of the next Rebuild.
IsBuilt →
bool · Read-only. Whether the sheet has been built, by Rebuild or by a first call that needed it. Until it is, the size properties report the settings’ figures.
Orientation →
FabricOrientation · The plane the sheet is built in: Vertical, the default, spans X and Y; Horizontal spans X and Z. Read at Rebuild.
Origin →
Vector3 · The middle of the sheet in world space, on both axes and in either orientation, so a sheet resized stays where it was put. (0, 3, 0) by default. Read at Rebuild, which throws InvalidOperationException for one that isn’t finite. To move a sheet, set it and rebuild: the particles live in world space, so a world matrix in the shader would draw the cloth away from where it is simulated.
ParticleCount →
int · Read-only. Points in the sheet, WidthInParticles × HeightInParticles, as built; the settings’ figure until then. The ids of a cut mesh’s seam vertices start here.
PinMode →
FabricPinMode · A preset layout of pins, pinned along with PinnedIndices. LeftEdge by default. Read at Rebuild; SetPinned changes the pins of the live sheet.
PinnedIndices →
ISet<int> · Read-only; the set itself is yours to change. More points to pin, by ParticleIndex, on top of PinMode’s: with FabricPinMode.None, a fully custom set. Any point may be pinned, inside ones included. Empty by default. Read at Rebuild, which throws ArgumentOutOfRangeException for an index outside the grid.
RestLength →
float · Read-only. The spacing between neighboring points, in meters, as built; the settings’ RestLength until then. The live sheet keeps it until the next Rebuild: the pins’ reach, the rest angles and the cuts are all measured in it. A cut’s coordinates are multiples of it.
Rotation →
Quaternion · The sheet’s starting attitude, about Origin, so it turns in place. Quaternion.Identity by default. Gravity, the wind and the colliders stay in world space, so only the pins hold a tilt. Any angle is safe: a rotation keeps every rest length and fold angle. Read at Rebuild, which normalizes it, and throws InvalidOperationException for one that isn’t finite or has no length.
Settings →
FabricSettings · The tunables, applied before every step, so a change takes effect at the next one; the sheet’s own, the grid, RestLength and SeedRipple, only at Rebuild. A fresh instance may be assigned at any time, which is how every setting goes back to its default; rebuild to take its sheet, unless its AutoRebuild is on. Throws ArgumentNullException for null.
TriangleCount →
int · Read-only. The triangles of CreateTriangleIndices: two per square of the grid, less those torn away. Read it again after a cut, a tear or a mend. A cut mesh can hold more, so draw FabricCutMesh.TriangleCount when you draw one.
Width →
float · Read-only. The flat sheet’s width, in meters: (WidthInParticles - 1) × RestLength. The range of a cut’s u coordinate, from 0 at the first column to Width at the last. BuiltWidth is the as-built figure.
WidthInParticles →
int · Read-only. Points across, as built; until the sheet is built, the settings’ WidthInParticles. The grid width to bind to a shader’s FabricWidthInParticles, and the row length of every flat index.
Methods
Fabric() →
Fabric(FabricEffect effect, FabricSettings settings) · A sheet driven by the solver given, with the settings given. Builds nothing: set the placement and the pins first, then Rebuild, or let the first Update build it. Throws ArgumentNullException for a null argument.
| Parameter | Type | Description |
|---|---|---|
effect |
FabricEffect |
The solver. Give each sheet its own: one effect holds one sheet. |
settings |
FabricSettings |
The tunables, kept by reference: changes made to the object later apply at the next step. |
CaptureSurface() →
CaptureSurface() · Returns FabricSurface: the sheet’s current pose, frozen, to pick against with world-space rays, which tells what point of the weave lies under a point of the world once the cloth has crumpled. Picks against the cut mesh, lips and all, so what can be picked is what CreateCutMesh draws. A GPU readback, of the positions alone, or of the whole sheet when a strip one point wide needs its normals: capture one per gesture, not per ray. Good until the next Rebuild. Builds the sheet if nothing has. Throws InvalidOperationException when the effect holds another sheet, given to it through SetParticles.
ClearTears() →
ClearTears() · Mends every severed thread in place, keeping the drape, and forgets the cut strokes. Folds a tear had killed come back at the angles they had when they died, so lips that drifted apart pull together with a visible twitch. The pins’ reach is measured again through the whole weave. Does nothing on whole cloth. Draw from fresh indices afterwards.
CopyTearState() →
CopyTearState() · Returns uint[]: a copy of the damage, one word of FabricTearFlags per point, all zero on whole cloth. What PenetrationCheck.Run and CreaseCheck.Run take for a torn sheet, and enough to save the damage and replay it through Tear, though damage replayed that way has no strokes, and its lips draw jagged.
CreateCutMesh() →
CreateCutMesh() · Returns FabricCutMesh: the sheet’s triangles with clean lips, the damaged squares clipped along the strokes that cut them, at whatever angle those ran. Indices from ParticleCount up name its seam vertices, which a shader draws through FabricEvaluateVertex. Drawing only: the simulation still tears whole threads, within about a cell of the drawn lip. It can hold more triangles than the whole sheet, so size buffers from it. Damage done by Tear has no stroke and stays jagged. Built once per change of damage: the same instance comes back until the next cut, tear, mend or rebuild, and CaptureSurface picks against it, so treat it as read-only. Builds the sheet if nothing has.
CreateTriangleIndices() →
CreateTriangleIndices() · Returns int[]: the sheet’s triangle list, three indices per triangle, indexing the particles directly: two triangles per square of the grid, less those torn away, which leaves the lips of a cut jagged at the grid’s scale. Needs a 32-bit index buffer, since 16 bits overflow past 65,536 particles. Upload it again after a cut, a mend or a rebuild; tears only remove triangles, so a buffer sized for whole cloth always fits. The same array comes back until the damage changes: treat it as read-only. Builds the sheet if nothing has.
Cut() →
Every overload cuts the live sheet, without resetting the drape, and returns int: how many threads it severed. A severed thread stops pulling, the lips no longer share a normal and collide as ordinary cloth, and the pins’ reach is measured again through what is left of the weave, so a piece cut free of every pin falls. A stroke running exactly along a line of points severs the threads on one side of it only, and never a thread it runs along. Each overload builds the sheet if nothing has.
Cut(Vector2 start, Vector2 end) · Cuts along one straight stroke on the flat sheet, in meters: (0, 0) is the point ParticleIndex(0, 0), u runs along Width and v along Height, so the stroke lands in the same place at any resolution. The stroke is clipped to two cells past the sheet’s edge. Throws ArgumentException for a point that isn’t finite.
Cut(ReadOnlySpan<Vector2> stroke) · Cuts along a polyline, one stroke per pair of consecutive points, and measures the pins’ reach once for the whole chain rather than once per stroke: a rip that wanders. Fewer than two points sever nothing. Every point is checked before anything is cut, so a point that isn’t finite throws ArgumentException and leaves the sheet as it was.
Cut(ReadOnlySpan<Ray> rays) · Cuts along a drag of world-space rays, unprojected through your own camera, so crumpled cloth is cut where the knife passed over it rather than where the flat sheet once lay. Each ray takes the nearest layer. Captures the pose itself, one GPU readback, then cuts as Cut(FabricStroke) does. Fewer than two rays sever nothing. For scissors through a pile, or any other rule, walk CaptureSurface’s layers yourself and pass the points to the Vector2 overload.
Cut(ReadOnlySpan<Ray> rays, FabricSurface surface) · The same, against a pose captured earlier, so a preview drawn from that surface cuts exactly where it was drawn: the sheet has moved on since, and the damage follows the pose. A FabricStroke fed during the drag does the same without picking each ray twice. Throws ArgumentNullException for a null surface, and ArgumentException for one captured from another sheet, or from this one before its last Rebuild.
Cut(FabricStroke stroke) · Cuts along a recorded drag. Samples on one layer join into a stroke, which breaks where it jumps across a fold onto cloth the knife never passed over. Where the knife left the cloth and came back across an open cut, the stroke carries on across it; and a stroke that left the cloth within reach of a cut or of the sheet’s edge is carried to it, since the last sample can land up to a sample short: either way the piece between comes free. Any other miss ends the stroke. Throws ArgumentNullException for a null stroke, and ArgumentException for one over a surface from another sheet or from before the last rebuild.
| Parameter | Type | Description |
|---|---|---|
start |
Vector2 |
Where the stroke starts on the flat sheet, in meters. |
end |
Vector2 |
Where it ends. |
stroke |
ReadOnlySpan<Vector2> |
The polyline’s points on the flat sheet, in meters, in order. |
rays |
ReadOnlySpan<Ray> |
The drag, one world-space ray per sample, in order. The direction need not be of length 1. |
surface |
FabricSurface |
The pose to pick the rays against, from this sheet’s CaptureSurface since its last rebuild. |
stroke (the last overload) |
FabricStroke |
The drag, picked sample by sample as it was drawn. |
IsPinned() →
IsPinned(int index) · Returns bool: whether a point of the live sheet is pinned, as SetPinned, Rebuild or the last upload left it. Free, unless an UpdateParticles of your own has changed the sheet since: the first call after one reads the pins back from the GPU, once. Builds the sheet if nothing has. Throws ArgumentOutOfRangeException for an index outside the sheet, and InvalidOperationException when the effect holds another sheet.
| Parameter | Type | Description |
|---|---|---|
index |
int |
The point, as ParticleIndex numbers it. |
ParticleIndex() →
ParticleIndex(int column, int row) · Returns int: the flat index of a point, row × WidthInParticles + column, row by row up from a vertical sheet’s bottom edge. What PinnedIndices, SetPinned, Tear and the arrays of ReadBack use. Range-checked, since a column and a row swapped still make a valid index almost every time: throws ArgumentOutOfRangeException for a column or a row outside the grid. Before the sheet is built, the grid is the settings’.
| Parameter | Type | Description |
|---|---|---|
column |
int |
From 0 at the first column to WidthInParticles - 1. |
row |
int |
From 0 at the first row, the bottom edge of a vertical sheet, to HeightInParticles - 1, the top. |
Rebuild() →
Rebuild() · Builds the sheet afresh and uploads it: a flat grid with the starting ripple, every tear mended, the pins as PinMode and PinnedIndices describe them, and the solver’s clock and diagnostics started again. Reads the settings’ grid, RestLength and SeedRipple, and Origin, Orientation and Rotation: until it runs, the live sheet keeps the ones it was built with. Optional the first time, since the first call that needs the sheet builds it; AutoRebuild calls it whenever one of them changes. Raises Rebuilt.
Everything is checked before the live sheet is touched, so a bad setting throws and leaves it running. Throws InvalidOperationException for a grid under 2 × 2 points, a spacing that isn’t finite and positive, more points than FabricEffect.MaxParticles, a SeedRipple, Origin or Rotation that isn’t finite, a rotation of no length, and a sheet too large for UseAerodynamicPressure; ArgumentException for any other setting that isn’t a finite number, or a SolverFrequencyHz that isn’t positive; and ArgumentOutOfRangeException for a pinned index outside the grid.
RefreshAnchors() →
RefreshAnchors() · Measures the pins’ reach along the weave now, rather than before the next step, and uploads it: each point’s nearest pin and its distance through the weave, a shortest path from every pin at once, on the CPU. The sheet does this by itself after a cut, a pin change, or a change of UseShearingConstraints. Call it after changing which points are pinned through UpdateParticles: it reads the pins back once, and puts the sheet’s measurements in place of the anchors you uploaded. Builds the sheet if nothing has.
SetPinned() →
SetPinned(int index, bool pinned) · Pins a point of the live sheet where it hangs now, or lets it go: with no kick either way, since a pin carries no motion. Takes effect at the next step, when the pins’ reach is measured again, once for any number of calls, on the CPU and with no readback. Setting what a point already is does nothing. PinMode and PinnedIndices are left alone: they still describe the next Rebuild, which drops these changes. Builds the sheet if nothing has. Throws ArgumentOutOfRangeException for an index outside the sheet, and InvalidOperationException when the effect holds another sheet.
| Parameter | Type | Description |
|---|---|---|
index |
int |
The point, as ParticleIndex numbers it. |
pinned |
bool |
true to pin it where it is, false to let it go. |
Step() →
Step() · Applies Settings, then runs exactly one step of TimeStep, whatever the clock says: for tests and tools that advance the cloth by hand. Builds or rebuilds the sheet first, as Update does. Leaves InterpolationFraction where it was. Throws what Rebuild and FabricEffect.Apply throw, before anything steps.
Tear() →
Tear(int indexA, int indexB) · Severs the one thread joining two neighboring points, sideways, up and down or diagonal, with all of Cut’s effects but clean lips: it has no stroke, so CreateCutMesh leaves its hole jagged. A thread already torn is left as it is. Builds the sheet if nothing has. Throws ArgumentOutOfRangeException for an index outside the sheet, and ArgumentException for two points that share no thread: the same point, or two not of one square of the grid.
| Parameter | Type | Description |
|---|---|---|
indexA |
int |
One end of the thread, as ParticleIndex numbers it. |
indexB |
int |
The other end: a neighbor of indexA across, up or down, or on a diagonal. |
Update() →
Update(float elapsedSeconds) · Once a frame. Builds the sheet first if nothing has, or rebuilds it when AutoRebuild is on and something it was built from has changed; measures the pins’ reach again if a cut or a pin change asked for it; applies Settings; then runs as many whole fixed steps as the time affords, up to MaxStepsPerFrame, as FabricEffect.Update does. Throws what Rebuild and FabricEffect.Apply throw, before anything steps.
| Parameter | Type | Description |
|---|---|---|
elapsedSeconds |
float |
The frame’s time, in seconds: gameTime.ElapsedGameTime.TotalSeconds. NaN and negative times count as none, and anything over 0.25 s as 0.25 s. |
Events
Rebuilt →
EventHandler · Raised after every rebuild: called, done by first use, or done by AutoRebuild. The grid, the triangles and the pins may all have changed, so this is the place to size your buffers and upload CreateTriangleIndices or CreateCutMesh, and to drop surfaces and hits gathered from the sheet before. Raised even when applying the settings at the end of the rebuild fails: the live sheet has been replaced either way. The sender is the sheet, the arguments EventArgs.Empty.