| 1 | /** |
| 2 | * Soft-terrain edge blend weights and neighbor sample coords (16×16 tiles). |
| 3 | * |
| 4 | * One-sided (neighbor not blendable): outer 50%, inner 25%. |
| 5 | * Both blendable: outer 40%, inner 20% so the shared 4-pixel strip steps |
| 6 | * 20% → 40% → 60% → 80% toward the other tile instead of a flat 50/50 seam. |
| 7 | * Samples use matching depth into the neighbor (not only the abutting edge). |
| 8 | * Cardinal edges only — diagonals are skipped (corner L-blends read as blotches). |
| 9 | */ |
| 10 | |
| 11 | export const SOFT_TERRAIN_EDGE_PX = 2; |
| 12 | export const SOFT_BLEND_OUTER = 0.5; |
| 13 | export const SOFT_BLEND_INNER = 0.25; |
| 14 | /** When the neighbor also soft-blends, ease weights so both sides form one ramp. */ |
| 15 | export const SOFT_BLEND_OUTER_DUAL = 0.4; |
| 16 | export const SOFT_BLEND_INNER_DUAL = 0.2; |
| 17 | |
| 18 | /** |
| 19 | * Distance from the edge facing neighbor (ndx, ndy): 0 = outer pixel, 1 = one in, null = N/A. |
| 20 | * Diagonal offsets always return null (not blended). |
| 21 | */ |
| 22 | export function softTerrainEdgeDepth (px, py, ndx, ndy, size = 16) |
| 23 | { |
| 24 | const last = size - 1; |
| 25 | let edgeX = null; |
| 26 | let edgeY = null; |
| 27 | if (ndx > 0) |
| 28 | { edgeX = last - px; } |
| 29 | else if (ndx < 0) |
| 30 | { edgeX = px; } |
| 31 | if (ndy > 0) |
| 32 | { edgeY = last - py; } |
| 33 | else if (ndy < 0) |
| 34 | { edgeY = py; } |
| 35 | |
| 36 | if (ndx !== 0 && ndy !== 0) |
| 37 | { |
| 38 | // Diagonals disabled — cardinal strips alone look cleaner at corners. |
| 39 | return null; |
| 40 | } |
| 41 | if (ndx !== 0) |
| 42 | { |
| 43 | if (edgeX == null || edgeX > 1) |
| 44 | { return null; } |
| 45 | return edgeX; |
| 46 | } |
| 47 | if (ndy !== 0) |
| 48 | { |
| 49 | if (edgeY == null || edgeY > 1) |
| 50 | { return null; } |
| 51 | return edgeY; |
| 52 | } |
| 53 | return null; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Blend weight for pixel (px, py) toward neighbor offset (ndx, ndy). |
| 58 | * @param {{ neighborBlendable?: boolean }} [options] |
| 59 | */ |
| 60 | export function softTerrainBlendWeight (px, py, ndx, ndy, size = 16, options = {}) |
| 61 | { |
| 62 | const depth = softTerrainEdgeDepth(px, py, ndx, ndy, size); |
| 63 | if (depth == null) |
| 64 | { return 0; } |
| 65 | const dual = !!options.neighborBlendable; |
| 66 | if (depth === 0) |
| 67 | { return dual ? SOFT_BLEND_OUTER_DUAL : SOFT_BLEND_OUTER; } |
| 68 | return dual ? SOFT_BLEND_INNER_DUAL : SOFT_BLEND_INNER; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Neighbor sprite pixel for this blend. |
| 73 | * Samples `depth` pixels into the neighbor from the abutting edge/corner |
| 74 | * so inner and outer self pixels pull toward different neighbor colors. |
| 75 | */ |
| 76 | export function softTerrainNeighborSample (px, py, ndx, ndy, size = 16) |
| 77 | { |
| 78 | const last = size - 1; |
| 79 | let edgeX = null; |
| 80 | let edgeY = null; |
| 81 | if (ndx > 0) |
| 82 | { edgeX = last - px; } |
| 83 | else if (ndx < 0) |
| 84 | { edgeX = px; } |
| 85 | if (ndy > 0) |
| 86 | { edgeY = last - py; } |
| 87 | else if (ndy < 0) |
| 88 | { edgeY = py; } |
| 89 | |
| 90 | const sx = ndx > 0 |
| 91 | ? Math.max(0, Math.min(last, edgeX ?? 0)) |
| 92 | : (ndx < 0 ? Math.max(0, Math.min(last, last - (edgeX ?? 0))) : px); |
| 93 | const sy = ndy > 0 |
| 94 | ? Math.max(0, Math.min(last, edgeY ?? 0)) |
| 95 | : (ndy < 0 ? Math.max(0, Math.min(last, last - (edgeY ?? 0))) : py); |
| 96 | return { sx, sy }; |
| 97 | } |
| 98 | |
| 99 | /** Cardinal neighbor offsets only (diagonals looked like corner blotches). */ |
| 100 | export const SOFT_TERRAIN_NEIGHBOR_DIRS = Object.freeze([ |
| 101 | Object.freeze({ ndx: 0, ndy: -1 }), |
| 102 | Object.freeze({ ndx: 1, ndy: 0 }), |
| 103 | Object.freeze({ ndx: 0, ndy: 1 }), |
| 104 | Object.freeze({ ndx: -1, ndy: 0 }) |
| 105 | ]); |
| 106 | |
| 107 | /** |
| 108 | * Mix self RGBA with weighted neighbor samples (weighted-average, then lerp by total weight). |
| 109 | * @param {{ r: number, g: number, b: number, a: number }} self |
| 110 | * @param {Array<{ color: { r: number, g: number, b: number, a: number }, weight: number }>} contribs |
| 111 | */ |
| 112 | export function mixSoftTerrainPixel (self, contribs) |
| 113 | { |
| 114 | if (!contribs?.length) |
| 115 | { return self; } |
| 116 | let totalW = 0; |
| 117 | let r = 0; |
| 118 | let g = 0; |
| 119 | let b = 0; |
| 120 | let a = 0; |
| 121 | for (const entry of contribs) |
| 122 | { |
| 123 | const w = Math.max(0, entry.weight); |
| 124 | if (w <= 0) |
| 125 | { continue; } |
| 126 | totalW += w; |
| 127 | r += entry.color.r * w; |
| 128 | g += entry.color.g * w; |
| 129 | b += entry.color.b * w; |
| 130 | a += entry.color.a * w; |
| 131 | } |
| 132 | if (totalW <= 0) |
| 133 | { return self; } |
| 134 | const inv = 1 / totalW; |
| 135 | const avg = { r: r * inv, g: g * inv, b: b * inv, a: a * inv }; |
| 136 | const t = Math.min(1, totalW); |
| 137 | const keep = 1 - t; |
| 138 | return { |
| 139 | r: Math.round(self.r * keep + avg.r * t), |
| 140 | g: Math.round(self.g * keep + avg.g * t), |
| 141 | b: Math.round(self.b * keep + avg.b * t), |
| 142 | a: Math.round(self.a * keep + avg.a * t) |
| 143 | }; |
| 144 | } |