Meshing and lighting per section
This commit is contained in:
+99
-8
@@ -2,7 +2,12 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import { resolve, toFileUrl } from "@std/path";
|
||||
import { type BlockRegistry, EverythingRegistry } from "@/common/everything_registry.ts";
|
||||
import {
|
||||
type BlockRegistry,
|
||||
EverythingRegistry,
|
||||
light_tables,
|
||||
type RenderLayer,
|
||||
} from "@/common/everything_registry.ts";
|
||||
import { load_and_check, load_order } from "@/tools/check_mods.ts";
|
||||
import { mod_data } from "@/common/mod_check.ts";
|
||||
import type { ServerModSource } from "@/server/game/load_mods.ts";
|
||||
@@ -11,8 +16,11 @@ import { start_game } from "@/server/game/load_mods.ts";
|
||||
import { apply_config } from "@/server/load_mods.ts";
|
||||
import { AIR_ID, PROTOCOL_VERSION } from "@/common/protocol.ts";
|
||||
import { decode_level_chunk } from "@/common/chunk_streaming.ts";
|
||||
import { CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
|
||||
import { Section, section_index, SECTION_VOLUME, type SectionData } from "@/common/section.ts";
|
||||
import { CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
|
||||
import { ChunkColumn, column_heights, section_index, SECTION_VOLUME } from "@/common/section.ts";
|
||||
import { WorldShape, type WorldShapeJson } from "@/common/world_shape.ts";
|
||||
import type { LayerMesh, SectionMesh, ToChunkWorker } from "@/client/workers/chunk_messages.ts";
|
||||
import type { ModelJson } from "@/common/block_models.ts";
|
||||
import type { SavedChunk, SavedChunks, WorldSave } from "@/server/game/saves.ts";
|
||||
import { FluidRules } from "@/common/fluids.ts";
|
||||
import { BlockRules } from "@/common/block_rules.ts";
|
||||
@@ -166,12 +174,95 @@ export function sent_block(
|
||||
return [join.blocks[value & ID_MASK], value >>> 16];
|
||||
}
|
||||
|
||||
// a chunk's blocks in one array, y from the bottom of the world then z then x, as the sections a mesh request sends
|
||||
export function section_data(blocks: Uint32Array): SectionData[] {
|
||||
return Array.from(
|
||||
{ length: blocks.length / SECTION_VOLUME },
|
||||
(_, i) => Section.of(blocks.subarray(i * SECTION_VOLUME, (i + 1) * SECTION_VOLUME)).data,
|
||||
// a chunk worker set up the way the client sets one up, with the loaded mods' blocks. models are the mods' block models
|
||||
// and visuals the blocks with them, see ToChunkWorker's init
|
||||
export function chunk_worker(
|
||||
shape: WorldShapeJson,
|
||||
models: Record<string, ModelJson> = {},
|
||||
visuals: [number, boolean][] = [],
|
||||
): Worker {
|
||||
const worker = new Worker(new URL("../client/workers/chunk_worker.ts", import.meta.url).href, { type: "module" });
|
||||
worker.postMessage({
|
||||
type: "init",
|
||||
shape,
|
||||
// with the registry's gaps, like the client sends it
|
||||
blocks_registry: EverythingRegistry.get_registry<BlockRegistry>("blocks").map((block) =>
|
||||
JSON.parse(JSON.stringify(block))
|
||||
),
|
||||
models,
|
||||
textures_info: { "cubi:missing": { x: 0, y: 0, width: 16, height: 16 } },
|
||||
image: { width: 1024, height: 1024 },
|
||||
visuals,
|
||||
});
|
||||
return worker;
|
||||
}
|
||||
|
||||
// the sections of the middle one of 3x3 chunks meshed by a chunk worker, asked for the way the client asks: all of
|
||||
// them, or the indexes in options.sections. the chunks are each block's value in one array, y from the bottom of the
|
||||
// world then z then x, null where there's none. what comes back is each section's mesh, and all of them put together:
|
||||
// their layers' quads one after another, their light, their visuals with indexes in the whole chunk (like an array's)
|
||||
// and the bounds of them all
|
||||
export async function mesh_column(
|
||||
worker: Worker,
|
||||
shape: WorldShapeJson,
|
||||
chunks: (Uint32Array | null)[],
|
||||
options: {
|
||||
chunk_x?: number;
|
||||
chunk_z?: number;
|
||||
biome_colors?: (Uint32Array | null)[];
|
||||
camera?: number[];
|
||||
sections?: number[];
|
||||
} = {},
|
||||
) {
|
||||
const { opacity } = light_tables(EverythingRegistry.get_registry<BlockRegistry>("blocks"));
|
||||
const columns = chunks.map((blocks) =>
|
||||
blocks && ChunkColumn.from_flat(0, 0, new WorldShape(shape), blocks, new Array(CHUNK_AREA).fill(""))
|
||||
);
|
||||
const count = columns[4]!.sections.length;
|
||||
const indexes = options.sections ?? Array.from({ length: count }, (_, index) => index);
|
||||
// with the section below and above them, like ClientLevel.request_meshes
|
||||
const first = Math.max(0, Math.min(...indexes) - 1);
|
||||
const end = Math.min(count, Math.max(...indexes) + 2);
|
||||
const message: ToChunkWorker = {
|
||||
type: "mesh",
|
||||
chunk_x: options.chunk_x ?? 0,
|
||||
chunk_z: options.chunk_z ?? 0,
|
||||
sections: indexes.map((index) => ({ index, version: 1 })),
|
||||
first,
|
||||
chunks: columns.map((column) => column?.sections.slice(first, end).map((section) => section.data) ?? null),
|
||||
sky: columns.map((column) => column && column_heights(column, opacity).slice(CHUNK_AREA)),
|
||||
biome_colors: options.biome_colors ?? chunks.map(() => null),
|
||||
camera: options.camera ?? [0, 0, 0],
|
||||
};
|
||||
const sections = await new Promise<SectionMesh[]>((resolve) => {
|
||||
worker.onmessage = (e) => resolve(e.data.sections);
|
||||
worker.postMessage(message);
|
||||
});
|
||||
|
||||
const layer = (name: RenderLayer): LayerMesh => {
|
||||
const meshes = sections.map((section) => section[name]);
|
||||
const vertices = new Uint8Array(meshes.reduce((sum, mesh) => sum + mesh.vertices.length, 0));
|
||||
let at = 0;
|
||||
for (const mesh of meshes) {
|
||||
vertices.set(mesh.vertices, at);
|
||||
at += mesh.vertices.length;
|
||||
}
|
||||
return { vertices, quad_count: meshes.reduce((sum, mesh) => sum + mesh.quad_count, 0) };
|
||||
};
|
||||
return {
|
||||
sections,
|
||||
solid: layer("solid"),
|
||||
cutout: layer("cutout"),
|
||||
translucent: layer("translucent"),
|
||||
light: sections.map((section) => section.light),
|
||||
visuals: Uint32Array.from(
|
||||
sections.flatMap(({ index, visuals }) =>
|
||||
[...visuals].map((value, i) => i % 2 === 0 ? index * SECTION_VOLUME + value : value)
|
||||
),
|
||||
),
|
||||
min_y: Math.min(...sections.map((section) => section.min_y)),
|
||||
max_y: Math.max(...sections.map((section) => section.max_y)),
|
||||
};
|
||||
}
|
||||
|
||||
// a stone floor at y 99 from -radius to radius on x and z, high above the terrain, for players to stand on at y 100
|
||||
|
||||
Reference in New Issue
Block a user