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

82 lines
3.9 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// occlusion culling, like sodium's OcclusionCuller but simpler: which sections the camera can see into, going out
// from its own section a face at a time. a section is only left through faces that see the ones it was entered
// through (see client/workers/visibility.ts), and never back towards the camera, so caves under the ground and the
// inside of hills aren't drawn
import { type Chunk, CHUNK_SIZE, type ClientLevel, type RenderSection } from "@/client/level/client_level.ts";
import { SECTION_HEIGHT } from "@/common/world_shape.ts";
import { ALL_DIRECTIONS, exits } from "@/client/workers/visibility.ts";
// tells searches apart, so sections don't need clearing between them
let searches = 0;
// the sections with something to draw the camera at x y z can see, near to far: in the order the search reaches them,
// a step at a time. undefined when the camera's chunk isn't loaded
export function reachable_sections(level: ClientLevel, x: number, y: number, z: number) {
const chunk_x = Math.floor(x / CHUNK_SIZE);
const chunk_z = Math.floor(z / CHUNK_SIZE);
const chunk = level.get_chunk(chunk_x, chunk_z);
if (!chunk) return undefined;
// above or below the world, from the section nearest to the camera
const count = chunk.sections.length;
const index = Math.min(count - 1, Math.max(0, Math.floor((y - level.shape.min_y) / SECTION_HEIGHT)));
// the loaded chunks in a grid, since looking each step's up in the level's map is most of what a search costs
let min_x = chunk_x, max_x = chunk_x, min_z = chunk_z, max_z = chunk_z;
for (const { x, z } of level.chunks.values()) {
min_x = Math.min(min_x, x);
max_x = Math.max(max_x, x);
min_z = Math.min(min_z, z);
max_z = Math.max(max_z, z);
}
const width = max_x - min_x + 1;
const depth = max_z - min_z + 1;
const grid: (Chunk | undefined)[] = new Array(width * depth);
for (const chunk of level.chunks.values()) grid[(chunk.x - min_x) * depth + chunk.z - min_z] = chunk;
const chunk_at = (x: number, z: number) =>
x < min_x || x > max_x || z < min_z || z > max_z ? undefined : grid[(x - min_x) * depth + z - min_z];
const search = ++searches;
const start = chunk.sections[index];
start.search = search;
const queue = [start];
const found: RenderSection[] = [];
// adds the section a step goes into, entered through the face opposite the step's direction
const step = (neighbor: RenderSection | undefined, direction: number) => {
if (!neighbor) return;
// every step goes one further from the camera's section, so all the ways into a section are known by the time
// it's taken from the queue
if (neighbor.search !== search) {
neighbor.search = search;
neighbor.incoming = 0;
queue.push(neighbor);
}
neighbor.incoming |= 1 << (direction ^ 1);
};
for (let i = 0; i < queue.length; i++) {
const section = queue[i];
// with no quads or visuals its bounds are empty, see ClientLevel.drawn
if (section.min_y <= section.max_y) found.push(section);
// the camera's section can be left any way, even when the camera is inside a block
let out = section === start ? ALL_DIRECTIONS : exits(section.visibility, section.incoming);
if (!out) continue;
// away from the camera's section on every axis
const { x, z, sections } = section.chunk;
const dx = x - chunk_x;
const dy = section.index - index;
const dz = z - chunk_z;
out &= (dx <= 0 ? 1 : 0) | (dx >= 0 ? 2 : 0) | (dy <= 0 ? 4 : 0) | (dy >= 0 ? 8 : 0) | (dz <= 0 ? 16 : 0) |
(dz >= 0 ? 32 : 0);
if (out & 1) step(chunk_at(x - 1, z)?.sections[section.index], 0);
if (out & 2) step(chunk_at(x + 1, z)?.sections[section.index], 1);
if (out & 4) step(sections[section.index - 1], 2);
if (out & 8) step(sections[section.index + 1], 3);
if (out & 16) step(chunk_at(x, z - 1)?.sections[section.index], 4);
if (out & 32) step(chunk_at(x, z + 1)?.sections[section.index], 5);
}
return found;
}