Block visuals

This commit is contained in:
2026-10-13 23:52:30 -03:00
parent 55ad08266e
commit 6ad476ac18
19 changed files with 854 additions and 14 deletions
+5
View File
@@ -35,6 +35,9 @@ export type ToChunkWorker =
models: Record<string, ModelJson>;
textures_info: Record<string, SpriteRegion>;
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][];
}
| {
type: "mesh";
@@ -89,6 +92,8 @@ export type FromChunkWorker =
// the chunk's light, one byte per block indexed like its blocks: block light << 4 | sky light, like sodium's
// packed light data. entities are lit from it
light: Uint8Array<ArrayBuffer>;
// the blocks in it with visuals, as pairs of the block's index in the chunk (like light's) and its value
visuals: Uint32Array<ArrayBuffer>;
}
| {
type: "sorted";
+24 -2
View File
@@ -100,6 +100,10 @@ const liquid_level_shift = new Uint8Array(TABLE_SIZE);
// waterloggable blocks: the numeric id of the liquid they hold, 0 for other blocks, and where their waterlogged state is
const waterlog_liquid = new Uint16Array(TABLE_SIZE);
const waterlogged_shift = new Uint8Array(TABLE_SIZE);
// blocks with visuals: HAS_VISUAL, or REPLACED_MODEL when the visual draws all of it, see ctx.render.register_visual
const block_visual = new Uint8Array(TABLE_SIZE);
const HAS_VISUAL = 1;
const REPLACED_MODEL = 2;
const region = new LightRegion();
// a model's quad ready for the mesher: uvs in the atlas, and for each corner how much of each of the face's
@@ -135,6 +139,10 @@ self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
models = message.models;
baked.clear();
build_block_tables();
block_visual.fill(0);
for (const [nid, replaces_model] of message.visuals) {
block_visual[nid] = replaces_model ? REPLACED_MODEL : HAS_VISUAL;
}
textures_info = message.textures_info;
image = message.image as Texture;
break;
@@ -142,7 +150,7 @@ self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
region.fill(message.chunks);
region.compute(light_tables);
blend_biome_colors(message.biome_colors);
const { solid, cutout, translucent, min_y, max_y } = make_chunk_mesh(
const { solid, cutout, translucent, min_y, max_y, visuals } = make_chunk_mesh(
message.chunk_x,
message.chunk_z,
message.chunks,
@@ -162,9 +170,11 @@ self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
translucent,
camera: message.camera,
light,
visuals,
},
[
light.buffer,
visuals.buffer,
solid.vertices.buffer,
cutout.vertices.buffer,
translucent.vertices.buffer,
@@ -644,6 +654,8 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Uint32Array
// for sorting the translucent quads
let centers = new Float32Array(256);
let faces = new Uint8Array(256);
// the blocks with visuals, see "meshed"
const visuals: number[] = [];
// where the neighbor on each face is, same order as FACE_NORMALS
const face_offsets = FACE_NORMALS.map(([nx, ny, nz]) => nx + ny * REGION_LAYER + nz * REGION_SIZE);
@@ -727,7 +739,16 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Uint32Array
if (liquid_nid === block_nid) continue;
}
for (const quad of block_quads(values[y * CHUNK_AREA + z * CHUNK_SIZE + x])) {
const value = values[y * CHUNK_AREA + z * CHUNK_SIZE + x];
if (block_visual[block_nid]) {
visuals.push(y * CHUNK_AREA + z * CHUNK_SIZE + x, value);
// with the block above and below, since a visual can reach past its block, like a large cog
if (y - 1 < mesh_min_y) mesh_min_y = y - 1;
if (y + 2 > mesh_max_y) mesh_max_y = y + 2;
if (block_visual[block_nid] === REPLACED_MODEL) continue;
}
for (const quad of block_quads(value)) {
if (quad.cull >= 0) {
const neighbor = region_block(
region,
@@ -778,6 +799,7 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Uint32Array
centers: centers.slice(0, quads.count * 3),
planes: quad_planes(quads),
},
visuals: Uint32Array.from(visuals),
};
}