Skip to content

Biomes and Vegetation on Voxel Terrain in Babylon.js · Part 3 of 12 in the series

Part 3: The shape of the land

Let the biomes shape the land: each one a row of numbers for its plains, hills, mountains, rocky knolls and dunes, blended where biomes meet, so that the savanna lies flat, the desert rolls in dunes and the tundra is a high plateau, while the start stays as it was.

Quest objectives

  • Describe the shape of a biome with five numbers
  • Blend the numbers of the biomes, not their heights
  • Know why the shape uses the climate at sea level
  • Make knolls and dunes from noise
  • Keep the ground around the start exactly as it was

The biomes have their own ground now, but the land under it is the same everywhere: the hills and mountains of the terrain series. A desert on rolling green hills that happen to be yellow is not a desert. This part lets each biome shape its land: the savanna low and flat with rocks standing in it, the desert in dunes, the conifer forest rugged, the tundra a high plateau under the highest mountains.

A row of numbers

The height of the ground at a place is made of a few parts in src/terrain.ts: a base height, plus hills from layered noise times 40, plus mountain ridges times 150. Those three numbers are the shape of the land, and they are all a biome has to change. So each biome gets a row of numbers, in meters: the height of its plains, and how high its hills and its mountains rise. Two more parts are new: knolls, steep bumps of rock, and dunes. In src/climate.ts, replace the list of biomes:

src/climate.ts
/**
 * The biomes, each with the climate it is at home in and a color for the map. A place
 * belongs to the biome whose climate is nearest to its own. `ground` is the texture that
 * covers its ground, and `tint` colors its soil and rock. `shape` holds the numbers that
 * shape its ground, in meters: see Shape.
 */
export const BIOMES = [
  {
    name: 'Meadow', temperature: 0.5, moisture: 0.4, color: '#7fae3c', ground: 'grass', tint: '#ffffff',
    shape: { base: 12, hills: 40, mountains: 150, knolls: 0, dunes: 0 },
  },
  {
    name: 'Broadleaf forest', temperature: 0.52, moisture: 0.76, color: '#3c7a2a', ground: 'litter', tint: '#f2f0e6',
    shape: { base: 14, hills: 46, mountains: 150, knolls: 0, dunes: 0 },
  },
  {
    name: 'Conifer forest', temperature: 0.3, moisture: 0.62, color: '#28584a', ground: 'needles', tint: '#e6e8f2',
    shape: { base: 20, hills: 55, mountains: 170, knolls: 10, dunes: 0 },
  },
  {
    name: 'Tundra', temperature: 0.1, moisture: 0.45, color: '#c3ccd6', ground: 'moss', tint: '#dfe6f5',
    shape: { base: 24, hills: 20, mountains: 190, knolls: 5, dunes: 0 },
  },
  {
    name: 'Savanna', temperature: 0.76, moisture: 0.5, color: '#c9a545', ground: 'straw', tint: '#ffd9b0',
    shape: { base: 7, hills: 12, mountains: 40, knolls: 22, dunes: 0 },
  },
  {
    name: 'Desert', temperature: 0.86, moisture: 0.14, color: '#e8bd7c', ground: 'sand', tint: '#ffc08a',
    shape: { base: 9, hills: 14, mountains: 70, knolls: 30, dunes: 9 },
  },
] as const;

The meadow's row holds exactly the numbers that were written into terrain.ts: 12, 40 and 150. The broadleaf forest is a little hillier, the conifer forest more so and with knolls, the tundra lies high and nearly level between the highest mountains. The savanna is low and flat, with knolls of 22 m, and the desert has dunes of 9 m and knolls of 30 m, which stand in it as towers of rock.

Blending shapes

Where two biomes meet, their shapes have to meet too. Working out one height per biome and blending the heights would mean all the noise of both for every column near a border. It is cheaper, and simpler, to blend the numbers by the biomes' weights and then work out one height from the blended row. Between savanna and meadow the base rises from 7 m to 12 m, the hills grow from 12 m to 40 m, and the knolls shrink to nothing.

