Meshing and lighting per section

This commit is contained in:
2026-11-07 11:41:21 -03:00
parent 71cbf901a5
commit b726ce9695
21 changed files with 736 additions and 544 deletions
+42 -34
View File
@@ -1,7 +1,13 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
import { type Chunk, CHUNK_SIZE, type ChunkMesh, ClientLevel } from "@/client/level/client_level.ts";
import {
type Chunk,
CHUNK_SIZE,
type ChunkMesh,
ClientLevel,
type RenderSection,
} from "@/client/level/client_level.ts";
import { profiler } from "@/client/profiler.ts";
import { Camera } from "@/client/camera.ts";
import { AssetManager } from "@/client/assets.ts";
@@ -42,15 +48,17 @@ import { draw_visuals } from "./visuals.ts";
// draws the level, like minecraft's LevelRenderer: terrain in layers, block breaking and entities
export class LevelRenderer {
frustum = new Frustum();
// chunks in view this frame, worked out once for all the layers
#visible: Chunk[] = [];
// sections in view this frame, near to far, worked out once for all the layers
#visible: RenderSection[] = [];
// the chunks they're in
#visible_chunks = new Set<Chunk>();
// 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 };
// entities drawn this frame, for the f3 performance window
#drawn_entities = 0;
get visible_chunks() {
get visible_sections() {
return this.#visible.length;
}
@@ -65,8 +73,8 @@ export class LevelRenderer {
for (const layer of ["solid", "cutout"] as const) {
if (!this.draw[layer]) continue;
profiler.section(layer, () => {
for (const chunk of this.#visible) {
const mesh = chunk.meshes[layer];
for (const section of this.#visible) {
const mesh = section.meshes[layer];
if (mesh) {
draw_facing_camera(layer, mesh, camera);
}
@@ -75,52 +83,44 @@ export class LevelRenderer {
}
}
// asks for meshes and sorting, then works out which chunks are in view, near to far
// asks for meshes and sorting, then works out which sections are in view, near to far
#find_visible(level: ClientLevel, camera: Camera) {
level.request_meshes(camera);
level.update_translucent_sorting(camera);
this.frustum.update(view_projection());
this.#visible.length = 0;
for (const chunk of level.chunks.values()) {
const drawn = Object.keys(chunk.meshes).length > 0 || level.visuals.has(chunk.x, chunk.z);
if (drawn && this.#in_view(chunk)) {
this.#visible.push(chunk);
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);
}
}
// 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
const distance_sq = (chunk: Chunk) => {
const dx = (chunk.x + 0.5) * CHUNK_SIZE - camera.x;
const dz = (chunk.z + 0.5) * CHUNK_SIZE - camera.z;
return dx * dx + dz * dz;
};
this.#visible.sort((a, b) => distance_sq(a) - distance_sq(b));
visible.sort((a, b) => a.distance - b.distance);
this.#visible = visible.map(({ section }) => section);
}
// translucent terrain, drawn after entities so they show through water and glass.
// chunks go back to front, and each chunk's quads are already sorted back to front
// translucent terrain, drawn after entities so they show through water and glass. sections go back to front, and
// each section's quads are already sorted back to front
render_translucent(_level: ClientLevel, camera: Camera) {
if (!this.draw.translucent) return;
const distance_sq = (x: number, z: number) => {
const dx = (x + 0.5) * CHUNK_SIZE - camera.x;
const dz = (z + 0.5) * CHUNK_SIZE - camera.z;
return dx * dx + dz * dz;
};
const chunks = this.#visible
.filter((chunk) => chunk.meshes.translucent)
.map((chunk) => ({ mesh: chunk.meshes.translucent!, distance: distance_sq(chunk.x, chunk.z) }))
const sections = this.#visible
.filter((section) => section.meshes.translucent)
.map((section) => ({ mesh: section.meshes.translucent!, distance: distance_sq(section, camera) }))
.sort((a, b) => b.distance - a.distance);
for (const { mesh } of chunks) {
for (const { mesh } of sections) {
draw_terrain("translucent", mesh.vertex_buffer, 0, mesh.quad_count, mesh.index_buffer);
}
}
#in_view(chunk: Chunk) {
const x = chunk.x * CHUNK_SIZE;
const z = chunk.z * CHUNK_SIZE;
return this.frustum.intersects_box(x, chunk.min_y, z, x + CHUNK_SIZE, chunk.max_y, z + CHUNK_SIZE);
#in_view(section: RenderSection) {
const x = section.chunk.x * CHUNK_SIZE;
const z = section.chunk.z * CHUNK_SIZE;
return this.frustum.intersects_box(x, section.min_y, z, x + CHUNK_SIZE, section.max_y, z + CHUNK_SIZE);
}
// every entity in view but the one the camera is in
@@ -178,7 +178,7 @@ export class LevelRenderer {
// blocks drawn with code, see client/rendering/visuals.ts. the ones in chunks in view get their on_frame first
render_visuals(level: ClientLevel, camera: Camera, partial_tick: number) {
if (!this.draw.visuals) return;
level.visuals.frame(this.#visible, level.ticks + partial_tick);
level.visuals.frame(this.#visible_chunks, level.ticks + partial_tick);
flush_batch();
set_current_texture(level.image.tex);
draw_visuals(level.visuals, level.image, [camera.x, camera.y, camera.z]);
@@ -251,6 +251,14 @@ export class LevelRenderer {
#baked = new Map<number, BakedQuad[]>();
}
// from the camera to the middle of the section's bounds, squared
function distance_sq(section: RenderSection, camera: Camera) {
const dx = (section.chunk.x + 0.5) * CHUNK_SIZE - camera.x;
const dy = (section.min_y + section.max_y) / 2 - camera.y;
const dz = (section.chunk.z + 0.5) * CHUNK_SIZE - camera.z;
return dx * dx + dy * dy + dz * dz;
}
// how far models can stick out of their entity's box, like a swinging arm or a dropped item bobbing
const ENTITY_CULL_MARGIN = 0.5;