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
+16 -21
View File
@@ -22,7 +22,7 @@ import {
} from "@/common/everything_registry.ts";
import { AIR, CHUNK_AREA, CHUNK_SIZE, Faces, ID_MASK, VOID } from "../../common/constants.ts";
import { SECTION_HEIGHT, type WorldShape } from "@/common/world_shape.ts";
import { ChunkColumn, column_heights, section_index, type SectionData } from "@/common/section.ts";
import { ChunkColumn, column_heights, section_index, type SectionBiomes, type SectionData } from "@/common/section.ts";
import { AssetManager } from "../assets.ts";
import { ChunkWorkerPool } from "../chunk_workers.ts";
import type { FaceGroup, FromChunkWorker, SectionLight } from "../workers/chunk_messages.ts";
@@ -53,7 +53,7 @@ export interface Chunk {
sky: Int16Array;
// one for each of its sections, from the bottom of the world up
sections: RenderSection[];
// each column's biome grass color, x then z, then each column's foliage color, then water color
// each of its biome palette's grass, foliage and water colors, three numbers a biome
biome_colors: Uint32Array;
}
@@ -226,12 +226,9 @@ export class ClientLevel {
// a whole chunk, replacing it if it was already here. its blocks are this client's numeric ids with their state bits
set_chunk(column: ChunkColumn) {
const { x, z } = column;
const biome_colors = new Uint32Array(CHUNK_AREA * 3);
column.biomes.forEach((biome, column) => {
biome_colors[column] = grass_color(biome);
biome_colors[CHUNK_AREA + column] = foliage_color(biome);
biome_colors[CHUNK_AREA * 2 + column] = water_color(biome);
});
const biome_colors = Uint32Array.from(
column.biomes.palette.flatMap((biome) => [grass_color(biome), foliage_color(biome), water_color(biome)]),
);
const sky = column_heights(column, this.#light.opacity).slice(CHUNK_AREA);
@@ -358,22 +355,17 @@ export class ClientLevel {
return block ? collision_shape_at(block, value) : true;
}
// the column's biome, undefined in unloaded chunks
biome_at(x: number, z: number): string | undefined {
const chunk_x = Math.floor(x / CHUNK_SIZE);
const chunk_z = Math.floor(z / CHUNK_SIZE);
return this.get_chunk(chunk_x, chunk_z)
?.column.biomes[(z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + (x - chunk_x * CHUNK_SIZE)];
// the biome at a block, undefined in unloaded chunks
biome_at(x: number, y: number, z: number): string | undefined {
return this.get_chunk(Math.floor(x / CHUNK_SIZE), Math.floor(z / CHUNK_SIZE))?.column.biome_at(x, y, z);
}
// the biome's water color at a column as 0xRRGGBB, see "tint". plains' in unloaded chunks
water_color(x: number, z: number) {
const chunk_x = Math.floor(x / CHUNK_SIZE);
const chunk_z = Math.floor(z / CHUNK_SIZE);
const chunk = this.get_chunk(chunk_x, chunk_z);
// the biome's water color at a block as 0xRRGGBB, see "tint". plains' in unloaded chunks
water_color(x: number, y: number, z: number) {
const chunk = this.get_chunk(Math.floor(x / CHUNK_SIZE), Math.floor(z / CHUNK_SIZE));
if (!chunk) return DEFAULT_WATER_COLOR;
return chunk
.biome_colors[CHUNK_AREA * 2 + (z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + (x - chunk_x * CHUNK_SIZE)];
const biome = chunk.column.biomes.palette.indexOf(chunk.column.biome_at(x, y, z));
return chunk.biome_colors[biome * 3 + 2];
}
// the fluid there and how high it is, for fluids pushing things along. unloaded chunks have none
@@ -620,12 +612,14 @@ export class ClientLevel {
const end = Math.min(chunk.sections.length, highest + 2);
const chunks: (SectionData[] | null)[] = [];
const sky: (Int16Array | null)[] = [];
const biomes: (SectionBiomes[] | null)[] = [];
const biome_colors: (Uint32Array | null)[] = [];
for (let dz = -1; dz <= 1; dz++) {
for (let dx = -1; dx <= 1; dx++) {
const neighbor = this.get_chunk(chunk.x + dx, chunk.z + dz);
chunks.push(neighbor?.column.sections.slice(first, end).map((section) => section.data) ?? null);
sky.push(neighbor?.sky ?? null);
biomes.push(neighbor?.column.biomes.sections.slice(first, end) ?? null);
biome_colors.push(neighbor?.biome_colors ?? null);
}
}
@@ -638,6 +632,7 @@ export class ClientLevel {
first,
chunks,
sky,
biomes,
biome_colors,
camera: [camera.x, camera.y, camera.z],
});