Instancing and Procedural Motion · Chapter 7 of 9
Instancing: Effectors and Masks
Written for version 2.7.0
Make copies react to something in the model: a node the grass parts around, a zone where copies shrink or vanish, and a painted map that says where they grow.
The last chapter built a field of grass that leans with the wind. The wind is the same everywhere: it knows nothing about what stands in the field. This chapter makes the copies react to things in the model. A node the grass parts around, a zone where blades shrink or vanish, and a painted picture that says where the meadow grows at all. Everything is added to the same Instancer FX graph, beside the wind.
The field this chapter starts from: a grid of blades, scattered, leaning with a Wind layer.
1. A node the field can feel
An effector is nothing special. It is any node whose place the Instancer FX graph reads. The node that reads it is Node Transform, in the Instance folder.
- Add a small sphere to the model and name it Ball. Leave it at the top of the Node Hierarchy, not under Grass Field, and move it into the field.
- Select Grass Field and tap Edit Instancer FX.
- Tap empty canvas, open the Instance folder and choose Node Transform.
- With the new node selected, look at the inspector. Its Node menu reads This Node. Tap it and choose Ball.
- Add a Distance from Math, under Geometric.
- Wire each blade's place into A of the Distance. That is the output of the Add that already feeds Position.
- Wire Position from Node Transform into B.
- Node Transform has three outputs: Position, Rotation (deg) and Scale. They describe the chosen node as it stands at this moment, so when the node moves, the numbers follow.
- This Node means the node that carries the Instancer, here Grass Field.
- The Node menu is also listed under Instancer FX Inputs in the detail panel, so the effector can be swapped for another node without opening the graph.
- Keep the Ball out of the prototype. Anything parented under Grass Field is drawn once per copy, and you would get nine hundred balls.
- Distance now carries, for every blade, how far that blade is from the Ball. It measures in all three directions, so a Ball resting on the ground is already its own radius away from the blades right under it, and a Ball lifted high above the field is far from all of them.
World and local
There is one catch. Node Transform answers in world space: where the node is in the model. A copy's place is measured from the Instancer's own node, along that node's axes. While Grass Field sits at the origin, unturned and at scale 1, the two agree. Move the field and they do not.
- The simple way: leave Grass Field at 0, 0, 0 with no rotation and a scale of 1, and move the Ball instead.
- If the field has to move, add a second Node Transform and leave its Node menu on This Node.
- Add a Subtract. Wire the Ball's Position into A and This Node's Position into B.
- Use the Subtract wherever the Ball's Position was used.
- The Subtract turns the Ball's place into a place measured from the field, which is what the blades use.
- This corrects for a field that has been moved. It does not correct for one that has been rotated or scaled. A turned or resized field still needs its effectors worked out by hand, so keep the field's rotation at zero and its scale at 1 if you can.
2. A soft falloff
A distance is not yet an effect. What is wanted is a number that is 1 at the Ball, 0 far away, and slides smoothly between the two. Smoothstep does exactly that between two edges.
- Add two Float nodes from Parameters. Give one the Label Outer Radius and the value 1.8, the other the Label Inner Radius and the value 0.6.
- Add a Smoothstep from Math.
- Wire Outer Radius into Edge 0 and Inner Radius into Edge 1.
- Wire the Distance into Value.
- The larger number goes into Edge 0 on purpose. Smoothstep gives 0 at Edge 0 and 1 at Edge 1, so with the outer radius first the result is 1 near the Ball and 0 beyond the outer edge.
- Inside the Inner Radius every blade gets the full effect. Between the two radii it fades. Past the Outer Radius nothing happens.
- Bring the two radii close together for a sharp edge, and far apart for a wide, gentle one.
- Both Floats appear under Instancer FX Inputs by their Labels. Drag up or down on a number to change it.
- This one number, the falloff, is all an effector is. The rest of the chapter is about where to send it.
3. Using the falloff
Flatten
The picture above already uses the falloff once: it makes the blades short. Each blade's height was a number wired into Y of the Construct Vector3 that feeds Scale. The falloff now scales that number down.
- Add a Remap. Wire the falloff into Value, and set Out Min to 1 and Out Max to 0.1.
- Add a Multiply. Wire the blade's height into A and the Remap into B.
- Wire the Multiply into Y of the Construct Vector3 that feeds Scale, in place of the height.
- Remap turns the falloff around: 1 where nothing should change, 0.1 at the Ball. Multiplying by 1 leaves a blade alone.
- Out Max is how flat the grass gets. At 0 the blades vanish into the ground, at 0.5 they only duck.
Lean away
Grass does not only duck under a ball, it is pushed aside. For that each blade needs to know which way the Ball is, not only how near.
- Add a Subtract. Wire the blade's place into A and the Ball's Position into B. The result points from the Ball to the blade.
- Add a Normalize from Math, under Geometric, and wire the Subtract into it. The direction now has a length of 1.
- Add a Split Vector and wire the Normalize into it.
- Add a Float, give it the Label Push and the value 55. Add a Multiply and wire the falloff into A and Push into B.
- Add two more Multiply nodes. Into the first wire the last Multiply and Z from the Split Vector. Into the second wire the last Multiply and X.
- Add an Add. Wire the wind's tilt into A and the first Multiply into B, and wire the Add into X of the rotation.
- Add a Subtract. Wire the wind's tilt into A and the second Multiply into B, and wire the Subtract into Z of the rotation.
- X of the rotation tips a blade towards +Z, and Z of the rotation tips it towards -X. That is why Z of the direction goes into X, and why X of the direction is subtracted, not added.
- The wind is still there. The push is added on top of it, so a blade far from the Ball only sways and a blade next to it is pushed over and still trembles.
- Push is in degrees. Around 50 lays the nearest blades well over. Past 90 they fold under the ground.
- Each blade also has its small random yaw from the last chapter, which turns the push with it by up to 25 degrees. It keeps the parting from looking combed.
Hide: a clearing
- For a hard edge, add a One Minus, wire the falloff into it and wire it into Visibility on Instance Transform.
- For a feathered edge, add an Instance Random and set its Seed to 2, and add an Is Greater from Logic.
- Wire the Instance Random's Value into A and the falloff into B.
- Wire the Is Greater into Visibility.
- Visibility draws a copy when it is 0.5 or more and hides it below that. There is no half visible blade, which is why One Minus gives a sharp ring: it is the line where the falloff crosses 0.5.
- Is Greater asks, for each blade, whether its own random number is bigger than the falloff. Where the falloff is 1 no blade passes. Where it is 0.3, seven in ten do. The clearing thins out towards its edge.
- A hidden copy keeps its number, so the blades that remain do not jump to new places when their neighbours vanish.
- The node called Compare is not the one for this. It answers whether two numbers are equal to within its Epsilon. For more than and less than use Is Greater and Is Less, from Logic.
| Send the falloff to | Through | What the field does |
|---|---|---|
| Rotation (deg), X and Z | The direction away from the effector, times a strength, added to the wind | Parts round the effector |
| Scale, Y | Remap from 1 down to a small number, times the blade's height | Ducks under it |
| Visibility | One Minus, or Is Greater against Instance Random | Opens a clearing |
4. More than one effector
- Add a second Node Transform and choose the second node in its Node menu.
- Add a second Distance, with the blade's place in A and the new Position in B.
- Add a Min from Math. Wire the two Distance nodes into A and B.
- Wire the Min into Value on the Smoothstep, in place of the first Distance.
- Min passes on the smaller of two numbers. For each blade that is the distance to whichever effector is nearer, so each one gets its own zone.
- For three effectors, feed the Min and a third Distance into another Min.
- Smooth Min, also in Math, does the same with a rounded join. Its Smoothness sets how far apart the two zones start to melt into each other. Use it when two effectors come close and the crease between their zones shows.
- Min is enough for flattening and hiding, which only ask how near. Leaning away also asks which way, and one direction cannot serve two effectors. Give each effector its own lean, with its own falloff, and add both to the wind.
5. Masks from pictures
A mask is a picture used as a map: white where something should happen, black where it should not. Reading one takes the same two nodes that read the Wind layer, Texture Object and Sample Texture. The only difference is that the texture is a picture you made, not a layer's target.
- Make a black and white picture: white where grass should grow. In the Selection Panel, open Model, and under Textures tap + and choose Import Image.
- In the Instancer FX graph add a Texture Object and choose the picture in its Texture menu.
- Add a Sample Texture. Wire the Texture Object into Texture, and the Construct Vector2 that carries Grid's U and V into UV. It is the same one the Wind sample uses.
- Add a Split Vector on the Color output and use X.
- For density, add an Instance Random with a Seed of its own and an Is Greater. Wire X into A and the random Value into B, and wire the Is Greater into Visibility.
- For height, add a Remap with X in Value, Out Min 0.45 and Out Max 1.3, multiply the blade's height by it and wire the result into Y of the scale.
- Grid's U and V lay the whole picture over the whole field, its top edge along the first row.
- With the mask in A and the random number in B, a blade is drawn when the mask is brighter than its own random number. White always wins, black never does, and grey lets some through. A soft edge in the picture becomes grass that thins out.
- For a hard edge, compare the mask with a plain number such as 0.5 instead of a random one.
- The picture is read in linear light. A mid grey in a painting app reads as about 0.2, not 0.5, so soft edges are darker to the graph than they look to you. Paint them a little lighter, or put a Pow of about 0.45 after the Split Vector.
- One picture can carry three masks, one each in red, green and blue. Split Vector gives them as X, Y and Z.
- A mask costs nothing to keep: it is read once per copy and never changes unless you change the picture.
6. All three in one graph
Mask, wind and effector do not get in each other's way, because each ends in a different place or is combined at the very end.
- Rotation: the wind's tilt plus each effector's lean. Add them.
- Scale: the blade's height times the mask's Remap times the effector's Remap. Multiply them.
- Visibility: when two things each decide whether a blade shows, pass both through a Min. The blade is drawn only when both say 1.
- Lay the graph out in rows, one job per row: place along the top, then wind, then the effector, then the mask, with Instance Transform at the right end of all of them.
- Give every Float a Label. An unlabelled Float is listed as Float in the detail panel, and five of those tell you nothing.
- Work on one row at a time. To check a row, wire its result straight into Scale Y for a moment: what it does becomes visible as height.
Recipes
| To get | Build |
|---|---|
| A ball rolling through grass | The lean from section 3 with a Push around 55, and a light flatten with Out Max about 0.5 so the blades under the ball duck |
| A path cleared through a field | A mask with a black line painted on white, into Visibility. For a path that follows something moving, see the next chapter |
| A meadow that grows only inside a shape | A white shape on black as the mask, into Visibility through Is Greater and Instance Random, and into Scale Y so the edge is shorter |
| A clearing round a tree or a stone | That node as the effector, One Minus or Is Greater into Visibility, and an Inner Radius a little wider than the object |
Limits, stated plainly
- An effector is a single point: the node's origin. The field feels a ball, not the shape of a long log. For a long shape use several effectors, or a mask.
- The field reacts to where the effector is now. When it has gone, the grass is up again at once. Grass that stays down and recovers slowly needs memory, which is the next chapter.
- Copies cannot change colour one by one. Visibility, place, turn and size are all the graph sets.
- Every blade works out its distance to every effector on every frame. A few effectors are nothing. Dozens of them over thousands of blades add up.
- In the Ray Traced preview the field holds still while the picture refines. Rendered frames follow the effector.
A layer that does not forget: the grass stays down where something has passed, and slowly stands back up.
Tell us which node was the effector, where the Grass Field stood, and what the field did instead of what you expected. A screenshot of the graph around Node Transform and the Smoothstep helps.