Client side chunk sections

This commit is contained in:
2026-11-07 10:39:16 -03:00
parent 8e14e9842a
commit 71cbf901a5
19 changed files with 162 additions and 116 deletions
+30 -39
View File
@@ -23,9 +23,10 @@ import {
} from "@/common/everything_registry.ts";
import { AIR, CHUNK_AREA, CHUNK_SIZE, Faces, ID_MASK, VOID } from "../../common/constants.ts";
import type { WorldShape } from "@/common/world_shape.ts";
import { ChunkColumn, section_index, type SectionData } from "@/common/section.ts";
import { AssetManager } from "../assets.ts";
import { ChunkWorkerPool } from "../chunk_workers.ts";
import type { FaceGroup, FromChunkWorker } from "../workers/chunk_messages.ts";
import type { FaceGroup, FromChunkWorker, SectionLight } from "../workers/chunk_messages.ts";
import { create_index_buffer, create_vertex_buffer, destroy_buffer, Texture } from "../renderer/mod.ts";
import { crosses_planes } from "../workers/translucent_sort.ts";
import { Camera } from "../camera.ts";
@@ -47,8 +48,8 @@ export { CHUNK_AREA, CHUNK_SIZE };
export interface Chunk {
x: number;
z: number;
// the dimension's whole height, y from its min_y then z then x
blocks: Uint32Array;
// its blocks and each column's biome, as the server sent them
column: ChunkColumn;
dirty: boolean;
// bumped on every mesh request so late results from older requests get ignored
mesh_version: number;
@@ -58,12 +59,10 @@ export interface Chunk {
max_y: number;
// only for translucent meshes that have to be sorted again as the camera moves
translucent_sort?: TranslucentSort;
// each column's biome id, x then z
biomes: string[];
// each column's biome grass color, x then z, then each column's foliage color, then water color
biome_colors: Uint32Array;
// from its last mesh, see get_light. undefined until it's been meshed
light?: Uint8Array;
// from its last mesh by section, see get_light. undefined until it's been meshed
light?: SectionLight[];
}
export interface ChunkMesh {
@@ -199,23 +198,22 @@ export class ClientLevel {
// a level_chunk from the server: the whole chunk, replacing it if it was already here
load_chunk(data: Uint8Array) {
const { x, z, blocks, biomes } = decode_level_chunk(
const { x, z, sections, biomes } = decode_level_chunk(
data,
(value) => this.#to_local[value & ID_MASK] | (value & ~ID_MASK),
);
if (!this.#in_storage_range(x, z)) {
return;
}
if (blocks.length !== this.shape.volume) {
throw new Error(`Chunk ${x}, ${z} isn't as tall as the dimension`);
}
this.set_chunk(x, z, blocks, biomes);
// throws when it isn't as tall as the dimension
this.set_chunk(new ChunkColumn(x, z, this.shape, sections, biomes));
}
// a whole chunk, replacing it if it was already here. blocks are this client's numeric ids with their state bits
set_chunk(x: number, z: number, blocks: Uint32Array, biomes: string[]) {
// 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);
biomes.forEach((biome, column) => {
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);
@@ -224,16 +222,14 @@ export class ClientLevel {
const chunk = this.get_chunk(x, z);
if (chunk) {
// its old mesh stays up until the new one is ready
chunk.blocks = blocks;
chunk.biomes = biomes;
chunk.column = column;
chunk.biome_colors = biome_colors;
this.#mark_dirty(chunk);
} else {
const chunk: Chunk = {
x,
z,
blocks,
biomes,
column,
biome_colors,
dirty: true,
mesh_version: 0,
@@ -302,10 +298,10 @@ export class ClientLevel {
const lx = block.x - block_chunk_x * CHUNK_SIZE;
const lz = block.z - block_chunk_z * CHUNK_SIZE;
const index = this.#index(block.x, block.y, block.z, block_chunk_x, block_chunk_z);
const old_nid = chunk.blocks[index] & ID_MASK;
chunk.blocks[index] = state === undefined ? default_block_value(nid, info) : block_value(nid, state);
const old_nid = chunk.column.get(block.x, block.y, block.z) & ID_MASK;
const value = state === undefined ? default_block_value(nid, info) : block_value(nid, state);
chunk.column.set(block.x, block.y, block.z, value);
this.#mark_dirty(chunk);
this.#mark_neighbors_dirty(block_chunk_x, block_chunk_z, lx, lz, old_nid, nid);
}
@@ -321,7 +317,8 @@ export class ClientLevel {
const chunk_z = Math.floor(z / CHUNK_SIZE);
const light = this.get_chunk(chunk_x, chunk_z)?.light;
if (!light) return this.shape.open_light;
return light[this.#index(Math.floor(x), block_y, Math.floor(z), chunk_x, chunk_z)];
const section = light[(block_y - this.shape.min_y) >> 4];
return typeof section === "number" ? section : section[section_index(Math.floor(x), block_y, Math.floor(z))];
}
// the id with its state bits, VOID outside loaded chunks
@@ -332,7 +329,7 @@ export class ClientLevel {
if (!chunk) {
return VOID;
}
return this.shape.contains(y) ? chunk.blocks[this.#index(x, y, z, chunk_x, chunk_z)] : AIR;
return chunk.column.get(x, y, z);
}
get_block(x: number, y: number, z: number) {
@@ -344,13 +341,7 @@ export class ClientLevel {
if (!chunk) {
return VOID;
}
return this.shape.contains(y) ? chunk.blocks[this.#index(x, y, z, chunk_x, chunk_z)] & ID_MASK : AIR;
}
// a block's place in its chunk's blocks and light. y has to be in the shape
#index(x: number, y: number, z: number, chunk_x: number, chunk_z: number) {
return (y - this.shape.min_y) * CHUNK_AREA + (z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE +
(x - chunk_x * CHUNK_SIZE);
return chunk.column.get(x, y, z) & ID_MASK;
}
// whether entities bump into the block there. unloaded chunks count as solid, so nothing falls out of the world
@@ -374,7 +365,7 @@ export class ClientLevel {
const chunk_x = Math.floor(x / CHUNK_SIZE);
const chunk_z = Math.floor(z / CHUNK_SIZE);
return this.get_chunk(chunk_x, chunk_z)
?.biomes[(z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + (x - chunk_x * CHUNK_SIZE)];
?.column.biomes[(z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + (x - chunk_x * CHUNK_SIZE)];
}
// the biome's water color at a column as 0xRRGGBB, see "tint". plains' in unloaded chunks
@@ -414,15 +405,14 @@ export class ClientLevel {
const lx = x - block_chunk_x * CHUNK_SIZE;
const lz = z - block_chunk_z * CHUNK_SIZE;
const index = this.#index(x, y, z, block_chunk_x, block_chunk_z);
const old_value = chunk.blocks[index];
const old_value = chunk.column.get(x, y, z);
const old_nid = old_value & ID_MASK;
const old_block = this.#blocks[old_nid];
// a waterlogged block leaves its fluid, like the server's destroy_block
const fluid = is_waterlogged(old_value, old_block)
? EverythingRegistry.get_id("blocks", old_block!.waterloggable!) ?? AIR
: AIR;
chunk.blocks[index] = fluid;
chunk.column.set(x, y, z, fluid);
this.#mark_dirty(chunk);
this.#mark_neighbors_dirty(block_chunk_x, block_chunk_z, lx, lz, old_nid, fluid);
}
@@ -619,13 +609,14 @@ export class ClientLevel {
chunk.dirty = false;
this.#dirty.delete(chunk);
chunk.mesh_version += 1;
// copies, the worker lights the whole 3x3 area
const chunks: (Uint32Array | null)[] = [];
// the worker lights the whole 3x3 area. posting copies the sections' blocks, only of the ones that aren't all
// one block
const chunks: (SectionData[] | 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?.blocks.slice() ?? null);
chunks.push(neighbor?.column.sections.map((section) => section.data) ?? null);
biome_colors.push(neighbor?.biome_colors ?? null);
}
}
@@ -638,7 +629,7 @@ export class ClientLevel {
chunks,
biome_colors,
camera: [camera.x, camera.y, camera.z],
}, chunks.filter((blocks) => blocks !== null).map((blocks) => blocks.buffer));
});
}
}