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
+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 {