Occlusion culling

This commit is contained in:
2026-11-07 17:59:28 -03:00
parent 22191d260c
commit 206cd38f26
7 changed files with 474 additions and 12 deletions
+2
View File
@@ -109,6 +109,8 @@ export interface SectionMesh {
};
// entities are lit from it
light: SectionLight;
// which of its faces can be seen through from which others, see client/workers/visibility.ts
visibility: number;
// the blocks in it with visuals, as pairs of the block's index in the section (see section_index) and its value
visuals: Uint32Array<ArrayBuffer>;
}
+25
View File
@@ -52,6 +52,7 @@ import {
REGION_VOID,
} from "./lighting.ts";
import { cell_index, fluid_quads, type FluidCell } from "./fluid_mesh.ts";
import { ALL_VISIBLE, section_visibility } from "./visibility.ts";
import { fluid_height, fluid_textures } from "@/common/everything_registry.ts";
type TexturesInfo = Record<string, SpriteRegion>;
@@ -87,6 +88,8 @@ const block_layers = new Uint8Array(TABLE_SIZE);
const block_cull_same = new Uint8Array(TABLE_SIZE);
// darkens the corners it touches (minecraft's ambient occlusion), full cubes with collision do
const block_occludes = new Uint8Array(TABLE_SIZE);
// blocks nothing can be seen through, drawn as whole opaque cubes, see visibility.ts
const block_hides_view = new Uint8Array(TABLE_SIZE);
// light can come around a corner past it
const block_lets_light_by = new Uint8Array(TABLE_SIZE);
const light_tables: LightTables = { opacity: new Uint8Array(TABLE_SIZE), emission: new Uint8Array(TABLE_SIZE) };
@@ -141,6 +144,7 @@ self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
block_visual.fill(0);
for (const [nid, replaces_model] of message.visuals) {
block_visual[nid] = replaces_model ? REPLACED_MODEL : HAS_VISUAL;
if (replaces_model) block_hides_view[nid] = 0;
}
textures_info = message.textures_info;
image = message.image as Texture;
@@ -164,6 +168,7 @@ self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
version,
...make_section_mesh(message.chunk_x, message.chunk_z, chunks, from, bottom, message.camera),
light: section_light(from),
visibility: visibility(chunks[4]![from >> 4], from),
};
});
post(
@@ -223,6 +228,25 @@ function section_light(from: number): SectionLight {
return same ? packed_light[0] : packed_light.slice();
}
const open_rows = new Uint16Array(256);
// which faces of the section see each other, see visibility.ts. from is its bottom in the region
function visibility(section: Section, from: number) {
if (!section.blocks) return block_hides_view[section.value & ID_MASK] ? 0 : ALL_VISIBLE;
for (let y = 0; y < SECTION_HEIGHT; y++) {
for (let z = 0; z < CHUNK_SIZE; z++) {
// the middle chunk of the region
const row = (from + y) * REGION_LAYER + (z + CHUNK_SIZE) * REGION_SIZE + CHUNK_SIZE;
let open = 0;
for (let x = 0; x < CHUNK_SIZE; x++) {
if (!block_hides_view[region.blocks[row + x]]) open |= 1 << x;
}
open_rows[y << 4 | z] = open;
}
}
return section_visibility(open_rows);
}
function post(message: FromChunkWorker, transfer: Transferable[]) {
self.postMessage(message, transfer);
}
@@ -316,6 +340,7 @@ function build_block_tables() {
block_layers[nid] = layer;
block_cull_same[nid] = (block.cull_same ?? layer === TRANSLUCENT) ? 1 : 0;
block_occludes[nid] = block.has_collision && layer !== TRANSLUCENT && is_full_cube(block) ? 1 : 0;
block_hides_view[nid] = layer === SOLID && is_full_cube(block) ? 1 : 0;
block_lets_light_by[nid] = layer !== SOLID || opacity === 0 ? 1 : 0;
light_tables.opacity[nid] = opacity;
light_tables.emission[nid] = block_light_emission(block);
+129
View File
@@ -0,0 +1,129 @@
// 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;
}