Meshing and lighting per section

This commit is contained in:
2026-11-07 11:41:21 -03:00
parent 71cbf901a5
commit b726ce9695
21 changed files with 736 additions and 544 deletions
+75 -67
View File
@@ -16,6 +16,7 @@ import {
type FaceGroup,
type FromChunkWorker,
type SectionLight,
type SectionMesh,
TERRAIN_QUAD_BYTES,
TERRAIN_VERTEX_BYTES,
type ToChunkWorker,
@@ -99,8 +100,7 @@ const waterlogged_shift = new Uint8Array(TABLE_SIZE);
const block_visual = new Uint8Array(TABLE_SIZE);
const HAS_VISUAL = 1;
const REPLACED_MODEL = 2;
// the 3x3 chunks being meshed, made for the dimension's height by "init". its y, like the mesher's, counts from the
// bottom of the world
// the sections being meshed and the ones around them, made for the dimension's height by "init"
let region: LightRegion;
// the dimension's min_y, which quads are moved up by to be where they are in the world
let world_min_y = 0;
@@ -149,33 +149,31 @@ self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
break;
case "mesh": {
const chunks = message.chunks.map((data) => data?.map((section) => new Section(section)) ?? null);
region.fill(chunks);
// the region's bottom in the world
const bottom = world_min_y + message.first * SECTION_HEIGHT;
region.fill(chunks, message.sky, bottom);
region.compute(light_tables);
blend_biome_colors(message.biome_colors);
const { solid, cutout, translucent, min_y, max_y, visuals } = make_chunk_mesh(
message.chunk_x,
message.chunk_z,
chunks,
message.camera,
);
const light = chunk_light();
const sections: SectionMesh[] = message.sections.map(({ index, version }) => {
// the section's bottom in the region
const from = (index - message.first) * SECTION_HEIGHT;
return {
index,
version,
...make_section_mesh(message.chunk_x, message.chunk_z, chunks, from, bottom, message.camera),
light: section_light(from),
};
});
post(
{
type: "meshed",
chunk_x: message.chunk_x,
chunk_z: message.chunk_z,
version: message.version,
min_y,
max_y,
solid,
cutout,
translucent,
sections,
camera: message.camera,
light,
visuals,
},
[
...light.filter((section) => typeof section !== "number").map((section) => section.buffer),
sections.flatMap(({ light, visuals, solid, cutout, translucent }) => [
...typeof light === "number" ? [] : [light.buffer],
visuals.buffer,
solid.vertices.buffer,
cutout.vertices.buffer,
@@ -183,7 +181,7 @@ self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
translucent.indices.buffer,
translucent.centers.buffer,
...translucent.planes.map((planes) => planes.buffer),
],
]),
);
break;
}
@@ -194,6 +192,7 @@ self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
type: "sorted",
chunk_x: message.chunk_x,
chunk_z: message.chunk_z,
index: message.index,
version: message.version,
sort_version: message.sort_version,
indices,
@@ -203,27 +202,23 @@ self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
}
};
// the middle chunk of the light region, packed for the main thread by section, see "meshed"
const section_light = new Uint8Array(SECTION_VOLUME);
// the light of the middle chunk's section from this y in the region, packed for the main thread, see SectionLight
const packed_light = new Uint8Array(SECTION_VOLUME);
function chunk_light(): SectionLight[] {
const sections: SectionLight[] = [];
for (let bottom = 0; bottom < region.height; bottom += SECTION_HEIGHT) {
let same = true;
for (let y = 0; y < SECTION_HEIGHT; y++) {
for (let z = 0; z < CHUNK_SIZE; z++) {
const from = (bottom + y) * REGION_LAYER + (z + CHUNK_SIZE) * REGION_SIZE + CHUNK_SIZE;
const to = (y << 8) | (z << 4);
for (let x = 0; x < CHUNK_SIZE; x++) {
const light = (region.block_light[from + x] << 4) | region.sky[from + x];
section_light[to + x] = light;
if (light !== section_light[0]) same = false;
}
function section_light(from: number): SectionLight {
let same = true;
for (let y = 0; y < SECTION_HEIGHT; y++) {
for (let z = 0; z < CHUNK_SIZE; z++) {
const at = (from + y) * REGION_LAYER + (z + CHUNK_SIZE) * REGION_SIZE + CHUNK_SIZE;
const to = (y << 8) | (z << 4);
for (let x = 0; x < CHUNK_SIZE; x++) {
const light = (region.block_light[at + x] << 4) | region.sky[at + x];
packed_light[to + x] = light;
if (light !== packed_light[0]) same = false;
}
}
sections.push(same ? section_light[0] : section_light.slice());
}
return sections;
return same ? packed_light[0] : packed_light.slice();
}
function post(message: FromChunkWorker, transfer: Transferable[]) {
@@ -553,8 +548,8 @@ function hides_fluid(nid: number) {
return nid !== AIR && block_layers[nid] === SOLID;
}
// a cell's block with its states, from the 3x3 chunks the region was filled from. 0 (air) above the world and for
// chunks that aren't there
// a cell's block with its states, from the 3x3 chunks' sections the region was filled from. 0 (air) outside them and
// for chunks that aren't there
function region_value(chunks: (Section[] | null)[], index: number, y: number) {
if (y < 0 || y >= region.height) return AIR;
const x = index % REGION_SIZE;
@@ -649,22 +644,39 @@ function underside(top: MeshQuad): MeshQuad {
};
}
// region has to be filled and lit first
// chunks are the 3x3 chunks' blocks with their states, the middle one's for models that change with them and the
// ones around it for fluids, whose surface follows their neighbors'
function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Section[] | null)[], camera: number[]) {
const middle = chunks[4]!;
// solid and cutout get a buffer per face group, translucent one for everything since it's sorted instead
const opaque = [SOLID, CUTOUT].map(() =>
Array.from({ length: FACE_GROUPS }, () => ({ buffer: new QuadBuffer(), min: Infinity, max: -Infinity }))
);
const translucent_quads = new QuadBuffer();
// quads being built, kept from one section to the next. solid and cutout get a buffer per face group, translucent one
// for everything since it's sorted instead
const opaque = [SOLID, CUTOUT].map(() =>
Array.from({ length: FACE_GROUPS }, () => ({ buffer: new QuadBuffer(), min: Infinity, max: -Infinity }))
);
const translucent_quads = new QuadBuffer();
// for sorting the translucent quads
let centers = new Float32Array(256);
let faces = new Uint8Array(256);
// the middle chunk's section from y from in the region, which has to be filled and lit first. bottom is the region's
// bottom in the world. chunks are the region's sections with their blocks' states, the middle one's for models that
// change with them and the ones around it for fluids, whose surface follows their neighbors'
function make_section_mesh(
chunk_x: number,
chunk_z: number,
chunks: (Section[] | null)[],
from: number,
bottom: number,
camera: number[],
) {
const section = chunks[4]![from >> 4];
for (const groups of opaque) {
for (const group of groups) {
group.buffer.count = 0;
group.min = Infinity;
group.max = -Infinity;
}
}
translucent_quads.count = 0;
mesh_min_y = Infinity;
mesh_max_y = -Infinity;
// for sorting the translucent quads
let centers = new Float32Array(256);
let faces = new Uint8Array(256);
// the blocks with visuals, see "meshed"
// the blocks with visuals, see SectionMesh
const visuals: number[] = [];
// where the neighbor on each face is, same order as FACE_NORMALS
@@ -673,7 +685,7 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Section[] |
// adds a lit quad of the block at x y z in the chunk to its layer
const emit = (quad: MeshQuad, layer_id: number, x: number, y: number, z: number, alpha: number) => {
const wx = chunk_x * CHUNK_SIZE + x;
const wy = world_min_y + y;
const wy = bottom + y;
const wz = chunk_z * CHUNK_SIZE + z;
const tint = quad.tint === GRASS
? blended[z * CHUNK_SIZE + x]
@@ -706,13 +718,9 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Section[] |
}
};
for (let y = 0; y < region.height; y++) {
const section = middle[y >> 4];
// nothing to mesh in a section of air
if (section.blocks === null && section.value === AIR) {
y |= 15;
continue;
}
// nothing to mesh in a section of air
const air = section.blocks === null && section.value === AIR;
for (let y = from; y < from + SECTION_HEIGHT && !air; y++) {
for (let z = 0; z < CHUNK_SIZE; z++) {
for (let x = 0; x < CHUNK_SIZE; x++) {
// the middle chunk of the region
@@ -758,10 +766,10 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Section[] |
const value = section.get(section_index(x, y, z));
if (block_visual[block_nid]) {
visuals.push(y * CHUNK_AREA + z * CHUNK_SIZE + x, value);
visuals.push(section_index(x, y, z), value);
// with the block above and below, since a visual can reach past its block, like a large cog
if (world_min_y + y - 1 < mesh_min_y) mesh_min_y = world_min_y + y - 1;
if (world_min_y + y + 2 > mesh_max_y) mesh_max_y = world_min_y + y + 2;
if (bottom + y - 1 < mesh_min_y) mesh_min_y = bottom + y - 1;
if (bottom + y + 2 > mesh_max_y) mesh_max_y = bottom + y + 2;
if (block_visual[block_nid] === REPLACED_MODEL) continue;
}
@@ -805,8 +813,8 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Section[] |
const [camera_x, camera_y, camera_z] = camera;
return {
min_y: mesh_min_y === Infinity ? 0 : mesh_min_y,
max_y: mesh_max_y === -Infinity ? 0 : mesh_max_y,
min_y: mesh_min_y,
max_y: mesh_max_y,
solid,
cutout,
translucent: {