BlueGit

timfa questward

Files

questward / shared / render / wetSand.js

wetSand.js

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