Water flowing

This commit is contained in:
2026-10-07 00:09:08 -03:00
parent 69c5c187db
commit b830196829
14 changed files with 1033 additions and 55 deletions
+135 -32
View File
@@ -52,6 +52,8 @@ import {
region_sky,
REGION_VOID,
} from "./lighting.ts";
import { cell_index, liquid_quads, type LiquidCell } from "./liquid_mesh.ts";
import { liquid_height, liquid_textures } from "$/common/everything_registry.ts";
type TexturesInfo = Record<string, SpriteRegion>;
@@ -92,6 +94,9 @@ const block_occludes = new Uint8Array(TABLE_SIZE);
// light can come around a corner past it
const block_lets_light_by = new Uint8Array(TABLE_SIZE);
const light_tables: LightTables = { opacity: new Uint8Array(TABLE_SIZE), emission: new Uint8Array(TABLE_SIZE) };
// 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);
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
@@ -137,7 +142,7 @@ self.onmessage = (event: MessageEvent<ToChunkWorker>) => {
const { solid, cutout, translucent, min_y, max_y } = make_chunk_mesh(
message.chunk_x,
message.chunk_z,
message.chunks[4]!,
message.chunks,
message.camera,
);
const light = chunk_light();
@@ -262,6 +267,11 @@ function build_block_tables() {
block_lets_light_by[nid] = layer !== SOLID || opacity === 0 ? 1 : 0;
light_tables.opacity[nid] = opacity;
light_tables.emission[nid] = block_light_emission(block);
liquid_range[nid] = block.liquid?.range ?? 0;
if (block.liquid) {
// 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);
}
});
block_lets_light_by[AIR] = 1;
block_layers[REGION_VOID] = SOLID;
@@ -505,9 +515,79 @@ function light_quad(quad: MeshQuad, index: number, y: number, face_offsets: numb
corner_ao.fill(1);
}
// whether a block hides the face of a liquid against it, like show_face. air's layer isn't set, so it's checked first
function hides_liquid(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
function region_value(chunks: (Uint32Array | null)[], index: number, y: number) {
if (y < 0 || y >= CHUNK_HEIGHT) return AIR;
const x = index % REGION_SIZE;
const z = Math.floor(index / REGION_SIZE) % REGION_SIZE;
const chunk = chunks[Math.floor(z / CHUNK_SIZE) * 3 + Math.floor(x / CHUNK_SIZE)];
return chunk ? chunk[y * CHUNK_AREA + (z % CHUNK_SIZE) * CHUNK_SIZE + (x % CHUNK_SIZE)] : AIR;
}
// the surface of the liquid with this numeric id in a cell, if the cell is that liquid
function liquid_cell_height(nid: number, value: number) {
const level = ((value >>> 16) >>> liquid_level_shift[nid]) & 7;
return liquid_height(level, liquid_range[nid]);
}
const liquid_cells: LiquidCell[] = Array.from(
{ length: 9 },
() => ({ same: false, height: 0, air: false, solid: false, top_same: false }),
);
// the 3x3 cells around the liquid at index and y, see LiquidCell
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.air = id === AIR;
cell.solid = hides_liquid(id);
cell.top_same = region_block(region, at + REGION_LAYER, y + 1) === nid;
}
}
return liquid_cells;
}
// a liquid's quad for the mesher, see liquid_mesh.ts. it's lit like a full block's face on that side
function liquid_mesh_quad(
block: BlockRegistry,
face: number,
positions: number[],
uvs: number[],
still: boolean,
): MeshQuad {
const textures = liquid_textures(block.textures);
const flat = FACE_AXIS[face] !== 1 || (positions[1] === positions[4] && positions[4] === positions[7] &&
positions[7] === positions[10]);
return {
positions,
uvs,
face,
cull: -1,
flush: true,
group: flat ? face : UNALIGNED_GROUP,
shade: FACE_SHADE[face],
sprite: textures_info[still ? textures.still : textures.flowing] ?? textures_info["engine:missing"],
light_weights: light_weights(face, positions),
tint: quad_tint(block, [still ? "still" : "flowing"]),
};
}
// region has to be filled and lit first
// values is the middle chunk's blocks with their states, for models that change with them
function make_chunk_mesh(chunk_x: number, chunk_z: number, values: Uint32Array, camera: number[]) {
// 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 liquids, whose surface follows their neighbors'
function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Uint32Array | null)[], camera: number[]) {
const values = 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 }))
@@ -522,6 +602,39 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, values: Uint32Array,
// 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);
// 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 wz = chunk_z * CHUNK_SIZE + z;
const tint = quad.tint === GRASS
? blended[z * CHUNK_SIZE + x]
: quad.tint === FOLIAGE
? blended[CHUNK_AREA + z * CHUNK_SIZE + x]
: quad.tint;
if (layer_id !== TRANSLUCENT) {
const group = opaque[layer_id][quad.group];
push_quad(group.buffer, quad, wx, y, wz, alpha, tint);
if (quad.group !== UNALIGNED_GROUP) {
const axis = FACE_AXIS[quad.face];
const plane = (axis === 0 ? wx : axis === 1 ? y : wz) + quad.positions[axis];
if (plane < group.min) group.min = plane;
if (plane > group.max) group.max = plane;
}
} else {
push_quad(translucent_quads, quad, wx, y, wz, alpha, tint);
// sorting treats every quad as facing along an axis, rotated ones too
const q = translucent_quads.count - 1;
centers = ensure_capacity(centers, (q + 1) * 3);
faces = ensure_capacity(faces, q + 1);
const p = quad.positions;
centers[q * 3] = wx + (p[0] + p[6]) / 2;
centers[q * 3 + 1] = y + (p[1] + p[7]) / 2;
centers[q * 3 + 2] = wz + (p[2] + p[8]) / 2;
faces[q] = quad.face;
}
};
for (let y = 0; y < CHUNK_HEIGHT; y++) {
for (let z = 0; z < CHUNK_SIZE; z++) {
for (let x = 0; x < CHUNK_SIZE; x++) {
@@ -534,8 +647,24 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, values: Uint32Array,
const layer_id = block_layers[block_nid];
const alpha = layer_id === TRANSLUCENT ? block_info.alpha ?? 1 : 1;
const wx = chunk_x * CHUNK_SIZE + x;
const wz = chunk_z * CHUNK_SIZE + z;
if (liquid_range[block_nid]) {
const cells = fill_liquid_cells(chunks, block_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) },
);
for (const { face, positions, uvs, still } of quads) {
const quad = liquid_mesh_quad(block_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);
}
continue;
}
for (const quad of block_quads(values[y * CHUNK_AREA + z * CHUNK_SIZE + x])) {
if (quad.cull >= 0) {
@@ -548,33 +677,7 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, values: Uint32Array,
}
light_quad(quad, index, y, face_offsets);
const tint = quad.tint === GRASS
? blended[z * CHUNK_SIZE + x]
: quad.tint === FOLIAGE
? blended[CHUNK_AREA + z * CHUNK_SIZE + x]
: quad.tint;
if (layer_id !== TRANSLUCENT) {
const group = opaque[layer_id][quad.group];
push_quad(group.buffer, quad, wx, y, wz, alpha, tint);
if (quad.group !== UNALIGNED_GROUP) {
const axis = FACE_AXIS[quad.face];
const plane = (axis === 0 ? wx : axis === 1 ? y : wz) + quad.positions[axis];
if (plane < group.min) group.min = plane;
if (plane > group.max) group.max = plane;
}
} else {
push_quad(translucent_quads, quad, wx, y, wz, alpha, tint);
// sorting treats every quad as facing along an axis, rotated ones too
const q = translucent_quads.count - 1;
centers = ensure_capacity(centers, (q + 1) * 3);
faces = ensure_capacity(faces, q + 1);
const p = quad.positions;
centers[q * 3] = wx + (p[0] + p[6]) / 2;
centers[q * 3 + 1] = y + (p[1] + p[7]) / 2;
centers[q * 3 + 2] = wz + (p[2] + p[8]) / 2;
faces[q] = quad.face;
}
emit(quad, layer_id, x, y, z, alpha);
}
}
}
+139
View File
@@ -0,0 +1,139 @@
// liquids drawn like luanti's flowing liquids (MapblockMeshGenerator::drawLiquidNode in its content_mapblock.cpp): each
// corner of the surface is as high as the liquid around it, so it slopes down where it flows away from a source. the
// sides between two cells of it are left out, except where it pours down past the surface of the one beside it
import { FACE_CORNERS } from "$/common/block_models.ts";
// one of the 3x3 cells around a liquid and level with it, and the cell above that one
export interface LiquidCell {
// the same liquid, at any level
same: boolean;
// its surface, 0 to 1, when it's the same liquid. 1 is a source
height: number;
air: boolean;
// hides the side of the liquid facing it, like a full block of stone
solid: boolean;
// the cell above it is the same liquid, so the liquid here goes right up to the top
top_same: boolean;
}
export interface LiquidQuad {
// the mesher's face index, see FACE_CORNERS
face: number;
// 4 corners in FACE_CORNERS' order, x y z in block space
positions: number[];
// u v per corner, in pixels of the texture
uvs: number[];
// drawn with the still texture, the top and bottom. the sides are flowing
still: boolean;
}
// how far above the cell's floor a surface with air on two sides still is, luanti's 0.2 / BS
const SHORE = 0.02;
// the horizontal neighbor each side faces, as dx dz, for faces 2 to 5: south (+z), north (-z), west (-x), east (+x)
const SIDE_DIRECTIONS = [[0, 1], [0, -1], [-1, 0], [1, 0]];
// cells indexed by (dz + 1) * 3 + (dx + 1), so 4 is the liquid itself
export function cell_index(dx: number, dz: number) {
return (dz + 1) * 3 + dx + 1;
}
// like luanti's getCornerLevel: the height of the surface at a corner (cx and cz are 0 or 1, which end of the cell),
// from the 4 cells that share it. a source or liquid above any of them makes it full, two of air pull it down to the
// floor, otherwise it's the average of the liquid among them
export function corner_height(cells: LiquidCell[], cx: number, cz: number) {
let sum = 0;
let count = 0;
let air = 0;
for (let dz = cz - 1; dz <= cz; dz++) {
for (let dx = cx - 1; dx <= cx; dx++) {
const cell = cells[cell_index(dx, dz)];
if (cell.top_same) return 1;
if (cell.same) {
if (cell.height >= 1) return 1;
sum += cell.height;
count++;
} else if (cell.air) {
air++;
}
}
}
if (air >= 2) return SHORE;
if (count > 0) return sum / count;
return 0.5;
}
// the part of the texture each face shows at a point, like a full block's default uvs (see default_uv in
// common/block_models.ts), so a lower side shows the bottom of the texture instead of squashing all of it
function face_uv(face: number, x: number, y: number, z: number): [number, number] {
switch (face) {
case 0:
return [x * 16, z * 16];
case 1:
return [x * 16, 16 - z * 16];
case 2:
return [x * 16, 16 - y * 16];
case 3:
return [16 - x * 16, 16 - y * 16];
case 4:
return [z * 16, 16 - y * 16];
default:
return [16 - z * 16, 16 - y * 16];
}
}
// a face's quad with the y of each corner from y_at (given the corner's x, its end on y, and z)
function quad(face: number, still: boolean, y_at: (x: number, top: number, z: number) => number): LiquidQuad {
const positions: number[] = [];
const uvs: number[] = [];
for (const [x, top, z] of FACE_CORNERS[face]) {
const y = y_at(x, top, z);
positions.push(x, y, z);
uvs.push(...face_uv(face, x, y, z));
}
return { face, positions, uvs, still };
}
// the quads of the liquid in the middle cell. above and below are the cells over and under it: whether they're the
// same liquid, and whether they're solid
export function liquid_quads(
cells: LiquidCell[],
above: { same: boolean; solid: boolean },
below: { same: boolean; solid: boolean },
): LiquidQuad[] {
const quads: LiquidQuad[] = [];
// by corner, x then z
const heights = [
corner_height(cells, 0, 0),
corner_height(cells, 0, 1),
corner_height(cells, 1, 0),
corner_height(cells, 1, 1),
];
const height = (x: number, z: number) => heights[x * 2 + z];
SIDE_DIRECTIONS.forEach(([dx, dz], i) => {
const face = i + 2;
const neighbor = cells[cell_index(dx, dz)];
// nothing between two cells of it, unless it goes higher here and pours down past the other's surface
if (neighbor.same && (!above.same || neighbor.top_same)) return;
if (neighbor.solid) return;
const side = quad(face, false, (x, top, z) => {
if (top) return above.same ? 1 : height(x, z);
return neighbor.same ? height(x, z) : 0;
});
// a side with no height, where the liquid next to it is level with it
const p = side.positions;
if (p[1] === p[4] && p[4] === p[7] && p[7] === p[10]) return;
quads.push(side);
});
// a full surface right under a solid block can't be seen
const full = heights.every((h) => h === 1);
if (!above.same && !(above.solid && full)) {
quads.push(quad(0, true, (x, _top, z) => height(x, z)));
}
if (!below.same && !below.solid) {
quads.push(quad(1, true, () => 0));
}
return quads;
}