Files
bworld/client/workers/chunk_messages.ts
T
2026-09-25 15:01:03 -03:00

80 lines
2.0 KiB
TypeScript

import type { BlockRegistry } from "$/common/everything_registry.ts";
import type { SpriteRegion } from "$/common/constants.ts";
import type { OreJson } from "$/common/mod_data.ts";
import type { SortType } from "./translucent_sort.ts";
// messages between the main thread and the chunk workers
export type ToChunkWorker =
| {
type: "init";
// the blocks registry without its functions, indexed by numeric id
blocks_registry: BlockRegistry[];
block_ids: Record<string, number>;
textures_info: Record<string, SpriteRegion>;
image: { width: number; height: number };
// mods' worldgen scripts and ores, so generation matches the server
worldgen_scripts: { mod: string; url: string }[];
ores: OreJson[];
}
| { type: "generate"; chunk_x: number; chunk_z: number; seed: string }
| {
type: "mesh";
chunk_x: number;
chunk_z: number;
version: number;
padded_chunk: Uint32Array;
// where the camera is, to sort the translucent quads
camera: number[];
}
| {
type: "sort";
chunk_x: number;
chunk_z: number;
version: number;
sort_version: number;
centers: Float32Array;
camera: number[];
};
// 4 vertices per quad
export interface LayerMesh {
vertices: Float32Array;
quad_count: number;
}
export type FromChunkWorker =
| {
type: "generated";
chunk_x: number;
chunk_z: number;
blocks: Uint32Array;
// blocks that landed in other chunks (tree leaves), flattened as x, y, z, numeric id
spills: Int32Array;
}
| {
type: "meshed";
chunk_x: number;
chunk_z: number;
version: number;
solid: LayerMesh;
cutout: LayerMesh;
translucent: LayerMesh & {
// sorted back to front for the camera the mesh was requested with
indices: Uint32Array;
sort_type: SortType;
// what resorting needs, see translucent_sort.ts
centers: Float32Array;
planes: [Float32Array, Float32Array, Float32Array];
};
camera: number[];
}
| {
type: "sorted";
chunk_x: number;
chunk_z: number;
version: number;
sort_version: number;
indices: Uint32Array;
};