Two things differ from the weights of the ground's cover. First, the strip in which two biomes mix is wider: a cover can change within a few steps, but mountains that rise 150 m on one side and 40 m on the other would end in a wall there. Second, the weights are those of the climate at sea level. The cover's climate gets colder with the height. The shape can't use the height: the height is what it is for. That fits the world, too. A mountain in a meadow is shaped like a meadow's mountain, whatever grows on its top.

In src/climate.ts, the blend gets a second width. Replace the constant BLEND:

src/climate.ts
/** How far past a border, in climate, a biome still mixes with its neighbor. */
export const BLEND = 0.07;
/** The same for the shape of the ground: it changes over a wider strip than the cover. */
const SHAPE_BLEND = 0.12;

biomeWeights() takes the width as a parameter, which is the cover's if nothing is said. Replace the function:

src/climate.ts
/**
 * How much of each biome a climate is: 1 for the only biome deep inside it, shared out
 * near a border, and always 1 in sum. The weights are written into `weights`.
 */
export function biomeWeights(
  temperature: number,
  moisture: number,
  weights: Float32Array | number[],
  blend = BLEND,
): void {
  let nearest = Infinity;
  for (let i = 0; i < BIOME_COUNT; i += 1) {
    const distance = Math.hypot(temperature - BIOMES[i].temperature, moisture - BIOMES[i].moisture);
    weights[i] = distance;
    nearest = Math.min(nearest, distance);
  }
  // A biome counts for as much as it is closer than `blend` beyond the nearest one.
  let sum = 0;
  for (let i = 0; i < BIOME_COUNT; i += 1) {
    const share = Math.max(1 - (weights[i] - nearest) / blend, 0);
    weights[i] = share * share;
    sum += weights[i];
  }
  for (let i = 0; i < BIOME_COUNT; i += 1) weights[i] /= sum;
}

And at the end of the file, the shape of a place:

src/climate.ts
/**
 * The numbers that shape a biome's ground, in meters: the height of its plains, how high
 * its hills, its mountains, its rocky knolls and its dunes rise.
 */
export interface Shape {
  base: number;
  hills: number;
  mountains: number;
  knolls: number;
  dunes: number;
}

const shapeWeights = new Float32Array(BIOME_COUNT);

/**
 * The shape of the ground at (x, z), written into `shape`: every biome's numbers, by the
 * biome's weight in the climate at sea level. The height can't count here: it is what the
 * shape is for.
 */
export function shapeAt(x: number, z: number, shape: Shape): void {
  const { temperature, moisture } = climateAt(x, z);
  biomeWeights(temperature, moisture, shapeWeights, SHAPE_BLEND);
  shape.base = shape.hills = shape.mountains = shape.knolls = shape.dunes = 0;
  for (let i = 0; i < BIOME_COUNT; i += 1) {
    const weight = shapeWeights[i];
    if (weight === 0) continue;
    const own = BIOMES[i].shape;
    shape.base += own.base * weight;
    shape.hills += own.hills * weight;
    shape.mountains += own.mountains * weight;
    shape.knolls += own.knolls * weight;
    shape.dunes += own.dunes * weight;
  }
}

shapeAt() is called for every column of every chunk, so it writes into an object it is handed instead of making a new one, and skips the biomes that have no weight: nearly always all but one.

Knolls and dunes

In src/terrain.ts, import the shape: import { shapeAt, type Shape } from './climate.ts';. Then replace everything from the comment about the grid asking for a whole column to the end of column():

src/terrain.ts
/** Rocky knolls, from 0 to 1: steep bumps where a noise is high, flat ground elsewhere. */
function knolls(x: number, z: number): number {
  const noise = noise2(x / 120 + 300, z / 120 - 200);
  return (Math.max(noise - 0.45, 0) / 0.55) ** 1.5;
}

