From d101fa2ef3ca3b327157b0ebc2525b105e8f282e Mon Sep 17 00:00:00 2001 From: Paula Date: Wed, 7 Oct 2026 19:18:10 -0300 Subject: [PATCH] Water flow texture --- client/workers/chunk_worker.ts | 11 ++++++ client/workers/liquid_mesh.ts | 49 +++++++++++++++++++++++---- docs/modding/blocks/index.md | 6 ++-- mods/bworld/blocks/water.json | 2 +- mods/bworld/textures/water_flow.json | 22 ++---------- mods/bworld/textures/water_still.json | 1 - tests/liquid_test.ts | 33 ++++++++++++++++++ 7 files changed, 94 insertions(+), 30 deletions(-) diff --git a/client/workers/chunk_worker.ts b/client/workers/chunk_worker.ts index 32c8670..b8b01b9 100644 --- a/client/workers/chunk_worker.ts +++ b/client/workers/chunk_worker.ts @@ -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); diff --git a/client/workers/liquid_mesh.ts b/client/workers/liquid_mesh.ts index e5014d2..09636ec 100644 --- a/client/workers/liquid_mesh.ts +++ b/client/workers/liquid_mesh.ts @@ -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)); diff --git a/docs/modding/blocks/index.md b/docs/modding/blocks/index.md index 647e994..c2c695a 100644 --- a/docs/modding/blocks/index.md +++ b/docs/modding/blocks/index.md @@ -119,8 +119,10 @@ shore. } ``` -- `textures` is one texture, or `{ still, flowing }`: `still` on its top and bottom, `flowing` on its sides, like - Luanti's special tiles. +- `textures` is one texture, or `{ still, flowing }`, like Minecraft's water. `still` is on its bottom and on a level + top. `flowing` is on its sides and on a sloping top, turned to run downhill. A block shows as many of `flowing`'s + pixels across as `still` has, so a 32×32 `flowing` next to a 16×16 `still` spreads over two blocks. With one texture, + it's all of that texture on every side. Either can be [animated](../resources/textures.md#animated-textures). - `liquid.range` is how many blocks it flows from a source, 1 to 7, and `7` when not set. - `liquid.ticks` is how many ticks each step of its flow takes, `5` when not set, like Minecraft's water. Lava's is 30. - `liquid.renewable`, `true` when not set: a spot with two sources beside it and something solid or a source under it diff --git a/mods/bworld/blocks/water.json b/mods/bworld/blocks/water.json index 3b542d1..ebf5e71 100644 --- a/mods/bworld/blocks/water.json +++ b/mods/bworld/blocks/water.json @@ -2,7 +2,7 @@ "format_version": 1, "block": { "id": "bworld:water", - "textures": "bworld:water_still", + "textures": { "still": "bworld:water_still", "flowing": "bworld:water_flow" }, "tint": { "color": "water" }, "render_layer": "translucent", "light_opacity": 1, diff --git a/mods/bworld/textures/water_flow.json b/mods/bworld/textures/water_flow.json index 2a0afda..d868b0c 100644 --- a/mods/bworld/textures/water_flow.json +++ b/mods/bworld/textures/water_flow.json @@ -1,23 +1,7 @@ { - "animation": { - - "frametime": 1, - - "frames": [ -0, - 1, 2, 3, 4, - 5, - 6, 7, - 8, - 9, 10, - 11, - 12, - 13, - 14, 15 -] - + "frametime": 1, + + "frames": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] } - } - diff --git a/mods/bworld/textures/water_still.json b/mods/bworld/textures/water_still.json index 65c3bf7..9f00e16 100644 --- a/mods/bworld/textures/water_still.json +++ b/mods/bworld/textures/water_still.json @@ -5,4 +5,3 @@ "frames": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] } } - diff --git a/tests/liquid_test.ts b/tests/liquid_test.ts index 565d379..53d34ce 100644 --- a/tests/liquid_test.ts +++ b/tests/liquid_test.ts @@ -233,3 +233,36 @@ Deno.test("the chunk worker draws water sloping down from a source, across a chu assert(!quads.some((corners) => corners.every(([x]) => x === 1))); assert(quads.some((corners) => corners.every(([x]) => x === 2))); }); + +Deno.test("with a flowing texture of its own, a sloping top flows downhill and the sides show part of it", () => { + // level water stays still, whatever the flowing texture + const lake: Record> = {}; + for (let dx = -1; dx <= 1; dx++) for (let dz = -1; dz <= 1; dz++) lake[`${dx},${dz}`] = liquid(1); + assert(liquid_quads(cells(lake), OPEN, SOLID, 0.5).every((q) => q.still)); + + // a source to the -x side, so it flows towards +x + const set = cells({ "0,0": liquid(liquid_height(1, 7)), "-1,0": liquid(1) }); + const quads = liquid_quads(set, OPEN, SOLID, 0.5); + const top = quads.find((q) => q.face === 0)!; + assertEquals(top.still, false); + // the middle half of the texture, turned so v (down the texture) runs with x, downstream + const uv_at = (x: number, z: number) => { + const k = [0, 1, 2, 3].find((k) => top.positions[k * 3] === x && top.positions[k * 3 + 2] === z)!; + return top.uvs.slice(k * 2, k * 2 + 2).map((n) => Math.round(n * 1000) / 1000); + }; + assertEquals(uv_at(0, 0), [12, 4]); + assertEquals(uv_at(1, 0), [12, 12]); + assertEquals(uv_at(0, 1), [4, 4]); + for (const n of top.uvs) assert(n >= 4 - 1e-9 && n <= 12 + 1e-9); + + // the sides show the flowing texture's top left half + for (const side of quads.filter((q) => q.face !== 0)) { + assertEquals(side.still, false); + for (const n of side.uvs) assert(n >= 0 && n <= 8); + } + + // with only one texture, the top stays still and the sides show all of it + const plain = liquid_quads(set, OPEN, SOLID); + assert(plain.find((q) => q.face === 0)!.still); + assert(plain.some((q) => q.face !== 0 && q.uvs.some((n) => n > 8))); +});