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