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
+1 -1
View File
@@ -57,7 +57,7 @@ Deno.test("a report reads back from what was copied", () => {
Deno.test("the benchmark's level comes out the same every time, with terrain in every layer", async () => {
await test_game("mods");
const blocks = bench_blocks(new Set());
const chunk = (x: number, z: number) => new BenchTerrain(blocks).chunk(x, z).blocks;
const chunk = (x: number, z: number) => new BenchTerrain(blocks).chunk(x, z).to_flat();
const lake = [Math.floor(ZONES.lake.x / CHUNK_SIZE), Math.floor(ZONES.lake.z / CHUNK_SIZE)] as const;
const near_lake = [lake[0] + 3, lake[1] + 3] as const;
assertEquals(chunk(0, 0), chunk(0, 0));
+6 -1
View File
@@ -6,6 +6,7 @@
// GameServer.use_worldgen_pool
import { assert, assertEquals } from "@std/assert";
import { is_within_view } from "@/common/chunk_streaming.ts";
import { to_flat } from "@/common/section.ts";
import { game_with_pool, load_view } from "./chunk_loading.ts";
// the workers and their ports live until the test closes them
@@ -32,7 +33,11 @@ Deno.test({
if (!is_within_view(cx, cz, player.chunks.view_distance, x, z)) continue;
const chunk = chunks.get(`${x},${z}`);
assert(chunk, `chunk ${x},${z} wasn't sent`);
assertEquals(chunk.blocks, t.game.world.generated_blocks(x, z).to_flat(), `chunk ${x},${z}`);
assertEquals(
to_flat(chunk.sections),
t.game.world.generated_blocks(x, z).to_flat(),
`chunk ${x},${z}`,
);
}
}
assert(t.pool!.generated - generated > chunks.size, "the pool made them, and their neighbors");
+7 -6
View File
@@ -4,10 +4,10 @@
// the server sends the world in whole chunks, like minecraft java: nearest first, in batches the client paces, and
// only to players who can see them
import { assert, assertEquals } from "@std/assert";
import { CHUNK_AREA, CHUNK_SIZE } from "@/common/constants.ts";
import { CHUNK_AREA } from "@/common/constants.ts";
import { decode_level_chunk, encode_level_chunk, is_within_view } from "@/common/chunk_streaming.ts";
import { PROTOCOL_VERSION } from "@/common/protocol.ts";
import { ChunkColumn } from "@/common/section.ts";
import { ChunkColumn, section_index, to_flat } from "@/common/section.ts";
import { WorldShape } from "@/common/world_shape.ts";
import { sent_block, test_game } from "./helpers.ts";
@@ -28,7 +28,8 @@ Deno.test("a chunk comes back as it was encoded, whatever its sections hold", ()
const decoded = decode_level_chunk(encode_level_chunk(ChunkColumn.from_flat(-3, 70000, shape, blocks, biomes)));
assertEquals(decoded.x, -3);
assertEquals(decoded.z, 70000);
assertEquals(decoded.blocks, blocks);
assertEquals(to_flat(decoded.sections), blocks);
assertEquals(decoded.sections.map((section) => section.blocks === null).indexOf(false), 1, "uniform ones stay so");
assertEquals(decoded.biomes, biomes);
// one biome, and the receiver's numbering
@@ -37,8 +38,8 @@ Deno.test("a chunk comes back as it was encoded, whatever its sections hold", ()
(value) => value === 7 ? 70 : value,
);
assertEquals(one.biomes, biomes.map(() => "bworld:plains"));
assertEquals(one.blocks[0], 70);
assertEquals(one.blocks[section], 1);
assertEquals(one.sections[0].value, 70);
assertEquals(one.sections[1].get(0), 1);
});
Deno.test("the view is round, with a ring of chunks past it for meshing its edge", () => {
@@ -136,7 +137,7 @@ Deno.test("block changes only go to players who have the chunk, the others get t
assertEquals(alice.blocks[game.world.block_ids["bworld:stone"]], "bworld:stone");
const chunk = decode_level_chunk(game.world.level_chunk(0, 0));
const y = 120 - game.world.shape.min_y;
assertEquals(chunk.blocks[y * CHUNK_AREA + 5 * CHUNK_SIZE + 3], game.world.get_block_value(3, 120, 5));
assertEquals(chunk.sections[y >> 4].get(section_index(3, y, 5)), game.world.get_block_value(3, 120, 5));
});
Deno.test("/viewdistance caps every player's view distance, and raising it gives them what they asked for", async () => {
+3 -3
View File
@@ -139,9 +139,9 @@ Deno.test("a dimension's blocks go from its min_y to max_y - 1, and are sent and
// what clients get: every block from the bottom up
const chunk = decode_level_chunk(world.level_chunk(2, 0));
assertEquals(chunk.blocks.length, CHUNK_AREA * 384);
assertEquals(chunk.blocks[8], world.get_block_value(GROUND_X, -64, 0));
assertEquals(chunk.blocks[383 * CHUNK_AREA + 8], world.get_block_value(GROUND_X, 319, 0));
assertEquals(chunk.sections.length, 384 / 16);
assertEquals(chunk.sections[0].get(8), world.get_block_value(GROUND_X, -64, 0));
assertEquals(chunk.sections[23].get(15 * CHUNK_AREA + 8), world.get_block_value(GROUND_X, 319, 0));
// a column of ground: glass up to 69 and air above, all one block in every section but the one with the ground's
// top in it
+16 -10
View File
@@ -1,12 +1,15 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// entities are lit from the light the chunk worker sends back with each mesh, one byte per block: block << 4 | sky
// entities are lit from the light the chunk worker sends back with each mesh, by section and one byte per block:
// block << 4 | sky
import { assertEquals } from "@std/assert";
import { BlockRegistry, EverythingRegistry } from "@/common/everything_registry.ts";
import { CHUNK_AREA, CHUNK_SIZE } from "@/common/constants.ts";
import { DEFAULT_SHAPE, type WorldShapeJson } from "@/common/world_shape.ts";
import { test_game } from "./helpers.ts";
import { section_data, test_game } from "./helpers.ts";
import type { SectionLight } from "@/client/workers/chunk_messages.ts";
import { section_index } from "@/common/section.ts";
// the middle chunk's light, with a stone roof at y 20 over x 0-7 and a light block at 12, 5, 8. y counts from the
// bottom of the world
@@ -35,29 +38,32 @@ async function mesh_light(shape: WorldShapeJson = DEFAULT_SHAPE) {
const empty = () => new Uint32Array(volume);
const chunks = [empty(), empty(), empty(), empty(), chunk, empty(), empty(), empty(), empty()];
const meshed = await new Promise<{ light: Uint8Array; max_y: number }>((resolve) => {
const meshed = await new Promise<{ light: SectionLight[]; max_y: number }>((resolve) => {
worker.onmessage = (e) => resolve(e.data);
worker.postMessage({
type: "mesh",
chunk_x: 0,
chunk_z: 0,
version: 1,
chunks,
chunks: chunks.map(section_data),
biome_colors: chunks.map(() => null),
camera: [0, 0, 0],
});
});
worker.terminate();
const light = (x: number, y: number, z: number) => {
const packed = meshed.light[at(x, y, z)];
const section = meshed.light[y >> 4];
const packed = typeof section === "number" ? section : section[section_index(x, y, z)];
return { block: packed >> 4, sky: packed & 15 };
};
return { light, size: meshed.light.length, max_y: meshed.max_y };
return { light, sections: meshed.light, max_y: meshed.max_y };
}
Deno.test("each mesh comes with its chunk's block and sky light, packed a byte per block", async () => {
const { light, size } = await mesh_light();
assertEquals(size, DEFAULT_SHAPE.volume);
const { light, sections } = await mesh_light();
assertEquals(sections.length, DEFAULT_SHAPE.height / 16);
// the open sky well above the roof is the same everywhere
assertEquals(sections.at(-1), 15);
assertEquals(light(12, 30, 8), { block: 0, sky: 15 }, "open sky");
assertEquals(light(12, 6, 8), { block: 13, sky: 15 }, "next to the light block");
// under the roof, sky light comes in sideways from the open side, a level less each block
@@ -73,8 +79,8 @@ Deno.test("without sky light, only blocks light the world", async () => {
});
Deno.test("a dimension that starts below 0 is lit the same, and meshed where its blocks are", async () => {
const { light, size, max_y } = await mesh_light({ min_y: -64, max_y: 320, sky_light: true });
assertEquals(size, CHUNK_AREA * 384);
const { light, sections, max_y } = await mesh_light({ min_y: -64, max_y: 320, sky_light: true });
assertEquals(sections.length, 384 / 16);
assertEquals(light(12, 30, 8), { block: 0, sky: 15 });
assertEquals(light(4, 10, 8).sky, 11);
assertEquals(max_y, -64 + 21, "the top of the roof");
+2 -2
View File
@@ -17,7 +17,7 @@ import { DEFAULT_SHAPE } from "@/common/world_shape.ts";
import { get_state_value, set_state_value } from "@/common/utils.ts";
import { cell_index, corner_height, fluid_quads, type FluidCell } from "@/client/workers/fluid_mesh.ts";
import { TERRAIN_VERTEX_BYTES } from "@/client/workers/chunk_messages.ts";
import { test_game } from "./helpers.ts";
import { section_data, test_game } from "./helpers.ts";
const OIL: BlockJson = {
id: "oily:oil",
@@ -194,7 +194,7 @@ async function mesh(blocks: [number, number, number, string, number?][]) {
chunk_x: 0,
chunk_z: 0,
version: 1,
chunks,
chunks: chunks.map(section_data),
biome_colors: chunks.map(() => null),
camera: [0, 0, 0],
});
+11 -3
View File
@@ -11,7 +11,8 @@ import { start_game } from "@/server/game/load_mods.ts";
import { apply_config } from "@/server/load_mods.ts";
import { AIR_ID, PROTOCOL_VERSION } from "@/common/protocol.ts";
import { decode_level_chunk } from "@/common/chunk_streaming.ts";
import { CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
import { CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
import { Section, section_index, SECTION_VOLUME, type SectionData } from "@/common/section.ts";
import type { SavedChunk, SavedChunks, WorldSave } from "@/server/game/saves.ts";
import { FluidRules } from "@/common/fluids.ts";
import { BlockRules } from "@/common/block_rules.ts";
@@ -161,11 +162,18 @@ export function sent_block(
const chunk_z = Math.floor(z / CHUNK_SIZE);
const chunk = messages.findLast((m) => m.type === "level_chunk" && m.x === chunk_x && m.z === chunk_z);
if (!chunk) return undefined;
const ly = y - join.shape.min_y;
const value = chunk.blocks[ly * CHUNK_AREA + (z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + (x - chunk_x * CHUNK_SIZE)];
const value = chunk.sections[(y - join.shape.min_y) >> 4].get(section_index(x, y, z));
return [join.blocks[value & ID_MASK], value >>> 16];
}
// a chunk's blocks in one array, y from the bottom of the world then z then x, as the sections a mesh request sends
export function section_data(blocks: Uint32Array): SectionData[] {
return Array.from(
{ length: blocks.length / SECTION_VOLUME },
(_, i) => Section.of(blocks.subarray(i * SECTION_VOLUME, (i + 1) * SECTION_VOLUME)).data,
);
}
// a stone floor at y 99 from -radius to radius on x and z, high above the terrain, for players to stand on at y 100
export function stone_floor(game: GameServer, radius = 3) {
for (let x = -radius; x <= radius; x++) {
+6 -1
View File
@@ -411,7 +411,12 @@ Deno.test({
);
const second = await run();
assert(second.length >= 49);
for (const chunk of second) assert(chunk.blocks[255 * 256] !== 0, `${chunk.x},${chunk.z} is from the save`);
for (const chunk of second) {
assert(
chunk.sections.at(-1)!.get(section_index(0, 15, 0)) !== 0,
`${chunk.x},${chunk.z} is from the save`,
);
}
} finally {
for (const dir of [sources, folder, build]) Deno.removeSync(dir, { recursive: true });
}
+1 -1
View File
@@ -90,7 +90,7 @@ Deno.test("the server's light is the same as what clients work out", async () =>
const chunks = [];
for (let dz = -1; dz <= 1; dz++) {
for (let dx = -1; dx <= 1; dx++) {
chunks.push(decode_level_chunk(world.level_chunk(CHUNK + dx, CHUNK + dz)).blocks);
chunks.push(decode_level_chunk(world.level_chunk(CHUNK + dx, CHUNK + dz)).sections);
}
}
region.fill(chunks);
+6 -3
View File
@@ -10,6 +10,7 @@ import { Frustum } from "@/client/rendering/frustum.ts";
import { group_faces_camera } from "@/client/rendering/level_renderer.ts";
import { FACE_GROUPS, type LayerMesh, TERRAIN_VERTEX_BYTES } from "@/client/workers/chunk_messages.ts";
import { decode_level_chunk } from "@/common/chunk_streaming.ts";
import type { Section } from "@/common/section.ts";
import { test_game } from "./helpers.ts";
// the matrix the renderer makes for a camera, see update_camera in client/renderer/core.ts
@@ -62,10 +63,10 @@ async function mesh_chunks(radius: number) {
visuals: [],
});
const generated = new Map<string, Uint32Array>();
const generated = new Map<string, Section[]>();
for (let x = -radius - 1; x <= radius + 1; x++) {
for (let z = -radius - 1; z <= radius + 1; z++) {
generated.set(`${x},${z}`, decode_level_chunk(game.world.level_chunk(x, z)).blocks);
generated.set(`${x},${z}`, decode_level_chunk(game.world.level_chunk(x, z)).sections);
}
}
const meshes = [];
@@ -73,7 +74,9 @@ async function mesh_chunks(radius: number) {
for (let z = -radius; z <= radius; z++) {
const chunks = [];
for (let dz = -1; dz <= 1; dz++) {
for (let dx = -1; dx <= 1; dx++) chunks.push(generated.get(`${x + dx},${z + dz}`)!.slice());
for (let dx = -1; dx <= 1; dx++) {
chunks.push(generated.get(`${x + dx},${z + dz}`)!.map((section) => section.data));
}
}
meshes.push(
await ask({
+2 -2
View File
@@ -9,7 +9,7 @@ import { DEFAULT_SHAPE } from "@/common/world_shape.ts";
import { generate_raw_chunk } from "@/common/generation.ts";
import { get_biome } from "@/common/worldgen/biomes.ts";
import { type LayerMesh, TERRAIN_VERTEX_BYTES } from "@/client/workers/chunk_messages.ts";
import { test_game } from "./helpers.ts";
import { section_data, test_game } from "./helpers.ts";
type Colors = { grass: number; foliage: number; water?: number };
@@ -53,7 +53,7 @@ async function mesh_block(id: string, x: number, middle: Colors, west: Colors |
chunk_x: 0,
chunk_z: 0,
version: 1,
chunks,
chunks: chunks.map(section_data),
biome_colors: colors,
camera: [0, 0, 0],
});
+2 -2
View File
@@ -19,7 +19,7 @@ import { AssetManager } from "@/client/assets.ts";
import { load_client_mods } from "@/client/mods.ts";
import { load_and_check } from "@/tools/check_mods.ts";
import { pack_mod } from "@/tools/pack_mod.ts";
import { copy_dir, test_game } from "./helpers.ts";
import { copy_dir, section_data, test_game } from "./helpers.ts";
const HALF = {
textures: { "0": "testmod:shaft" },
@@ -108,7 +108,7 @@ Deno.test("the chunk mesher lists the blocks with visuals, and leaves out models
chunk_x: 0,
chunk_z: 0,
version: 1,
chunks,
chunks: chunks.map(section_data),
biome_colors: chunks.map(() => null),
camera: [0, 0, 0],
});