178 lines
6.6 KiB
TypeScript
178 lines
6.6 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||
|
||
// fluids drawn like luanti's flowing fluids (MapblockMeshGenerator::drawLiquidNode in its content_mapblock.cpp): each
|
||
// corner of the surface is as high as the fluid 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 fluid and level with it, and the cell above that one
|
||
export interface FluidCell {
|
||
// the same fluid, at any level
|
||
same: boolean;
|
||
// its surface, 0 to 1, when it's the same fluid. 1 is a source
|
||
height: number;
|
||
air: boolean;
|
||
// hides the side of the fluid facing it, like a full block of stone
|
||
solid: boolean;
|
||
// the cell above it is the same fluid, so the fluid here goes right up to the top
|
||
top_same: boolean;
|
||
}
|
||
|
||
export interface FluidQuad {
|
||
// 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 bottom, and the top unless it flows. 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 fluid 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 fluid above any of them makes it full, two of air pull it down to the
|
||
// floor, otherwise it's the average of the fluid among them
|
||
export function corner_height(cells: FluidCell[], 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). uv_scale shrinks the
|
||
// part of the texture it shows, from the texture's corner
|
||
function quad(
|
||
face: number,
|
||
still: boolean,
|
||
y_at: (x: number, top: number, z: number) => number,
|
||
uv_scale = 1,
|
||
): FluidQuad {
|
||
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);
|
||
const [u, v] = face_uv(face, x, y, z);
|
||
uvs.push(u * uv_scale, v * uv_scale);
|
||
}
|
||
return { face, positions, uvs, still };
|
||
}
|
||
|
||
// the top of fluid flowing towards (flow_x, flow_z), like minecraft's LiquidBlockRenderer: the flowing texture turned
|
||
// so it runs downstream, a scale-sized square from its middle
|
||
function flowing_top(heights: number[], flow_x: number, flow_z: number, scale: number): FluidQuad {
|
||
const angle = Math.atan2(flow_z, flow_x) - Math.PI / 2;
|
||
const cos = Math.cos(angle) * 16 * scale;
|
||
const sin = Math.sin(angle) * 16 * scale;
|
||
const positions: number[] = [];
|
||
const uvs: number[] = [];
|
||
for (const [x, , z] of FACE_CORNERS[0]) {
|
||
positions.push(x, heights[x * 2 + z], z);
|
||
const dx = x - 0.5;
|
||
const dz = z - 0.5;
|
||
uvs.push(8 + dx * cos + dz * sin, 8 + dz * cos - dx * sin);
|
||
}
|
||
return { face: 0, positions, uvs, still: false };
|
||
}
|
||
|
||
// the quads of the fluid in the middle cell. above and below are the cells over and under it: whether they're the
|
||
// same fluid, and whether they're solid. flowing_scale is for a fluid with a flowing texture of its own, how much of
|
||
// it a block shows, so it's as sharp as the still one (a 32×32 one next to a 16×16 still one shows half, like
|
||
// minecraft's water). with it, the sides show that much and a sloping top flows. without, the sides show all of the
|
||
// texture and the top is always still
|
||
export function fluid_quads(
|
||
cells: FluidCell[],
|
||
above: { same: boolean; solid: boolean },
|
||
below: { same: boolean; solid: boolean },
|
||
flowing_scale?: number,
|
||
): FluidQuad[] {
|
||
const quads: FluidQuad[] = [];
|
||
// 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;
|
||
}, flowing_scale);
|
||
// a side with no height, where the fluid 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)) {
|
||
// downhill, from how the corners slope
|
||
const flow_x = height(0, 0) + height(0, 1) - height(1, 0) - height(1, 1);
|
||
const flow_z = height(0, 0) + height(1, 0) - height(0, 1) - height(1, 1);
|
||
if (flowing_scale !== undefined && (Math.abs(flow_x) > 1e-6 || Math.abs(flow_z) > 1e-6)) {
|
||
quads.push(flowing_top(heights, flow_x, flow_z, flowing_scale));
|
||
} else {
|
||
quads.push(quad(0, true, (x, _top, z) => height(x, z)));
|
||
}
|
||
}
|
||
if (!below.same && !below.solid) {
|
||
quads.push(quad(1, true, () => 0));
|
||
}
|
||
return quads;
|
||
}
|