Rename all "liquid" mentions to "fluid"

This commit is contained in:
2026-10-25 13:54:53 -03:00
parent bcca8fa4a5
commit 0cfa4d9b4a
58 changed files with 547 additions and 548 deletions
+59 -59
View File
@@ -55,8 +55,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";
import { cell_index, fluid_quads, type FluidCell } from "./fluid_mesh.ts";
import { fluid_height, fluid_textures } from "$/common/everything_registry.ts";
type TexturesInfo = Record<string, SpriteRegion>;
@@ -94,11 +94,11 @@ 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);
// 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);
// fluids: their range, 0 for blocks that aren't one, and where their level state is in the state bits
const fluid_range = new Uint8Array(TABLE_SIZE);
const fluid_level_shift = new Uint8Array(TABLE_SIZE);
// waterloggable blocks: the numeric id of the fluid they hold, 0 for other blocks, and where their waterlogged state is
const waterlog_fluid = new Uint16Array(TABLE_SIZE);
const waterlogged_shift = new Uint8Array(TABLE_SIZE);
// blocks with visuals: HAS_VISUAL, or REPLACED_MODEL when the visual draws all of it, see ctx.render.register_visual
const block_visual = new Uint8Array(TABLE_SIZE);
@@ -275,7 +275,7 @@ 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);
waterlog_fluid.fill(0);
blocks_registry.forEach((block, nid) => {
if (!block || nid === AIR) return;
const layer = LAYER_IDS[block.render_layer ?? "solid"];
@@ -286,13 +286,13 @@ 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) {
fluid_range[nid] = block.fluid?.range ?? 0;
if (block.fluid) {
// 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);
fluid_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;
waterlog_fluid[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);
}
@@ -538,8 +538,8 @@ 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) {
// whether a block hides the face of a fluid against it, like show_face. air's layer isn't set, so it's checked first
function hides_fluid(nid: number) {
return nid !== AIR && block_layers[nid] === SOLID;
}
@@ -553,62 +553,62 @@ function region_value(chunks: (Uint32Array | null)[], index: number, y: number)
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]);
// the surface of the fluid with this numeric id in a cell, if the cell is that fluid
function fluid_cell_height(nid: number, value: number) {
const level = ((value >>> 16) >>> fluid_level_shift[nid]) & 7;
return fluid_height(level, fluid_range[nid]);
}
const liquid_cells: LiquidCell[] = Array.from(
const fluid_cells: FluidCell[] = Array.from(
{ length: 9 },
() => ({ same: false, height: 0, air: false, solid: false, top_same: false }),
);
// 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) {
// the numeric id of the fluid in a cell: the block, or what it's waterlogged with. 0 for none
function cell_fluid(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;
if (fluid_range[id]) return id;
const fluid = waterlog_fluid[id];
if (!fluid) return 0;
return ((region_value(chunks, index, y) >>> 16) >>> waterlogged_shift[id]) & 1 ? fluid : 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) {
// the 3x3 cells around the fluid at index and y, see FluidCell. a waterlogged block is a source of its fluid
function fill_fluid_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 cell = fluid_cells[cell_index(dx, dz)];
const at = index + dx + dz * REGION_SIZE;
const id = region_block(region, at, y);
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.same = cell_fluid(chunks, at, y) === nid;
cell.height = !cell.same ? 0 : id === nid ? fluid_cell_height(nid, region_value(chunks, at, y)) : 1;
cell.air = id === AIR;
cell.solid = hides_liquid(id);
cell.top_same = cell_liquid(chunks, at + REGION_LAYER, y + 1) === nid;
cell.solid = hides_fluid(id);
cell.top_same = cell_fluid(chunks, at + REGION_LAYER, y + 1) === nid;
}
}
return liquid_cells;
return fluid_cells;
}
// for a liquid with a flowing texture of its own, how much of it a block shows: as many pixels across as the still one,
// so minecraft's 32×32 flowing water next to its 16×16 still water shows half. see liquid_quads
// for a fluid with a flowing texture of its own, how much of it a block shows: as many pixels across as the still one,
// so minecraft's 32×32 flowing water next to its 16×16 still water shows half. see fluid_quads
function flowing_scale(block: BlockRegistry): number | undefined {
const textures = liquid_textures(block.textures);
const textures = fluid_textures(block.textures);
if (textures.flowing === textures.still) return undefined;
const still = textures_info[textures.still] ?? textures_info["engine:missing"];
const flowing = textures_info[textures.flowing] ?? textures_info["engine:missing"];
return Math.min(1, still.width / flowing.width);
}
// 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(
// a fluid's quad for the mesher, see fluid_mesh.ts. it's lit like a full block's face on that side
function fluid_mesh_quad(
block: BlockRegistry,
face: number,
positions: number[],
uvs: number[],
still: boolean,
): MeshQuad {
const textures = liquid_textures(block.textures);
const textures = fluid_textures(block.textures);
const flat = FACE_AXIS[face] !== 1 || (positions[1] === positions[4] && positions[4] === positions[7] &&
positions[7] === positions[10]);
return {
@@ -625,7 +625,7 @@ function liquid_mesh_quad(
};
}
// the liquid's surface seen from below, so it shows from under water: the top quad facing down, lit and shaded like
// the fluid's surface seen from below, so it shows from under water: the top quad facing down, lit and shaded like
// the top. its corners go the other way round, the lighting the mesher just worked out with them
const UNDERSIDE_CORNERS = [0, 3, 2, 1];
@@ -641,7 +641,7 @@ 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 liquids, whose surface follows their neighbors'
// ones around it for fluids, 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
@@ -707,36 +707,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;
// 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);
// the fluid here, the block's own or what it's waterlogged with, then the block in it
const fluid_nid = cell_fluid(chunks, index, y);
if (fluid_nid) {
const fluid_info = blocks_registry[fluid_nid];
const fluid_layer = block_layers[fluid_nid];
const fluid_alpha = fluid_layer === TRANSLUCENT ? fluid_info.alpha ?? 1 : 1;
const cells = fill_fluid_cells(chunks, fluid_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(
const quads = fluid_quads(
cells,
{
same: cell_liquid(chunks, index + REGION_LAYER, y + 1) === liquid_nid,
solid: hides_liquid(above),
same: cell_fluid(chunks, index + REGION_LAYER, y + 1) === fluid_nid,
solid: hides_fluid(above),
},
{
same: cell_liquid(chunks, index - REGION_LAYER, y - 1) === liquid_nid,
solid: hides_liquid(below),
same: cell_fluid(chunks, index - REGION_LAYER, y - 1) === fluid_nid,
solid: hides_fluid(below),
},
flowing_scale(liquid_info),
flowing_scale(fluid_info),
);
for (const { face, positions, uvs, still } of quads) {
const quad = liquid_mesh_quad(liquid_info, face, positions, uvs, still);
const quad = fluid_mesh_quad(fluid_info, face, positions, uvs, still);
light_quad(quad, index, y, face_offsets);
// like minecraft's liquids, no ambient occlusion
// like minecraft's fluids, no ambient occlusion
corner_ao.fill(1);
emit(quad, liquid_layer, x, y, z, liquid_alpha);
if (face === 0) emit(underside(quad), liquid_layer, x, y, z, liquid_alpha);
emit(quad, fluid_layer, x, y, z, fluid_alpha);
if (face === 0) emit(underside(quad), fluid_layer, x, y, z, fluid_alpha);
}
if (liquid_nid === block_nid) continue;
if (fluid_nid === block_nid) continue;
}
const value = values[y * CHUNK_AREA + z * CHUNK_SIZE + x];
@@ -1,25 +1,25 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// 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
// 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 liquid and level with it, and the cell above that one
export interface LiquidCell {
// the same liquid, at any level
// 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 liquid. 1 is a source
// its surface, 0 to 1, when it's the same fluid. 1 is a source
height: number;
air: boolean;
// hides the side of the liquid facing it, like a full block of stone
// hides the side of the fluid 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
// the cell above it is the same fluid, so the fluid here goes right up to the top
top_same: boolean;
}
export interface LiquidQuad {
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
@@ -36,15 +36,15 @@ 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
// 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 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) {
// 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;
@@ -92,7 +92,7 @@ function quad(
still: boolean,
y_at: (x: number, top: number, z: number) => number,
uv_scale = 1,
): LiquidQuad {
): FluidQuad {
const positions: number[] = [];
const uvs: number[] = [];
for (const [x, top, z] of FACE_CORNERS[face]) {
@@ -104,9 +104,9 @@ function quad(
return { face, positions, uvs, still };
}
// the top of liquid flowing towards (flow_x, flow_z), like minecraft's LiquidBlockRenderer: the flowing texture turned
// 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): LiquidQuad {
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;
@@ -121,18 +121,18 @@ function flowing_top(heights: number[], flow_x: number, flow_z: number, scale: n
return { face: 0, positions, uvs, still: false };
}
// 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. flowing_scale is for a liquid with a flowing texture of its own, how much of
// 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 liquid_quads(
cells: LiquidCell[],
export function fluid_quads(
cells: FluidCell[],
above: { same: boolean; solid: boolean },
below: { same: boolean; solid: boolean },
flowing_scale?: number,
): LiquidQuad[] {
const quads: LiquidQuad[] = [];
): FluidQuad[] {
const quads: FluidQuad[] = [];
// by corner, x then z
const heights = [
corner_height(cells, 0, 0),
@@ -152,7 +152,7 @@ export function liquid_quads(
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 liquid next to it is level with it
// 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);