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
+25 -41
View File
@@ -5,7 +5,7 @@
// draws their instances, placed and turned by the time
import { assert, assertEquals, assertRejects, assertThrows } from "@std/assert";
import { mat4 } from "gl-matrix";
import { BlockRegistry, EverythingRegistry } from "@/common/everything_registry.ts";
import { EverythingRegistry } from "@/common/everything_registry.ts";
import { CHUNK_AREA, CHUNK_SIZE } from "@/common/constants.ts";
import { DEFAULT_SHAPE } from "@/common/world_shape.ts";
import { block_value } from "@/common/utils.ts";
@@ -19,7 +19,8 @@ import { AssetManager } from "@/client/assets.ts";
import { load_client_mods } from "@/client/mods.ts";
import { load_and_check } from "@/tools/check_mods.ts";
import { pack_mod } from "@/tools/pack_mod.ts";
import { copy_dir, section_data, test_game } from "./helpers.ts";
import { chunk_worker, copy_dir, mesh_column, test_game } from "./helpers.ts";
import { section_index } from "@/common/section.ts";
const HALF = {
textures: { "0": "testmod:shaft" },
@@ -85,34 +86,16 @@ const index_of = (x: number, y: number, z: number) => y * CHUNK_AREA + z * CHUNK
Deno.test("the chunk mesher lists the blocks with visuals, and leaves out models they replace", async () => {
const { nid, value } = await registries();
const worker = new Worker(new URL("../client/workers/chunk_worker.ts", import.meta.url).href, { type: "module" });
worker.postMessage({
type: "init",
shape: DEFAULT_SHAPE,
blocks_registry: JSON.parse(JSON.stringify(EverythingRegistry.get_registry<BlockRegistry>("blocks"))),
models: Object.fromEntries(EverythingRegistry.entries("models")),
textures_info: { "cubi:missing": { x: 0, y: 0, width: 16, height: 16 } },
image: { width: 1024, height: 1024 },
visuals: [[nid("bworld:stone"), true], [nid("bworld:cobblestone"), false]],
});
const worker = chunk_worker(DEFAULT_SHAPE, Object.fromEntries(EverythingRegistry.entries("models")), [
[nid("bworld:stone"), true],
[nid("bworld:cobblestone"), false],
]);
const chunks = Array.from({ length: 9 }, () => new Uint32Array(DEFAULT_SHAPE.volume));
const middle = chunks[4];
middle[index_of(1, 64, 1)] = value("bworld:stone");
middle[index_of(4, 64, 1)] = value("bworld:cobblestone");
middle[index_of(7, 64, 1)] = value("bworld:dirt");
// deno-lint-ignore no-explicit-any
const meshed = await new Promise<any>((resolve) => {
worker.onmessage = (e) => resolve(e.data);
worker.postMessage({
type: "mesh",
chunk_x: 0,
chunk_z: 0,
version: 1,
chunks: chunks.map(section_data),
biome_colors: chunks.map(() => null),
camera: [0, 0, 0],
});
});
const meshed = await mesh_column(worker, DEFAULT_SHAPE, chunks);
worker.terminate();
assertEquals([...meshed.visuals], [
@@ -155,14 +138,14 @@ Deno.test("a visual is made with its block, told when its client data changes, a
};
},
});
const shaft = (axis: number) => Uint32Array.of(index_of(1, 64, 1), value("testmod:shaft", axis));
const shaft = (axis: number) => Uint32Array.of(section_index(1, 64, 1), value("testmod:shaft", axis));
visuals.meshed(0, 0, 0, shaft(1));
visuals.meshed(0, 0, 64, shaft(1));
assertEquals(log.splice(0), ["create testmod:shaft 1 64 1 axis 1"]);
// the half model has one face, the block's own model is a cube
assertEquals(drawn(visuals), ["1 quads x1", "6 quads x1"]);
// meshed again for something else, it stays
visuals.meshed(0, 0, 0, shaft(1));
visuals.meshed(0, 0, 64, shaft(1));
assertEquals(log.splice(0), []);
data.speed = 32;
@@ -177,15 +160,15 @@ Deno.test("a visual is made with its block, told when its client data changes, a
assertEquals(log.splice(0), ["frame 10.5"]);
// its states changing makes it again
visuals.meshed(0, 0, 0, shaft(2));
visuals.meshed(0, 0, 64, shaft(2));
assertEquals(log.splice(0), ["delete 1 64 1", "create testmod:shaft 1 64 1 axis 2"]);
assertEquals(drawn(visuals), ["1 quads x1", "6 quads x1"]);
// and it goes with the block, or with the chunk, and so do its instances
visuals.meshed(0, 0, 0, new Uint32Array());
visuals.meshed(0, 0, 64, new Uint32Array());
assertEquals(log.splice(0), ["delete 1 64 1"]);
assert(!visuals.has(0, 0));
assertEquals(drawn(visuals), []);
visuals.meshed(0, 0, 0, shaft(1));
visuals.meshed(0, 0, 64, shaft(1));
visuals.unload(0, 0);
assertEquals(log.splice(0), ["create testmod:shaft 1 64 1 axis 1", "delete 1 64 1"]);
assertEquals(drawn(visuals), []);
@@ -216,13 +199,13 @@ Deno.test("a visual that throws is logged and left out, and not made again until
const console_error = console.error;
console.error = (...args) => errors.push(args);
try {
visuals.meshed(0, 0, 0, Uint32Array.of(index_of(1, 64, 1), value("testmod:shaft", 1)));
visuals.meshed(0, 0, 0, Uint32Array.of(index_of(1, 64, 1), value("testmod:shaft", 1)));
visuals.meshed(0, 0, 64, Uint32Array.of(section_index(1, 64, 1), value("testmod:shaft", 1)));
visuals.meshed(0, 0, 64, Uint32Array.of(section_index(1, 64, 1), value("testmod:shaft", 1)));
assertEquals([made, errors.length], [1, 1]);
assertEquals(drawn(visuals), []);
// an on_frame that throws isn't called again
visuals.meshed(0, 0, 0, Uint32Array.of(index_of(1, 64, 1), value("testmod:shaft", 2)));
visuals.meshed(0, 0, 64, Uint32Array.of(section_index(1, 64, 1), value("testmod:shaft", 2)));
for (let frame = 0; frame < 3; frame++) visuals.frame([{ x: 0, z: 0 }], frame);
assertEquals([made, errors.length], [3, 2]);
} finally {
@@ -248,9 +231,9 @@ Deno.test("an instance's placement, spin and light are kept for the gpu, and onl
},
});
const shafts = Uint32Array.of(
...[1, 2, 3].flatMap((x) => [index_of(x, 64, 1), value("testmod:shaft")]),
...[1, 2, 3].flatMap((x) => [section_index(x, 64, 1), value("testmod:shaft")]),
);
visuals.meshed(0, 0, 0, shafts);
visuals.meshed(0, 0, 64, shafts);
const group = [...visuals.groups()].find((group) => group.instances.length > 0)!;
const at = (i: number) => [...group.data.subarray(i * INSTANCE_FLOATS, (i + 1) * INSTANCE_FLOATS)];
const sent = () => {
@@ -275,13 +258,14 @@ Deno.test("an instance's placement, spin and light are kept for the gpu, and onl
// a new light comes with the chunk's mesh
light = 0x0f;
visuals.meshed(0, 0, 0, shafts);
visuals.meshed(0, 0, 64, shafts);
assertEquals([at(0)[21], at(0)[22]], [0.5 / 16, 15.5 / 16]);
assertEquals(sent(), [0, 1]);
block_visuals.clear();
});
// a visual on one shaft, at 1 64 1, that makes what make does
// a visual on one shaft, at 1 64 1, that makes what make does. mesh meshes the section from y 64 with blocks at their
// indexes in it
function one_visual(nid: (id: string) => number, value: (id: string) => number, make: (instances: Instances) => void) {
const visuals = new Visuals({ get_block_data: () => undefined, get_light: () => 0 });
block_visuals.clear();
@@ -293,8 +277,8 @@ function one_visual(nid: (id: string) => number, value: (id: string) => number,
return {};
},
});
const mesh = (blocks: number[]) => visuals.meshed(0, 0, 0, Uint32Array.from(blocks));
mesh([index_of(1, 64, 1), value("testmod:shaft")]);
const mesh = (blocks: number[]) => visuals.meshed(0, 0, 64, Uint32Array.from(blocks));
mesh([section_index(1, 64, 1), value("testmod:shaft")]);
return { visuals, mesh };
}
@@ -378,7 +362,7 @@ Deno.test("a fluid's box is its texture cut to fit and tinted in its color, a mo
box!.delete();
assertEquals(boxes(), []);
mesh([]);
mesh([index_of(1, 64, 1), value("testmod:shaft")]);
mesh([section_index(1, 64, 1), value("testmod:shaft")]);
assertEquals(boxes().length, 1);
visuals.unload(0, 0);
assertEquals(boxes(), []);