3D biomes
This commit is contained in:
@@ -5,7 +5,7 @@ import type { BlockRegistry } from "@/common/everything_registry.ts";
|
||||
import type { SpriteRegion } from "@/common/constants.ts";
|
||||
import type { ModelJson } from "@/common/block_models.ts";
|
||||
import type { WorldShapeJson } from "@/common/world_shape.ts";
|
||||
import type { SectionData } from "@/common/section.ts";
|
||||
import type { SectionBiomes, SectionData } from "@/common/section.ts";
|
||||
import type { SortType } from "./translucent_sort.ts";
|
||||
|
||||
// messages between the main thread and the chunk workers
|
||||
@@ -58,7 +58,9 @@ export type ToChunkWorker =
|
||||
chunks: (SectionData[] | null)[];
|
||||
// the same chunks' sky heights, see column_heights in common/section.ts
|
||||
sky: (Int16Array | null)[];
|
||||
// the same chunks' biome colors (see Chunk.biome_colors in client_level.ts), blended across their edges
|
||||
// the same chunks' biome cells, of the same sections as chunks, and their biome palettes' colors (see
|
||||
// Chunk.biome_colors in client_level.ts), blended across their edges
|
||||
biomes: (SectionBiomes[] | null)[];
|
||||
biome_colors: (Uint32Array | null)[];
|
||||
// where the camera is, to sort the translucent quads
|
||||
camera: number[];
|
||||
|
||||
@@ -22,7 +22,7 @@ import {
|
||||
type ToChunkWorker,
|
||||
UNALIGNED_GROUP,
|
||||
} from "./chunk_messages.ts";
|
||||
import { Section, section_index, SECTION_VOLUME } from "@/common/section.ts";
|
||||
import { BIOME_CELL_HEIGHT, Section, section_index, SECTION_VOLUME, type SectionBiomes } from "@/common/section.ts";
|
||||
import { SECTION_HEIGHT } from "@/common/world_shape.ts";
|
||||
import {
|
||||
bake_block,
|
||||
@@ -153,7 +153,9 @@ self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
|
||||
const bottom = world_min_y + message.first * SECTION_HEIGHT;
|
||||
region.fill(chunks, message.sky, bottom);
|
||||
region.compute(light_tables);
|
||||
blend_biome_colors(message.biome_colors);
|
||||
biomes = message.biomes;
|
||||
biome_colors = message.biome_colors;
|
||||
blended_any = false;
|
||||
const sections: SectionMesh[] = message.sections.map(({ index, version }) => {
|
||||
// the section's bottom in the region
|
||||
const from = (index - message.first) * SECTION_HEIGHT;
|
||||
@@ -225,18 +227,41 @@ function post(message: FromChunkWorker, transfer: Transferable[]) {
|
||||
self.postMessage(message, transfer);
|
||||
}
|
||||
|
||||
// like minecraft's biome blend at its default radius: each column's grass, foliage and water color is the average of the 5x5
|
||||
// columns around it, so biome edges fade instead of being a line. neighbors that aren't there are left out
|
||||
// like minecraft's biome blend at its default radius: each column's grass, foliage and water color is the average of
|
||||
// the 5x5 columns around it at the same height, so biome edges fade instead of being a line. neighbors that aren't
|
||||
// there are left out. colors aren't blended up and down
|
||||
const BLEND_RADIUS = 2;
|
||||
// the middle chunk's blended colors, x then z, grass then foliage then water
|
||||
// the 3x3 chunks' biome cells and colors of the request being meshed, see "mesh"
|
||||
let biomes: (SectionBiomes[] | null)[] = [];
|
||||
let biome_colors: (Uint32Array | null)[] = [];
|
||||
// the middle chunk's blended colors at a layer of cells, x then z, grass then foliage then water
|
||||
const blended = new Uint32Array(CHUNK_AREA * 3);
|
||||
// the 3x3 chunks' biome palette index of each column at that layer, so a layer with the same biomes as the last one
|
||||
// blended isn't blended again, unless none has been for this request
|
||||
const blended_cells = new Uint8Array(9 * CHUNK_AREA);
|
||||
let blended_any = false;
|
||||
const layer_cells = new Uint8Array(9 * CHUNK_AREA);
|
||||
// what MeshQuad.tint is for the biome's colors, they aren't colors since those only go up to 0xffffff
|
||||
const GRASS = -1;
|
||||
const FOLIAGE = -2;
|
||||
const WATER = -3;
|
||||
|
||||
function blend_biome_colors(colors: (Uint32Array | null)[]) {
|
||||
for (const offset of [0, CHUNK_AREA, CHUNK_AREA * 2]) {
|
||||
// blends the colors of the layer of biome cells y is in, in the region's section from (see "mesh")
|
||||
function blend_biome_colors(from: number, y: number) {
|
||||
const layer = ((y & 15) >> 2) << 8;
|
||||
for (let i = 0; i < 9; i++) {
|
||||
const cells = biomes[i]?.[from >> 4];
|
||||
if (cells === undefined) continue;
|
||||
if (typeof cells === "number") layer_cells.fill(cells, i * CHUNK_AREA, (i + 1) * CHUNK_AREA);
|
||||
else layer_cells.set(cells.subarray(layer, layer + CHUNK_AREA), i * CHUNK_AREA);
|
||||
}
|
||||
let same = blended_any;
|
||||
for (let i = 0; i < layer_cells.length && same; i++) same = layer_cells[i] === blended_cells[i];
|
||||
if (same) return;
|
||||
blended_cells.set(layer_cells);
|
||||
blended_any = true;
|
||||
|
||||
for (let part = 0; part < 3; part++) {
|
||||
for (let z = 0; z < CHUNK_SIZE; z++) {
|
||||
for (let x = 0; x < CHUNK_SIZE; x++) {
|
||||
let r = 0;
|
||||
@@ -248,9 +273,12 @@ function blend_biome_colors(colors: (Uint32Array | null)[]) {
|
||||
// which of the 3x3 chunks, and where in it
|
||||
const sx = x + dx + CHUNK_SIZE;
|
||||
const sz = z + dz + CHUNK_SIZE;
|
||||
const chunk = colors[Math.floor(sz / CHUNK_SIZE) * 3 + Math.floor(sx / CHUNK_SIZE)];
|
||||
if (!chunk) continue;
|
||||
const color = chunk[offset + (sz % CHUNK_SIZE) * CHUNK_SIZE + (sx % CHUNK_SIZE)];
|
||||
const chunk = Math.floor(sz / CHUNK_SIZE) * 3 + Math.floor(sx / CHUNK_SIZE);
|
||||
const colors = biome_colors[chunk];
|
||||
if (!colors || !biomes[chunk]) continue;
|
||||
const cell =
|
||||
layer_cells[chunk * CHUNK_AREA + (sz % CHUNK_SIZE) * CHUNK_SIZE + (sx % CHUNK_SIZE)];
|
||||
const color = colors[cell * 3 + part];
|
||||
r += color >> 16;
|
||||
g += (color >> 8) & 0xff;
|
||||
b += color & 0xff;
|
||||
@@ -260,7 +288,7 @@ function blend_biome_colors(colors: (Uint32Array | null)[]) {
|
||||
const color = count === 0
|
||||
? 0xffffff
|
||||
: (Math.round(r / count) << 16) | (Math.round(g / count) << 8) | Math.round(b / count);
|
||||
blended[offset + z * CHUNK_SIZE + x] = color;
|
||||
blended[part * CHUNK_AREA + z * CHUNK_SIZE + x] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -721,6 +749,7 @@ function make_section_mesh(
|
||||
// nothing to mesh in a section of air
|
||||
const air = section.blocks === null && section.value === AIR;
|
||||
for (let y = from; y < from + SECTION_HEIGHT && !air; y++) {
|
||||
if (y % BIOME_CELL_HEIGHT === 0) blend_biome_colors(from, y);
|
||||
for (let z = 0; z < CHUNK_SIZE; z++) {
|
||||
for (let x = 0; x < CHUNK_SIZE; x++) {
|
||||
// the middle chunk of the region
|
||||
|
||||
Reference in New Issue
Block a user