Waterlogging

This commit is contained in:
2026-10-07 20:56:24 -03:00
parent 78117f65c9
commit 757ee74bc7
26 changed files with 459 additions and 73 deletions
+45 -13
View File
@@ -97,6 +97,9 @@ const light_tables: LightTables = { opacity: new Uint8Array(TABLE_SIZE), emissio
// liquids: their range, 0 for blocks that aren't one, and where their level state is in the state bits
const liquid_range = new Uint8Array(TABLE_SIZE);
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);
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
@@ -259,6 +262,10 @@ function quad_tint(block: BlockRegistry, variables: string[]) {
}
function build_block_tables() {
// the registry has gaps, which forEach skips
const nids = new Map<string, number>();
blocks_registry.forEach((block, nid) => block && nids.set(block.id, nid));
waterlog_liquid.fill(0);
blocks_registry.forEach((block, nid) => {
if (!block || nid === AIR) return;
const layer = LAYER_IDS[block.render_layer ?? "solid"];
@@ -274,6 +281,11 @@ function build_block_tables() {
// the level state is the last one, see block_from_json
liquid_level_shift[nid] = (block.states ?? []).slice(0, -1).reduce((bits, state) => bits + state.bits, 0);
}
if (block.waterloggable) {
waterlog_liquid[nid] = nids.get(block.waterloggable) ?? 0;
// the waterlogged state is the last one, see block_from_json
waterlogged_shift[nid] = (block.states ?? []).slice(0, -1).reduce((bits, state) => bits + state.bits, 0);
}
});
block_lets_light_by[AIR] = 1;
block_layers[REGION_VOID] = SOLID;
@@ -543,18 +555,27 @@ const liquid_cells: LiquidCell[] = Array.from(
() => ({ same: false, height: 0, air: false, solid: false, top_same: false }),
);
// the 3x3 cells around the liquid at index and y, see LiquidCell
// the numeric id of the liquid in a cell: the block, or what it's waterlogged with. 0 for none
function cell_liquid(chunks: (Uint32Array | null)[], index: number, y: number) {
const id = region_block(region, index, y);
if (liquid_range[id]) return id;
const liquid = waterlog_liquid[id];
if (!liquid) return 0;
return ((region_value(chunks, index, y) >>> 16) >>> waterlogged_shift[id]) & 1 ? liquid : 0;
}
// the 3x3 cells around the liquid at index and y, see LiquidCell. a waterlogged block is a source of its liquid
function fill_liquid_cells(chunks: (Uint32Array | null)[], nid: number, index: number, y: number) {
for (let dz = -1; dz <= 1; dz++) {
for (let dx = -1; dx <= 1; dx++) {
const cell = liquid_cells[cell_index(dx, dz)];
const at = index + dx + dz * REGION_SIZE;
const id = region_block(region, at, y);
cell.same = id === nid;
cell.height = cell.same ? liquid_cell_height(nid, region_value(chunks, at, y)) : 0;
cell.same = cell_liquid(chunks, at, y) === nid;
cell.height = !cell.same ? 0 : id === nid ? liquid_cell_height(nid, region_value(chunks, at, y)) : 1;
cell.air = id === AIR;
cell.solid = hides_liquid(id);
cell.top_same = region_block(region, at + REGION_LAYER, y + 1) === nid;
cell.top_same = cell_liquid(chunks, at + REGION_LAYER, y + 1) === nid;
}
}
return liquid_cells;
@@ -675,25 +696,36 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Uint32Array
const layer_id = block_layers[block_nid];
const alpha = layer_id === TRANSLUCENT ? block_info.alpha ?? 1 : 1;
if (liquid_range[block_nid]) {
const cells = fill_liquid_cells(chunks, block_nid, index, y);
// the liquid here, the block's own or what it's waterlogged with, then the block in it
const liquid_nid = cell_liquid(chunks, index, y);
if (liquid_nid) {
const liquid_info = blocks_registry[liquid_nid];
const liquid_layer = block_layers[liquid_nid];
const liquid_alpha = liquid_layer === TRANSLUCENT ? liquid_info.alpha ?? 1 : 1;
const cells = fill_liquid_cells(chunks, liquid_nid, index, y);
const above = region_block(region, index + REGION_LAYER, y + 1);
const below = region_block(region, index - REGION_LAYER, y - 1);
const quads = liquid_quads(
cells,
{ same: above === block_nid, solid: hides_liquid(above) },
{ same: below === block_nid, solid: hides_liquid(below) },
flowing_scale(block_info),
{
same: cell_liquid(chunks, index + REGION_LAYER, y + 1) === liquid_nid,
solid: hides_liquid(above),
},
{
same: cell_liquid(chunks, index - REGION_LAYER, y - 1) === liquid_nid,
solid: hides_liquid(below),
},
flowing_scale(liquid_info),
);
for (const { face, positions, uvs, still } of quads) {
const quad = liquid_mesh_quad(block_info, face, positions, uvs, still);
const quad = liquid_mesh_quad(liquid_info, face, positions, uvs, still);
light_quad(quad, index, y, face_offsets);
// like minecraft's liquids, no ambient occlusion
corner_ao.fill(1);
emit(quad, layer_id, x, y, z, alpha);
if (face === 0) emit(underside(quad), layer_id, x, y, z, alpha);
emit(quad, liquid_layer, x, y, z, liquid_alpha);
if (face === 0) emit(underside(quad), liquid_layer, x, y, z, liquid_alpha);
}
continue;
if (liquid_nid === block_nid) continue;
}
for (const quad of block_quads(values[y * CHUNK_AREA + z * CHUNK_SIZE + x])) {