BlueGit

timfa questward

Files

questward / shared / render / wetSand.js

wetSand.js

wetSand.js 2.8 KiB Download Raw
1/**
2 * Wet-sand edge blend toward liquid water.
3 *
4 * Within the outer 30% of a sand tile facing water (cardinal or diagonal),
5 * pixels lerp toward the wet sprite: edge = 100% wet, beyond 30% = dry.
6 * In winter, shallow water is ice — only deep water counts (see isLiquidWaterGround).
7 */
8
9export const WET_SAND_BAND_FRACTION = 0.3;
10
11/** All eight neighbors — diagonals wet the corner fan toward water. */
12export const WET_SAND_NEIGHBOR_DIRS = Object.freeze([
13 Object.freeze({ ndx: -1, ndy: -1 }),
14 Object.freeze({ ndx: 0, ndy: -1 }),
15 Object.freeze({ ndx: 1, ndy: -1 }),
16 Object.freeze({ ndx: -1, ndy: 0 }),
17 Object.freeze({ ndx: 1, ndy: 0 }),
18 Object.freeze({ ndx: -1, ndy: 1 }),
19 Object.freeze({ ndx: 0, ndy: 1 }),
20 Object.freeze({ ndx: 1, ndy: 1 })
21]);
22
23/**
24 * Distance in pixels from the edge(s) facing neighbor (ndx, ndy).
25 * Cardinal: axis distance from that edge.
26 * Diagonal: Chebyshev max so the wet region is a square fan from the corner.
27 * @returns {number|null}
28 */
29export function wetSandEdgeDistance (px, py, ndx, ndy, size = 16)
30{
31 const last = size - 1;
32 let edgeX = null;
33 let edgeY = null;
34 if (ndx > 0)
35 { edgeX = last - px; }
36 else if (ndx < 0)
37 { edgeX = px; }
38 if (ndy > 0)
39 { edgeY = last - py; }
40 else if (ndy < 0)
41 { edgeY = py; }
42
43 if (ndx !== 0 && ndy !== 0)
44 {
45 if (edgeX == null || edgeY == null)
46 { return null; }
47 return Math.max(edgeX, edgeY);
48 }
49 if (ndx !== 0)
50 { return edgeX; }
51 if (ndy !== 0)
52 { return edgeY; }
53 return null;
54}
55
56/**
57 * Wet blend weight for pixel (px, py) toward water neighbor (ndx, ndy).
58 * 1 at the abutting edge/corner, 0 beyond WET_SAND_BAND_FRACTION of the tile.
59 */
60export function wetSandBlendWeight (px, py, ndx, ndy, size = 16)
61{
62 const dist = wetSandEdgeDistance(px, py, ndx, ndy, size);
63 if (dist == null)
64 { return 0; }
65 const band = WET_SAND_BAND_FRACTION * size;
66 if (!(band > 0) || dist >= band)
67 { return 0; }
68 return 1 - dist / band;
69}
70
71/**
72 * Max wetness from all liquid-water neighbor directions.
73 * @param {Array<{ ndx: number, ndy: number }>} waterDirs
74 */
75export function wetSandWetness (px, py, waterDirs, size = 16)
76{
77 let max = 0;
78 for (const dir of waterDirs ?? [])
79 {
80 const w = wetSandBlendWeight(px, py, dir.ndx, dir.ndy, size);
81 if (w > max)
82 { max = w; }
83 }
84 return max;
85}
86
87/**
88 * Lerp dry → wet RGBA by wetness t in [0,1].
89 * @param {{ r: number, g: number, b: number, a: number }} dry
90 * @param {{ r: number, g: number, b: number, a: number }} wet
91 */
92export function mixWetSandPixel (dry, wet, t)
93{
94 const w = Math.max(0, Math.min(1, Number(t) || 0));
95 if (w <= 0)
96 { return dry; }
97 if (w >= 1)
98 { return wet; }
99 const keep = 1 - w;
100 return {
101 r: Math.round(dry.r * keep + wet.r * w),
102 g: Math.round(dry.g * keep + wet.g * w),
103 b: Math.round(dry.b * keep + wet.b * w),
104 a: Math.round(dry.a * keep + wet.a * w)
105 };
106}