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
+31 -11
View File
@@ -44,6 +44,8 @@ import { FACE_GROUPS, UNALIGNED_GROUP } from "@/client/workers/chunk_messages.ts
import { FACE_AXIS, FACE_NORMALS } from "@/client/workers/translucent_sort.ts";
import { Frustum } from "./frustum.ts";
import { draw_visuals } from "./visuals.ts";
import { reachable_sections } from "./occlusion.ts";
import { SECTION_HEIGHT } from "@/common/world_shape.ts";
// draws the level, like minecraft's LevelRenderer: terrain in layers, block breaking and entities
export class LevelRenderer {
@@ -52,6 +54,9 @@ export class LevelRenderer {
#visible: RenderSection[] = [];
// the chunks they're in
#visible_chunks = new Set<Chunk>();
// what the camera could see the last time it was searched for, which only changes when it moves to another section
// or the sections change, see occlusion.ts
#reachable: { sections: RenderSection[] | undefined; level: ClientLevel; version: number; key: string } | undefined;
// what to draw, switched off in the f3 performance window to see what each costs
draw = { solid: true, cutout: true, translucent: true, entities: true, visuals: true };
@@ -83,24 +88,39 @@ export class LevelRenderer {
}
}
// asks for meshes and sorting, then works out which sections are in view, near to far
// asks for meshes and sorting, then works out which sections are in view, near to far like sodium, so what's in
// front fills the depth buffer first and the gpu can skip shading what's behind it
#find_visible(level: ClientLevel, camera: Camera) {
level.request_meshes(camera);
level.update_translucent_sorting(camera);
this.frustum.update(view_projection());
const visible: { section: RenderSection; distance: number }[] = [];
this.#visible_chunks.clear();
for (const section of level.drawn) {
if (this.#in_view(section)) {
visible.push({ section, distance: distance_sq(section, camera) });
this.#visible_chunks.add(section.chunk);
}
const reachable = this.#reachable_sections(level, camera);
if (reachable) {
this.#visible = reachable.filter((section) => this.#in_view(section));
} else {
// the camera's chunk isn't loaded, so there's nothing to search from
this.#visible = [...level.drawn].filter((section) => this.#in_view(section))
.map((section) => ({ section, distance: distance_sq(section, camera) }))
.sort((a, b) => a.distance - b.distance)
.map(({ section }) => section);
}
// near to far, like sodium, so what's in front fills the depth buffer first and the gpu can skip shading what's
// behind it
visible.sort((a, b) => a.distance - b.distance);
this.#visible = visible.map(({ section }) => section);
for (const section of this.#visible) this.#visible_chunks.add(section.chunk);
}
#reachable_sections(level: ClientLevel, camera: Camera) {
const key = `${Math.floor(camera.x / CHUNK_SIZE)},${Math.floor(camera.y / SECTION_HEIGHT)},${
Math.floor(camera.z / CHUNK_SIZE)
}`;
const last = this.#reachable;
if (last?.level === level && last.version === level.sections_version && last.key === key) return last.sections;
const sections = profiler.section(
"occlusion",
() => reachable_sections(level, camera.x, camera.y, camera.z),
);
this.#reachable = { sections, level, version: level.sections_version, key };
return sections;
}
// translucent terrain, drawn after entities so they show through water and glass. sections go back to front, and
+81
View File
@@ -0,0 +1,81 @@
// 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;
}