Files
bworld/client/workers/visibility.ts
T
2026-11-07 17:59:28 -03:00

130 lines
4.2 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// which faces of a section can be seen through from which others, for occlusion culling (see
// client/rendering/occlusion.ts). worked out by the chunk workers as they mesh a section
// the 6 directions out of a section, and the faces they leave through, are a bit each: -x, +x, -y, +y, -z, +z. a
// direction's opposite is direction ^ 1
export const ALL_DIRECTIONS = 0b111111;
// a bit for each of the 15 pairs of different faces
const PAIR_BITS = new Uint16Array(36);
{
let bit = 0;
for (let a = 0; a < 6; a++) {
for (let b = a + 1; b < 6; b++) {
PAIR_BITS[a * 6 + b] = PAIR_BITS[b * 6 + a] = 1 << bit++;
}
}
}
// every face sees every other, like a section of air, or one that hasn't been meshed yet
export const ALL_VISIBLE = (1 << 15) - 1;
// the directions a section can be left in when it was entered through the faces in incoming
export function exits(visibility: number, incoming: number) {
// most sections are all air or all stone. entered through one face, all the others
if (visibility === ALL_VISIBLE) return incoming & (incoming - 1) ? ALL_DIRECTIONS : ALL_DIRECTIONS & ~incoming;
if (visibility === 0) return 0;
let out = 0;
for (let a = 0; a < 6; a++) {
if (!(incoming & (1 << a))) continue;
for (let b = 0; b < 6; b++) {
if (visibility & PAIR_BITS[a * 6 + b]) out |= 1 << b;
}
}
return out;
}
const unvisited = new Uint16Array(256);
const region = new Uint16Array(256);
// a section's visibility from the cells that can be seen through, by row (y << 4 | z, a bit per x). each connected
// region of them connects every pair of faces it touches. a region grows a row at a time, each row filled along its
// open runs at once, until it stops growing
export function section_visibility(open: Uint16Array) {
let count = 0;
for (let r = 0; r < 256; r++) count += bit_count(open[r]);
if (count === 4096) return ALL_VISIBLE;
if (count === 0) return 0;
unvisited.set(open);
let result = 0;
for (let start = 0; start < 256; start++) {
while (unvisited[start] !== 0) {
const seed = unvisited[start] & -unvisited[start];
region[start] = fill_row(seed, unvisited[start]);
let lo = start;
let hi = start;
let grown = true;
while (grown) {
grown = false;
const first = Math.max(0, lo - 16);
const last = Math.min(255, hi + 16);
// up then back down, so a region that winds back reaches its far end in fewer sweeps
for (let i = 0; i < 2 * (last - first + 1); i++) {
const r = i <= last - first ? first + i : last - (i - (last - first + 1));
let near = region[r];
if (r >= 16) near |= region[r - 16];
if (r < 240) near |= region[r + 16];
if (r & 15) near |= region[r - 1];
if ((r & 15) !== 15) near |= region[r + 1];
const reached = near & unvisited[r];
if (reached & ~region[r]) {
region[r] = fill_row(reached, unvisited[r]);
if (r < lo) lo = r;
if (r > hi) hi = r;
grown = true;
}
}
}
let faces = 0;
for (let r = lo; r <= hi; r++) {
const row = region[r];
if (row === 0) continue;
region[r] = 0;
unvisited[r] &= ~row;
const y = r >> 4;
const z = r & 15;
if (row & 1) faces |= 1;
if (row & 0x8000) faces |= 2;
if (y === 0) faces |= 4;
if (y === 15) faces |= 8;
if (z === 0) faces |= 16;
if (z === 15) faces |= 32;
}
// one region touching every face connects them all, nothing else can add to it
if (faces === ALL_DIRECTIONS) return ALL_VISIBLE;
for (let a = 0; a < 6; a++) {
for (let b = a + 1; b < 6; b++) {
if (faces & (1 << a) && faces & (1 << b)) result |= PAIR_BITS[a * 6 + b];
}
}
}
}
return result;
}
// the cells of open connected to seeds within a row, filling each way and doubling the reach at each step
function fill_row(seeds: number, open: number) {
let up = seeds;
let through_up = open;
let down = seeds;
let through_down = open;
for (let step = 1; step <= 8; step <<= 1) {
up |= through_up & (up << step);
through_up &= through_up << step;
down |= through_down & (down >> step);
through_down &= through_down >> step;
}
return up | down;
}
function bit_count(row: number) {
let count = 0;
for (; row; row &= row - 1) count++;
return count;
}