Lesson 3.1 · III Modelling
Distributing and instances
What it's about: The most common step in node editors: create points and put something on each one. Spikes on a body, links along a chain, rivets on a metal sheet. This takes three things:
- where: points along curves and over surfaces,
- which way round: frames instead of bare points,
- how many, without getting slow: instances, that is computing a shape once and showing it many times.

1. Frames
A point says where something goes, but not which way
round. A frame is a point with three axes:
{origin, x, y, z}. place(shape, frame) places
a shape so that its own axes land on those of the frame. A shape that
stands on the origin and points up (z) then stands on the frame and
points along its z axis. On a surface, that is the normal.
Two small helpers in the prelude:
spin(frame, angle)rotates a frame about its z axis,sized(frame, k)makes its axes k times as long. Whatever is placed on it then becomes k times as large, becauseplacetakes the axes as they are. This is how each spike of the sea urchin gets its own length.
2. Along curves
divide(curve, n) gives n points at equal
distances. That sounds obvious, but it isn't: the parameter of
a NURBS curve runs faster where the control points are further apart.
Even a quarter circle with its weights (Lesson 1.2) has unequal
distances for equal parameter steps. So divide measures the
curve: a table of arc length, that is how far you have
travelled up to each parameter. In this table it looks up the parameters
at which a quarter, a half and so on of the length is reached. On a
closed curve the start is not repeated at the end.
frames_along(curve, n) gives frames as well: z along the
curve, x and y across it, chosen so that they rotate around the curve as
little as possible. This is the same method as for the tube around the
knot (Lesson 3.2, double reflection). It lives in
geom::distribute, and the sweep uses the same.

The chain needs exactly this. Each link is an oval with its long side
in the direction of travel, and every second one is rotated by 90°:
if i % 2 == 0 then f else spin(f, 90). So that the links
interlock, a link is as long as the distance to the next one plus twice
the wire thickness and a little clearance. The model works out the
distance itself: the length of the curve divided by the number of links.
With an even number, the chain closes cleanly.
3. Over surfaces
scatter(surface, n, seed:, spacing:) scatters n frames
at random over a surface or a mesh. Two things make the difference
between "random" and "nicely random":
- By area, not by parameter. If you simply rolled
dice for (u, v), the points would crowd at the poles of a sphere, where
the parameter lines converge.
scatterdivides the surface into a fine grid, weights each cell by its area, and rolls first for the cell, then for the spot within it. The test counts: half the points lie on the upper hemisphere, and a quarter on the polar cap that has a quarter of the area. - Minimum spacing. Pure chance forms clumps and gaps
(on the left in the picture above). With
spacing, every new point that comes too close to one already placed is rejected. This is called dart throwing. The result looks sown (on the right); experts call it blue noise, and it is also known as "Poisson disk sampling". To make this fast, the placed points sit in a grid whose cells are the size of the spacing; a new point is only compared with its neighbouring cells. If not all of them fit on the surface, you get fewer. At 3 mm spacing the sea urchin gets at most 504 spikes.
For regular patterns there is grid_on(surface, nu, nv):
frames at the centres of nu × nv cells of the (u, v) grid, z again the
normal. It is a single line in the prelude,
[frame_on(s, (i + 0.5) / nu, (j + 0.5) / nv) | j <- range(nv), i <- range(nu)],
and so it follows the surface's parameters, not its
length: on a surface of revolution the spacing around is even, but up
the side it is as uneven as the profile's parameter steps. If you need
even spacing, take scatter with spacing; if
you need a grid that fits the shape (studs in rows), take
grid_on.
The z axis is the normal, pointing outwards. Whether
∂S/∂u × ∂S/∂v points outwards is decided by the same sign of the volume
as for STL (Lesson 1.3). The same seed gives the same
points, everywhere, because the random numbers are a pure function of
(seed, index), as with rand.
4. Instances: compute once, show many times
instance(shape, frames) is a value type of its own: a
shape and a list of placements. The shape is tessellated
once, and finely enough for the largest copy. A copy
scaled up three times needs a third of the tolerance. Moving, rotating
and mirroring only change the placements. realize turns it
into separate shapes when needed.
Where the gain is and where it isn't is shown by a measurement with 1,600 spheres on a grid:
| command line (STL) | browser | |
|---|---|---|
separately (translate per sphere) |
2.2 s | 1,971 ms |
| as instances | 2.0 s | 3 ms |
On the command line it hardly helps: an STL file needs all 6.3
million triangles, and checking and writing them is what takes the time,
not the tessellation. In the browser, by contrast, the graphics
card gets the shape only once, plus 1,600 matrices (4 × 4
numbers each). It draws the copies itself; three.js calls this
InstancedMesh. The browser only assembles the STL when you
download it, with the same function as kf, so it is
byte-identical.

As instances the spikes overlap the body; the STL then has 451 parts,
and a slicer merges them when printing. union (Lesson 3.7)
makes one solid, but only from copies placed one by one:
-D whole=1 folds every spike onto the body with
union. That takes noticeably longer, because every copy
needs its own intersections.
5. How it is checked
Even spacing is checked by measuring along the curve: all distances
between neighbouring points of divide are equal, also on a
circle with weights. For scatter the tests count whether
every part of the surface gets the share of the points that matches its
area (the polar cap its quarter), whether no pair lies nearer than
spacing and whether every z axis points outwards; the same
seed must give the same points. Instances must give as many triangles
and the same volume as the same shapes placed one by one.
6. Limits
- Copies have no properties of their own: colour or material per copy, like the attributes of node editors, does not exist.
- Things are distributed on surfaces, not inside a solid.
Try it yourself
cargo run --release -p kf -- render examples/urchin.kf -D spacing=0 -o out/urchin.png
cargo run --release -p kf -- render examples/chain.kf -D links=47 -o out/chain.png
- Set
spacingto 0 for the sea urchin. Where do gaps appear, and where clumps? - Give the chain an odd number of links. What happens where it closes, and why?
- Use
grid_onto put small cones on a vase (examples/vase.kf). Why are they denser in some places than in others, and how doesscatterhelp? - Something to think about: what would an instance have to carry in addition so that each copy can have its own colour, and where would that have to arrive in the browser?