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
+40 -22
View File
@@ -40,17 +40,24 @@ export type ToChunkWorker =
// 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. the chunks meshed hold all of its height
// 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;
version: 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
// bottom of the world up, null for a chunk that isn't loaded
// 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 colors (see Chunk.biome_colors in client_level.ts), blended across their edges
biome_colors: (Uint32Array | null)[];
// where the camera is, to sort the translucent quads
@@ -60,6 +67,8 @@ export type ToChunkWorker =
type: "sort";
chunk_x: number;
chunk_z: number;
// the section's index in its chunk
index: number;
version: number;
sort_version: number;
centers: Float32Array;
@@ -78,36 +87,45 @@ export interface LayerMesh {
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;
// the blocks in it with visuals, as pairs of the block's index in the section (see section_index) and its value
visuals: Uint32Array<ArrayBuffer>;
}
export type FromChunkWorker =
| {
type: "meshed";
chunk_x: number;
chunk_z: number;
version: number;
// the lowest and highest y of the chunk's quads in the world, for frustum culling
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];
};
// the sections asked for, in the same order
sections: SectionMesh[];
// what the translucent quads were sorted for
camera: number[];
// the chunk's light by section, from the bottom of the world up, see SectionLight. entities are lit from it
light: SectionLight[];
// the blocks in it with visuals, as pairs of the block's index in the chunk (y from the bottom of the world, then
// z, then x) and its value
visuals: Uint32Array<ArrayBuffer>;
}
| {
type: "sorted";
chunk_x: number;
chunk_z: number;
index: number;
version: number;
sort_version: number;
indices: Uint32Array;
+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: {
+32 -26
View File
@@ -6,16 +6,18 @@
// something that isn't fully clear, block light starts at blocks that give off light. both spread to
// neighbors losing max(1, the neighbor's opacity) per step.
//
// minecraft stores light and updates it as blocks change. here it's worked out from scratch for the
// 3x3 chunks around the chunk being meshed, which gives the same result: light reaches at most 15
// blocks, so nothing outside those chunks can light the middle one or its border
// minecraft stores light and updates it as blocks change. here it's worked out from scratch around the sections being
// meshed: the 3x3 chunks around theirs, from the section below them to the one above. that gives the same result, since
// light reaches at most 15 blocks, so nothing outside can light them or their border. the only light that comes from
// further is the sky's straight down, which starts where each column's sky height says (see column_heights in
// common/section.ts) instead of being followed down from the top.
//
// y in a region counts from the bottom of the world, 0 is the dimension's min_y. in a dimension without sky light
// there's only block light
// y in a region counts from its bottom, which is a section's. in a dimension without sky light there's only block
// light
import { AIR, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts";
import type { Section } from "@/common/section.ts";
import type { WorldShapeJson } from "@/common/world_shape.ts";
import { SECTION_HEIGHT, type WorldShapeJson } from "@/common/world_shape.ts";
export const REGION_SIZE = CHUNK_SIZE * 3;
export const REGION_LAYER = REGION_SIZE * REGION_SIZE;
@@ -29,34 +31,42 @@ export interface LightTables {
}
export class LightRegion {
// the dimension's, in blocks
readonly height: number;
// in blocks, of the sections last filled in
height = 0;
readonly sky_light: boolean;
// block ids without their state bits, indexed y * REGION_LAYER + z * REGION_SIZE + x
blocks: Uint16Array;
sky: Uint8Array;
block_light: Uint8Array;
// the lowest y that still sees the sky, per column
// the lowest y that still sees the sky, per column. the region's height when that's above it
#heights = new Int32Array(REGION_LAYER);
#queue = new Int32Array(1 << 18);
#queue_length = 0;
// room for the dimension's whole height
constructor(shape: WorldShapeJson) {
this.height = shape.max_y - shape.min_y;
this.sky_light = shape.sky_light;
const volume = REGION_LAYER * this.height;
const volume = REGION_LAYER * (shape.max_y - shape.min_y);
this.blocks = new Uint16Array(volume);
this.sky = new Uint8Array(volume);
this.block_light = new Uint8Array(volume);
}
// the 3x3 chunks around the one being meshed, going +x then +z, starting at -x -z, by section. missing ones are
// void
fill(chunks: (Section[] | null)[]) {
// the same sections of the 3x3 chunks around the one being meshed, going +x then +z, starting at -x -z. missing
// chunks are void. sky is each chunk's sky height by column (see column_heights), and bottom the y of the region's
// bottom in the world
fill(chunks: (Section[] | null)[], sky: (ArrayLike<number> | null)[], bottom: number) {
this.height = chunks[4]!.length * SECTION_HEIGHT;
for (let i = 0; i < 9; i++) {
const source = chunks[i];
const origin = Math.floor(i / 3) * CHUNK_SIZE * REGION_SIZE + (i % 3) * CHUNK_SIZE;
for (let z = 0; z < CHUNK_SIZE; z++) {
for (let x = 0; x < CHUNK_SIZE; x++) {
const height = sky[i] ? sky[i]![z * CHUNK_SIZE + x] - bottom : this.height;
this.#heights[origin + z * REGION_SIZE + x] = Math.min(Math.max(height, 0), this.height);
}
}
for (let y = 0; y < this.height; y++) {
const section = source?.[y >> 4];
for (let z = 0; z < CHUNK_SIZE; z++) {
@@ -80,19 +90,14 @@ export class LightRegion {
}
#compute_sky({ opacity }: LightTables) {
const { blocks, sky, height: world_height } = this;
sky.fill(0);
const { sky, height: region_height } = this;
sky.fill(0, 0, REGION_LAYER * region_height);
this.#queue_length = 0;
if (!this.sky_light) return;
// straight down from the top, until something isn't fully clear
// straight down from the top, to the sky height
for (let column = 0; column < REGION_LAYER; column++) {
let y = world_height - 1;
while (y >= 0 && opacity[blocks[y * REGION_LAYER + column]] === 0) {
sky[y * REGION_LAYER + column] = 15;
y--;
}
this.#heights[column] = y + 1;
for (let y = this.#heights[column]; y < region_height; y++) sky[y * REGION_LAYER + column] = 15;
}
// only the lit cells next to a darker one can spread: the bottom of each column's sunlight,
@@ -108,7 +113,7 @@ export class LightRegion {
if (z < REGION_SIZE - 1) {
highest_neighbor = Math.max(highest_neighbor, this.#heights[column + REGION_SIZE]);
}
const top = Math.min(world_height - 1, Math.max(height, highest_neighbor - 1));
const top = Math.min(region_height - 1, Math.max(height, highest_neighbor - 1));
for (let y = height; y <= top; y++) {
this.#push(y * REGION_LAYER + column);
}
@@ -120,10 +125,11 @@ export class LightRegion {
#compute_block_light({ opacity, emission }: LightTables) {
const { blocks, block_light } = this;
block_light.fill(0);
const volume = REGION_LAYER * this.height;
block_light.fill(0, 0, volume);
this.#queue_length = 0;
for (let i = 0; i < blocks.length; i++) {
for (let i = 0; i < volume; i++) {
const level = emission[blocks[i]];
if (level > 0) {
block_light[i] = level;