World shape for dimension

This commit is contained in:
2026-11-06 22:49:43 -03:00
parent 7458a2d42a
commit 818ce22dd6
60 changed files with 749 additions and 349 deletions
+15 -13
View File
@@ -6,7 +6,8 @@
import { assert, assertEquals, assertRejects, assertThrows } from "@std/assert";
import { mat4 } from "gl-matrix";
import { BlockRegistry, EverythingRegistry } from "@/common/everything_registry.ts";
import { CHUNK_AREA, CHUNK_HEIGHT, CHUNK_SIZE } from "@/common/constants.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";
import type { ModListing } from "@/common/mod_loader.ts";
import { read_mod } from "@/common/mod.ts";
@@ -87,13 +88,14 @@ Deno.test("the chunk mesher lists the blocks with visuals, and leaves out models
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 chunks = Array.from({ length: 9 }, () => new Uint32Array(CHUNK_AREA * CHUNK_HEIGHT));
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");
@@ -155,12 +157,12 @@ 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));
visuals.meshed(0, 0, shaft(1));
visuals.meshed(0, 0, 0, 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, shaft(1));
visuals.meshed(0, 0, 0, shaft(1));
assertEquals(log.splice(0), []);
data.speed = 32;
@@ -175,15 +177,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, shaft(2));
visuals.meshed(0, 0, 0, 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, new Uint32Array());
visuals.meshed(0, 0, 0, new Uint32Array());
assertEquals(log.splice(0), ["delete 1 64 1"]);
assert(!visuals.has(0, 0));
assertEquals(drawn(visuals), []);
visuals.meshed(0, 0, shaft(1));
visuals.meshed(0, 0, 0, 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), []);
@@ -214,13 +216,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, Uint32Array.of(index_of(1, 64, 1), value("testmod:shaft", 1)));
visuals.meshed(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, 0, Uint32Array.of(index_of(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, Uint32Array.of(index_of(1, 64, 1), value("testmod:shaft", 2)));
visuals.meshed(0, 0, 0, Uint32Array.of(index_of(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,7 +250,7 @@ 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")]),
);
visuals.meshed(0, 0, shafts);
visuals.meshed(0, 0, 0, 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 = () => {
@@ -273,7 +275,7 @@ 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, shafts);
visuals.meshed(0, 0, 0, shafts);
assertEquals([at(0)[21], at(0)[22]], [0.5 / 16, 15.5 / 16]);
assertEquals(sent(), [0, 1]);
block_visuals.clear();
@@ -291,7 +293,7 @@ function one_visual(nid: (id: string) => number, value: (id: string) => number,
return {};
},
});
const mesh = (blocks: number[]) => visuals.meshed(0, 0, Uint32Array.from(blocks));
const mesh = (blocks: number[]) => visuals.meshed(0, 0, 0, Uint32Array.from(blocks));
mesh([index_of(1, 64, 1), value("testmod:shaft")]);
return { visuals, mesh };
}