Water tweaks

This commit is contained in:
2026-10-07 11:43:19 -03:00
parent 40d5a4f5af
commit 8f8030c02e
4 changed files with 63 additions and 4 deletions
+11 -2
View File
@@ -202,6 +202,12 @@ async function mesh(blocks: [number, number, number, string, number?][]) {
return quads;
}
// the y of a quad's normal from its corners' winding, counterclockwise is the front
function facing_y(corners: number[][]) {
const [a, b, c] = corners;
return (b[2] - a[2]) * (c[0] - a[0]) - (b[0] - a[0]) * (c[2] - a[2]);
}
Deno.test("the chunk worker draws water sloping down from a source, across a chunk's edge too", async () => {
// three rows of it: a source at x -1, in the chunk to the -x side, then flowing water at x 0 and 1, on stone
const blocks: [number, number, number, string, number?][] = [];
@@ -210,9 +216,12 @@ Deno.test("the chunk worker draws water sloping down from a source, across a chu
blocks.push([0, 9, z, "bworld:stone"], [1, 9, z, "bworld:stone"]);
}
const quads = await mesh(blocks);
// the tops of the middle chunk's water, by the x of their corners
// the tops of the middle chunk's water, by the x of their corners. each is drawn facing up and, to be seen from
// under water, facing down
const tops = quads.filter((corners) => corners.every(([, y]) => y > 10));
assertEquals(tops.length, 6);
assertEquals(tops.length, 12);
assertEquals(tops.filter((corners) => facing_y(corners) > 0).length, 6);
assertEquals(tops.filter((corners) => facing_y(corners) < 0).length, 6);
const height_at = (x: number) =>
Math.max(...tops.flat().filter(([cx]) => Math.abs(cx - x) < 1e-6).map(([, y]) => y - 10));
// full where it meets the source, then down