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;
+35 -22
View File
@@ -2,8 +2,8 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// blocks drawn with code, like a shaft that turns, see ctx.render.register_visual. like flywheel's visuals: one per
// block, made when its chunk is meshed and kept by chunk. what they draw are instances of models, and every instance of
// a model is drawn in one draw call from a buffer of where each is, how it spins and its light (see vs_instance in
// block, made when its section is meshed and kept by chunk. what they draw are instances of models, and every instance
// of a model is drawn in one draw call from a buffer of where each is, how it spins and its light (see vs_instance in
// client/renderer/core.ts). the gpu turns rotating ones by the time, so a shaft that keeps turning costs nothing per
// frame. an item is a model too, one per way an item can look, and a fluid's box is a model of its own
import { mat4, vec3 } from "gl-matrix";
@@ -29,6 +29,7 @@ import {
states_of,
} from "@/common/block_models.ts";
import { CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
import { SECTION_HEIGHT } from "@/common/world_shape.ts";
import type { ModelFace } from "@/common/mod_data.ts";
import { ItemStack } from "@/common/inventory.ts";
import { chunk_key, item_texture } from "@/common/utils.ts";
@@ -186,7 +187,7 @@ interface Entry {
// a level's visuals
export class Visuals {
// by chunk_key, then the block's index in its chunk counted from y 0, see #entry
// by chunk_key, then the block's index in its chunk counted from y 0, see #index
#chunks = new Map<number, Map<number, Entry>>();
#level: VisualsLevel;
// baked once per level, since a level's mods don't change
@@ -213,32 +214,41 @@ export class Visuals {
yield* this.#fluid_groups;
}
// the blocks with visuals a chunk was meshed with, as pairs of the block's index in the chunk (from min_y, the
// bottom of the world) and its value. visuals for blocks that are gone or changed go, and new ones are made. the
// chunk's light came with its mesh, so the ones that stay take it
meshed(chunk_x: number, chunk_z: number, min_y: number, found: Uint32Array) {
// the blocks with visuals a chunk's section was meshed with, as pairs of the block's index in the section (see
// section_index) and its value. bottom is the section's lowest y. visuals in it for blocks that are gone or
// changed go, and new ones are made. the section's light came with its mesh, so the ones that stay take it
meshed(chunk_x: number, chunk_z: number, bottom: number, found: Uint32Array) {
const key = chunk_key(chunk_x, chunk_z);
const old = this.#chunks.get(key) ?? new Map<number, Entry>();
const kept = new Map<number, Entry>();
const entries = this.#chunks.get(key) ?? new Map<number, Entry>();
const old = new Map<number, Entry>();
for (const [index, entry] of entries) {
if (entry.y >= bottom && entry.y < bottom + SECTION_HEIGHT) old.set(index, entry);
}
for (let i = 0; i < found.length; i += 2) {
const local = found[i];
const index = local + min_y * CHUNK_AREA;
const x = chunk_x * CHUNK_SIZE + (local & 15);
const y = bottom + (local >> 8);
const z = chunk_z * CHUNK_SIZE + ((local >> 4) & 15);
const index = this.#index(x, y, z);
const value = found[i + 1];
const entry = old.get(index);
old.delete(index);
if (entry?.value === value) {
this.#relight(entry);
kept.set(index, entry);
continue;
}
if (entry) this.#delete(entry);
const x = chunk_x * CHUNK_SIZE + (local % CHUNK_SIZE);
const z = chunk_z * CHUNK_SIZE + Math.floor(local / CHUNK_SIZE) % CHUNK_SIZE;
const made = this.#create(x, min_y + Math.floor(local / CHUNK_AREA), z, value);
if (made) kept.set(index, made);
if (entry) {
this.#delete(entry);
entries.delete(index);
}
const made = this.#create(x, y, z, value);
if (made) entries.set(index, made);
}
for (const entry of old.values()) this.#delete(entry);
if (kept.size > 0) this.#chunks.set(key, kept);
for (const [index, entry] of old) {
this.#delete(entry);
entries.delete(index);
}
if (entries.size > 0) this.#chunks.set(key, entries);
else this.#chunks.delete(key);
}
@@ -280,10 +290,13 @@ export class Visuals {
}
#entry(x: number, y: number, z: number) {
const chunk_x = Math.floor(x / CHUNK_SIZE);
const chunk_z = Math.floor(z / CHUNK_SIZE);
const index = y * CHUNK_AREA + (z - chunk_z * CHUNK_SIZE) * CHUNK_SIZE + (x - chunk_x * CHUNK_SIZE);
return this.#chunks.get(chunk_key(chunk_x, chunk_z))?.get(index);
const key = chunk_key(Math.floor(x / CHUNK_SIZE), Math.floor(z / CHUNK_SIZE));
return this.#chunks.get(key)?.get(this.#index(x, y, z));
}
// a block's place in its chunk's map
#index(x: number, y: number, z: number) {
return y * CHUNK_AREA + (z & 15) * CHUNK_SIZE + (x & 15);
}
#create(x: number, y: number, z: number, value: number): Entry | undefined {