Meshing and lighting per section

This commit is contained in:
2026-11-07 11:41:21 -03:00
parent 71cbf901a5
commit b726ce9695
21 changed files with 736 additions and 544 deletions
+33 -25
View File
@@ -7,8 +7,7 @@ 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 { section_data, test_game } from "./helpers.ts";
import type { SectionLight } from "@/client/workers/chunk_messages.ts";
import { chunk_worker, mesh_column, test_game } from "./helpers.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
@@ -18,17 +17,7 @@ async function mesh_light(shape: WorldShapeJson = DEFAULT_SHAPE) {
await test_game("mods");
const blocks = EverythingRegistry.get_registry<BlockRegistry>("blocks");
const nid = (id: string) => blocks.findIndex((b) => b?.id === id);
const worker = new Worker(new URL("../client/workers/chunk_worker.ts", import.meta.url).href, { type: "module" });
worker.postMessage({
type: "init",
shape,
blocks_registry: JSON.parse(JSON.stringify(blocks)),
models: {},
textures_info: { "cubi:missing": { x: 0, y: 0, width: 16, height: 16 } },
image: { width: 1024, height: 1024 },
visuals: [],
});
const worker = chunk_worker(shape);
const chunk = new Uint32Array(volume);
const at = (x: number, y: number, z: number) => y * CHUNK_AREA + z * CHUNK_SIZE + x;
for (let x = 0; x < 8; x++) {
@@ -38,18 +27,7 @@ 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: 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.map(section_data),
biome_colors: chunks.map(() => null),
camera: [0, 0, 0],
});
});
const meshed = await mesh_column(worker, shape, chunks);
worker.terminate();
const light = (x: number, y: number, z: number) => {
const section = meshed.light[y >> 4];
@@ -85,3 +63,33 @@ Deno.test("a dimension that starts below 0 is lit the same, and meshed where its
assertEquals(light(4, 10, 8).sky, 11);
assertEquals(max_y, -64 + 21, "the top of the roof");
});
Deno.test("a section meshed on its own is lit and drawn the same as when its whole column is", async () => {
await test_game("mods");
const blocks = EverythingRegistry.get_registry<BlockRegistry>("blocks");
const nid = (id: string) => blocks.findIndex((b) => b?.id === id);
const at = (x: number, y: number, z: number) => y * CHUNK_AREA + z * CHUNK_SIZE + x;
// ground up to 40 everywhere, and in the middle chunk a roof far above it over x 0-7, which shades sections well
// below it, and a light just under a section's top, which lights the one above
const ground = () => new Uint32Array(DEFAULT_SHAPE.volume).fill(nid("bworld:stone"), 0, at(0, 41, 0));
const chunks = Array.from({ length: 9 }, ground);
for (let x = 0; x < 8; x++) {
for (let z = 0; z < CHUNK_SIZE; z++) chunks[4][at(x, 200, z)] = nid("bworld:stone");
}
chunks[4][at(12, 47, 8)] = nid("bworld:light");
chunks[4][at(3, 41, 3)] = nid("bworld:glass");
const worker = chunk_worker(DEFAULT_SHAPE);
const whole = await mesh_column(worker, DEFAULT_SHAPE, chunks);
for (const [index, expected] of whole.sections.entries()) {
const [alone] = (await mesh_column(worker, DEFAULT_SHAPE, chunks, { sections: [index] })).sections;
assertEquals(alone.light, expected.light, `section ${index}'s light`);
for (const layer of ["solid", "cutout", "translucent"] as const) {
assertEquals(alone[layer].vertices, expected[layer].vertices, `section ${index}'s ${layer} quads`);
}
}
worker.terminate();
// the roof's shade and the light are in the sections looked at
assertEquals(typeof whole.light[2], "object");
assertEquals(typeof whole.light[3], "object");
});