A crop grows through four stages without a single line of code running for it on each tick. Then it has to be drawn, a few thousand times over, in a field that a phone can still render. This is how wheat does both.
Wheat · Seed
Planted just now.
The pictures are screenshots of the game: a three-by-three patch of wheat at noon in summer, one per stage. The slider is a plot's
age in ticks, and everything to the right of it comes from a JavaScript copy of the game's growthTicksAt and
ticksUntilMature. Water the plot and watch the same four stages arrive in half the time, while the number of events
the plot ever sends stays at one or two.
CubiCrops is a voxel farming game drawn with three.js and WebGL 2. This post follows its wheat from the rules that make it grow to the triangles that draw it: why a plot is four numbers and its growth is derived from the tick, how a crop knows its stage without being told, how the models are generated voxel by voxel, and how three levels of detail let a field of 1,760 ripe plots cost a quarter of what it would.
The simplest way to make a crop grow is to add one to a counter on every tick. With a thousand plots that is a thousand writes every tenth of a second, a thousand changes to save, and, on a server, a thousand changes to send. None of it is needed, because growth is predictable: given when the plot was planted and when it was watered, its growth at any tick is arithmetic. So a plot stores only that:
src/core/sim/state.tsexport interface Plot {
cropId: number;
plantedAtTick: number;
growthTicks: number;
wateredAtTick: number;
}
cropId is −1 for empty soil, and wateredAtTick is −1 for a plot nobody has watered.
A plot changes only when somebody does something to it: tills, plants, waters, harvests. Those are commands, they emit one
plot:changed event each, and between them the plot does not change at all. Everything else is derived when somebody
reads it:
src/core/sim/crops.tsexport function growthTicksAt(plot: Plot, tick: number, devGrowth = false): number {
if (plot.cropId < 0) return 0;
const ticks = cropStageTicks(plot.cropId, devGrowth),
max = ticks[ticks.length - 1],
anchor = Math.max(plot.wateredAtTick, plot.plantedAtTick),
elapsed = Math.max(tick - anchor, 0),
moist =
plot.wateredAtTick >= 0
? Math.max(Math.min(elapsed, plot.wateredAtTick + moistureTicks(plot) - anchor), 0)
: 0;
return Math.min(plot.growthTicks + elapsed + moist, max);
}
A crop grows one tick of growth per tick, always. While the soil is moist, which lasts a whole in-game day after watering, it
grows a second tick on top. Watering banks the growth so far into growthTicks and restarts the clock from the
watering, so a plot watered twice does not need to remember the first time.
It was not always like this. Until September 16, a crop grew only while its soil was moist: a plot nobody watered sat as a seed forever. The change made watering a reward instead of a requirement, which mattered even more once sprinkler bots and devices could run out of water: a dry field slows down, it never stops.
A stage is just where the growth falls in the crop's stageTicks. Wheat, like every crop in the game today, uses the
same four thresholds:
src/core/sim/crops.ts1: {
name: 'wheat',
seeds: 'wheat_seeds',
stageTicks: [0, 1200, 2400, 3600],
moistureTicks: TICKS_PER_DAY,
yieldCoins: 12,
sway: 1.5,
mapColor: 0xe2bf4a,
},
export function stage(plot: Plot, tick: number, devGrowth = false): number {
if (plot.cropId < 0) return -1;
const g = growthTicksAt(plot, tick, devGrowth),
t = cropStageTicks(plot.cropId, devGrowth);
let s = 0;
while (s + 1 < t.length && g >= t[s + 1]) s++;
return s;
}
| Stage | Name | Growth | Dry soil | Watered |
|---|---|---|---|---|
| 0 | Seed | 0 | 0:00 | 0:00 |
| 1 | Sprout | 1,200 | 2:00 | 1:00 |
| 2 | Growing | 2,400 | 4:00 | 2:00 |
| 3 | Ripe | 3,600 | 6:00 | 3:00 |
The times are real minutes at ten ticks a second. Twelve coins for six minutes, or three if you water. The dev growth switch in the desktop settings divides every threshold by ten for testing, and the game's time scale and sleep both speed up the ticks themselves, so a crop lives through a skipped night like everything else.
Since nothing tells anyone that a crop moved to the next stage, everyone who cares asks when they need to know:
isMature at the tick the command runs. That is the only moment the
ripeness of a crop is a fact.
stage and
ticksUntilMature, which counts the moist part of the wait double.
ticksUntilMature when it finds nothing ripe and sets its alarm for
the nearest crop. Growth sends no event, so the alarm is the only thing that wakes it.
Crops are not blocks. Each crop and stage is its own glTF model, generated by
scripts/generate-crops.mjs from voxels of 0.04 m, twenty-five to a block. The script places boxes of voxels in code,
so the models are deterministic and a change to a crop is a change to a few numbers. Wheat is seven stalks with their own position
and full height, and four seeds:
scripts/generate-crops.mjs// Wheat: seed positions, then each stalk's plot offset and full height in voxels.
const wheatSeeds = [[-4, -3], [3, -2], [-2, 4], [4, 4]];
const wheatStalks = [
[-5, -4, 19], [0, -5, 24], [5, -3, 21], [-4, 3, 17], [2, 4, 22], [6, 3, 16], [0, 0, 20],
];
const wheatHeight = (stage, i, height) =>
stage === 1 ? 3 + (i % 3) * 2 : stage === 2 ? Math.round(height * 0.68) : height;
const wheatHeadPalette = (stage) => (stage === 3 ? gold : [0x78972c, 0x94ad36, 0xb2bf43, 0xc4cc55]);
const wheatHeadSegments = (stage) => (stage === 3 ? 4 : 3);
A sprout is short stalks of three to seven voxels. A growing stalk reaches about two thirds of its height and gets a green head of three segments. A ripe one stands at full height, its stalk turns olive and its head grows a fourth segment in gold. Leaves branch off every four voxels, alternating sides.
The look is baked into the vertex colours, so the game's crop shader does very little:
Every plot that shows the same model in the same chunk is one instance of one
InstancedMesh. A field is therefore a handful of draw calls per chunk: one per stage and level of detail that occurs
in it, plus the soil and a soft contact shadow under each crop that grows with its stage. Grouping by chunk lets the GPU skip a
whole chunk's plots when the chunk is out of view.
The crop material makes one deliberate cheat. A crop is lit as if by a 10 o'clock sun, 60° above the horizon, from every side at once: every vertical face receives the light the sun-facing side would. The effect is that a field has no dark side: from whichever direction you look at it, the stalks look sunlit. The light's strength and colour still follow the time of day, and the voxel light in each instance's colour dims it in shade and adds lantern light at night.
Wind moves the crops in the vertex shader, more the higher the vertex, so the base stays planted. Each crop has its own
sway: wheat 1.5, lettuce 0.1. All crops share one compiled program that differs only in that one uniform.
2,228 triangles is fine for the nine plots in front of you. It is not fine for the thousand behind them, each a few pixels tall. So every crop and stage has two more models, generated by the same script from the same layout, but from boxes instead of voxels:
The boxes also drop faces nobody can see: the bottom of a box that stands on the soil, the top of a stalk that a head sits on. The colours are the same palette with the same height shading, so the switch between levels is a change of shape, not of colour.
Wheat's models, counted from the glTF files:
| Wheat, triangles | Level 0 | Level 1 | Level 2 | Level 2 of 0 |
|---|---|---|---|---|
| Seed | 104 | 40 | 40 | 38% |
| Sprout | 388 | 110 | 70 | 18% |
| Growing | 1,564 | 362 | 126 | 8% |
| Ripe | 2,228 | 482 | 126 | 6% |
| Tilled soil under every plot | 380 | 12 as a flat box far away | 3% | |
Which level a plot shows is decided by its horizontal distance from the camera, against two thresholds from the Graphics preset. The soil has its own: beyond it, the clods become a flat box textured with the clods' tops seen from above, which keeps the furrows between plots for 12 triangles instead of 380.
| Preset | Level 1 from | Level 2 from | Flat soil from |
|---|---|---|---|
| Low | 7 blocks | 16 blocks | 6 blocks |
| Medium | 9 | 21 | 8 |
| High (default) | 12 | 28 | 10 |
| Very high | 28 | 60 | 24 |
Sorting every plot by distance on every frame would cost more than it saves. The renderer sorts them again only after the camera has moved a whole block, and moves a plot to another batch only when its level actually changes. A player walking through a field reshuffles a ring of plots at the edges of the bands, not the field.
A field from above
Ripe wheat, each plot coloured by the level it shows. Tap or drag on the field to move the camera.
Here is the same thing in the game. We planted a field of 40 × 44 ripe plots, 1,760 in all, and took two screenshots from a pillar at its edge: once with the High preset's levels, once with the full model forced everywhere.
The other nine crops follow the same pattern: the same four stages, the same three levels of detail, and a few arrays in a script for each shape.