// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // occlusion culling: the chunk workers work out which faces of a section see each other (client/workers/visibility.ts) // and the renderer only draws the sections the camera can see into through them (client/rendering/occlusion.ts) import { assert, assertEquals } from "@std/assert"; import { ALL_VISIBLE, exits, section_visibility } from "@/client/workers/visibility.ts"; import { reachable_sections } from "@/client/rendering/occlusion.ts"; import { type BlockRegistry, EverythingRegistry } from "@/common/everything_registry.ts"; import { CHUNK_AREA, CHUNK_SIZE } from "@/common/constants.ts"; import { ChunkColumn } from "@/common/section.ts"; import { DEFAULT_SHAPE } from "@/common/world_shape.ts"; import { chunk_worker, mesh_column, test_game } from "./helpers.ts"; import { client_level } from "./client_level.ts"; // the faces a section can be left through from each face, as [-x, +x, -y, +y, -z, +z] masks const exits_by_face = (visibility: number) => Array.from({ length: 6 }, (_, face) => exits(visibility, 1 << face)); // the same, found by flood filling the open cells one at a time function flood_filled(open: (x: number, y: number, z: number) => boolean) { const seen = new Uint8Array(4096); const result = new Array(6).fill(0); for (let start = 0; start < 4096; start++) { if (seen[start] || !open(start & 15, start >> 8, (start >> 4) & 15)) continue; seen[start] = 1; const stack = [start]; let faces = 0; while (stack.length) { const cell = stack.pop()!; const [x, y, z] = [cell & 15, cell >> 8, (cell >> 4) & 15]; faces |= (x === 0 ? 1 : 0) | (x === 15 ? 2 : 0) | (y === 0 ? 4 : 0) | (y === 15 ? 8 : 0) | (z === 0 ? 16 : 0) | (z === 15 ? 32 : 0); for ( const [nx, ny, nz] of [[x - 1, y, z], [x + 1, y, z], [x, y - 1, z], [x, y + 1, z], [x, y, z - 1], [ x, y, z + 1, ]] ) { if (Math.min(nx, ny, nz) < 0 || Math.max(nx, ny, nz) > 15) continue; const next = ny << 8 | nz << 4 | nx; if (!seen[next] && open(nx, ny, nz)) { seen[next] = 1; stack.push(next); } } } for (let a = 0; a < 6; a++) { if (faces & (1 << a)) result[a] |= faces & ~(1 << a); } } return result; } function rows(open: (x: number, y: number, z: number) => boolean) { const rows = new Uint16Array(256); for (let y = 0; y < 16; y++) { for (let z = 0; z < 16; z++) { for (let x = 0; x < 16; x++) if (open(x, y, z)) rows[y << 4 | z] |= 1 << x; } } return rows; } Deno.test("a section's faces see each other through the cells that can be seen through", () => { assertEquals(section_visibility(rows(() => true)), ALL_VISIBLE); assertEquals(section_visibility(rows(() => false)), 0); // a wall across x: -x and +x don't see each other, the other faces see both halves assertEquals(exits_by_face(section_visibility(rows((x) => x !== 8))), [ 0b111100, 0b111100, 0b111011, 0b110111, 0b101111, 0b011111, ]); // a tunnel along z, and nothing else assertEquals(exits_by_face(section_visibility(rows((x, y) => x === 3 && y === 3))), [ 0, 0, 0, 0, 0b100000, 0b010000, ]); // random sections, from mostly open to mostly closed, and some with winding walls let seed = 12345; const random = () => (seed = (seed * 1103515245 + 12345) % 2147483648) / 2147483648; for (let i = 0; i < 300; i++) { const density = 0.2 + (i % 7) * 0.1; const cells = Array.from({ length: 4096 }, () => random() < density); const open = i % 3 === 0 ? (x: number, y: number, z: number) => (x * 7 + y * 3 + z * 5) % 11 !== 0 || cells[y << 8 | z << 4 | x] : (x: number, y: number, z: number) => cells[y << 8 | z << 4 | x]; assertEquals(exits_by_face(section_visibility(rows(open))), flood_filled(open), `section ${i}`); } }); Deno.test("solid full blocks hide what's behind them in a meshed section, and others don't", async () => { await test_game("mods"); const blocks = EverythingRegistry.get_registry("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; // stone up to 63, a stone floor across section 6 at y 100, and section 8 full of water const ground = () => { const blocks = new Uint32Array(DEFAULT_SHAPE.volume).fill(nid("bworld:stone"), 0, at(0, 64, 0)); blocks.fill(nid("bworld:stone"), at(0, 100, 0), at(0, 101, 0)); blocks.fill(nid("bworld:water"), at(0, 128, 0), at(0, 144, 0)); return blocks; }; const chunks = Array.from({ length: 9 }, ground); const worker = chunk_worker(DEFAULT_SHAPE); const { sections } = await mesh_column(worker, DEFAULT_SHAPE, chunks); worker.terminate(); assertEquals(sections[2].visibility, 0); assertEquals(sections[5].visibility, ALL_VISIBLE); assertEquals(sections[8].visibility, ALL_VISIBLE); // the floor splits its section in two, which each see the sides but not each other assertEquals(exits(sections[6].visibility, 0b0100), 0b110011); assertEquals(exits(sections[6].visibility, 0b1000), 0b110011); }); // a level of 7x7 chunks of ground up to section 3 with open air above it, none of it meshed, and every section with // something to draw. the visibility a mesh gives is set by hand: none for the ground, every face for the air async function level_with_ground() { await test_game("mods"); const { level } = client_level(DEFAULT_SHAPE); for (let x = -3; x <= 3; x++) { for (let z = -3; z <= 3; z++) { const air = new Uint32Array(DEFAULT_SHAPE.volume); level.set_chunk( ChunkColumn.from_flat(x, z, DEFAULT_SHAPE, air, new Array(CHUNK_AREA).fill("bworld:plains")), ); for (const section of level.get_chunk(x, z)!.sections) { section.visibility = section.index <= 3 ? 0 : ALL_VISIBLE; section.min_y = DEFAULT_SHAPE.min_y + section.index * 16; section.max_y = section.min_y + 16; level.drawn.add(section); } } } const reached = (x: number, y: number, z: number) => new Set( reachable_sections(level, x, y, z)!.map((section) => `${section.chunk.x},${section.index},${section.chunk.z}` ), ); return { level, reached }; } Deno.test("from above the ground, the camera sees the ground's top and not what's under it", async () => { const { level, reached } = await level_with_ground(); // a cave in section 1 of chunk 0, 0, open on every face but closed in by the ground around it level.get_chunk(0, 0)!.sections[1].visibility = ALL_VISIBLE; const seen = reached(8, 100, 8); assert(seen.has("3,3,-3") && seen.has("0,3,0") && seen.has("-2,15,1"), "the ground's top and the sky"); assert(!seen.has("0,2,0") && !seen.has("0,1,0") && !seen.has("2,2,2"), "under the ground"); // and from inside the cave, only it and what closes it in const in_cave = reached(8, 24, 8); assertEquals( [...in_cave].sort(), ["-1,1,0", "0,0,0", "0,1,-1", "0,1,0", "0,1,1", "0,2,0", "1,1,0"], ); level.dispose(); }); Deno.test("the camera doesn't see back around what's in front of it", async () => { const { level, reached } = await level_with_ground(); // a wall on the ground two chunks to +x, across the whole level and up to section 8 for (let z = -3; z <= 3; z++) { for (const section of level.get_chunk(2, z)!.sections) { if (section.index <= 8) section.visibility = 0; } } const seen = reached(8, 70, 8); assert(seen.has("2,4,0"), "the wall"); assert(!seen.has("3,4,0"), "right behind the wall"); assert(seen.has("3,9,0"), "above it"); level.dispose(); });