3D biomes
This commit is contained in:
+2
-2
@@ -169,8 +169,8 @@ export class Hud {
|
||||
|
||||
#render_info(client: Client) {
|
||||
const { day, hour, minute } = clock(client.level.time);
|
||||
const { x, z } = client.player;
|
||||
const biome = client.level.biome_at(Math.floor(x), Math.floor(z));
|
||||
const { x, y, z } = client.player;
|
||||
const biome = client.level.biome_at(Math.floor(x), Math.floor(y), Math.floor(z));
|
||||
const lines = [
|
||||
`Day ${day}`,
|
||||
`${String(hour).padStart(2, "0")}:${String(minute).padStart(2, "0")}`,
|
||||
|
||||
@@ -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],
|
||||
});
|
||||
|
||||
+9
-9
@@ -92,9 +92,9 @@ const rules_world: SharedWorld = {
|
||||
const shape = client?.level.collision_shape(x, y, z) ?? false;
|
||||
return covers_face(shape === true ? FULL_SHAPE : shape === false ? EMPTY_SHAPE : shape, face);
|
||||
},
|
||||
biome_at: (x, z) => client?.level.biome_at(Math.floor(x), Math.floor(z)),
|
||||
season_at(x, z) {
|
||||
const biome = this.biome_at(x, z);
|
||||
biome_at: (x, y, z) => client?.level.biome_at(Math.floor(x), Math.floor(y), Math.floor(z)),
|
||||
season_at(x, y, z) {
|
||||
const biome = this.biome_at(x, y, z);
|
||||
return biome === undefined ? undefined : biome_season(biome, client!.level.time);
|
||||
},
|
||||
};
|
||||
@@ -340,12 +340,12 @@ function client_context(listing: ModListing): ClientContext {
|
||||
return { x, y, z };
|
||||
},
|
||||
get biome() {
|
||||
const { x, z } = need_client().player;
|
||||
return rules_world.biome_at(x, z);
|
||||
const { x, y, z } = need_client().player;
|
||||
return rules_world.biome_at(x, y, z);
|
||||
},
|
||||
get season() {
|
||||
const { x, z } = need_client().player;
|
||||
return rules_world.season_at(x, z);
|
||||
const { x, y, z } = need_client().player;
|
||||
return rules_world.season_at(x, y, z);
|
||||
},
|
||||
is_swimming: () => need_client().player.swimming,
|
||||
is_sneaking: () => need_client().player.holding_sneak,
|
||||
@@ -358,8 +358,8 @@ function client_context(listing: ModListing): ClientContext {
|
||||
},
|
||||
get_client_data: (x, y, z, field) =>
|
||||
structuredClone(need_client().level.get_block_data(x, y, z)?.[field]) as never,
|
||||
biome_at: (x, z) => rules_world.biome_at(x, z),
|
||||
season_at: (x, z) => rules_world.season_at(x, z),
|
||||
biome_at: (x, y, z) => rules_world.biome_at(x, y, z),
|
||||
season_at: (x, y, z) => rules_world.season_at(x, y, z),
|
||||
get min_y() {
|
||||
return need_client().level.shape.min_y;
|
||||
},
|
||||
|
||||
@@ -56,7 +56,7 @@ export class GameRenderer {
|
||||
const { x, y, z } = client.camera;
|
||||
const [bx, by, bz] = [Math.floor(x), Math.floor(y), Math.floor(z)];
|
||||
if (!client.level.is_fluid(bx, by, bz)) return;
|
||||
const color = client.level.water_color(bx, bz);
|
||||
const color = client.level.water_color(bx, by, bz);
|
||||
draw_rect(0, 0, canvas.width, canvas.height, [
|
||||
(color >> 16) / 255,
|
||||
((color >> 8) & 0xff) / 255,
|
||||
|
||||
@@ -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