dsh-game-material-master 0.2.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +383 -0
- package/README.md +83 -6
- package/lib/client.js +4436 -877
- package/lib/config.js +45 -5
- package/lib/directions.js +120 -5
- package/lib/index.js +375 -12
- package/lib/jsonio.js +49 -3
- package/lib/links.js +10 -2
- package/lib/tilegen.js +1702 -0
- package/lib/tilegeom.js +1392 -0
- package/lib/tilemap.js +560 -0
- package/lib/tilemedia.js +243 -0
- package/lib/tools.js +397 -19
- package/lib/wire.js +100 -4
- package/package.json +3 -2
package/lib/tilegeom.js
ADDED
|
@@ -0,0 +1,1392 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 地图地块模块的**几何内核**。
|
|
3
|
+
*
|
|
4
|
+
* 纯计算、不碰网络、不碰磁盘 —— 唯一有真实算法复杂度的部分,可以脱离 DSH 单测。
|
|
5
|
+
* 与研究期验证过的 Python 实现逐函数对应(`research/tile-isometric/tools/`),
|
|
6
|
+
* TS 版必须跑出同样的数字,见 `scripts/verify-tile.mjs`。
|
|
7
|
+
*
|
|
8
|
+
* ## 三条不可动摇的原则(来自调研报告,违规会静默出错)
|
|
9
|
+
*
|
|
10
|
+
* 1. **几何量必须从图里量,不允许硬编码任何尺寸。**
|
|
11
|
+
* 研究期把一个 2048 的模板图压到 1536 之后,写死的「宽 1884 / 高 942 / 中心 1024」
|
|
12
|
+
* 全部失效,地块被裁出透明缺口、拼图能看见网格线,**而且不报任何错**。
|
|
13
|
+
* 2. **规整(量 → 对齐 → 裁)不可跳过。** 模型即使走模板填充仍有 0.2%~0.6% 漂移,
|
|
14
|
+
* 位置也不固定;只有模板不规整,拼图就会错位。
|
|
15
|
+
* 3. **不允许用 AI 生成的菱形当模板。** 自由生成达标率 0%(比例 1.03~1.92)。
|
|
16
|
+
*
|
|
17
|
+
* ## 抠底那一套为什么长这样(白边的根因)
|
|
18
|
+
*
|
|
19
|
+
* 一张 2048² 的树缩到 40px 宽,一个输出像素要平均 37×37=1369 个源像素。
|
|
20
|
+
* 边缘那一格里大部分是「半覆盖的抗锯齿像素」,颜色**已经在白底上合成过**,天生偏亮。
|
|
21
|
+
* 如果这时用硬阈值定 alpha(`α≥0.5 → 1`),就得到「完全不透明 + 偏亮颜色」的像素 ——
|
|
22
|
+
* 一圈肉眼可见的白描边。实测一张 64×96 的地块里有 184 个这样的像素。
|
|
23
|
+
*
|
|
24
|
+
* 正确做法(本文件 `matteForeground`):
|
|
25
|
+
* ① 软 alpha,且**必须带死区**(纯白底图的压缩噪声 dist≈2~10,
|
|
26
|
+
* 无死区会把整张底判成 α≈0.03 的「前景」,白边反而更严重);
|
|
27
|
+
* ② 颜色只取「够实」的像素(α ≥ 0.75),其余用最近可信邻居填充;
|
|
28
|
+
* ③ 颜色与 alpha **分开**缩放。
|
|
29
|
+
* 实测白边像素 184 → 0,alpha 档位 2 → 115~208,剪影面积不变。
|
|
30
|
+
*/
|
|
31
|
+
import { borderColor, foregroundMask, maskBounds, maskCount } from "./tilemedia.js";
|
|
32
|
+
/** 目标菱形的固定尺寸:宽 2 份、高 1 份(严格的 2:1 等距)。 */
|
|
33
|
+
export const DIAMOND_RATIO = 2;
|
|
34
|
+
/**
|
|
35
|
+
* 裁切遮罩在数学菱形之外的外推量(归一化距离)。
|
|
36
|
+
*
|
|
37
|
+
* ⚠️ 这是「从哪儿开始淡出」的起点,**必须 ≈ 1**(菱形边界)。
|
|
38
|
+
* 写成 0.04 之类的「小余量」会得到灾难性结果:等于把整个菱形内部都当成边界外,
|
|
39
|
+
* 只剩中心极窄一条不透明(实测 64×96 只活下来 4 个像素)。
|
|
40
|
+
*
|
|
41
|
+
* 为什么不能就用严格的 1:2:1 菱形在上下尖端每行只有 0~1 个像素,
|
|
42
|
+
* 像素中心几乎不会正好落在边界线上,用 `d <= 1` 当遮罩会把尖端整行切掉 ——
|
|
43
|
+
* 实测少 16~41 个像素,重新测量时比例变成 1.9375(真值 2.0)。
|
|
44
|
+
*
|
|
45
|
+
* 所以让判定起点略微外推:菱形主体 100% 不透明,再往外 1px 线性淡出。
|
|
46
|
+
* 掩码比数学菱形大不到 1px,肉眼不可见,但尖端能保住。
|
|
47
|
+
*/
|
|
48
|
+
export const DIAMOND_MASK_SLACK = 1.0;
|
|
49
|
+
/**
|
|
50
|
+
* 规整时把目标菱形放大的**比例**(相对于单元格菱形的半宽/半高)。
|
|
51
|
+
*
|
|
52
|
+
* ## 为什么需要它(拼缝白线的真正原因)
|
|
53
|
+
*
|
|
54
|
+
* 源图里菱形的边是**抗锯齿**的:从实心到全透明只有约 3 个源像素。
|
|
55
|
+
* 而源图 2048、单元格只有 64 宽 —— 缩放系数约 0.03,也就是**每个输出像素
|
|
56
|
+
* 要平均约 30×60 个源像素**。在菱形边界上,采样核一半在菱形内、一半在外,
|
|
57
|
+
* 于是边界像素的 alpha 只到 ~213,往外一圈掉到 42。
|
|
58
|
+
*
|
|
59
|
+
* 后果:单元格菱形的整条边都是半透明的,相邻两块拼起来两边都半透明,
|
|
60
|
+
* 背景从缝里透出来 —— 地图上出现一圈白线(实测就是这个问题)。
|
|
61
|
+
*
|
|
62
|
+
* 修法:**取到源菱形内侧一点**。把目标菱形放大 12%,单元格边界对应的源坐标
|
|
63
|
+
* 就落到源菱形内部约 12% 半宽处 —— 那里采样核完全在实心区,alpha 是 255。
|
|
64
|
+
* 再让掩盖把菱形之外切掉,于是「边界不透明 + 不带出体外像素」同时成立。
|
|
65
|
+
*
|
|
66
|
+
* ⚠️ 必须是**比例**而不是固定像素数。曾经写成「多取 6px」,
|
|
67
|
+
* 在 32×48 的单元格上等于放大 37%,把地块横向拉伸了 27%(x/y 缩放比失衡)。
|
|
68
|
+
* 比例则天然按两个方向等比放大,不会变形 —— `verify-tile.mjs` 的 P1a 钉这条。
|
|
69
|
+
*/
|
|
70
|
+
export const OVERSCAN_RATIO = 0.12;
|
|
71
|
+
export const DEFAULT_SETTINGS = { cellWidth: 64, cellHeight: 96 };
|
|
72
|
+
/** 菱形高 = 单元格宽 / 2(2:1 等距)。**与 cellHeight 无关**。 */
|
|
73
|
+
export function diamondHeight(s) {
|
|
74
|
+
return s.cellWidth / DIAMOND_RATIO;
|
|
75
|
+
}
|
|
76
|
+
/** 菱形在单元格里的中心 y:上下各留同样多的余量给装饰。 */
|
|
77
|
+
export function diamondCenterY(s) {
|
|
78
|
+
return s.cellHeight / 2;
|
|
79
|
+
}
|
|
80
|
+
/** 独立装饰的底部锚点:地面上「站得住」的高度(详见 research 报告 §8.3)。 */
|
|
81
|
+
export function decorAnchorY(s) {
|
|
82
|
+
// 菱形下顶点 y = cy + dh/2;菱形中轴处的地面线比下顶点高 dh/4(2:1 菱形的几何性质);
|
|
83
|
+
// 再扣 2px 抗锯齿与实测误差。64×96 → 48 + 16 − 8 − 2 = 54。
|
|
84
|
+
return Math.round(diamondCenterY(s) + diamondHeight(s) / 2 - diamondHeight(s) / 4 - 2);
|
|
85
|
+
}
|
|
86
|
+
export function assertSettings(s) {
|
|
87
|
+
if (!Number.isInteger(s.cellWidth) || s.cellWidth < 16 || s.cellWidth > 512) {
|
|
88
|
+
throw new Error(`单元格宽必须是 16~512 的整数,收到 ${s.cellWidth}`);
|
|
89
|
+
}
|
|
90
|
+
if (!Number.isInteger(s.cellHeight) || s.cellHeight < 32 || s.cellHeight > 768) {
|
|
91
|
+
throw new Error(`单元格高必须是 32~768 的整数,收到 ${s.cellHeight}`);
|
|
92
|
+
}
|
|
93
|
+
if (s.cellWidth % 4 !== 0) {
|
|
94
|
+
throw new Error(`单元格宽必须能被 4 整除(菱形半宽要落在整数格上),收到 ${s.cellWidth}`);
|
|
95
|
+
}
|
|
96
|
+
if (s.cellHeight <= diamondHeight(s)) {
|
|
97
|
+
throw new Error(`单元格高(${s.cellHeight})必须大于菱形高(${diamondHeight(s)}),否则没有空间放高出地面的装饰`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
// ── 菱形遮罩 ──────────────────────────────────────────────────────────────
|
|
101
|
+
/**
|
|
102
|
+
* 目标菱形(单元格坐标系)的四顶点。
|
|
103
|
+
*
|
|
104
|
+
* 64×96 时:上 (32,32) 右 (64,48) 下 (32,64) 左 (0,48)。
|
|
105
|
+
*/
|
|
106
|
+
export function diamondPoints(s) {
|
|
107
|
+
const cx = s.cellWidth / 2;
|
|
108
|
+
const cy = diamondCenterY(s);
|
|
109
|
+
const hw = s.cellWidth / 2;
|
|
110
|
+
const hh = diamondHeight(s) / 2;
|
|
111
|
+
return { top: [cx, cy - hh], right: [cx + hw, cy], bottom: [cx, cy + hh], left: [cx - hw, cy] };
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* 菱形软遮罩。`|dx|/hw + |dy|/hh <= 1` 处为 255,边界带 `feather` 像素羽化。
|
|
115
|
+
*
|
|
116
|
+
* 返回 0~255 的单通道。羽化是为了避免硬边在缩放后出现台阶。
|
|
117
|
+
*/
|
|
118
|
+
export function diamondAlpha(s, feather = 0.7) {
|
|
119
|
+
const { cellWidth: w, cellHeight: h } = s;
|
|
120
|
+
const cx = w / 2;
|
|
121
|
+
const cy = diamondCenterY(s);
|
|
122
|
+
const hw = w / 2;
|
|
123
|
+
const hh = diamondHeight(s) / 2;
|
|
124
|
+
const out = new Uint8Array(w * h);
|
|
125
|
+
const ramp = Math.max(1e-6, feather / hh);
|
|
126
|
+
for (let y = 0; y < h; y++) {
|
|
127
|
+
for (let x = 0; x < w; x++) {
|
|
128
|
+
const d = Math.abs(x + 0.5 - cx) / hw + Math.abs(y + 0.5 - cy) / hh;
|
|
129
|
+
const a = Math.max(0, Math.min(1, (1 - d) / ramp + 0.5));
|
|
130
|
+
out[y * w + x] = Math.round(a * 255);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return out;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* 点是否落在数学菱形内(规整裁切用;不做羽化)。
|
|
137
|
+
*
|
|
138
|
+
* ⚠️ 直接用这个布尔值当 alpha 遮罩会**切掉菱形边缘那一圈像素**:
|
|
139
|
+
* 2:1 菱形的边界是一条 26.565° 的斜线,像素中心几乎不会正好落在线上,
|
|
140
|
+
* 于是「<= 1」的行会整体少一个像素 —— 实测 64×96 的菱形会少 16~41 个像素、
|
|
141
|
+
* 重新测量时比例变成 31/16 = 1.9375(真值 2.0)。
|
|
142
|
+
*
|
|
143
|
+
* 正确做法见 `regularizeToCell`:用**带 1px 羽化的软遮罩**,
|
|
144
|
+
* 边界像素给半透明而不是直接判 0。
|
|
145
|
+
*/
|
|
146
|
+
export function insideDiamond(s, x, y) {
|
|
147
|
+
const cx = s.cellWidth / 2;
|
|
148
|
+
const cy = diamondCenterY(s);
|
|
149
|
+
return (Math.abs(x + 0.5 - cx) / (s.cellWidth / 2) + Math.abs(y + 0.5 - cy) / (diamondHeight(s) / 2) <= 1);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* 菱形软遮罩的**归一化距离**:<= 1 在菱形内,越接近 1 越靠边。
|
|
153
|
+
*
|
|
154
|
+
* 用来做「带羽化的裁切」:`alpha = clamp((1 + halfFeather - d) / feather)`。
|
|
155
|
+
*/
|
|
156
|
+
export function diamondDistance(s, x, y) {
|
|
157
|
+
const cx = s.cellWidth / 2;
|
|
158
|
+
const cy = diamondCenterY(s);
|
|
159
|
+
return (Math.abs(x + 0.5 - cx) / (s.cellWidth / 2) + Math.abs(y + 0.5 - cy) / (diamondHeight(s) / 2));
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* 按等距网格排布 cols×rows 个菱形的模板。
|
|
163
|
+
*
|
|
164
|
+
* 位置公式(与研究期 `mk-templates2.py` 一致):
|
|
165
|
+
* 格子 (r, c) 的菱形中心 = 画布中心 + ((c−r)·tw/2, (c+r)·th/2) − 包围盒中心偏移
|
|
166
|
+
* 相邻格正好边对边贴合,所以 2×2 拼出来是一整块大菱形。
|
|
167
|
+
*/
|
|
168
|
+
export function renderTemplate(options = {}) {
|
|
169
|
+
const size = options.size ?? 2048;
|
|
170
|
+
const cols = options.cols ?? 1;
|
|
171
|
+
const rows = options.rows ?? 1;
|
|
172
|
+
const margin = options.margin ?? 0.92;
|
|
173
|
+
const color = options.color ?? [255, 0, 255];
|
|
174
|
+
const ss = Math.max(1, Math.floor(options.supersample ?? 4));
|
|
175
|
+
// 包围盒(以 tw 为单位):宽 = (cols+rows)/2 · tw,高 = (cols+rows)/4 · tw
|
|
176
|
+
const span = (cols + rows) / 2;
|
|
177
|
+
const twUnit = Math.min(size * margin / span, size * margin / (span / 2));
|
|
178
|
+
// 中心:所有菱形中心的平均(用包围盒中心校正,避免 (0,0) 起点带来的偏移)
|
|
179
|
+
const cxUnit = ((0 - 0) + (cols - 1 - (rows - 1))) / 4 * twUnit;
|
|
180
|
+
const cyUnit = ((0 + 0) + (cols - 1 + rows - 1)) / 4 * (twUnit / 2);
|
|
181
|
+
const S = size * ss;
|
|
182
|
+
const rgba = Buffer.alloc(S * S * 4);
|
|
183
|
+
const tw = twUnit * ss;
|
|
184
|
+
const th = (twUnit / 2) * ss;
|
|
185
|
+
const diamonds = [];
|
|
186
|
+
const fillDiamond = (cxSS, cySS, mark) => {
|
|
187
|
+
// ⚠️ 坐标系统一:`tw` / `th` 是**超采样画布**上的尺寸(上面已经乘过 ss),
|
|
188
|
+
// 所以半径直接用 `tw/2`,不要再乘 ss —— 乘两次会得到 4 倍宽的菱形
|
|
189
|
+
//(表现是模板比例 2.03、只占画布 23%),而且不报任何错。自检 G1 钉这条。
|
|
190
|
+
const hw = tw / 2;
|
|
191
|
+
const hh = th / 2;
|
|
192
|
+
const y0 = Math.max(0, Math.floor(cySS - hh));
|
|
193
|
+
const y1 = Math.min(S - 1, Math.ceil(cySS + hh));
|
|
194
|
+
for (let y = y0; y <= y1; y++) {
|
|
195
|
+
const dy = Math.abs(y + 0.5 - cySS) / hh;
|
|
196
|
+
if (dy > 1)
|
|
197
|
+
continue;
|
|
198
|
+
const half = hw * (1 - dy);
|
|
199
|
+
const x0 = Math.max(0, Math.ceil(cxSS - half));
|
|
200
|
+
const x1 = Math.min(S - 1, Math.floor(cxSS + half));
|
|
201
|
+
for (let x = x0; x <= x1; x++) {
|
|
202
|
+
const i = (y * S + x) * 4;
|
|
203
|
+
rgba[i] = color[0];
|
|
204
|
+
rgba[i + 1] = color[1];
|
|
205
|
+
rgba[i + 2] = color[2];
|
|
206
|
+
rgba[i + 3] = 255;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (mark)
|
|
210
|
+
diamonds.push({ cx: cxSS / ss, cy: cySS / ss, width: tw / ss, height: th / ss });
|
|
211
|
+
// 注意:这四个字段都要换算回降采样后的坐标。漏掉任何一个,
|
|
212
|
+
// 下游拿到的包围盒就是错的 —— 表现是仿射缩放算错、地块被裁出缺口,且不报错。
|
|
213
|
+
};
|
|
214
|
+
for (let r = 0; r < rows; r++) {
|
|
215
|
+
for (let c = 0; c < cols; c++) {
|
|
216
|
+
const cx = (size / 2 + ((c - r) * twUnit) / 2 - cxUnit) * ss;
|
|
217
|
+
const cy = (size / 2 + ((c + r) * (twUnit / 2)) / 2 - cyUnit) * ss;
|
|
218
|
+
fillDiamond(cx, cy, true);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
// 2×2 地基:洋红外框(整个网格的等距外轮廓)+ 红色分格线(每个子菱形的对角线)。
|
|
222
|
+
// 分格线是给模型看的 —— 让它理解「这是 4 格」,而不是「一整块地面」。
|
|
223
|
+
// 实测:不给分格线时模型会把 4 格画成一整块地基(比例 1.27)。
|
|
224
|
+
if (options.grid === true) {
|
|
225
|
+
const outline = [255, 0, 255];
|
|
226
|
+
const divider = [255, 60, 60];
|
|
227
|
+
// 网格整体是一个大菱形:宽 = (cols+rows)/2 · tw,高 = 宽/2
|
|
228
|
+
const bigW = ((cols + rows) / 2) * twUnit * ss;
|
|
229
|
+
const bigH = bigW / 2;
|
|
230
|
+
const bigCx = (size / 2) * ss;
|
|
231
|
+
const bigCy = (size / 2) * ss;
|
|
232
|
+
const width = Math.max(2, Math.round(bigW * 0.004));
|
|
233
|
+
const corners = [
|
|
234
|
+
[bigCx, bigCy - bigH / 2],
|
|
235
|
+
[bigCx + bigW / 2, bigCy],
|
|
236
|
+
[bigCx, bigCy + bigH / 2],
|
|
237
|
+
[bigCx - bigW / 2, bigCy]
|
|
238
|
+
];
|
|
239
|
+
for (let i = 0; i < 4; i++) {
|
|
240
|
+
const a = corners[i];
|
|
241
|
+
const b = corners[(i + 1) % 4];
|
|
242
|
+
for (let k = 0; k < width; k++) {
|
|
243
|
+
line(rgba, S, a[0], a[1] + k, b[0], b[1] + k, outline, ss);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
for (let r = 0; r < rows; r++) {
|
|
247
|
+
for (let c = 0; c < cols; c++) {
|
|
248
|
+
const cx = (size / 2 + ((c - r) * twUnit) / 2 - cxUnit) * ss;
|
|
249
|
+
const cy = (size / 2 + ((c + r) * (twUnit / 2)) / 2 - cyUnit) * ss;
|
|
250
|
+
line(rgba, S, cx, cy - th / 2, cx, cy + th / 2, divider, ss);
|
|
251
|
+
line(rgba, S, cx - tw / 2, cy, cx + tw / 2, cy, divider, ss);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
const bitmap = downsample({ width: S, height: S, rgba }, size, size, ss);
|
|
256
|
+
// `diamonds[]` 在 fillDiamond 里**已经**换算成降采样坐标了(cx/ss、width/ss),
|
|
257
|
+
// 这里不能再除一次 —— 除两次会得到「缩小 4 倍」的包围盒:菱形只剩画布 23%、
|
|
258
|
+
// 中心落在 (64,64) 而不是画布中心,而且不报任何错。自检 G1 钉这条。
|
|
259
|
+
return { bitmap, bounds: boundsOfDiamonds(diamonds, size), diamonds };
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* 建筑模板:**地基范围 + 立体容积**。
|
|
263
|
+
*
|
|
264
|
+
* 为什么不能用 `renderTemplate({grid:true})` 那份:那份是一整块实心洋红菱形,
|
|
265
|
+
* 模型会把它理解成「把这块地面填满」—— 实测「中世纪石屋」生成出来是一张
|
|
266
|
+
* **平铺的菱形石板地面**(`ratioMeasured` 2.26),完全没有墙和屋顶。
|
|
267
|
+
* 提示词里写「往上长高 1.4 倍」也没用:参考图里根本没有「上」的空间,
|
|
268
|
+
* 整张图从上到下都是那块菱形。
|
|
269
|
+
*
|
|
270
|
+
* 这份模板改成:
|
|
271
|
+
* · 洋红**轮廓**画出 4 格地基(不是实心填充)—— 明确「底面在哪」;
|
|
272
|
+
* · 中间铺一层浅灰地面,让模型知道这里是地不是墙;
|
|
273
|
+
* · 从地基四个角往**上**拉出等距立方体的线框 —— 明确「能长多高、往哪长」。
|
|
274
|
+
*
|
|
275
|
+
* 输出画布就是「2×2 格块」的形状(宽 4·halfW、高 3·halfH + 高度),
|
|
276
|
+
* 与 `regularizeBuilding` 的落位尺寸一一对应。
|
|
277
|
+
*/
|
|
278
|
+
/**
|
|
279
|
+
* 底面各格在模板里的**菱形中心**(逻辑单位,未乘缩放系数)。
|
|
280
|
+
*
|
|
281
|
+
* ★ 以 `tilemap.shapeBaseOffset` 的**同一个点**为原点 —— 即
|
|
282
|
+
* 「各格菱形中心相对**锚点格中心**的偏移」,也就是
|
|
283
|
+
* `dx = ((min(c−r) + max(c−r))/2)·cellWidth/2`、
|
|
284
|
+
* `dy = ((min(c+r) + max(c+r))/2)·cellWidth/4`。
|
|
285
|
+
* 最终整块会平移到 `(cx, baseCy)`。
|
|
286
|
+
*
|
|
287
|
+
* ⚠️ **不能用「各格中心的包围盒中点」当原点**:两者对非对称形状**不是同一个点**。
|
|
288
|
+
* 实测 `3×1`(格子 `(0,0)(0,1)(0,2)`):
|
|
289
|
+
* · 拼图口径(`shapeBaseOffset`)→ 底心相对锚点 `dx = +32`
|
|
290
|
+
* · 包围盒中点口径 → `dx = 0`
|
|
291
|
+
* 于是模板把 `3×1` 画在了与 `2×2` **完全相同**的位置上(两者半宽半高都是
|
|
292
|
+
* 64×32),两张模板**逐字节相同** —— 用户报的「3×1 模板和 2×2 没区别」。
|
|
293
|
+
* 统一到拼图口径之后,`3×1` 的底面会在模板里**偏右半格**,一眼能看出不同。
|
|
294
|
+
*
|
|
295
|
+
* 为什么按「一格一个菱形」而不是「一个大包围菱形」:
|
|
296
|
+
* L 形、T 形这类**非矩形**占地,用一个包围菱形画出来模型会以为整块都是地基,
|
|
297
|
+
* 于是把凹口也填成地面。逐格画才能把「哪些格是地基」讲清楚。
|
|
298
|
+
*/
|
|
299
|
+
export function buildingBaseCells(settings, shape) {
|
|
300
|
+
if (shape.length === 0)
|
|
301
|
+
return [{ cx: 0, cy: 0 }];
|
|
302
|
+
const stepX = settings.cellWidth / 2;
|
|
303
|
+
const stepY = settings.cellWidth / 4;
|
|
304
|
+
// 锚点格中心 → 底面中心的偏移(与 tilemap.shapeBaseOffset 同式)
|
|
305
|
+
let minDR = Infinity, maxDR = -Infinity, minDC = Infinity, maxDC = -Infinity;
|
|
306
|
+
for (const [r, c] of shape) {
|
|
307
|
+
if (r < minDR)
|
|
308
|
+
minDR = r;
|
|
309
|
+
if (r > maxDR)
|
|
310
|
+
maxDR = r;
|
|
311
|
+
if (c < minDC)
|
|
312
|
+
minDC = c;
|
|
313
|
+
if (c > maxDC)
|
|
314
|
+
maxDC = c;
|
|
315
|
+
}
|
|
316
|
+
const baseX = ((minDC - minDR) + (maxDC - maxDR)) / 2;
|
|
317
|
+
const baseY = ((minDC + minDR) + (maxDC + maxDR)) / 2;
|
|
318
|
+
return shape.map(([r, c]) => ({
|
|
319
|
+
cx: ((c - r) - baseX) * stepX,
|
|
320
|
+
cy: ((c + r) - baseY) * stepY
|
|
321
|
+
}));
|
|
322
|
+
}
|
|
323
|
+
export function renderBuildingTemplate(options) {
|
|
324
|
+
const settings = options.settings;
|
|
325
|
+
const cols = options.cols ?? 2;
|
|
326
|
+
const rows = options.rows ?? 2;
|
|
327
|
+
const heightRatio = options.heightRatio ?? 1.5;
|
|
328
|
+
const ss = Math.max(1, Math.floor(options.supersample ?? 2));
|
|
329
|
+
// 形状是三处口径的**唯一真源**:包围菱形(宽高)、逐格底面、线框。
|
|
330
|
+
const shape = options.shape ?? rectShapeLocal(cols, rows);
|
|
331
|
+
const T = buildingTemplateLayout(settings, { cols, rows, shape, heightRatio, pad: options.pad ?? 8 });
|
|
332
|
+
const { halfW, halfH, boxH, pad } = T;
|
|
333
|
+
const W = T.width;
|
|
334
|
+
const H = T.height;
|
|
335
|
+
// 长边缩放到目标尺寸(短边等比),保证模板足够大给模型细节
|
|
336
|
+
const target = options.size ?? 2048;
|
|
337
|
+
const scale = target / Math.max(W, H);
|
|
338
|
+
const outW = Math.max(64, Math.round(W * scale));
|
|
339
|
+
const outH = Math.max(64, Math.round(H * scale));
|
|
340
|
+
const S = { w: outW * ss, h: outH * ss };
|
|
341
|
+
const rgba = Buffer.alloc(S.w * S.h * 4);
|
|
342
|
+
const k = scale * ss; // 逻辑单位 → 超采样像素
|
|
343
|
+
const cx = (pad + halfW) * k;
|
|
344
|
+
const baseCy = T.baseCenterY * k; // 底面(包围菱形中心)
|
|
345
|
+
const magenta = [255, 0, 255];
|
|
346
|
+
const ground = [238, 238, 234];
|
|
347
|
+
const wire = [120, 190, 255];
|
|
348
|
+
const lw = Math.max(2, Math.round(halfW * k * 0.006));
|
|
349
|
+
const fillTri = (pts, color) => {
|
|
350
|
+
let minY = Infinity, maxY = -Infinity;
|
|
351
|
+
for (const p of pts) {
|
|
352
|
+
minY = Math.min(minY, p[1]);
|
|
353
|
+
maxY = Math.max(maxY, p[1]);
|
|
354
|
+
}
|
|
355
|
+
for (let y = Math.max(0, Math.floor(minY)); y <= Math.min(S.h - 1, Math.ceil(maxY)); y++) {
|
|
356
|
+
const xs = [];
|
|
357
|
+
for (let i = 0; i < pts.length; i++) {
|
|
358
|
+
const a = pts[i];
|
|
359
|
+
const b = pts[(i + 1) % pts.length];
|
|
360
|
+
if ((a[1] <= y && b[1] > y) || (b[1] <= y && a[1] > y)) {
|
|
361
|
+
xs.push(a[0] + ((y - a[1]) / (b[1] - a[1])) * (b[0] - a[0]));
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
xs.sort((p, q) => p - q);
|
|
365
|
+
for (let i = 0; i + 1 < xs.length; i += 2) {
|
|
366
|
+
const x0 = Math.max(0, Math.ceil(xs[i]));
|
|
367
|
+
const x1 = Math.min(S.w - 1, Math.floor(xs[i + 1]));
|
|
368
|
+
for (let x = x0; x <= x1; x++) {
|
|
369
|
+
const idx = (y * S.w + x) * 4;
|
|
370
|
+
rgba[idx] = color[0];
|
|
371
|
+
rgba[idx + 1] = color[1];
|
|
372
|
+
rgba[idx + 2] = color[2];
|
|
373
|
+
rgba[idx + 3] = 255;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
};
|
|
378
|
+
// 地基:底面 + 洋红轮廓。
|
|
379
|
+
//
|
|
380
|
+
// **实心矩形走老路径**(一个大包围菱形),非矩形才逐格画。
|
|
381
|
+
//
|
|
382
|
+
// 为什么矩形不逐格:老的 2×2 模板就是这一张大菱形,实测效果没问题;
|
|
383
|
+
// 改成逐格会**少一圈外轮廓、多一道内部网格线**,与老图不一致 ——
|
|
384
|
+
// 老项目里已经渲染好的 `grid2x2.png` 就得全部重跑。保持逐字节一致更划算。
|
|
385
|
+
//
|
|
386
|
+
// 非矩形必须逐格:画包围大菱形等于告诉模型「凹口也是地基」,
|
|
387
|
+
// 它会把凹口填成地面 —— L 形就没意义了。
|
|
388
|
+
const baseCells = buildingBaseCells(settings, shape);
|
|
389
|
+
const cellHalfW = (settings.cellWidth / 4) * k; // 单格菱形半宽
|
|
390
|
+
const cellHalfH = cellHalfW / 2;
|
|
391
|
+
const bTop = [cx, baseCy - halfH * k];
|
|
392
|
+
const bRight = [cx + halfW * k, baseCy];
|
|
393
|
+
const bBottom = [cx, baseCy + halfH * k];
|
|
394
|
+
const bLeft = [cx - halfW * k, baseCy];
|
|
395
|
+
const isRect = rectOfShapeLocal(shape) !== undefined;
|
|
396
|
+
if (isRect) {
|
|
397
|
+
// 老路径:一张大菱形,填灰 + 洋红轮廓
|
|
398
|
+
const quad = [bTop, bRight, bBottom, bLeft];
|
|
399
|
+
fillTri(quad, ground);
|
|
400
|
+
for (let i = 0; i < 4; i++) {
|
|
401
|
+
const a = quad[i];
|
|
402
|
+
const b = quad[(i + 1) % 4];
|
|
403
|
+
for (let t = -lw; t <= lw; t++)
|
|
404
|
+
line(rgba, S.w, a[0], a[1] + t, b[0], b[1] + t, magenta, ss);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
else {
|
|
408
|
+
// 非矩形:**逐格**画(每格浅灰 + 洋红),凹口自然留空。
|
|
409
|
+
for (const cell of baseCells) {
|
|
410
|
+
const bx = cx + cell.cx * k;
|
|
411
|
+
const by = baseCy + cell.cy * k;
|
|
412
|
+
const cTop = [bx, by - cellHalfH];
|
|
413
|
+
const cRight = [bx + cellHalfW, by];
|
|
414
|
+
const cBottom = [bx, by + cellHalfH];
|
|
415
|
+
const cLeft = [bx - cellHalfW, by];
|
|
416
|
+
const quad = [cTop, cRight, cBottom, cLeft];
|
|
417
|
+
// 矩形已经用大菱形铺过灰底了,逐格只需要**分格线**,不必再填一遍 ——
|
|
418
|
+
// 填了会在底面里叠出一圈比底面更亮的格子,看着像浮着几个小菱形。
|
|
419
|
+
if (!isRect)
|
|
420
|
+
fillTri(quad, ground);
|
|
421
|
+
for (let i = 0; i < 4; i++) {
|
|
422
|
+
const a = quad[i];
|
|
423
|
+
const b = quad[(i + 1) % 4];
|
|
424
|
+
for (let t = -lw; t <= lw; t++)
|
|
425
|
+
line(rgba, S.w, a[0], a[1] + t, b[0], b[1] + t, magenta, ss);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
// ★ 多格形状再画出**逐格分隔线**。
|
|
430
|
+
//
|
|
431
|
+
// ⚠️ 为什么非画不可(真机踩过):等距投影下 `3×1` 与 `2×2` 的底面
|
|
432
|
+
// **占用完全相同的菱形**(半宽半高都是 64×32,只是内部切法不同)——
|
|
433
|
+
// 实测两张模板**逐像素完全一样**,用户报的「3×1 模板和 2×2 没区别」
|
|
434
|
+
// 不是错觉,是渲染结果真的相同。轮廓一样的时候,
|
|
435
|
+
// 「一共几格、怎么排」只能靠**分格线**表达。
|
|
436
|
+
//
|
|
437
|
+
// 单格不画(没有内部分隔)。用稍暗的洋红,免得与底面外轮廓糊成一片。
|
|
438
|
+
if (shape.length > 1) {
|
|
439
|
+
const divider = [190, 0, 190];
|
|
440
|
+
for (const cell of baseCells) {
|
|
441
|
+
const bx = cx + cell.cx * k;
|
|
442
|
+
const by = baseCy + cell.cy * k;
|
|
443
|
+
const quad = [
|
|
444
|
+
[bx, by - cellHalfH],
|
|
445
|
+
[bx + cellHalfW, by],
|
|
446
|
+
[bx, by + cellHalfH],
|
|
447
|
+
[bx - cellHalfW, by]
|
|
448
|
+
];
|
|
449
|
+
for (let i = 0; i < 4; i++) {
|
|
450
|
+
const a = quad[i];
|
|
451
|
+
const b = quad[(i + 1) % 4];
|
|
452
|
+
for (let t = -lw; t <= lw; t++)
|
|
453
|
+
line(rgba, S.w, a[0], a[1] + t, b[0], b[1] + t, divider, ss);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
// 立方体线框:**包围菱形**的四个顶点往上拉,顶面再画一个同样的轮廓。
|
|
458
|
+
// 高度是整体概念,逐格画会变成一排小盒子。
|
|
459
|
+
const top = bTop;
|
|
460
|
+
const right = bRight;
|
|
461
|
+
const bottom = bBottom;
|
|
462
|
+
const left = bLeft;
|
|
463
|
+
// 立方体线框:四个角往上拉,顶面再画一个同样的菱形轮廓
|
|
464
|
+
const lift = (p) => [p[0], p[1] - boxH * k];
|
|
465
|
+
const corners = [top, right, bottom, left];
|
|
466
|
+
for (const p of corners) {
|
|
467
|
+
const q = lift(p);
|
|
468
|
+
for (let t = -lw; t <= lw; t++)
|
|
469
|
+
line(rgba, S.w, p[0] + t, p[1], q[0] + t, q[1], wire, ss);
|
|
470
|
+
}
|
|
471
|
+
const lifted = corners.map(lift);
|
|
472
|
+
for (let i = 0; i < 4; i++) {
|
|
473
|
+
const a = lifted[i];
|
|
474
|
+
const b = lifted[(i + 1) % 4];
|
|
475
|
+
for (let t = -lw; t <= lw; t++)
|
|
476
|
+
line(rgba, S.w, a[0], a[1] + t, b[0], b[1] + t, wire, ss);
|
|
477
|
+
}
|
|
478
|
+
// 只把「底面各格」登记为几何基准 —— 建筑的高度是自由的,不参与规整
|
|
479
|
+
const diamonds = baseCells.map((cell) => ({
|
|
480
|
+
cx: (cx + cell.cx * k) / ss,
|
|
481
|
+
cy: (baseCy + cell.cy * k) / ss,
|
|
482
|
+
width: ((settings.cellWidth / 2) * k) / ss,
|
|
483
|
+
height: ((settings.cellWidth / 4) * k) / ss
|
|
484
|
+
}));
|
|
485
|
+
const bitmap = downsample({ width: S.w, height: S.h, rgba }, outW, outH, ss);
|
|
486
|
+
return {
|
|
487
|
+
bitmap,
|
|
488
|
+
bounds: { left: 0, top: 0, right: outW - 1, bottom: outH - 1 },
|
|
489
|
+
diamonds
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
/** 从菱形列表算包围盒(并夹到画布内)。 */
|
|
493
|
+
function boundsOfDiamonds(diamonds, size) {
|
|
494
|
+
let left = size;
|
|
495
|
+
let top = size;
|
|
496
|
+
let right = -1;
|
|
497
|
+
let bottom = -1;
|
|
498
|
+
for (const d of diamonds) {
|
|
499
|
+
// 用「比边界内缩一点」的实测口径,与 Python 版 template_diamond 一致
|
|
500
|
+
left = Math.min(left, Math.round(d.cx - d.width / 2));
|
|
501
|
+
right = Math.max(right, Math.round(d.cx + d.width / 2) - 1);
|
|
502
|
+
top = Math.min(top, Math.round(d.cy - d.height / 2));
|
|
503
|
+
bottom = Math.max(bottom, Math.round(d.cy + d.height / 2) - 1);
|
|
504
|
+
}
|
|
505
|
+
return {
|
|
506
|
+
left: Math.max(0, left),
|
|
507
|
+
top: Math.max(0, top),
|
|
508
|
+
right: Math.min(size - 1, right),
|
|
509
|
+
bottom: Math.min(size - 1, bottom)
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
function line(rgba, S, x0, y0, x1, y1, color, ss = 1) {
|
|
513
|
+
const steps = Math.max(1, Math.ceil(Math.max(Math.abs(x1 - x0), Math.abs(y1 - y0))));
|
|
514
|
+
for (let i = 0; i <= steps; i++) {
|
|
515
|
+
const t = i / steps;
|
|
516
|
+
const x = Math.round(x0 + (x1 - x0) * t);
|
|
517
|
+
const y = Math.round(y0 + (y1 - y0) * t);
|
|
518
|
+
for (let dy = 0; dy < ss; dy++) {
|
|
519
|
+
for (let dx = 0; dx < ss; dx++) {
|
|
520
|
+
const px = x + dx;
|
|
521
|
+
const py = y + dy;
|
|
522
|
+
if (px < 0 || py < 0 || px >= S || py >= S)
|
|
523
|
+
continue;
|
|
524
|
+
const i2 = (py * S + px) * 4;
|
|
525
|
+
rgba[i2] = color[0];
|
|
526
|
+
rgba[i2 + 1] = color[1];
|
|
527
|
+
rgba[i2 + 2] = color[2];
|
|
528
|
+
rgba[i2 + 3] = 255;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
/**
|
|
534
|
+
* 超采样降采样(box filter)。
|
|
535
|
+
*
|
|
536
|
+
* `preserveAlpha` 为真时按 premultiplied 处理:先把 RGB 乘上 alpha 再平均,
|
|
537
|
+
* 最后除回去。这是「不感知 alpha 的缩放会留白边」的正确解法之一 ——
|
|
538
|
+
* 透明像素的颜色不该参与平均。
|
|
539
|
+
*/
|
|
540
|
+
export function downsample(src, width, height, factor, preserveAlpha = true) {
|
|
541
|
+
if (factor <= 1)
|
|
542
|
+
return src;
|
|
543
|
+
const out = Buffer.alloc(width * height * 4);
|
|
544
|
+
const area = factor * factor;
|
|
545
|
+
for (let y = 0; y < height; y++) {
|
|
546
|
+
for (let x = 0; x < width; x++) {
|
|
547
|
+
let r = 0;
|
|
548
|
+
let g = 0;
|
|
549
|
+
let b = 0;
|
|
550
|
+
let a = 0;
|
|
551
|
+
for (let dy = 0; dy < factor; dy++) {
|
|
552
|
+
const sy = y * factor + dy;
|
|
553
|
+
if (sy >= src.height)
|
|
554
|
+
continue;
|
|
555
|
+
for (let dx = 0; dx < factor; dx++) {
|
|
556
|
+
const sx = x * factor + dx;
|
|
557
|
+
if (sx >= src.width)
|
|
558
|
+
continue;
|
|
559
|
+
const i = (sy * src.width + sx) * 4;
|
|
560
|
+
const av = src.rgba[i + 3];
|
|
561
|
+
if (preserveAlpha) {
|
|
562
|
+
const w = av / 255;
|
|
563
|
+
r += src.rgba[i] * w;
|
|
564
|
+
g += src.rgba[i + 1] * w;
|
|
565
|
+
b += src.rgba[i + 2] * w;
|
|
566
|
+
a += av;
|
|
567
|
+
}
|
|
568
|
+
else {
|
|
569
|
+
r += src.rgba[i];
|
|
570
|
+
g += src.rgba[i + 1];
|
|
571
|
+
b += src.rgba[i + 2];
|
|
572
|
+
a += av;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
const aAvg = a / area;
|
|
577
|
+
const o = (y * width + x) * 4;
|
|
578
|
+
if (preserveAlpha && aAvg > 0) {
|
|
579
|
+
const sumAlpha = a / 255;
|
|
580
|
+
out[o] = Math.max(0, Math.min(255, Math.round(r / Math.max(1e-6, sumAlpha))));
|
|
581
|
+
out[o + 1] = Math.max(0, Math.min(255, Math.round(g / Math.max(1e-6, sumAlpha))));
|
|
582
|
+
out[o + 2] = Math.max(0, Math.min(255, Math.round(b / Math.max(1e-6, sumAlpha))));
|
|
583
|
+
}
|
|
584
|
+
else {
|
|
585
|
+
out[o] = Math.round(r / area);
|
|
586
|
+
out[o + 1] = Math.round(g / area);
|
|
587
|
+
out[o + 2] = Math.round(b / area);
|
|
588
|
+
}
|
|
589
|
+
out[o + 3] = Math.round(aAvg);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
return { width, height, rgba: out };
|
|
593
|
+
}
|
|
594
|
+
/** 把位图整体放大 scale 倍(最近邻,像素画保持硬边)。 */
|
|
595
|
+
export function upscale(src, scale) {
|
|
596
|
+
if (scale <= 1)
|
|
597
|
+
return src;
|
|
598
|
+
const width = src.width * scale;
|
|
599
|
+
const height = src.height * scale;
|
|
600
|
+
const out = Buffer.alloc(width * height * 4);
|
|
601
|
+
for (let y = 0; y < height; y++) {
|
|
602
|
+
const sy = Math.floor(y / scale);
|
|
603
|
+
for (let x = 0; x < width; x++) {
|
|
604
|
+
const sx = Math.floor(x / scale);
|
|
605
|
+
const i = (sy * src.width + sx) * 4;
|
|
606
|
+
const o = (y * width + x) * 4;
|
|
607
|
+
out[o] = src.rgba[i];
|
|
608
|
+
out[o + 1] = src.rgba[i + 1];
|
|
609
|
+
out[o + 2] = src.rgba[i + 2];
|
|
610
|
+
out[o + 3] = src.rgba[i + 3];
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
return { width, height, rgba: out };
|
|
614
|
+
}
|
|
615
|
+
// ── 抠底 / 颜色凝固 ───────────────────────────────────────────────────────
|
|
616
|
+
/** 软 alpha 的默认参数(死区 + 满值点)。 */
|
|
617
|
+
export const MATTE_LO = 40;
|
|
618
|
+
export const MATTE_HI = 190;
|
|
619
|
+
/**
|
|
620
|
+
* 把「已抠过底」或「还没抠底」的位图统一成(可信前景色 + 软 alpha)。
|
|
621
|
+
*
|
|
622
|
+
* 返回两个缓冲区:`rgb`(3 通道,已凝固)与 `alpha`(单通道 0~255)。
|
|
623
|
+
* 分开返回是刻意的 —— 下游要**分开缩放**它们,见 `resizeRgbAlpha`。
|
|
624
|
+
*/
|
|
625
|
+
export function matteForeground(src, options = {}) {
|
|
626
|
+
const lo = options.lo ?? MATTE_LO;
|
|
627
|
+
const hi = options.hi ?? MATTE_HI;
|
|
628
|
+
const trust = options.trust ?? 0.75;
|
|
629
|
+
const rings = options.rings ?? 4;
|
|
630
|
+
const { width, height, rgba } = src;
|
|
631
|
+
const n = width * height;
|
|
632
|
+
const alpha = Buffer.alloc(n);
|
|
633
|
+
const rgb = Buffer.alloc(n * 3);
|
|
634
|
+
// ① 软 alpha:带 alpha 就信它;否则从「离背景色的距离」映射(**必须带死区**)
|
|
635
|
+
let useOwnAlpha = false;
|
|
636
|
+
for (let i = 3; i < rgba.length; i += 4) {
|
|
637
|
+
if (rgba[i] < 249) {
|
|
638
|
+
useOwnAlpha = true;
|
|
639
|
+
break;
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
if (useOwnAlpha) {
|
|
643
|
+
for (let i = 0, p = 3; i < n; i++, p += 4)
|
|
644
|
+
alpha[i] = rgba[p];
|
|
645
|
+
}
|
|
646
|
+
else {
|
|
647
|
+
const bg = borderColor(src);
|
|
648
|
+
const span = Math.max(1e-6, hi - lo);
|
|
649
|
+
for (let i = 0, p = 0; i < n; i++, p += 4) {
|
|
650
|
+
const d = Math.abs(rgba[p] - bg[0]) + Math.abs(rgba[p + 1] - bg[1]) + Math.abs(rgba[p + 2] - bg[2]);
|
|
651
|
+
alpha[i] = Math.round(Math.max(0, Math.min(1, (d - lo) / span)) * 255);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
// ② 可信像素的颜色原样留下,其余位置先占位(下一步填充)
|
|
655
|
+
const isTrust = new Uint8Array(n);
|
|
656
|
+
for (let i = 0, p = 0; i < n; i++, p += 4) {
|
|
657
|
+
if (alpha[i] / 255 >= trust) {
|
|
658
|
+
isTrust[i] = 1;
|
|
659
|
+
rgb[i * 3] = rgba[p];
|
|
660
|
+
rgb[i * 3 + 1] = rgba[p + 1];
|
|
661
|
+
rgb[i * 3 + 2] = rgba[p + 2];
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
// 一个可信像素都没有:说明这张图整幅都在死区里(纯白底 / 只有压缩噪声)。
|
|
665
|
+
// **不在这里抛错** —— 「图里到底有没有主体」是调用方的事,它手里才有业务语义。
|
|
666
|
+
// 这里只保证「alpha 全 0 + 颜色为 0」这种可判定的结果。
|
|
667
|
+
if (maskCount(isTrust) === 0)
|
|
668
|
+
return { rgb, alpha, width, height };
|
|
669
|
+
// ③ 颜色凝固:把可信颜色往外扩 rings 圈(BFS),让后续重采样吃不到背景色
|
|
670
|
+
solidify(rgb, isTrust, width, height, rings);
|
|
671
|
+
return { rgb, alpha, width, height };
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* 把 `rgb` 在 `seed` 之外的区域用最近 seed 的颜色填充(8 邻域 BFS,扩 `rings` 圈)。
|
|
675
|
+
*/
|
|
676
|
+
export function solidify(rgb, seed, width, height, rings) {
|
|
677
|
+
const known = Uint8Array.from(seed);
|
|
678
|
+
let frontier = [];
|
|
679
|
+
for (let i = 0; i < known.length; i++)
|
|
680
|
+
if (known[i] === 1)
|
|
681
|
+
frontier.push(i);
|
|
682
|
+
for (let step = 0; step < rings && frontier.length > 0; step++) {
|
|
683
|
+
const next = [];
|
|
684
|
+
for (const index of frontier) {
|
|
685
|
+
const x = index % width;
|
|
686
|
+
const y = (index - x) / width;
|
|
687
|
+
for (let dy = -1; dy <= 1; dy++) {
|
|
688
|
+
for (let dx = -1; dx <= 1; dx++) {
|
|
689
|
+
const nx = x + dx;
|
|
690
|
+
const ny = y + dy;
|
|
691
|
+
if (nx < 0 || ny < 0 || nx >= width || ny >= height)
|
|
692
|
+
continue;
|
|
693
|
+
const ni = ny * width + nx;
|
|
694
|
+
if (known[ni] === 1)
|
|
695
|
+
continue;
|
|
696
|
+
rgb[ni * 3] = rgb[index * 3];
|
|
697
|
+
rgb[ni * 3 + 1] = rgb[index * 3 + 1];
|
|
698
|
+
rgb[ni * 3 + 2] = rgb[index * 3 + 2];
|
|
699
|
+
known[ni] = 1;
|
|
700
|
+
next.push(ni);
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
frontier = next;
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
/**
|
|
708
|
+
* 颜色与 alpha **分开**缩放。
|
|
709
|
+
*
|
|
710
|
+
* 这是「白边」修复里最容易漏的一步:颜色经过 `solidify` 之后已经与背景无关,
|
|
711
|
+
* 随便怎么重采样都不会吃到白;alpha 单独用高质量滤波才保得住模型的抗锯齿。
|
|
712
|
+
* 把两者混在一起做一次 RGBA 缩放,边缘那些「半透明的浅色」就会被平均成
|
|
713
|
+
* 「不透明的浅色」—— 那就是白描边。
|
|
714
|
+
*/
|
|
715
|
+
export function resizeRgbAlpha(rgb, alpha, width, height, newWidth, newHeight) {
|
|
716
|
+
const rgbImg = { width, height, rgba: Buffer.alloc(width * height * 4) };
|
|
717
|
+
for (let i = 0; i < width * height; i++) {
|
|
718
|
+
rgbImg.rgba[i * 4] = rgb[i * 3];
|
|
719
|
+
rgbImg.rgba[i * 4 + 1] = rgb[i * 3 + 1];
|
|
720
|
+
rgbImg.rgba[i * 4 + 2] = rgb[i * 3 + 2];
|
|
721
|
+
rgbImg.rgba[i * 4 + 3] = 255;
|
|
722
|
+
}
|
|
723
|
+
const alphaImg = { width, height, rgba: Buffer.alloc(width * height * 4) };
|
|
724
|
+
for (let i = 0; i < width * height; i++) {
|
|
725
|
+
alphaImg.rgba[i * 4] = alpha[i];
|
|
726
|
+
alphaImg.rgba[i * 4 + 1] = alpha[i];
|
|
727
|
+
alphaImg.rgba[i * 4 + 2] = alpha[i];
|
|
728
|
+
alphaImg.rgba[i * 4 + 3] = 255;
|
|
729
|
+
}
|
|
730
|
+
const rgbSmall = bilinearResize(rgbImg, newWidth, newHeight);
|
|
731
|
+
const alphaSmall = bilinearResize(alphaImg, newWidth, newHeight);
|
|
732
|
+
const out = Buffer.alloc(newWidth * newHeight * 4);
|
|
733
|
+
for (let i = 0; i < newWidth * newHeight; i++) {
|
|
734
|
+
out[i * 4] = rgbSmall.rgba[i * 4];
|
|
735
|
+
out[i * 4 + 1] = rgbSmall.rgba[i * 4 + 1];
|
|
736
|
+
out[i * 4 + 2] = rgbSmall.rgba[i * 4 + 2];
|
|
737
|
+
out[i * 4 + 3] = alphaSmall.rgba[i * 4];
|
|
738
|
+
}
|
|
739
|
+
return { width: newWidth, height: newHeight, rgba: out };
|
|
740
|
+
}
|
|
741
|
+
/**
|
|
742
|
+
* 双线性重采样(RGB 分量按 premultiplied 处理,避免透明像素的颜色渗入)。
|
|
743
|
+
*
|
|
744
|
+
* 不走 ffmpeg:这条路径在测量循环里会被调用很多次,进程开销比算法本身还大。
|
|
745
|
+
*/
|
|
746
|
+
export function bilinearResize(src, newWidth, newHeight) {
|
|
747
|
+
const out = Buffer.alloc(newWidth * newHeight * 4);
|
|
748
|
+
const sx = src.width / newWidth;
|
|
749
|
+
const sy = src.height / newHeight;
|
|
750
|
+
for (let y = 0; y < newHeight; y++) {
|
|
751
|
+
const fy = Math.min(src.height - 1, Math.max(0, (y + 0.5) * sy - 0.5));
|
|
752
|
+
const y0 = Math.floor(fy);
|
|
753
|
+
const y1 = Math.min(src.height - 1, y0 + 1);
|
|
754
|
+
const wy = fy - y0;
|
|
755
|
+
for (let x = 0; x < newWidth; x++) {
|
|
756
|
+
const fx = Math.min(src.width - 1, Math.max(0, (x + 0.5) * sx - 0.5));
|
|
757
|
+
const x0 = Math.floor(fx);
|
|
758
|
+
const x1 = Math.min(src.width - 1, x0 + 1);
|
|
759
|
+
const wx = fx - x0;
|
|
760
|
+
let r = 0;
|
|
761
|
+
let g = 0;
|
|
762
|
+
let b = 0;
|
|
763
|
+
let a = 0;
|
|
764
|
+
for (const [px, py, weight] of [
|
|
765
|
+
[x0, y0, (1 - wx) * (1 - wy)],
|
|
766
|
+
[x1, y0, wx * (1 - wy)],
|
|
767
|
+
[x0, y1, (1 - wx) * wy],
|
|
768
|
+
[x1, y1, wx * wy]
|
|
769
|
+
]) {
|
|
770
|
+
const i = (py * src.width + px) * 4;
|
|
771
|
+
const av = src.rgba[i + 3] / 255;
|
|
772
|
+
r += src.rgba[i] * av * weight;
|
|
773
|
+
g += src.rgba[i + 1] * av * weight;
|
|
774
|
+
b += src.rgba[i + 2] * av * weight;
|
|
775
|
+
a += av * weight;
|
|
776
|
+
}
|
|
777
|
+
const o = (y * newWidth + x) * 4;
|
|
778
|
+
if (a > 1e-6) {
|
|
779
|
+
out[o] = Math.max(0, Math.min(255, Math.round(r / a)));
|
|
780
|
+
out[o + 1] = Math.max(0, Math.min(255, Math.round(g / a)));
|
|
781
|
+
out[o + 2] = Math.max(0, Math.min(255, Math.round(b / a)));
|
|
782
|
+
}
|
|
783
|
+
out[o + 3] = Math.max(0, Math.min(255, Math.round(a * 255)));
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
return { width: newWidth, height: newHeight, rgba: out };
|
|
787
|
+
}
|
|
788
|
+
/**
|
|
789
|
+
* 把 `src` 仿射映射成一张 `outW×outH` 的图。
|
|
790
|
+
*
|
|
791
|
+
* 参数按「源图里的平行四边形 → 目标图的整个画布」给:
|
|
792
|
+
* `map` 描述目标像素 (X, Y) 对应的源坐标 (x, y):
|
|
793
|
+
* x = a·X + b·Y + c
|
|
794
|
+
* y = d·X + e·Y + f
|
|
795
|
+
*/
|
|
796
|
+
export function affineTransform(src, outW, outH, map) {
|
|
797
|
+
const out = Buffer.alloc(outW * outH * 4);
|
|
798
|
+
for (let Y = 0; Y < outH; Y++) {
|
|
799
|
+
for (let X = 0; X < outW; X++) {
|
|
800
|
+
const x = map.a * X + map.b * Y + map.c;
|
|
801
|
+
const y = map.d * X + map.e * Y + map.f;
|
|
802
|
+
const o = (Y * outW + X) * 4;
|
|
803
|
+
sampleBilinear(src, x, y, out, o);
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
return { width: outW, height: outH, rgba: out };
|
|
807
|
+
}
|
|
808
|
+
function sampleBilinear(src, x, y, out, o) {
|
|
809
|
+
const x0 = Math.floor(x);
|
|
810
|
+
const y0 = Math.floor(y);
|
|
811
|
+
const wx = x - x0;
|
|
812
|
+
const wy = y - y0;
|
|
813
|
+
let r = 0;
|
|
814
|
+
let g = 0;
|
|
815
|
+
let b = 0;
|
|
816
|
+
let a = 0;
|
|
817
|
+
for (const [px, py, weight] of [
|
|
818
|
+
[x0, y0, (1 - wx) * (1 - wy)],
|
|
819
|
+
[x0 + 1, y0, wx * (1 - wy)],
|
|
820
|
+
[x0, y0 + 1, (1 - wx) * wy],
|
|
821
|
+
[x0 + 1, y0 + 1, wx * wy]
|
|
822
|
+
]) {
|
|
823
|
+
if (px < 0 || py < 0 || px >= src.width || py >= src.height || weight === 0)
|
|
824
|
+
continue;
|
|
825
|
+
const i = (py * src.width + px) * 4;
|
|
826
|
+
const av = src.rgba[i + 3] / 255;
|
|
827
|
+
r += src.rgba[i] * av * weight;
|
|
828
|
+
g += src.rgba[i + 1] * av * weight;
|
|
829
|
+
b += src.rgba[i + 2] * av * weight;
|
|
830
|
+
a += av * weight;
|
|
831
|
+
}
|
|
832
|
+
if (a > 1e-6) {
|
|
833
|
+
out[o] = Math.max(0, Math.min(255, Math.round(r / a)));
|
|
834
|
+
out[o + 1] = Math.max(0, Math.min(255, Math.round(g / a)));
|
|
835
|
+
out[o + 2] = Math.max(0, Math.min(255, Math.round(b / a)));
|
|
836
|
+
}
|
|
837
|
+
else {
|
|
838
|
+
out[o] = 0;
|
|
839
|
+
out[o + 1] = 0;
|
|
840
|
+
out[o + 2] = 0;
|
|
841
|
+
}
|
|
842
|
+
out[o + 3] = Math.max(0, Math.min(255, Math.round(a * 255)));
|
|
843
|
+
}
|
|
844
|
+
/** 最小二乘拟合 y → x,返回 (k, c) 使 x ≈ k·y + c。 */
|
|
845
|
+
function fitLine(ys, xs) {
|
|
846
|
+
const n = ys.length;
|
|
847
|
+
let sy = 0;
|
|
848
|
+
let sx = 0;
|
|
849
|
+
let syy = 0;
|
|
850
|
+
let syx = 0;
|
|
851
|
+
for (let i = 0; i < n; i++) {
|
|
852
|
+
sy += ys[i];
|
|
853
|
+
sx += xs[i];
|
|
854
|
+
syy += ys[i] * ys[i];
|
|
855
|
+
syx += ys[i] * xs[i];
|
|
856
|
+
}
|
|
857
|
+
const denom = n * syy - sy * sy;
|
|
858
|
+
if (Math.abs(denom) < 1e-9)
|
|
859
|
+
return { k: 1, c: 0 };
|
|
860
|
+
const k = (n * syx - sy * sx) / denom;
|
|
861
|
+
const c = (sx - k * sy) / n;
|
|
862
|
+
return { k, c };
|
|
863
|
+
}
|
|
864
|
+
/** 稳健拟合:最小二乘 + 按残差裁剪,迭代 4 次。 */
|
|
865
|
+
function robustFit(ys, xs) {
|
|
866
|
+
let keep = xs.map(() => true);
|
|
867
|
+
let k = 0;
|
|
868
|
+
let c = 0;
|
|
869
|
+
for (let iter = 0; iter < 4; iter++) {
|
|
870
|
+
const ky = ys.filter((_, i) => keep[i]);
|
|
871
|
+
const kx = xs.filter((_, i) => keep[i]);
|
|
872
|
+
if (ky.length < 8)
|
|
873
|
+
break;
|
|
874
|
+
const fit = fitLine(ky, kx);
|
|
875
|
+
k = fit.k;
|
|
876
|
+
c = fit.c;
|
|
877
|
+
const residuals = ys.map((y, i) => Math.abs(xs[i] - (k * y + c)));
|
|
878
|
+
const active = residuals.filter((_, i) => keep[i]).sort((a, b) => a - b);
|
|
879
|
+
const p80 = active[Math.min(active.length - 1, Math.floor(active.length * 0.8))];
|
|
880
|
+
const limit = Math.max(2, p80);
|
|
881
|
+
const next = residuals.map((r) => r <= limit);
|
|
882
|
+
if (next.every((v, i) => v === keep[i]))
|
|
883
|
+
break;
|
|
884
|
+
keep = next;
|
|
885
|
+
}
|
|
886
|
+
let residual = 0;
|
|
887
|
+
for (let i = 0; i < ys.length; i++) {
|
|
888
|
+
if (!keep[i])
|
|
889
|
+
continue;
|
|
890
|
+
residual = Math.max(residual, Math.abs(xs[i] - (k * ys[i] + c)));
|
|
891
|
+
}
|
|
892
|
+
return { k, c, residual };
|
|
893
|
+
}
|
|
894
|
+
/**
|
|
895
|
+
* 量出「地面菱形」的几何。
|
|
896
|
+
*
|
|
897
|
+
* 只量**下半部分**的两条下边:装饰(树、建筑)在菱形上方,会把上半部分的
|
|
898
|
+
* 边界拟合彻底污染。
|
|
899
|
+
*
|
|
900
|
+
* ## 为什么下顶点要「实测」而不是取两条边的交点
|
|
901
|
+
*
|
|
902
|
+
* 直觉做法是拟合左右两条下边、求交点当下顶点。实测**不可靠**:
|
|
903
|
+
* 靠近下顶点的几行只有几个像素宽,斜率在那里最陡,会把拟合线拉偏,
|
|
904
|
+
* 交点落到真正的顶点**下方**。合成图上实测 halfHeight = 121(真值 102),
|
|
905
|
+
* 比例算成 1.678 —— 而图本身是标准的 2:1。
|
|
906
|
+
*
|
|
907
|
+
* 所以:斜率只用来**评估质量**(残差),顶点位置一律从掩码实测。
|
|
908
|
+
*/
|
|
909
|
+
export function measureGroundDiamond(src) {
|
|
910
|
+
const fg = foregroundMask(src);
|
|
911
|
+
const bounds = maskBounds(fg, src.width, src.height);
|
|
912
|
+
if (bounds === undefined)
|
|
913
|
+
throw new Error("图里找不到地块(前景为空)");
|
|
914
|
+
const height = bounds.bottom - bounds.top + 1;
|
|
915
|
+
// ① 左右边界:只用「有足够宽度」的行去拟合斜率(评估质量用)
|
|
916
|
+
const yStart = bounds.top + Math.floor(height * 0.5);
|
|
917
|
+
const leftY = [];
|
|
918
|
+
const leftX = [];
|
|
919
|
+
const rightY = [];
|
|
920
|
+
const rightX = [];
|
|
921
|
+
for (let y = yStart; y <= bounds.bottom; y++) {
|
|
922
|
+
let min = -1;
|
|
923
|
+
let max = -1;
|
|
924
|
+
const row = y * src.width;
|
|
925
|
+
for (let x = bounds.left; x <= bounds.right; x++) {
|
|
926
|
+
if (fg[row + x] === 0)
|
|
927
|
+
continue;
|
|
928
|
+
if (min < 0)
|
|
929
|
+
min = x;
|
|
930
|
+
max = x;
|
|
931
|
+
}
|
|
932
|
+
if (min < 0 || max - min < 8)
|
|
933
|
+
continue;
|
|
934
|
+
leftY.push(y);
|
|
935
|
+
leftX.push(min);
|
|
936
|
+
rightY.push(y);
|
|
937
|
+
rightX.push(max);
|
|
938
|
+
}
|
|
939
|
+
if (leftX.length < 12)
|
|
940
|
+
throw new Error("下半部分前景太少,量不到地面边");
|
|
941
|
+
const fl = robustFit(leftY, leftX);
|
|
942
|
+
const fr = robustFit(rightY, rightX);
|
|
943
|
+
if (Math.abs(fl.k - fr.k) < 1e-6)
|
|
944
|
+
throw new Error("左右边平行,拟合失败");
|
|
945
|
+
// ② 顶点实测:掩码自身的最下 / 最左 / 最右像素
|
|
946
|
+
let bottomRow = -1;
|
|
947
|
+
for (let y = bounds.bottom; y >= bounds.top; y--) {
|
|
948
|
+
const row = y * src.width;
|
|
949
|
+
for (let x = bounds.left; x <= bounds.right; x++) {
|
|
950
|
+
if (fg[row + x] === 1) {
|
|
951
|
+
bottomRow = y;
|
|
952
|
+
break;
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
if (bottomRow >= 0)
|
|
956
|
+
break;
|
|
957
|
+
}
|
|
958
|
+
if (bottomRow < 0)
|
|
959
|
+
throw new Error("量不到菱形下顶点");
|
|
960
|
+
// 下顶点的 y 取「最后一行 + 1」—— 菱形是闭合图形,下顶点落在最后一行的下沿
|
|
961
|
+
const bottomY = bottomRow + 1;
|
|
962
|
+
let bottomMinX = bounds.right;
|
|
963
|
+
let bottomMaxX = bounds.left;
|
|
964
|
+
{
|
|
965
|
+
const row = bottomRow * src.width;
|
|
966
|
+
for (let x = bounds.left; x <= bounds.right; x++) {
|
|
967
|
+
if (fg[row + x] === 0)
|
|
968
|
+
continue;
|
|
969
|
+
if (x < bottomMinX)
|
|
970
|
+
bottomMinX = x;
|
|
971
|
+
if (x > bottomMaxX)
|
|
972
|
+
bottomMaxX = x;
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
const bottomX = (bottomMinX + bottomMaxX + 1) / 2;
|
|
976
|
+
// ③ 左右顶点:下半部分 x 的极值
|
|
977
|
+
let leftIndex = 0;
|
|
978
|
+
for (let i = 1; i < leftX.length; i++)
|
|
979
|
+
if (leftX[i] < leftX[leftIndex])
|
|
980
|
+
leftIndex = i;
|
|
981
|
+
let rightIndex = 0;
|
|
982
|
+
for (let i = 1; i < rightX.length; i++)
|
|
983
|
+
if (rightX[i] > rightX[rightIndex])
|
|
984
|
+
rightIndex = i;
|
|
985
|
+
const halfWidth = bottomX - leftX[leftIndex];
|
|
986
|
+
const halfHeight = bottomY - leftY[leftIndex];
|
|
987
|
+
if (!(halfWidth > 0) || !(halfHeight > 0))
|
|
988
|
+
throw new Error("量到的菱形尺寸不合法");
|
|
989
|
+
return {
|
|
990
|
+
halfWidth,
|
|
991
|
+
halfHeight,
|
|
992
|
+
bottomX,
|
|
993
|
+
bottomY,
|
|
994
|
+
leftX: leftX[leftIndex],
|
|
995
|
+
rightX: rightX[rightIndex],
|
|
996
|
+
residual: Math.max(fl.residual, fr.residual),
|
|
997
|
+
ratio: halfWidth / halfHeight
|
|
998
|
+
};
|
|
999
|
+
}
|
|
1000
|
+
/**
|
|
1001
|
+
* 规整成标准单元格。
|
|
1002
|
+
*
|
|
1003
|
+
* 1. 把实测菱形仿射映射到目标菱形(上 (32,32) 右 (64,48) 下 (32,64) 左 (0,48));
|
|
1004
|
+
* 2. 按**数学菱形**裁 alpha(菱形外一律 0);
|
|
1005
|
+
* 3. 剔洋红残留。
|
|
1006
|
+
*
|
|
1007
|
+
* ## 为什么要 `overscanRatio`(默认 12%)
|
|
1008
|
+
*
|
|
1009
|
+
* 源图里菱形的边是**抗锯齿**的,从实心到全透明只有约 3 个源像素。
|
|
1010
|
+
* 而源图 2048、单元格只有 64 宽 —— 缩放系数约 0.03,即**每个输出像素平均
|
|
1011
|
+
* 30×60 个源像素**。在菱形边界上采样核一半在内一半在外,边界像素的 alpha
|
|
1012
|
+
* 只到 ~213,再往外一圈掉到 42。
|
|
1013
|
+
*
|
|
1014
|
+
* 后果:单元格菱形的**整条边**都是半透明的;相邻两块拼起来两边都半透明,
|
|
1015
|
+
* 背景从缝里透出来 —— 地图上出现一圈白线。
|
|
1016
|
+
*
|
|
1017
|
+
* 修法不是「把半透明变不透明」(那会把锯齿变成硬边、白边又回来了),
|
|
1018
|
+
* 而是**往源菱形内侧取一点**:把目标菱形按比例放大 12%,单元格边界对应的
|
|
1019
|
+
* 源坐标就落在实心区里,采样核完全在内部,alpha 是 255。
|
|
1020
|
+
* 再让遮罩把菱形之外切掉 —— 于是「边界不透明」与「不带出体外像素」同时成立。
|
|
1021
|
+
*/
|
|
1022
|
+
export function regularizeToCell(src, settings, measure, mode, fallbackReason, overscanRatio = OVERSCAN_RATIO) {
|
|
1023
|
+
assertSettings(settings);
|
|
1024
|
+
const target = diamondPoints(settings);
|
|
1025
|
+
const targetCx = (target.left[0] + target.right[0]) / 2;
|
|
1026
|
+
const targetCy = diamondCenterY(settings);
|
|
1027
|
+
const targetHalfW = (target.right[0] - target.left[0]) / 2;
|
|
1028
|
+
const targetHalfH = (target.bottom[1] - target.top[1]) / 2;
|
|
1029
|
+
let centerX;
|
|
1030
|
+
let centerY;
|
|
1031
|
+
let halfWidth;
|
|
1032
|
+
let halfHeight;
|
|
1033
|
+
if (mode === "measured") {
|
|
1034
|
+
const m = measure;
|
|
1035
|
+
centerX = (m.leftX + m.rightX) / 2;
|
|
1036
|
+
centerY = m.bottomY - m.halfHeight;
|
|
1037
|
+
halfWidth = m.halfWidth;
|
|
1038
|
+
halfHeight = m.halfHeight;
|
|
1039
|
+
}
|
|
1040
|
+
else {
|
|
1041
|
+
const t = measure;
|
|
1042
|
+
centerX = t.centerX;
|
|
1043
|
+
centerY = t.centerY;
|
|
1044
|
+
halfWidth = t.halfWidth;
|
|
1045
|
+
halfHeight = t.halfHeight;
|
|
1046
|
+
}
|
|
1047
|
+
// 目标菱形按**比例**放大,让单元格边界对应的源坐标落在源菱形内侧。
|
|
1048
|
+
// 两个方向各自按自己的半宽/半高取同样的比例,所以缩放比永远相等(不变形)。
|
|
1049
|
+
const growW = Math.max(0, overscanRatio) * targetHalfW;
|
|
1050
|
+
const growH = Math.max(0, overscanRatio) * targetHalfH;
|
|
1051
|
+
const sx = (targetHalfW + growW) / halfWidth;
|
|
1052
|
+
const sy = (targetHalfH + growH) / halfHeight;
|
|
1053
|
+
const bitmap = affineTransform(src, settings.cellWidth, settings.cellHeight, {
|
|
1054
|
+
// 以目标菱形中心为基准缩放:srcX = centerX + (x - targetCx) / sx
|
|
1055
|
+
a: 1 / sx,
|
|
1056
|
+
b: 0,
|
|
1057
|
+
c: centerX - targetCx / sx,
|
|
1058
|
+
d: 0,
|
|
1059
|
+
e: 1 / sy,
|
|
1060
|
+
f: centerY - targetCy / sy
|
|
1061
|
+
});
|
|
1062
|
+
// 按数学菱形裁 alpha + 剔洋红残留。
|
|
1063
|
+
//
|
|
1064
|
+
// 边界的处理是**正确性**问题,不是美观问题:
|
|
1065
|
+
// 2:1 菱形在上下两个尖端处每行只有 0~1 个像素,像素中心几乎不会正好落在
|
|
1066
|
+
// 边界线上。用严格的「d <= 1」当遮罩,尖端那几行会被整行切掉 ——
|
|
1067
|
+
// 实测 64×96 的菱形少 16~41 个像素,重新测量时比例变成 1.9375(真值 2.0)。
|
|
1068
|
+
//
|
|
1069
|
+
// 所以判定边界外推 `DIAMOND_MASK_SLACK`:菱形主体 100% 不透明,
|
|
1070
|
+
// 再往外 `DIAMOND_MASK_FEATHER` 的距离内线性淡出。掩码比数学菱形略大一点点,
|
|
1071
|
+
// 肉眼不可见(不到 1px),但尖端不会再被切掉。
|
|
1072
|
+
let purged = 0;
|
|
1073
|
+
// 羽化宽度 = 1.5 像素,换算成归一化距离(半高 hh 对应归一化距离 1)
|
|
1074
|
+
const feather = 1.5 / (diamondHeight(settings) / 2);
|
|
1075
|
+
for (let y = 0; y < settings.cellHeight; y++) {
|
|
1076
|
+
for (let x = 0; x < settings.cellWidth; x++) {
|
|
1077
|
+
const i = (y * settings.cellWidth + x) * 4;
|
|
1078
|
+
const d = diamondDistance(settings, x, y);
|
|
1079
|
+
const edge = d <= DIAMOND_MASK_SLACK
|
|
1080
|
+
? 1
|
|
1081
|
+
: Math.max(0, 1 - (d - DIAMOND_MASK_SLACK) / feather);
|
|
1082
|
+
const magenta = Math.abs(bitmap.rgba[i] - 255) + Math.abs(bitmap.rgba[i + 1]) + Math.abs(bitmap.rgba[i + 2] - 255) < 60;
|
|
1083
|
+
if (magenta || edge <= 0) {
|
|
1084
|
+
if (magenta && bitmap.rgba[i + 3] > 0)
|
|
1085
|
+
purged++;
|
|
1086
|
+
bitmap.rgba[i + 3] = 0;
|
|
1087
|
+
continue;
|
|
1088
|
+
}
|
|
1089
|
+
bitmap.rgba[i + 3] = Math.round(Math.min(bitmap.rgba[i + 3], edge * 255));
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
const report = { mode, scale: [Number(sx.toFixed(4)), Number(sy.toFixed(4))], magentaPurged: purged };
|
|
1093
|
+
if (mode === "measured") {
|
|
1094
|
+
const m = measure;
|
|
1095
|
+
report.ratioMeasured = Number(m.ratio.toFixed(4));
|
|
1096
|
+
report.edgeResidual = Number(m.residual.toFixed(2));
|
|
1097
|
+
report.halfWidth = Number(m.halfWidth.toFixed(1));
|
|
1098
|
+
report.halfHeight = Number(m.halfHeight.toFixed(1));
|
|
1099
|
+
}
|
|
1100
|
+
if (fallbackReason !== undefined)
|
|
1101
|
+
report.fallbackReason = fallbackReason;
|
|
1102
|
+
return { bitmap, report };
|
|
1103
|
+
}
|
|
1104
|
+
/**
|
|
1105
|
+
* 判断「测量是否可信」。
|
|
1106
|
+
*
|
|
1107
|
+
* 两种已知的不可信情形(都实测过):
|
|
1108
|
+
* · 前景占比 > 0.75 —— 没抠出背景(草地颜色和背景太接近时会这样,拟合出的斜率毫无意义);
|
|
1109
|
+
* · 半高 > 图高 40% —— 量到了装饰而不是地面。
|
|
1110
|
+
*/
|
|
1111
|
+
export function measurementLooksSane(src, measure) {
|
|
1112
|
+
const fg = foregroundMask(src);
|
|
1113
|
+
const frac = maskCount(fg) / (src.width * src.height);
|
|
1114
|
+
if (frac > 0.75)
|
|
1115
|
+
return `前景占比 ${frac.toFixed(2)}(没抠出背景)`;
|
|
1116
|
+
if (measure.halfHeight > src.height * 0.4)
|
|
1117
|
+
return `实测半高 ${measure.halfHeight.toFixed(0)} 超过图高的 40%(量到装饰了)`;
|
|
1118
|
+
if (measure.ratio < 1.2 || measure.ratio > 3.5)
|
|
1119
|
+
return `实测比例 ${measure.ratio.toFixed(3)} 明显不是 2:1`;
|
|
1120
|
+
if (measure.residual > src.width * 0.08)
|
|
1121
|
+
return `边缘拟合残差 ${measure.residual.toFixed(0)} 过大`;
|
|
1122
|
+
return undefined;
|
|
1123
|
+
}
|
|
1124
|
+
/**
|
|
1125
|
+
* 把「独立装饰贴图」(一棵树、一块巨石)规整成单元格大小的透明图。
|
|
1126
|
+
*
|
|
1127
|
+
* 缩放策略:宽度归一化到 `cellWidth × 5/8`,再用「锚点以上的可用空间」压高度。
|
|
1128
|
+
* 统一宽度的目的是**统一世界尺度** —— 否则会出现一棵树巨大、一棵很小,
|
|
1129
|
+
* 树干粗细也不一致。
|
|
1130
|
+
*
|
|
1131
|
+
* 摆放:水平居中;底部锚在 `decorAnchorY()`(地面上「站得住」的高度,
|
|
1132
|
+
* 不是菱形中心、也不是菱形下顶点 —— 那两个都实测过,前者树浮空、后者树干
|
|
1133
|
+
* 插到地块前沿之外)。
|
|
1134
|
+
*/
|
|
1135
|
+
/**
|
|
1136
|
+
* 建筑模板的**逻辑尺寸**(不含超采样)。
|
|
1137
|
+
*
|
|
1138
|
+
* `renderBuildingTemplate` 与 `regularizeBuilding` 必须用同一套数 ——
|
|
1139
|
+
* 两边各写一份就会错位,而且只表现为「建筑在地图上偏移」,不报错。
|
|
1140
|
+
*/
|
|
1141
|
+
export function buildingTemplateLayout(settings, options = {}) {
|
|
1142
|
+
const cols = options.cols ?? 2;
|
|
1143
|
+
const rows = options.rows ?? 2;
|
|
1144
|
+
const heightRatio = options.heightRatio ?? 1.5;
|
|
1145
|
+
const pad = options.pad ?? 8;
|
|
1146
|
+
// 包围菱形半宽由**形状**决定。
|
|
1147
|
+
// ⚠️ 注意这里用的是「x 极差 + 一格」,对**实心矩形**等价于老公式
|
|
1148
|
+
// `(cols+rows)/2` —— 所以 2×2 的模板与老版本逐字节一致。
|
|
1149
|
+
// 顺带一个反直觉但正确的事实:`3×1` 与 `2×2` 的等距底面**本来就是同一个菱形**
|
|
1150
|
+
// (`(3+1)/2 = (2+2)/2`),所以两者的模板应该一样,不用去区分。
|
|
1151
|
+
const halfW = options.shape === undefined
|
|
1152
|
+
? (settings.cellWidth / 2) * ((cols + rows) / 2)
|
|
1153
|
+
: shapeHalfWidthLocal(options.shape, settings.cellWidth);
|
|
1154
|
+
const halfH = halfW / 2;
|
|
1155
|
+
const boxH = (settings.cellWidth / 2) * heightRatio;
|
|
1156
|
+
return {
|
|
1157
|
+
halfW, halfH, boxH, pad,
|
|
1158
|
+
width: halfW * 2 + pad * 2,
|
|
1159
|
+
height: halfH * 2 + boxH + pad * 2,
|
|
1160
|
+
baseCenterY: pad + halfH + boxH
|
|
1161
|
+
};
|
|
1162
|
+
}
|
|
1163
|
+
/**
|
|
1164
|
+
* 形状的包围菱形半宽(与 `tilegen.shapeHalfWidth`、`tilemap.shapeDiamondHalf`
|
|
1165
|
+
* **同一条公式**)。
|
|
1166
|
+
*
|
|
1167
|
+
* ```
|
|
1168
|
+
* 半宽 = ((max x − min x)·cellWidth/2 + cellWidth) / 2 // x = c − r
|
|
1169
|
+
* ```
|
|
1170
|
+
*
|
|
1171
|
+
* ⚠️ 这里留一份本地实现是为了**不引入反向依赖**(`tilegen` 已经 import 了
|
|
1172
|
+
* `tilegeom`)。三处一致性由 `verify-tile.mjs` 的对照断言钉住。
|
|
1173
|
+
*
|
|
1174
|
+
* ⚠️ **不能用「列数 + 行数」那一类**(如 `cellWidth·(Δdc + Δdr + 2)/4`):
|
|
1175
|
+
* 它对**实心矩形**恰好等价(矩形里 `x` 的极差正好等于 `Δdc + Δdr`),
|
|
1176
|
+
* 但形状一旦**沿对角线走**(L 形、T 形…),`x = c − r` 会把两项**抵消**,
|
|
1177
|
+
* 真实极差**小于**行列之和。实测 L 形(2×2 缺一格):老公式给 64,
|
|
1178
|
+
* 真实只要 32 —— 于是蓝色立方体线框比洋红底面宽一倍多
|
|
1179
|
+
* (实测底面/线框宽比 **0.373**,矩形是 0.991),模型看到一个过大的空框。
|
|
1180
|
+
*
|
|
1181
|
+
* ⚠️ 也**不能**按「格心 + 半格」取 `max(|x| + q)`:那会把**垂直方向邻居**
|
|
1182
|
+
* 的贡献再算一遍(十字会得到 48,而地图与拼图都按 32 走)——
|
|
1183
|
+
* 模板就和实际摆放口径不一致了。
|
|
1184
|
+
*
|
|
1185
|
+
* 矩形下本式等价于 `(cols+rows)/2`,所以 2×2 模板逐字节不变。
|
|
1186
|
+
*/
|
|
1187
|
+
export function shapeHalfWidthLocal(shape, cellWidth) {
|
|
1188
|
+
if (shape.length === 0)
|
|
1189
|
+
return cellWidth / 2;
|
|
1190
|
+
let minX = Infinity;
|
|
1191
|
+
let maxX = -Infinity;
|
|
1192
|
+
for (const [r, c] of shape) {
|
|
1193
|
+
const x = c - r;
|
|
1194
|
+
if (x < minX)
|
|
1195
|
+
minX = x;
|
|
1196
|
+
if (x > maxX)
|
|
1197
|
+
maxX = x;
|
|
1198
|
+
}
|
|
1199
|
+
return ((maxX - minX) * (cellWidth / 2) + cellWidth) / 2;
|
|
1200
|
+
}
|
|
1201
|
+
/** 由「列 × 行」造形状(本地版,避免 tilegeom → tilegen 的反向依赖)。 */
|
|
1202
|
+
export function rectShapeLocal(cols, rows) {
|
|
1203
|
+
const shape = [];
|
|
1204
|
+
for (let r = 0; r < rows; r++) {
|
|
1205
|
+
for (let c = 0; c < cols; c++)
|
|
1206
|
+
shape.push([r, c]);
|
|
1207
|
+
}
|
|
1208
|
+
return shape;
|
|
1209
|
+
}
|
|
1210
|
+
/** 形状是否恰好是**实心矩形**(是的话返回 [列, 行],否则 undefined)。 */
|
|
1211
|
+
export function rectOfShapeLocal(shape) {
|
|
1212
|
+
if (shape.length === 0)
|
|
1213
|
+
return undefined;
|
|
1214
|
+
let maxR = 0;
|
|
1215
|
+
let maxC = 0;
|
|
1216
|
+
for (const [r, c] of shape) {
|
|
1217
|
+
if (r > maxR)
|
|
1218
|
+
maxR = r;
|
|
1219
|
+
if (c > maxC)
|
|
1220
|
+
maxC = c;
|
|
1221
|
+
}
|
|
1222
|
+
const cols = maxC + 1;
|
|
1223
|
+
const rows = maxR + 1;
|
|
1224
|
+
return shape.length === cols * rows ? [cols, rows] : undefined;
|
|
1225
|
+
}
|
|
1226
|
+
/**
|
|
1227
|
+
* 量出建筑在原图里的**底面**。
|
|
1228
|
+
*
|
|
1229
|
+
* 建筑不能像地形那样靠「拟合一个 2:1 菱形」定位 —— 它的轮廓是自由的
|
|
1230
|
+
* (有屋顶、有山墙),拟合出来的形状没有意义。这里改用「抠底 + 包围盒」:
|
|
1231
|
+
* · 抠出前景(白底 → 透明);
|
|
1232
|
+
* · 底面正前方的尖角 = 包围盒下边缘;
|
|
1233
|
+
* · 底面中心 x = 包围盒中心 x。
|
|
1234
|
+
*
|
|
1235
|
+
* 实测确认:等距建筑从这个视角看,最左/最右点就是底面的左右角,
|
|
1236
|
+
* 所以包围盒的宽 ≈ 底面宽度、水平中心 ≈ 底面中心。
|
|
1237
|
+
*/
|
|
1238
|
+
export function measureBuilding(src) {
|
|
1239
|
+
const fg = foregroundMask(src);
|
|
1240
|
+
const bounds = maskBounds(fg, src.width, src.height);
|
|
1241
|
+
if (bounds === undefined)
|
|
1242
|
+
throw new Error("图里没有找到建筑(整幅都是背景)");
|
|
1243
|
+
if (maskCount(fg) < 200)
|
|
1244
|
+
throw new Error("建筑前景太少(可能是一张空白图)");
|
|
1245
|
+
const halfWidth = (bounds.right - bounds.left + 1) / 2;
|
|
1246
|
+
// 底面菱形高 = 宽 / 2(严格 2:1 等距),所以半高 = 半宽 / 2
|
|
1247
|
+
const halfHeight = halfWidth / 2;
|
|
1248
|
+
return {
|
|
1249
|
+
centerX: (bounds.left + bounds.right + 1) / 2,
|
|
1250
|
+
// 包围盒下边缘 = 底面前尖角 = 菱形下顶点
|
|
1251
|
+
centerY: bounds.bottom + 1 - halfHeight,
|
|
1252
|
+
halfWidth,
|
|
1253
|
+
halfHeight,
|
|
1254
|
+
bounds
|
|
1255
|
+
};
|
|
1256
|
+
}
|
|
1257
|
+
/**
|
|
1258
|
+
* 把生成的建筑放进「2×2 格块」的贴图里。
|
|
1259
|
+
*
|
|
1260
|
+
* 与地形的 `regularizeToCell` 有本质区别:
|
|
1261
|
+
* · 地形是**一整块地面**,规整到 1 格 64×96 就完了;
|
|
1262
|
+
* · 建筑是**立体的**,砖块只有底面占 2×2 格,墙和屋顶要往上长。
|
|
1263
|
+
* 把整张建筑塞进 1 格 64×96(原来的做法)会把它压扁成一张平铺地板
|
|
1264
|
+
* —— 实测「中世纪石屋」就是这么变成菱形石板的。
|
|
1265
|
+
*
|
|
1266
|
+
* 摆放:抠底 → 量底面 → **等比**缩放让底面宽度等于目标底面宽 → 底面中心对准目标位置。
|
|
1267
|
+
* 高度由实际量到的建筑形状决定(不是硬编码 2 格高)。
|
|
1268
|
+
*/
|
|
1269
|
+
export function regularizeBuilding(src, settings, options = {}) {
|
|
1270
|
+
assertSettings(settings);
|
|
1271
|
+
const cols = options.cols ?? 2;
|
|
1272
|
+
const rows = options.rows ?? 2;
|
|
1273
|
+
const measure = measureBuilding(src);
|
|
1274
|
+
const { rgb, alpha } = matteForeground(src);
|
|
1275
|
+
// 底面宽度 = 包围菱形宽:矩形是 `cellWidth·(cols+rows)/2`,
|
|
1276
|
+
// 通用形状按格子集合算(L 形和同样跨度的矩形不一样宽,别混用)。
|
|
1277
|
+
const targetFootW = options.shape === undefined
|
|
1278
|
+
? settings.cellWidth * ((cols + rows) / 2)
|
|
1279
|
+
: shapeHalfWidthLocal(options.shape, settings.cellWidth) * 2;
|
|
1280
|
+
const k = targetFootW / (measure.halfWidth * 2);
|
|
1281
|
+
// 成品宽度 = 底面宽度(量到的包围盒宽就是底面宽)。
|
|
1282
|
+
// ⚠️ 不能直接把「整幅缩放后的宽度」当成品宽度再横向居中 —— 那样会把底面推偏,
|
|
1283
|
+
// 实测建筑会横着超出格块、压到旁边的格子上。
|
|
1284
|
+
const outW = Math.round(targetFootW);
|
|
1285
|
+
const outBaseCx = outW / 2;
|
|
1286
|
+
const outBaseCy = Math.round(measure.centerY * k);
|
|
1287
|
+
const outH = Math.max(outBaseCy + Math.round(measure.halfHeight * k) + 2, Math.round(src.height * k));
|
|
1288
|
+
// 用 RGB + alpha 直接做仿射采样:源是「RGB + 独立 alpha」两张平面
|
|
1289
|
+
const srcRgba = Buffer.alloc(src.width * src.height * 4);
|
|
1290
|
+
for (let i = 0; i < src.width * src.height; i++) {
|
|
1291
|
+
srcRgba[i * 4] = rgb[i * 3];
|
|
1292
|
+
srcRgba[i * 4 + 1] = rgb[i * 3 + 1];
|
|
1293
|
+
srcRgba[i * 4 + 2] = rgb[i * 3 + 2];
|
|
1294
|
+
srcRgba[i * 4 + 3] = alpha[i];
|
|
1295
|
+
}
|
|
1296
|
+
const bitmap = affineTransform({ width: src.width, height: src.height, rgba: srcRgba }, outW, outH,
|
|
1297
|
+
// srcX = measure.centerX + (x − outBaseCx) / k
|
|
1298
|
+
{ a: 1 / k, b: 0, c: measure.centerX - outBaseCx / k, d: 0, e: 1 / k, f: 0 });
|
|
1299
|
+
return {
|
|
1300
|
+
// `baseFraction` 让拼图知道底面在贴图里的位置(建筑贴图高度不是固定值,不能猜)
|
|
1301
|
+
bitmap: Object.assign(bitmap, { baseFraction: outBaseCy / outH }),
|
|
1302
|
+
report: {
|
|
1303
|
+
mode: options.mode ?? "measured",
|
|
1304
|
+
scale: [Number(k.toFixed(4)), Number(k.toFixed(4))],
|
|
1305
|
+
magentaPurged: 0,
|
|
1306
|
+
halfWidth: Number(measure.halfWidth.toFixed(1)),
|
|
1307
|
+
halfHeight: Number(measure.halfHeight.toFixed(1)),
|
|
1308
|
+
srcBounds: [measure.bounds.left, measure.bounds.top, measure.bounds.right, measure.bounds.bottom]
|
|
1309
|
+
}
|
|
1310
|
+
};
|
|
1311
|
+
}
|
|
1312
|
+
export function regularizeDecorSprite(src, settings, options = {}) {
|
|
1313
|
+
assertSettings(settings);
|
|
1314
|
+
const useMatte = options.matte !== false;
|
|
1315
|
+
const fg = foregroundMask(src);
|
|
1316
|
+
const bounds = maskBounds(fg, src.width, src.height);
|
|
1317
|
+
if (bounds === undefined)
|
|
1318
|
+
throw new Error("图里没有找到装饰");
|
|
1319
|
+
if (maskCount(fg) < 50)
|
|
1320
|
+
throw new Error("装饰前景太少(可能是一张空白图)");
|
|
1321
|
+
const cropW = bounds.right - bounds.left + 1;
|
|
1322
|
+
const cropH = bounds.bottom - bounds.top + 1;
|
|
1323
|
+
const anchorY = decorAnchorY(settings);
|
|
1324
|
+
const targetW = settings.cellWidth * (5 / 8);
|
|
1325
|
+
const k = Math.min(targetW / cropW, anchorY / cropH);
|
|
1326
|
+
const cellW = Math.max(1, Math.round(cropW * k));
|
|
1327
|
+
const cellH = Math.max(1, Math.round(cropH * k));
|
|
1328
|
+
let crop;
|
|
1329
|
+
if (useMatte) {
|
|
1330
|
+
const { rgb, alpha } = matteForeground(src);
|
|
1331
|
+
const rgbCrop = Buffer.alloc(cropW * cropH * 3);
|
|
1332
|
+
const alphaCrop = Buffer.alloc(cropW * cropH);
|
|
1333
|
+
for (let y = 0; y < cropH; y++) {
|
|
1334
|
+
for (let x = 0; x < cropW; x++) {
|
|
1335
|
+
const si = (y + bounds.top) * src.width + (x + bounds.left);
|
|
1336
|
+
rgbCrop[(y * cropW + x) * 3] = rgb[si * 3];
|
|
1337
|
+
rgbCrop[(y * cropW + x) * 3 + 1] = rgb[si * 3 + 1];
|
|
1338
|
+
rgbCrop[(y * cropW + x) * 3 + 2] = rgb[si * 3 + 2];
|
|
1339
|
+
alphaCrop[y * cropW + x] = alpha[si];
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
crop = resizeRgbAlpha(rgbCrop, alphaCrop, cropW, cropH, cellW, cellH);
|
|
1343
|
+
}
|
|
1344
|
+
else {
|
|
1345
|
+
// 旧路线(只用于对照实验):硬阈值抠底,RGB 原样留着 —— 会留白边
|
|
1346
|
+
const bg = borderColor(src);
|
|
1347
|
+
const tmp = Buffer.alloc(cropW * cropH * 4);
|
|
1348
|
+
for (let y = 0; y < cropH; y++) {
|
|
1349
|
+
for (let x = 0; x < cropW; x++) {
|
|
1350
|
+
const si = ((y + bounds.top) * src.width + (x + bounds.left)) * 4;
|
|
1351
|
+
const o = (y * cropW + x) * 4;
|
|
1352
|
+
tmp[o] = src.rgba[si];
|
|
1353
|
+
tmp[o + 1] = src.rgba[si + 1];
|
|
1354
|
+
tmp[o + 2] = src.rgba[si + 2];
|
|
1355
|
+
const d = Math.abs(src.rgba[si] - bg[0]) + Math.abs(src.rgba[si + 1] - bg[1]) + Math.abs(src.rgba[si + 2] - bg[2]);
|
|
1356
|
+
tmp[o + 3] = d <= 66 ? 0 : 255;
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
crop = bilinearResize({ width: cropW, height: cropH, rgba: tmp }, cellW, cellH);
|
|
1360
|
+
}
|
|
1361
|
+
const out = Buffer.alloc(settings.cellWidth * settings.cellHeight * 4);
|
|
1362
|
+
const left = Math.round(settings.cellWidth / 2 - cellW / 2);
|
|
1363
|
+
const top = anchorY - cellH;
|
|
1364
|
+
for (let y = 0; y < cellH; y++) {
|
|
1365
|
+
const ty = top + y;
|
|
1366
|
+
if (ty < 0 || ty >= settings.cellHeight)
|
|
1367
|
+
continue;
|
|
1368
|
+
for (let x = 0; x < cellW; x++) {
|
|
1369
|
+
const tx = left + x;
|
|
1370
|
+
if (tx < 0 || tx >= settings.cellWidth)
|
|
1371
|
+
continue;
|
|
1372
|
+
const si = (y * cellW + x) * 4;
|
|
1373
|
+
const o = (ty * settings.cellWidth + tx) * 4;
|
|
1374
|
+
out[o] = crop.rgba[si];
|
|
1375
|
+
out[o + 1] = crop.rgba[si + 1];
|
|
1376
|
+
out[o + 2] = crop.rgba[si + 2];
|
|
1377
|
+
out[o + 3] = crop.rgba[si + 3];
|
|
1378
|
+
}
|
|
1379
|
+
}
|
|
1380
|
+
return {
|
|
1381
|
+
bitmap: { width: settings.cellWidth, height: settings.cellHeight, rgba: out },
|
|
1382
|
+
report: {
|
|
1383
|
+
mode: "sprite",
|
|
1384
|
+
srcBounds: [bounds.left, bounds.top, bounds.right, bounds.bottom],
|
|
1385
|
+
srcSize: [cropW, cropH],
|
|
1386
|
+
scale: Number(k.toFixed(4)),
|
|
1387
|
+
cellSize: [cellW, cellH],
|
|
1388
|
+
placedAt: [left, top],
|
|
1389
|
+
anchorY
|
|
1390
|
+
}
|
|
1391
|
+
};
|
|
1392
|
+
}
|