Instancing and Procedural Motion · Chapter 2 of 9
Instancing: Grids, Circles and Scatter
Written for version 2.7.0
Arranging copies with a few nodes: Grid for rows and columns, a ring and a spiral from Sin and Cos, stairs and stacks from Instance Index, and Instance Random to scatter, turn, size and thin them.
The last chapter ended with a row: Instance Index times a spacing, wired into X. Every layout in this chapter is that idea again. The graph works out a place for a copy from the copy's own number, and Position takes it. Only the arithmetic in between changes: a grid, a ring, a spiral, a flight of stairs, and then random numbers to break a pattern up or thin it out.
What an instance is, Add Instancer, Count and Seed, and a first row with Instance Index.
A grid
Grid, in the Instance folder, turns a copy's number into a Column and a Row. Space the two out, make a place of them, and the copies stand in rows.
- Open the Instancer FX graph and add a Grid from the Instance folder.
- Add two Scale And Offset nodes from Math. Wire Column into the Value of one and Row into the Value of the other.
- Add a Construct Vector3 from Channels. Wire the first Scale And Offset into X and the second into Z.
- Wire the Construct Vector3 into Position on Instance Transform.
- Select the Grid and set Columns, the number of copies in a row. It starts at 10.
- On both Scale And Offset nodes, set Scale to the gap between copies.
- To centre the grid on the host, set each Offset to minus half the distance from the first copy to the last along that side.
| Grid output | What it carries |
|---|---|
| Column | The copy's place along its row: 0, 1, 2, up to Columns less one |
| Row | Which row it is in: 0 for the first Columns copies, 1 for the next, and so on |
| U | Column as a fraction, 0 at the first column and 1 at the last |
| V | Row as a fraction, 0 at the first row and 1 at the last |
- The copies fill one row and then start the next. The number of rows is Count divided by Columns, rounded up, so 24 copies in 6 columns make 4 rows. If Count is not a full multiple of Columns the last row is short.
- With an Offset of 0 the first copy stands on the host and the grid runs away from it. The distance from the first copy to the last is the gap times one less than the number along that side. For 6 columns 1 apart that is 5, so the Offset is -2.5. For 4 rows it is 3, so -1.5.
- Columns is rounded to a whole number and never goes below 1.
- X and Z lay the grid on the floor. Wire Row into Y in place of Z and it stands up as a wall.
- U and V say where a copy is in the grid as a fraction, whatever Count and Columns are. They can drive anything that should change across the grid, such as a height that rises from one side to the other. Later chapters use them to read a picture, one spot for each copy.
- With a single column U is 0, and with a single row V is 0.
A ring
A circle needs an angle for each copy. Divide the copy's number by the Count and you have how far round it is as a fraction. Multiply by a full turn and the fraction is an angle. Sin and Cos turn the angle into a place on a circle.
- Add an Instance Index and a Divide from Math. Wire Index into A and Count into B.
- Add a PI from Math and a Multiply. Wire the Divide into A and the 2π output of PI into B. The result is the copy's angle.
- Add a Sin and a Cos from Math and wire the angle into both.
- Add a Construct Vector3. Wire Sin into X and Cos into Z.
- Add a Float from Parameters. Set its Value to the radius you want and type Radius as its Label.
- Add a second Multiply. Wire the Construct Vector3 into A and the Float into B.
- Wire that Multiply into Position.
- Sin, Cos, Degrees and Radians are in Math, under Trigonometry.
- Sin and Cos work in radians, not degrees. A full turn is 2π radians, a half turn π. The PI node has an output for each, and one for a quarter turn.
- Index divided by Count stops one step short of a full turn, so the copies are evenly spaced and the last does not land on the first. Normalized would: it reaches 1 on the last copy, which is the same place on a circle as 0.
- Normalized is the right choice for an arc that should have a copy at both ends. Normalized times π is a half circle.
- The radius is a Float on a wire, not a number typed into B. A typed number on a Multiply that is working on a vector changes only the first of the three, X. A wire from a Float reaches all three.
- Because the Float has a Label, Radius also shows under Instancer FX Inputs in the detail panel, where it can be dragged up or down without opening the graph.
- The ring follows Count. Type a new Count and the copies close up or spread out round the same circle.
Recipe: a round fence
A ring only moves the copies. Each one still faces the way it was made, which does not show on a round column and shows at once on a fence panel. To turn each copy with the ring, send the same angle into Rotation.
- Build the ring above with a fence panel as the prototype: an Empty with the boards and one post under it, modelled wide along X.
- Add a Degrees from Math. Wire the angle, the Multiply that feeds Sin and Cos, into it.
- Add a second Construct Vector3 and wire Degrees into Y.
- Wire it into Rotation (deg) on Instance Transform.
- Set Radius so the panels meet: the panel's width times the Count, divided by 2π. Sixteen panels 1.12 wide close at a radius of 2.86.
- Rotation is in degrees and the angle is in radians, so it has to be converted on the way. Degrees turns radians into degrees. Radians, beside it in the palette, goes the other way.
- Y is the turn about the upright axis. Leave X and Z at 0 and the copies stay level.
- Sin into X and Cos into Z is what makes the angle and the turn agree. A copy turned by its own angle has its front, the side that looked along Z when you made it, pointing straight out from the centre. Swap Sin and Cos and the ring looks the same, but the turn no longer matches it.
- To face the centre instead, put an Add between Degrees and Y and add 180.
- If your object was made facing another way, add 90 or -90 there until it sits right.
- Columns for a colonnade need none of this. A round column looks the same from every side.
Recipe: a spiral staircase
Lift each copy of a ring a little more than the last and the ring opens into a spiral. Normalized does the lifting: it runs from 0 on the first copy to 1 on the last.
- Make one tread, a long flat box, with its length along Z and the node's origin in its middle. Parent it under an Empty and build the ring and the turn from the round fence on that Empty.
- Set Radius so the inner ends of the treads meet at the centre. Here the treads are 1.5 long and Radius is 0.9.
- Add a Multiply. Wire Normalized from Instance Index into A and set B to the height of the whole stair, here 3.6.
- Wire the Multiply into Y of the Construct Vector3 that holds Sin and Cos.
- Because the height comes from Normalized, the stair is always as tall as B says. More treads make smaller steps, not a taller stair.
- For steps that always rise by the same amount, use Index times the rise of one step in place of Normalized. Then the stair grows taller with Count.
- This stair makes one turn. For more, put a Multiply after the angle and multiply it by the number of turns.
- The pole in the picture is a separate node. Anything parented under the Empty is part of the prototype and would be drawn once for every tread.
- With a small ball for a prototype and no turn, the same graph is a helix, a coil of beads.
Stairs and stacks
Instance Index can feed more than one axis at once. Give each axis its own Multiply, so that each has its own step.
- Add an Instance Index and two Multiply nodes. Wire Index into A of both.
- Set B of the first to the run, how far each step goes forward. Set B of the second to the rise, how far it goes up.
- Add a Construct Vector3. Wire the first Multiply into Z and the second into Y.
- Wire the Construct Vector3 into Position.
- For a stack or a tower, wire only Y and set the rise to the height of the object. The copies then sit exactly on top of each other.
- For a tower that twists, also wire Index times a few degrees into Y of a Construct Vector3 and that into Rotation (deg).
- A run in X and a run in Z together make a diagonal row.
- The first copy stays on the host, since 0 times anything is 0. The stairs climb away from there.
Instance Random
Everything so far is regular. Instance Random gives each copy numbers of its own. They are random from one copy to the next and fixed for each copy: copy 12 rolls the same numbers on every frame, so nothing flickers.
| On the node | What it is | Good for |
|---|---|---|
| Value | One number from 0 to 1 | A size, a turn, a yes or no |
| Vector | Three numbers, each from 0 to 1 | A nudge in three directions at once |
| Seed | A number of your choosing that sets which run of numbers this node gives | Keeping two Instance Random nodes from rolling the same |
- Value and Vector on one node are separate rolls, and so are the three parts of Vector. None of them follows another.
- Every roll is between 0 and 1. A Remap moves it to the range you want. Leave In Min at 0 and In Max at 1, and set Out Min and Out Max to the ends of the range. Out Min -3 and Out Max 3 centres the roll on 0.
- A Scale And Offset does the same job with two numbers: Scale is the size of the range and Offset where it starts.
- There are two seeds. The Seed on a node separates that node from the other Instance Random nodes in the graph. The Seed in the Instancer section of the detail panel reshuffles all of them together.
Recipe: a field of stones
- Place. Add two Instance Random nodes and set the Seed of the second to 1. Send the Value of each through its own Remap, with Out Min -3 and Out Max 3, into X and Z of a Construct Vector3, and wire that into Position.
- Turn. Add a third Instance Random with Seed 2. Multiply its Value by 360, wire the result into Y of a second Construct Vector3, and wire that into Rotation (deg).
- Size. Add a fourth Instance Random with Seed 3. Send its Value through a Remap with Out Min 0.5 and Out Max 1.7, and wire the Remap straight into Scale.
- Two Instance Random nodes with the same Seed give a copy the same number. Used for X and for Z that puts every stone where X equals Z, which is a diagonal line. It is the usual reason a scatter comes out as a streak.
- Any Seed will do as long as each node has its own. 0, 1, 2 and 3 are easy to keep track of.
- A single number wired into Scale sizes the copy evenly: the one number is used for X, Y and Z.
- A full random turn suits stones. It hides the fact that every stone is the same mesh.
- To roughen a grid without losing it, build a small random offset the same way, with two Remaps of about a third of the gap each way, and add it to the grid's place with an Add before Position.
- A scatter is not even. Some stones will crowd together, some will overlap, and there will be bare patches. No node spaces random copies apart. A roughened grid is the way to get an even spread.
- Change the Seed in the Instancer section for a different field built by the same rules.
Recipe: a city block
Scale can be different on each axis. Wire a random number into Y alone and every copy gets its own height.
- Make a box and parent it under an Empty, placed so that the bottom of the box sits on the Empty's origin.
- Lay the copies out with a grid: here 36 copies, 6 Columns, a gap of 1 and both Offsets at -2.5.
- Add an Instance Random and a Remap. Wire Value into the Remap and set Out Min to 0.5 and Out Max to 3.6.
- Add a Construct Vector3 and set its X and Z to 1. Wire the Remap into Y.
- Wire the Construct Vector3 into Scale.
- Where the origin sits decides which way a copy grows. Scale stretches a copy away from the host's origin. With the bottom of the box on the origin it grows upward and stays on the ground. With the origin in the middle of the box it would grow up and down at once, and half of every building would be under the floor.
- X and Z of a Construct Vector3 start at 0. A Scale of 0 across squashes a copy to nothing, so set both to 1 before wiring it in.
- For buildings that are wider or narrower too, feed X and Z from an Instance Random with a different Seed.
- U or V from the Grid in place of the random number makes a skyline that rises from one side of the block to the other.
Thinning with Visibility
Visibility on Instance Transform decides whether a copy is drawn: 0.5 or more draws it, less hides it. Feed it a test on a random number and some of the copies drop out.
- Add an Instance Random with a Seed no other node uses.
- Add a Step from Math. Wire the random Value into B and set A to the share of copies to hide, for example 0.25 for about a quarter.
- Wire Step into Visibility.
- Step answers one question: is B at least A? It gives 1 for yes and 0 for no.
- The share is only roughly what you asked for. The rolls are random, so 0.25 hides about a quarter, not exactly a quarter.
- A hidden copy keeps its number and its rolls. Thinning never moves or reshuffles the copies that are left.
- Is Greater and Is Less, in the Logic folder, do the same job with A and B read in the usual order: Is Greater gives 1 when A is more than B.
- Compare, in Math, asks a different question: are A and B equal, to within Epsilon? Use it to pick out one copy or one line. Compare the Grid's Column with 2, put the result through a One Minus and wire that into Visibility, and column 2 is cleared: a street through the block.
- Give the test its own Seed. If it shares a Seed with the node that sets the height, it is always the shortest buildings that go.
To get this, wire that
| Layout | What to wire |
|---|---|
| A row | Index times a spacing, into X |
| A grid on the floor | Grid's Column and Row, each through a Scale And Offset, into X and Z |
| A wall | The same, into X and Y |
| A ring | Index divided by Count, times 2π. Sin of that into X and Cos into Z, times a radius |
| An arc | Normalized times the arc's angle in radians, in place of the ring's angle |
| Copies that face outward | The ring's angle through Degrees, into Y of Rotation (deg) |
| A spiral | A ring, plus Normalized times a height into Y |
| Stairs | Index times a run into Z and Index times a rise into Y |
| A stack | Index times the object's height, into Y |
| A scatter | Two Instance Random nodes with different Seeds, each through a Remap, into X and Z |
| A random turn | Instance Random times 360, into Y of Rotation (deg) |
| Random sizes | Instance Random through a Remap, straight into Scale |
| Random heights | The same into Y of a Construct Vector3 whose X and Z are 1, into Scale |
| Gaps | Instance Random into B of a Step, into Visibility |
Limits worth knowing
- Every layout is measured from the host. Move, turn or scale the Empty and the whole arrangement goes with it.
- Nothing here changes how many copies there are. Count is still the number in the Instancer section, and a hidden copy still counts towards it.
- Instancer FX has no noise or pattern nodes. Instance Random gives each copy an unrelated number, so neighbours do not vary together. Patterns that drift across a field come from a layer, in a later chapter.
- There is no node that keeps copies from overlapping, and none that spreads them evenly at random.
- Every copy is the same object with the same materials. A graph can place, turn, size and hide a copy. It cannot give one copy a different colour.
- Rotation combines its three numbers in the order Y, X, Z. A turn about Y on its own, as in this chapter, is simple. Mixing it with a tilt is covered with the grass field.
Let another mesh decide where the copies go: Vertex Count and the Mesh Vertex node.
Tell us which layout you were building, the nodes in the graph, the Count, and what the viewport showed instead of what you expected. A screenshot of the graph helps.