3D biomes

This commit is contained in:
2026-11-07 12:13:43 -03:00
parent b726ce9695
commit 22191d260c
53 changed files with 874 additions and 316 deletions
+41 -20
View File
@@ -1,12 +1,13 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// the shape of the world and which biome each column gets, from a generator's data (see generator.ts): its density
// functions compiled for one seed. generate_noise_chunk calls density and cave through here, so engine access mods can
// the shape of the world and which biome each column gets at each height, from a generator's data (see generator.ts):
// its density functions compiled for one seed. generate_noise_chunk calls density and cave through here, so engine access mods can
// hook them
import { type Density, DensityCompiler } from "./density.ts";
import { type DensityJson, type NoiseSettingsJson, range_test, type RangeJson } from "./data.ts";
import type { Generator } from "./generator.ts";
import { BIOME_CELL_HEIGHT } from "@/common/section.ts";
// what the terrain worked out about a column
export interface TerrainColumn {
@@ -20,8 +21,8 @@ export interface TerrainColumn {
interface CompiledRule {
biome: string;
// climate values by their index in the noise settings' climate, the surface's y, steep
holds(climate: number[], surface: number, steep: boolean): boolean;
// climate values by their index in the noise settings' climate, the surface's y, steep, and the biome cell's lowest y
holds(climate: number[], surface: number, steep: boolean, y: number): boolean;
}
export class NoiseTerrain {
@@ -33,6 +34,8 @@ export class NoiseTerrain {
#caves: Density;
#climate: Density[];
#rules: CompiledRule[];
// whether any rule looks at y or depth. when none do, a column's biome is the same all the way up
readonly by_height: boolean;
constructor(seed: string, generator: Generator) {
this.seed = seed;
@@ -51,6 +54,7 @@ export class NoiseTerrain {
const names = Object.keys(this.settings.climate);
this.#climate = names.map((name) => compile(this.settings.climate[name], `climate.${name}`));
this.#rules = generator.rules.map((rule) => compile_rule(rule.biome, rule.when ?? {}, names));
this.by_height = generator.rules.some((rule) => "y" in (rule.when ?? {}) || "depth" in (rule.when ?? {}));
}
// with_climate works the climate out too, which is cheap right after the height since they share their inputs
@@ -74,26 +78,37 @@ export class NoiseTerrain {
return this.#caves(x, y, z);
}
// the first of the biome source's rules that holds for the column, with its ground at surface_y
biome(column: TerrainColumn, surface_y: number, steep: boolean): string {
const climate = column.climate ?? this.climate(column.x, column.z);
// the first of the biome source's rules that holds for the biome cell from y up, in a column with its ground at
// surface_y. the column's climate is worked out once, and kept on it
biome(column: TerrainColumn, surface_y: number, steep: boolean, y: number): string {
const climate = column.climate ??= this.climate(column.x, column.z);
for (const rule of this.#rules) {
if (rule.holds(climate, surface_y, steep)) return rule.biome;
if (rule.holds(climate, surface_y, steep, y)) return rule.biome;
}
throw new Error(`no rule of biome source ${this.settings.biome_source} holds at ${column.x}, ${column.z}`);
throw new Error(
`no rule of biome source ${this.settings.biome_source} holds at ${column.x}, ${y}, ${column.z}`,
);
}
// a column's biome without generating its chunk, from its height before 3d noise, so it can be a little off near
// the highest y in a column where it's this biome, the ground's or a biome cell's lowest one below it (cells start
// at min_y), without generating its chunk. that's from its height before 3d noise, so it can be a little off near
// coasts and cliffs. for searching far away, like /locatebiome
estimate_biome(x: number, z: number): { biome: string; y: number } {
const column = this.column(x, z);
const y = Math.round(column.height);
const flat = this.biome(column, y, false);
locate_biome(biome: string, x: number, z: number, min_y: number): number | undefined {
const column = this.column(x, z, true);
const ground = Math.round(column.height);
let steep = false;
// most columns are the same either way, the neighbors are only worth sampling when they aren't
if (flat === this.biome(column, y, true)) return { biome: flat, y };
const neighbors = [[x - 1, z], [x + 1, z], [x, z - 1], [x, z + 1]].map(([nx, nz]) => this.column(nx, nz));
const steep = neighbors.some((neighbor) => Math.abs(neighbor.height - column.height) >= this.settings.steep);
return { biome: steep ? this.biome(column, y, true) : flat, y };
if (this.biome(column, ground, false, ground) !== this.biome(column, ground, true, ground)) {
const neighbors = [[x - 1, z], [x + 1, z], [x, z - 1], [x, z + 1]].map(([nx, nz]) => this.column(nx, nz));
steep = neighbors.some((neighbor) => Math.abs(neighbor.height - column.height) >= this.settings.steep);
}
if (this.biome(column, ground, steep, ground) === biome) return ground;
if (!this.by_height) return undefined;
const cell = min_y + Math.floor((ground - min_y) / BIOME_CELL_HEIGHT) * BIOME_CELL_HEIGHT;
for (let y = cell - BIOME_CELL_HEIGHT; y >= min_y; y -= BIOME_CELL_HEIGHT) {
if (this.biome(column, ground, steep, y) === biome) return y;
}
return undefined;
}
}
@@ -105,6 +120,12 @@ function compile_rule(biome: string, when: Record<string, RangeJson | boolean>,
} else if (name === "surface") {
const test = range_test(condition as RangeJson);
tests.push((_climate, surface) => test(surface));
} else if (name === "y") {
const test = range_test(condition as RangeJson);
tests.push((_climate, _surface, _steep, y) => test(y));
} else if (name === "depth") {
const test = range_test(condition as RangeJson);
tests.push((_climate, surface, _steep, y) => test(surface - y));
} else {
const index = names.indexOf(name);
const test = range_test(condition as RangeJson);
@@ -113,9 +134,9 @@ function compile_rule(biome: string, when: Record<string, RangeJson | boolean>,
}
return {
biome,
holds(climate, surface_y, steep) {
holds(climate, surface_y, steep, y) {
for (let i = 0; i < tests.length; i++) {
if (!tests[i](climate, surface_y, steep)) return false;
if (!tests[i](climate, surface_y, steep, y)) return false;
}
return true;
},