Files
bworld/client/workers/liquid_mesh.ts
T
2026-10-07 00:09:08 -03:00

140 lines
4.9 KiB
TypeScript

// 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;
}