// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later import type { BlockRegistry } from "@/common/everything_registry.ts"; import type { SpriteRegion } from "@/common/constants.ts"; import type { ModelJson } from "@/common/block_models.ts"; import type { WorldShapeJson } from "@/common/world_shape.ts"; import type { SectionBiomes, SectionData } from "@/common/section.ts"; import type { SortType } from "./translucent_sort.ts"; // messages between the main thread and the chunk workers // a terrain vertex, 24 bytes: position as float32x3, atlas uv as unorm16x2, directional shade times ambient occlusion // and alpha as unorm8x4 (shade repeated in rgb), and lightmap coordinates as unorm8x2 plus two unused bytes export const TERRAIN_VERTEX_BYTES = 24; export const TERRAIN_QUAD_BYTES = 4 * TERRAIN_VERTEX_BYTES; // solid and cutout quads are grouped by the way they face, so groups facing away from the camera can be skipped, like // sodium's block face culling. the groups follow FACE_NORMALS' order, then the quads that aren't axis aligned export const FACE_GROUPS = 7; export const UNALIGNED_GROUP = 6; export interface FaceGroup { first: number; count: number; // the lowest and highest plane the group's quads lie on, along its axis min: number; max: number; } export type ToChunkWorker = | { type: "init"; // the blocks registry without its functions, indexed by numeric id blocks_registry: BlockRegistry[]; // mods' block models by id, the engine's are built in models: Record; textures_info: Record; image: { width: number; height: number }; // the numeric ids of blocks with visuals, and whether each visual replaces the block's model, see // ctx.render.register_visual visuals: [number, boolean][]; // the dimension's height and sky light shape: WorldShapeJson; } // sections of one chunk to mesh, sent together so they're lit together | { type: "mesh"; chunk_x: number; chunk_z: number; // the sections to mesh, by index from the bottom of the world, and the mesh_version each is meshed for sections: { index: number; version: number }[]; // the index of the lowest section in chunks first: number; // the blocks of the 3x3 chunks around it, going +x then +z from -x -z, for lighting. each one's sections from the // one below the lowest to mesh to the one above the highest, as far as the world goes. null for a chunk that // isn't loaded chunks: (SectionData[] | null)[]; // the same chunks' sky heights, see column_heights in common/section.ts sky: (Int16Array | null)[]; // the same chunks' biome cells, of the same sections as chunks, and their biome palettes' colors (see // Chunk.biome_colors in client_level.ts), blended across their edges biomes: (SectionBiomes[] | null)[]; biome_colors: (Uint32Array | null)[]; // where the camera is, to sort the translucent quads camera: number[]; } | { type: "sort"; chunk_x: number; chunk_z: number; // the section's index in its chunk index: number; version: number; sort_version: number; centers: Float32Array; camera: number[]; }; // a section's light, a byte per block indexed like its blocks (see section_index): block light << 4 | sky light, like // sodium's packed light data. one number when it's the same everywhere, like the open sky above the ground export type SectionLight = number | Uint8Array; // 4 vertices per quad, TERRAIN_VERTEX_BYTES each export interface LayerMesh { vertices: Uint8Array; quad_count: number; // solid and cutout only, see FACE_GROUPS. the quads are stored group after group groups?: FaceGroup[]; } // a section the worker meshed export interface SectionMesh { index: number; version: number; // the lowest and highest y of the section's quads and visuals in the world, for frustum culling. Infinity and // -Infinity when it has none min_y: number; max_y: 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]; }; // entities are lit from it light: SectionLight; // which of its faces can be seen through from which others, see client/workers/visibility.ts visibility: number; // the blocks in it with visuals, as pairs of the block's index in the section (see section_index) and its value visuals: Uint32Array; } export type FromChunkWorker = | { type: "meshed"; chunk_x: number; chunk_z: number; // the sections asked for, in the same order sections: SectionMesh[]; // what the translucent quads were sorted for camera: number[]; } | { type: "sorted"; chunk_x: number; chunk_z: number; index: number; version: number; sort_version: number; indices: Uint32Array; };