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
+18 -6
View File
@@ -6,10 +6,11 @@
import { Alea } from "@paulaboks/rng";
import { AIR, CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
import type { WorldShape } from "@/common/world_shape.ts";
import { ChunkColumn } from "@/common/section.ts";
import { BIOME_CELL_HEIGHT, ChunkColumn } from "@/common/section.ts";
import type { FeatureChunk, Position } from "@/common/mod_api/worldgen.ts";
import type { OreJson } from "@/common/mod_data.ts";
import { generate_noise_chunk } from "./worldgen/noise_chunk.ts";
import { get_biome } from "./worldgen/biomes.ts";
import type { Generator } from "./worldgen/generator.ts";
import { named_noise_2d, named_noise_3d } from "./worldgen/noise.ts";
@@ -33,12 +34,13 @@ export interface WorldgenSetup {
export interface SetupFeature {
id: string;
generate: (chunk: FeatureChunk) => void;
// a code feature's biomes: it only runs in chunks with one of them. data features check each position themselves
// a code feature's biomes: it only runs in chunks with one of them somewhere. data features check each position
// themselves
biomes?: Set<string>;
}
export interface RawChunk {
// only what this chunk generated itself, with each column's biome
// only what this chunk generated itself, with its biomes
column: ChunkColumn;
// blocks it generated in other chunks (from features like a mod's trees), flattened as x, y, z, numeric id
spills: Int32Array;
@@ -68,7 +70,8 @@ export function generate_raw_chunk(
const blocks = scratch.fill(AIR);
const spills: number[] = [];
const heights = new Int32Array(CHUNK_AREA);
const biomes: string[] = new Array(CHUNK_AREA);
// a biome cell's, see column_biomes
const biomes: string[] = new Array(CHUNK_AREA * shape.height / BIOME_CELL_HEIGHT);
const set = (x: number, y: number, z: number, nid: number) => {
if (!shape.contains(y)) {
@@ -155,11 +158,17 @@ function generate_features(
block_ids: Record<string, number>,
worldgen: WorldgenSetup,
) {
const shape = worldgen.shape;
const ids_by_nid: string[] = [];
for (const [id, nid] of Object.entries(block_ids)) {
ids_by_nid[nid] = id;
}
// the cell of biomes a y is in, the top or bottom one past the world's height
const cell = (y: number) => {
const clamped = Math.min(Math.max(y, shape.min_y), shape.max_y - 1);
return Math.floor((clamped - shape.min_y) / BIOME_CELL_HEIGHT) * CHUNK_AREA;
};
const local = (x: number, z: number, what: string) => {
const lx = x - chunk_x * CHUNK_SIZE;
const lz = z - chunk_z * CHUNK_SIZE;
@@ -169,7 +178,6 @@ function generate_features(
return lz * CHUNK_SIZE + lx;
};
const shape = worldgen.shape;
const chunk_biomes = new Set(biomes);
for (const feature of worldgen.features) {
if (feature.biomes && ![...feature.biomes].some((biome) => chunk_biomes.has(biome))) continue;
@@ -184,7 +192,11 @@ function generate_features(
noise_2d: (name) => named_noise_2d(seed, name),
noise_3d: (name) => named_noise_3d(seed, name),
height_at: (x, z) => heights[local(x, z, "height_at")],
biome_at: (x, z) => biomes[local(x, z, "biome_at")],
biome_at: (x, y, z) => biomes[cell(y) + local(x, z, "biome_at")],
set_biome(x, y, z, biome) {
if (!get_biome(biome)) throw new Error(`set_biome: ${biome} isn't a biome`);
if (shape.contains(y)) biomes[cell(y) + local(x, z, "set_biome")] = biome;
},
get_block(x, y, z) {
if (!shape.contains(y)) {
return undefined;