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
+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");