// entities are lit from the light the chunk worker sends back with each mesh, one byte per block: block << 4 | sky import { assertEquals } from "@std/assert"; import { BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts"; import { CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE } from "$/common/constants.ts"; import { test_game } from "./helpers.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 async function mesh_light() { await test_game("mods"); const blocks = EverythingRegistry.get_registry("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", blocks_registry: JSON.parse(JSON.stringify(blocks)), models: {}, textures_info: { "engine:missing": { x: 0, y: 0, width: 16, height: 16 } }, image: { width: 1024, height: 1024 }, }); const chunk = new Uint32Array(CHUNK_AREA * CHUNK_HEIGHT); 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(CHUNK_AREA * CHUNK_HEIGHT); const chunks = [empty(), empty(), empty(), empty(), chunk, empty(), empty(), empty(), empty()]; const meshed = await new Promise<{ light: Uint8Array }>((resolve) => { worker.onmessage = (e) => resolve(e.data); worker.postMessage({ type: "mesh", chunk_x: 0, chunk_z: 0, version: 1, chunks, 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)]; return { block: packed >> 4, sky: packed & 15 }; }; return { light, size: meshed.light.length }; } 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, CHUNK_AREA * CHUNK_HEIGHT); 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"); });