/**
 * Dunes, from 0 to 1: the noise is stretched along z, so its waves run across x like the
 * dunes across the wind. Where it crosses 0 is a crest, and a second noise bends the crests.
 */
function dunes(x: number, z: number): number {
  const bend = noise2(x / 350 + 70, z / 350) * 1.2;
  return (1 - Math.abs(noise2(x / 80 + bend, z / 240 - 40))) ** 2;
}

// The grid asks for a whole column before the next one, so the last column's
// numbers are kept.
let columnX = Number.NaN;
let columnZ = Number.NaN;
let columnHeight = 0;
let columnMountains = 0;
const shape: Shape = { base: 0, hills: 0, mountains: 0, knolls: 0, dunes: 0 };

function column(x: number, z: number): void {
  if (x === columnX && z === columnZ) return;
  columnX = x;
  columnZ = z;
  columnMountains = mountains(x, z);
  // How high everything rises here is up to the biomes.
  shapeAt(x, z, shape);
  // Rolling hills everywhere, and ridges where the land is mountainous: high where the
  // noise is near 0. The small number in the hypot rounds the crest off.
  const hills = layered2(x / 400, z / 400, 5) * shape.hills;
  const ridge = Math.max(1 - Math.hypot(layered2(x / 500 - 20, z / 500 + 90, 4) * 1.6, 0.15), 0);
  columnHeight = shape.base + hills + columnMountains * ridge ** 3 * shape.mountains;
  // The noise for knolls and dunes is only worked out where a biome has them.
  if (shape.knolls > 0) columnHeight += knolls(x, z) * shape.knolls;
  if (shape.dunes > 0) columnHeight += dunes(x, z) * shape.dunes;
}
  • knolls() cuts a noise off at 0.45. Below that, which is most of the land, it gives 0, and the ground stays as it is. Above, a bump rises, steeper toward its top. Steep ground is painted as rock by the shader, in the biome's tint, so the knolls are bare rock without any more code.

  • dunes() reads a noise that is stretched three times longer along z than along x. One minus its absolute value is highest where the noise crosses 0: those lines are the crests, and they run mostly along z. Squaring makes the crests sharp and the hollows wide. A second, slow noise shifts the first along x, which bends the crests.

  • column() asks for the shape, and uses its numbers where the fixed ones were. The noise for knolls and dunes is only worked out where a biome has any.

Both new parts read the terrain's own 2D noise at places far from the ones the hills use. No new noise is made from the terrain's random numbers, so the hills themselves are untouched.

What stays the same

In the meadow, the blended row is the meadow's row, and that holds the old numbers: the height comes out the same, to the last digit. The start lies in a meadow, and the numbers that move the climate's noise in part 1 were picked so that around the start not even the wider blend of the shape reaches another biome. Whatever was built there in the building series stands on the same ground.

Nothing else needs a change. The chunks, the far view, the light, the walking and the search for a biome all ask terrain.ts for the ground. The price is time: every column now asks for the climate first, which about doubles the work for a column's height. The 3D noise for the overhangs and caves still costs far more than that.

Try it

Press N until the HUD says Desert, and look around: dunes in long crests, and towers of red rock between them. Land with F and walk up a dune. Go on to the savanna: flat to the horizon, with knolls to climb. The tundra is a high, nearly level plain, and the conifer forest is the roughest land to cross. Switch the map on with G and fly along a border: the cover changes at the line, the height over a wider strip. Then go back to the start. The sample building stands where it stood.

The savanna: low, flat land, with knolls of bare rock standing in it.
The shape of the land. Click, then: N flies to the next biome, F lands and flies (Q/E down/up), G shows the map. T digs and builds terrain, B places blocks.
Open in a new tab

Comments

The comments are GitHub Discussions: sign in with GitHub to write one.

The comments come from GitHub, through giscus. Loading them sends your IP address to giscus.app and GitHub.