Water flow texture
This commit is contained in:
@@ -560,6 +560,16 @@ function fill_liquid_cells(chunks: (Uint32Array | null)[], nid: number, index: n
|
||||
return liquid_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
|
||||
function flowing_scale(block: BlockRegistry): number | undefined {
|
||||
const textures = liquid_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(
|
||||
block: BlockRegistry,
|
||||
@@ -673,6 +683,7 @@ function make_chunk_mesh(chunk_x: number, chunk_z: number, chunks: (Uint32Array
|
||||
cells,
|
||||
{ same: above === block_nid, solid: hides_liquid(above) },
|
||||
{ same: below === block_nid, solid: hides_liquid(below) },
|
||||
flowing_scale(block_info),
|
||||
);
|
||||
for (const { face, positions, uvs, still } of quads) {
|
||||
const quad = liquid_mesh_quad(block_info, face, positions, uvs, still);
|
||||
|
||||
@@ -23,7 +23,7 @@ export interface LiquidQuad {
|
||||
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
|
||||
// drawn with the still texture: the bottom, and the top unless it flows. the sides are flowing
|
||||
still: boolean;
|
||||
}
|
||||
|
||||
@@ -82,24 +82,52 @@ function face_uv(face: number, x: number, y: number, z: number): [number, number
|
||||
}
|
||||
}
|
||||
|
||||
// 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 {
|
||||
// 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,
|
||||
): 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));
|
||||
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 liquid 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 {
|
||||
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 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
|
||||
// same liquid, and whether they're solid. flowing_scale is for a liquid 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[],
|
||||
above: { same: boolean; solid: boolean },
|
||||
below: { same: boolean; solid: boolean },
|
||||
flowing_scale?: number,
|
||||
): LiquidQuad[] {
|
||||
const quads: LiquidQuad[] = [];
|
||||
// by corner, x then z
|
||||
@@ -120,7 +148,7 @@ export function liquid_quads(
|
||||
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 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;
|
||||
@@ -130,7 +158,14 @@ export function liquid_quads(
|
||||
// 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)));
|
||||
// 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));
|
||||
|
||||
Reference in New Issue
Block a user