96 lines
4.9 KiB
TypeScript
96 lines
4.9 KiB
TypeScript
// 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, 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 { 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
|
|
// bottom of the world
|
|
async function mesh_light(shape: WorldShapeJson = DEFAULT_SHAPE) {
|
|
const volume = CHUNK_AREA * (shape.max_y - shape.min_y);
|
|
await test_game("mods");
|
|
const blocks = EverythingRegistry.get_registry<BlockRegistry>("blocks");
|
|
const nid = (id: string) => blocks.findIndex((b) => b?.id === id);
|
|
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++) {
|
|
for (let z = 0; z < CHUNK_SIZE; z++) chunk[at(x, 20, z)] = nid("bworld:stone");
|
|
}
|
|
chunk[at(12, 5, 8)] = nid("bworld:light");
|
|
const empty = () => new Uint32Array(volume);
|
|
const chunks = [empty(), empty(), empty(), empty(), chunk, empty(), empty(), empty(), empty()];
|
|
|
|
const meshed = await mesh_column(worker, shape, chunks);
|
|
worker.terminate();
|
|
const light = (x: number, y: number, z: number) => {
|
|
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, 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, 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
|
|
assertEquals(light(7, 10, 8).sky, 14);
|
|
assertEquals(light(4, 10, 8).sky, 11);
|
|
assertEquals(light(4, 20, 8), { block: 0, sky: 0 }, "inside the stone");
|
|
});
|
|
|
|
Deno.test("without sky light, only blocks light the world", async () => {
|
|
const { light } = await mesh_light({ min_y: 0, max_y: 256, sky_light: false });
|
|
assertEquals(light(12, 30, 8), { block: 0, sky: 0 }, "the open air is dark");
|
|
assertEquals(light(12, 6, 8), { block: 13, sky: 0 }, "next to the light block");
|
|
});
|
|
|
|
Deno.test("a dimension that starts below 0 is lit the same, and meshed where its blocks are", async () => {
|
|
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");
|
|
});
|
|
|
|
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");
|
|
});
|