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
+16 -48
View File
@@ -5,13 +5,17 @@
// would have shown
import { assert, assertEquals } from "@std/assert";
import { mat4 } from "gl-matrix";
import { BlockRegistry, EverythingRegistry } from "@/common/everything_registry.ts";
import { Frustum } from "@/client/rendering/frustum.ts";
import { group_faces_camera } from "@/client/rendering/level_renderer.ts";
import { FACE_GROUPS, type LayerMesh, TERRAIN_VERTEX_BYTES } from "@/client/workers/chunk_messages.ts";
import {
FACE_GROUPS,
type LayerMesh,
type SectionMesh,
TERRAIN_VERTEX_BYTES,
} from "@/client/workers/chunk_messages.ts";
import { decode_level_chunk } from "@/common/chunk_streaming.ts";
import type { Section } from "@/common/section.ts";
import { test_game } from "./helpers.ts";
import { to_flat } from "@/common/section.ts";
import { chunk_worker, mesh_column, test_game } from "./helpers.ts";
// the matrix the renderer makes for a camera, see update_camera in client/renderer/core.ts
function camera_matrix(x: number, y: number, z: number, yaw: number, pitch: number) {
@@ -34,61 +38,25 @@ Deno.test("the frustum keeps what's in front of the camera and drops what's behi
assert(!frustum.intersects_box(-8, 60, -1100, 8, 80, -1090), "past the far plane");
});
// real chunks as the server sends them, meshed the way the client does it
// real chunks as the server sends them, meshed the way the client does it. each section's mesh
async function mesh_chunks(radius: number) {
const { game } = await test_game("mods", undefined, "culling-seed");
const blocks = EverythingRegistry.get_registry<BlockRegistry>("blocks");
const worker = new Worker(new URL("../client/workers/chunk_worker.ts", import.meta.url).href, { type: "module" });
// deno-lint-ignore no-explicit-any
const replies = new Map<string, (message: any) => void>();
worker.onmessage = (e) => {
const key = `${e.data.type} ${e.data.chunk_x} ${e.data.chunk_z}`;
replies.get(key)?.(e.data);
replies.delete(key);
};
// deno-lint-ignore no-explicit-any
const ask = (message: any, reply: string) =>
// deno-lint-ignore no-explicit-any
new Promise<any>((resolve) => {
replies.set(`${reply} ${message.chunk_x} ${message.chunk_z}`, resolve);
worker.postMessage(message);
});
worker.postMessage({
type: "init",
shape: game.world.shape,
blocks_registry: JSON.parse(JSON.stringify(blocks)),
models: {},
textures_info: { "cubi:missing": { x: 0, y: 0, width: 16, height: 16 } },
image: { width: 1024, height: 1024 },
visuals: [],
});
const generated = new Map<string, Section[]>();
const worker = chunk_worker(game.world.shape);
const generated = new Map<string, Uint32Array>();
for (let x = -radius - 1; x <= radius + 1; x++) {
for (let z = -radius - 1; z <= radius + 1; z++) {
generated.set(`${x},${z}`, decode_level_chunk(game.world.level_chunk(x, z)).sections);
generated.set(`${x},${z}`, to_flat(decode_level_chunk(game.world.level_chunk(x, z)).sections));
}
}
const meshes = [];
const meshes: SectionMesh[] = [];
for (let x = -radius; x <= radius; x++) {
for (let z = -radius; z <= radius; z++) {
const chunks = [];
for (let dz = -1; dz <= 1; dz++) {
for (let dx = -1; dx <= 1; dx++) {
chunks.push(generated.get(`${x + dx},${z + dz}`)!.map((section) => section.data));
}
for (let dx = -1; dx <= 1; dx++) chunks.push(generated.get(`${x + dx},${z + dz}`)!);
}
meshes.push(
await ask({
type: "mesh",
chunk_x: x,
chunk_z: z,
version: 1,
chunks,
biome_colors: chunks.map(() => null),
camera: [0, 0, 0],
}, "meshed"),
);
const { sections } = await mesh_column(worker, game.world.shape, chunks, { chunk_x: x, chunk_z: z });
meshes.push(...sections);
}
}
worker.terminate();