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
+10 -4
View File
@@ -125,12 +125,18 @@ export interface BiomeRuleJson {
id: string;
before?: string;
after?: string;
// every condition holds: a range of a climate value from the noise settings, of "surface" (the ground's y), or
// "steep": true or false. no conditions always holds
// every condition holds: a range of a climate value from the noise settings, or of one of RULE_VALUES, or "steep":
// true or false. no conditions always holds
when?: Record<string, RangeJson | boolean>;
biome: string;
}
// what biome rules look at besides a generator's climate, so its climate values can't be named these: the ground's y,
// a biome cell's lowest y and how far that is below the ground (0 at the ground, negative above it)
export const RULE_VALUES = ["surface", "y", "depth"];
// whether a column is steep, see NoiseSettingsJson.steep
export const RULE_NAMES = [...RULE_VALUES, "steep"];
// dimensions: a world to play and the noise settings that generate it, like neoforge's level stems. the world plays the
// one with "default": true, and only one loaded mod can have one
@@ -293,8 +299,8 @@ const noise_settings_schema: z.ZodType<NoiseSettingsJson> = z.object({
caves: z.object({ density, cell_height }),
preliminary_surface: density,
climate: z.record(
z.string("isn't a name a climate value can have: lowercase letters and _, but not surface or steep")
.regex(/^[a-z_]+$/).refine((name) => name !== "surface" && name !== "steep"),
z.string(`isn't a name a climate value can have: lowercase letters and _, but not ${RULE_NAMES.join(", ")}`)
.regex(/^[a-z_]+$/).refine((name) => !RULE_NAMES.includes(name)),
density,
),
steep: z.number().min(0),
+3 -2
View File
@@ -46,7 +46,7 @@ export interface FeatureJson {
type: string;
config?: Record<string, unknown>;
placement?: PlacementJson[];
// only where the column is one of these biomes
// only where the biome at the position is one of these
biomes?: string | string[];
step?: FeatureStep;
}
@@ -233,7 +233,8 @@ export function compile_feature(
for (const position of positions) {
if (
biomes &&
!(inside(chunk, position.x, position.z) && biomes.has(chunk.biome_at(position.x, position.z)))
!(inside(chunk, position.x, position.z) &&
biomes.has(chunk.biome_at(position.x, position.y, position.z)))
) {
continue;
}
+9 -2
View File
@@ -4,7 +4,14 @@
// a generator: noise settings, and the density functions, noises and biome rules it uses. compiled once for each
// seed, see NoiseTerrain. the data comes from mods' worldgen folders, see docs/modding/worldgen/terrain.md
import { EverythingRegistry } from "../everything_registry.ts";
import type { BiomeRuleJson, BiomeSourceJson, DensityFunctionJson, NoiseJson, NoiseSettingsJson } from "./data.ts";
import {
type BiomeRuleJson,
type BiomeSourceJson,
type DensityFunctionJson,
type NoiseJson,
type NoiseSettingsJson,
RULE_NAMES,
} from "./data.ts";
import type { DensitySources } from "./density.ts";
import { NoiseTerrain } from "./terrain.ts";
@@ -75,7 +82,7 @@ function merge_rules(settings: NoiseSettingsJson, sources: BiomeSourceJson[]): B
rules.splice(rule.before ? at : at + 1, 0, rule);
}
}
const names = new Set([...Object.keys(settings.climate), "surface", "steep"]);
const names = new Set([...Object.keys(settings.climate), ...RULE_NAMES]);
const seen = new Set<string>();
for (const rule of rules) {
if (seen.has(rule.id)) throw new Error(`biome source ${base.id}: two rules are called ${rule.id}`);
+17 -5
View File
@@ -7,6 +7,7 @@
// are features, after it
import { CHUNK_AREA, CHUNK_SIZE } from "@/common/constants.ts";
import type { WorldShape } from "@/common/world_shape.ts";
import { BIOME_CELL_HEIGHT } from "@/common/section.ts";
import { get_biome } from "./biomes.ts";
import type { NoiseTerrain, TerrainColumn } from "./terrain.ts";
import {
@@ -17,8 +18,9 @@ import {
type SurfaceContext,
} from "./surface_rules.ts";
// fills blocks (indexed (y - min_y) * CHUNK_AREA + z * CHUNK_SIZE + x) and each column's surface height and biome. block is
// a block's numeric id with its default states
// fills blocks (indexed (y - min_y) * CHUNK_AREA + z * CHUNK_SIZE + x), each column's surface height and each biome
// cell's biome (indexed like blocks, a layer of cells at a time, see column_biomes). block is a block's numeric id with
// its default states
export function generate_noise_chunk(
blocks: Uint32Array,
heights: Int32Array,
@@ -39,6 +41,9 @@ export function generate_noise_chunk(
const cave_levels = height / cave_cell_height + 1;
const x0 = chunk_x * CHUNK_SIZE;
const z0 = chunk_z * CHUNK_SIZE;
const cells = height / BIOME_CELL_HEIGHT;
// the cell a y is in, below the world the lowest
const ground_cell = (y: number) => Math.max(0, Math.floor((y - min_y) / BIOME_CELL_HEIGHT));
// density at the corners of every cell
const corner_columns: TerrainColumn[] = [];
@@ -160,10 +165,17 @@ export function generate_noise_chunk(
const ground = column.height;
const neighbors = [column_at(x - 1, z), column_at(x + 1, z), column_at(x, z - 1), column_at(x, z + 1)];
const steep = neighbors.some((neighbor) => Math.abs(neighbor.height - ground) >= settings.steep);
const biome = terrain.biome(column, surface_y, steep);
heights[z * CHUNK_SIZE + x] = surface_y;
biomes[z * CHUNK_SIZE + x] = biome;
// the biome of each cell, worked out once for the whole column when no rule looks at heights. the ground's
// surface is its own cell's
let biome = "";
for (let cell = 0; cell < cells; cell++) {
const y = min_y + cell * BIOME_CELL_HEIGHT;
if (cell === 0 || terrain.by_height) biome = terrain.biome(column, surface_y, steep, y);
biomes[cell * CHUNK_AREA + z * CHUNK_SIZE + x] = biome;
}
biome = biomes[ground_cell(surface_y) * CHUNK_AREA + z * CHUNK_SIZE + x];
context.x = x0 + x;
context.z = z0 + z;
+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;
},