Optimize renderer

This commit is contained in:
2026-09-24 18:52:20 -03:00
parent 14aba6b129
commit d12fa84b00
10 changed files with 452 additions and 149 deletions
+36
View File
@@ -0,0 +1,36 @@
import type { BlockRegistry } from "$/common/everything_registry.ts";
import type { SpriteRegion } from "$/common/constants.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 };
}
| { type: "generate"; chunk_x: number; chunk_z: number; seed: string }
| { type: "mesh"; chunk_x: number; chunk_z: number; version: number; padded_chunk: Uint32Array };
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;
opaque_vertices: Float32Array;
opaque_count: number;
transparent_vertices: Float32Array;
transparent_count: number;
};
@@ -3,6 +3,8 @@
import type { BlockRegistry } from "$/common/everything_registry.ts";
import type { SpriteRegion } from "$/common/constants.ts";
import type { Texture } from "../renderer/types.ts";
import type { FromChunkWorker, ToChunkWorker } from "./chunk_messages.ts";
import { generate_chunk } from "../generation.ts";
const pad = 0.5;
@@ -260,21 +262,87 @@ const FACE_PUSHING_FUNCTIONS = {
right: push_right_face,
} as const;
self.onmessage = (event) => {
const { chunk_x, chunk_z, padded_chunk, blocks_registry, textures_info, image } = event.data;
const [opaque_vertices, opaque_count, transparent_vertices, transparent_count] = make_chunk_mesh(
let blocks_registry: BlockRegistry[] = [];
let block_ids: Record<string, number> = {};
let textures_info: TexturesInfo = {};
let image: Texture;
self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
const message = event.data;
switch (message.type) {
case "init":
blocks_registry = message.blocks_registry;
block_ids = message.block_ids;
textures_info = message.textures_info;
image = message.image as Texture;
break;
case "generate":
generate(message.chunk_x, message.chunk_z, message.seed);
break;
case "mesh": {
const [opaque_vertices, opaque_count, transparent_vertices, transparent_count] = make_chunk_mesh(
message.chunk_x,
message.chunk_z,
message.padded_chunk,
blocks_registry,
textures_info,
image,
);
post(
{
type: "meshed",
chunk_x: message.chunk_x,
chunk_z: message.chunk_z,
version: message.version,
opaque_vertices,
opaque_count,
transparent_vertices,
transparent_count,
},
[opaque_vertices.buffer, transparent_vertices.buffer],
);
break;
}
}
};
function post(message: FromChunkWorker, transfer: Transferable[]) {
self.postMessage(message, transfer);
}
function generate(chunk_x: number, chunk_z: number, seed: string) {
const blocks = new Uint32Array(CHUNK_SIZE * CHUNK_SIZE * CHUNK_HEIGHT);
const spills: number[] = [];
generate_chunk(
{
add_block(block) {
const nid = block_ids[block.id];
if (nid === undefined || block.y < 0 || block.y >= CHUNK_HEIGHT) {
return;
}
const block_chunk_x = Math.floor(block.x / CHUNK_SIZE);
const block_chunk_z = Math.floor(block.z / CHUNK_SIZE);
if (block_chunk_x !== chunk_x || block_chunk_z !== chunk_z) {
spills.push(block.x, block.y, block.z, nid);
return;
}
const lx = block.x - chunk_x * CHUNK_SIZE;
const lz = block.z - chunk_z * CHUNK_SIZE;
blocks[block.y * CHUNK_SIZE * CHUNK_SIZE + lz * CHUNK_SIZE + lx] = nid;
},
},
chunk_x,
chunk_z,
padded_chunk,
blocks_registry,
textures_info,
image,
seed,
);
self.postMessage(
{ opaque_vertices, opaque_count, transparent_vertices, transparent_count, chunk_x, chunk_z },
[opaque_vertices.buffer, transparent_vertices.buffer],
);
};
const spills_array = new Int32Array(spills);
post({ type: "generated", chunk_x, chunk_z, blocks, spills: spills_array }, [
blocks.buffer,
spills_array.buffer,
]);
}
function make_chunk_mesh(
chunk_x: number,