dsh-game-material-master 0.4.0 → 0.7.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 +101 -12
- package/README.md +86 -11
- package/lib/ark.js +1 -1
- package/lib/{tilemedia.js → bitmap.js} +21 -3
- package/lib/client.js +3295 -158
- package/lib/config.js +70 -2
- package/lib/customimage.js +518 -0
- package/lib/imagegen.js +15 -15
- package/lib/imageprovider.js +151 -0
- package/lib/index.js +305 -9
- package/lib/jsonio.js +24 -3
- package/lib/links.js +11 -2
- package/lib/mapauto.js +126 -0
- package/lib/mapdoc.js +617 -0
- package/lib/mapexport.js +188 -0
- package/lib/mapgen.js +1591 -0
- package/lib/mapgeom.js +320 -0
- package/lib/maprender.js +484 -0
- package/lib/mapsample.js +36 -0
- package/lib/mapsamplegen.js +760 -0
- package/lib/maptiles.js +253 -0
- package/lib/media.js +34 -0
- package/lib/pipeline.js +197 -15
- package/lib/rigexport.js +29 -98
- package/lib/riggen.js +34 -30
- package/lib/rigpath.js +47 -10
- package/lib/rigpreview.js +91 -84
- package/lib/rigsemantics.js +4 -4
- package/lib/rigvalidate.js +72 -14
- package/lib/spine.js +147 -80
- package/lib/store.js +12 -3
- package/lib/tilegen.js +7 -11
- package/lib/tilegeom.js +1 -1
- package/lib/tools.js +253 -29
- package/lib/wire.js +198 -3
- package/lib/zip.js +127 -0
- package/package.json +10 -4
package/lib/maprender.js
ADDED
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 地图编辑器(模块六)的**渲染计划**:宿主是唯一渲染器。
|
|
3
|
+
*
|
|
4
|
+
* ## 为什么要有「计划」这一层
|
|
5
|
+
*
|
|
6
|
+
* 旧模块(`tilemap.ts` + 界面里的复刻)把「变体怎么挑、装饰怎么居中、裁剪偏移
|
|
7
|
+
* 多少、建筑锚点在哪」写了两遍 —— 两边只要有一处口径不同,就表现为
|
|
8
|
+
* 「保存后整体抬高 / 树木悬空 / 点到的格子和涂到的不是同一格」,而且**不报错**。
|
|
9
|
+
*
|
|
10
|
+
* 现在只有一条路:宿主算出**绘制计划**(每一项 = 用哪块图、画在哪个像素),
|
|
11
|
+
* 客户端拿 `<canvas>` 原样执行,导出 PNG 也执行同一份计划。于是
|
|
12
|
+
* 「预览 = 导出」是构造出来的,不是靠自检碰运气对出来的。
|
|
13
|
+
*
|
|
14
|
+
* ## 计划里有什么
|
|
15
|
+
*
|
|
16
|
+
* 有:`{l, r, c, t, x, y, a}` —— 层号、**规范**格坐标(wrap 后)、第 0 帧的全局图块
|
|
17
|
+
* 序号、目标左上角**绝对像素**(可越界)、不透明度(恒 1,字段保留)。多帧地块额外带
|
|
18
|
+
* 一条 `cycle`(该格按帧序排列的图块序号),`fps` / `frameCount` / `cycles` 描述动画。
|
|
19
|
+
* 没有:源矩形(在 `legend` 里按序号查一次)、任何需要二次判断的东西。
|
|
20
|
+
*
|
|
21
|
+
* ## 三件容易静默坏掉的事
|
|
22
|
+
*
|
|
23
|
+
* 1. **环绕**:`range` 可以越界(负 / 超过 cols / rows),同一格会在多个像素位置出现。
|
|
24
|
+
* 读格一律走 `wrapCell`,像素位置用**未 wrap** 的坐标 —— 把两者搞反就是「接缝处
|
|
25
|
+
* 画的是地图另一头的格子」,画面看着正常、内容全错。
|
|
26
|
+
* 2. **A2 拼块**:一格发 **4 条 op**(TL/TR/BL/BR),每条指向素材里的一个**小格**。
|
|
27
|
+
* 索引公式见 `a2SmallIndex` 的注释(对着 `maptiles.buildTiles` 的切分写的)。
|
|
28
|
+
* 3. **动画**:A2 的「帧」就是变体;四个象限必须用**同一个全局帧号**,所以 4 条 op
|
|
29
|
+
* 各带一条长度相同的 `cycle`。`t` 必须等于 `cycle[0]`,否则第 0 帧和其余帧错位。
|
|
30
|
+
*/
|
|
31
|
+
import { encodePng } from "./png.js";
|
|
32
|
+
import { cellIndex } from "./mapdoc.js";
|
|
33
|
+
import { cellCenter, cellTopLeft, chunkKey, chunkOf, layoutOf, parseChunkKey, wrapCell } from "./mapgeom.js";
|
|
34
|
+
import { a2QuadrantShapes, corner16Mask, nearestMask } from "./mapauto.js";
|
|
35
|
+
import { flatTiles } from "./maptiles.js";
|
|
36
|
+
/** 单张导出图的最大像素数(超过就明确拒绝,而不是把进程 OOM 掉)。 */
|
|
37
|
+
export const MAX_RENDER_PIXELS = 64_000_000;
|
|
38
|
+
/** 动画帧率:AsLib 的 60 帧 ≈ 1 秒切一次。 */
|
|
39
|
+
const DEFAULT_FPS = 1;
|
|
40
|
+
/**
|
|
41
|
+
* 计划的包围盒。
|
|
42
|
+
*
|
|
43
|
+
* 只按**整格**算:A2 是半格拼块、普通块恰好一格,所以包围盒 = 网格脚印,
|
|
44
|
+
* 且同一份输入永远得到同一个矩形。旧实现按「最大贴图溢出 + 层高偏移」外扩,
|
|
45
|
+
* 那两个来源(跨格大贴图、层高)已经随旧模型一起删掉了。
|
|
46
|
+
*/
|
|
47
|
+
export function planBounds(context, doc) {
|
|
48
|
+
const layout = layoutOf(context.grid, doc.cols, doc.rows);
|
|
49
|
+
return { originX: 0, originY: 0, width: layout.width, height: layout.height };
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* 每个图集第 0 块的全局序号。
|
|
53
|
+
*
|
|
54
|
+
* 从 `flatTiles` 派生(不是自己再算一遍图集顺序 —— 序号的真源只有那一处)。
|
|
55
|
+
*/
|
|
56
|
+
function tilesetBaseIndex(tilesets) {
|
|
57
|
+
const out = new Map();
|
|
58
|
+
for (const flat of flatTiles(tilesets)) {
|
|
59
|
+
if (!out.has(flat.tilesetId))
|
|
60
|
+
out.set(flat.tilesetId, flat.index);
|
|
61
|
+
}
|
|
62
|
+
return out;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* A2 素材里「块 (variant, shape) 的某个象限」对应的小格序号(0-based,图集内)。
|
|
66
|
+
*
|
|
67
|
+
* `maptiles.buildTiles` 把 A2 图切成**小格行优先**的一张表:
|
|
68
|
+
* 小格列数 = `floor(imageWidth / 块宽) * 2`(只切完整的逻辑块,横向不留半块),
|
|
69
|
+
* 共 10 行小格。块 (variant, shape) 的 4 个小格占:
|
|
70
|
+
* 小格列 = `variant * 2 + (0|1)`(TL/TR 取 0,BL/BR 取 1)
|
|
71
|
+
* 小格行 = `shape * 2 + (0|1)`(TL/TR 取 0,BL/BR 取 1)
|
|
72
|
+
* 于是图集内的 0-based 序号 = `小格行 * 小格列数 + 小格列`。
|
|
73
|
+
*
|
|
74
|
+
* 手算核对(`cell = 32×32`、图 `64×160` → 小格 16×16、小格网格 4 列 × 10 行):
|
|
75
|
+
* variant 0 占小格列 0..1,variant 1 占 2..3;shape 0 占小格行 0..1 … shape 4 占 8..9。
|
|
76
|
+
* (variant 0, shape 0, TL) = 0*4+0 = 0 → 1-based 1(图左上角那块)✓
|
|
77
|
+
* (variant 0, shape 0, BR) = 1*4+1 = 5 → 6(第一个块右下角)✓
|
|
78
|
+
* (variant 1, shape 0, TL) = 0*4+2 = 2 → 3(第二个块左上角)✓
|
|
79
|
+
* (variant 0, shape 4, BR) = 9*4+1 = 37 → 38(图最底行、variant 0 的右半)✓
|
|
80
|
+
*/
|
|
81
|
+
function a2SmallIndex(tileset, smallCols, variant, shape, quad) {
|
|
82
|
+
const smallRow = shape * 2 + Math.floor(quad / 2);
|
|
83
|
+
const smallCol = variant * 2 + (quad % 2);
|
|
84
|
+
return smallRow * smallCols + smallCol;
|
|
85
|
+
}
|
|
86
|
+
/** 解出一个格子的全部 op(A2 是 4 条,其余 1 条)。 */
|
|
87
|
+
function cellOpsAt(env, layerId, layerIndex, rr, cc) {
|
|
88
|
+
const w = wrapCell(env.layout, rr, cc);
|
|
89
|
+
// 非环绕时越界就是越界(`wrapCell` 原样返回),必须在这里拦住。
|
|
90
|
+
if (w.r < 0 || w.c < 0 || w.r >= env.doc.rows || w.c >= env.doc.cols)
|
|
91
|
+
return [];
|
|
92
|
+
const data = env.doc.data[layerId];
|
|
93
|
+
const value = data === undefined ? 0 : data[cellIndex(env.doc, w.r, w.c)];
|
|
94
|
+
if (!(value > 0))
|
|
95
|
+
return [];
|
|
96
|
+
const flat = env.context.lookup.get(value);
|
|
97
|
+
const tileset = flat === undefined ? undefined : env.tilesetById.get(flat.tilesetId);
|
|
98
|
+
const base = flat === undefined ? undefined : env.baseIndex.get(flat.tilesetId);
|
|
99
|
+
if (flat === undefined || tileset === undefined || base === undefined) {
|
|
100
|
+
env.sink.missing.add(value);
|
|
101
|
+
return [];
|
|
102
|
+
}
|
|
103
|
+
const local = value - base;
|
|
104
|
+
if (local < 0 || local >= tileset.tiles.length) {
|
|
105
|
+
env.sink.missing.add(value);
|
|
106
|
+
return [];
|
|
107
|
+
}
|
|
108
|
+
// 「(r,c) 是不是同一类」= 同一张图集的块。变体 / 形状是渲染时推导出来的,
|
|
109
|
+
// 存的是哪一块都不影响连接判断。**不要**改成比较 `shape`:界面上从调色板随便点
|
|
110
|
+
// 一块(不同变体 / 形状)就会被判成断开,大片地面碎成小方块,而且不报错。
|
|
111
|
+
const at = (nr, nc) => {
|
|
112
|
+
const cell = wrapCell(env.layout, nr, nc);
|
|
113
|
+
if (cell.r < 0 || cell.c < 0 || cell.r >= env.doc.rows || cell.c >= env.doc.cols)
|
|
114
|
+
return false;
|
|
115
|
+
const raw = data === undefined ? 0 : data[cellIndex(env.doc, cell.r, cell.c)];
|
|
116
|
+
if (!(raw > 0))
|
|
117
|
+
return false;
|
|
118
|
+
const neighbor = env.context.lookup.get(raw);
|
|
119
|
+
return neighbor !== undefined && neighbor.tilesetId === tileset.id;
|
|
120
|
+
};
|
|
121
|
+
// ⚠️ 这条分支是 A2 笔刷能不能用的关键:调色板给 A2 素材只出一个条目(`mapgen.paletteFor`
|
|
122
|
+
// 挑的是该素材里 shape 最大、variant 0 的那一小格当缩略图,写进图层数据的就是它的序号),
|
|
123
|
+
// 所以存下来的值本身**只是代表** ——
|
|
124
|
+
// 少了这条分支,用户拿 A2 笔刷刷下去只会画出一个 16px 小格(而不是 4 象限拼块)。
|
|
125
|
+
if (tileset.shape === "a2quad")
|
|
126
|
+
return a2Ops(env, layerIndex, tileset, base, at, w, rr, cc);
|
|
127
|
+
return wholeCellOps(env, layerIndex, tileset, base, local, at, w, rr, cc);
|
|
128
|
+
}
|
|
129
|
+
/** 整格地块(`plain` / `corner16`):一条 op,必要时带逐行动画的循环。 */
|
|
130
|
+
function wholeCellOps(env, layerIndex, tileset, base, local, at, w, rr, cc) {
|
|
131
|
+
let position = local;
|
|
132
|
+
if (tileset.shape === "corner16") {
|
|
133
|
+
const configured = [];
|
|
134
|
+
for (const tile of tileset.tiles) {
|
|
135
|
+
if (typeof tile.mask === "number" && Number.isInteger(tile.mask))
|
|
136
|
+
configured.push(tile.mask);
|
|
137
|
+
}
|
|
138
|
+
if (configured.length === 0) {
|
|
139
|
+
// 一块掩码都没配:契约规定退「图集的第一块」,绝不留空(留空 = 屏幕上一个洞)。
|
|
140
|
+
env.sink.unmasked.add(tileset.id);
|
|
141
|
+
position = 0;
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
const wanted = corner16Mask(at, w.r, w.c);
|
|
145
|
+
const used = configured.includes(wanted) ? wanted : nearestMask(wanted, configured);
|
|
146
|
+
if (used === undefined) {
|
|
147
|
+
env.sink.unmasked.add(tileset.id);
|
|
148
|
+
position = 0;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
const found = tileset.tiles.findIndex((tile) => tile.mask === used);
|
|
152
|
+
if (found >= 0)
|
|
153
|
+
position = found;
|
|
154
|
+
if (used !== wanted) {
|
|
155
|
+
const key = `${tileset.id}|${wanted}|${used}`;
|
|
156
|
+
const row = env.sink.fallback.get(key);
|
|
157
|
+
if (row === undefined)
|
|
158
|
+
env.sink.fallback.set(key, { tilesetId: tileset.id, wanted, used, cells: 1 });
|
|
159
|
+
else
|
|
160
|
+
row.cells++;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const cols = Math.max(1, Math.floor(tileset.imageWidth / Math.max(1, tileset.cell.width)));
|
|
166
|
+
const rows = Math.max(1, Math.floor(tileset.imageHeight / Math.max(1, tileset.cell.height)));
|
|
167
|
+
let cycle;
|
|
168
|
+
if (tileset.animRows === true && rows > 1) {
|
|
169
|
+
// 同列共用一个「逻辑块」,各帧就是各行同列的块。相位统一从第 0 行开始 ——
|
|
170
|
+
// 每一格各自从「用户画的那一行」起步的话,同一片水会各流各的(不同频)。
|
|
171
|
+
const column = position % cols;
|
|
172
|
+
const frames = [];
|
|
173
|
+
for (let row = 0; row < rows; row++) {
|
|
174
|
+
const index = row * cols + column;
|
|
175
|
+
if (index >= tileset.tiles.length)
|
|
176
|
+
break;
|
|
177
|
+
frames.push(base + index);
|
|
178
|
+
}
|
|
179
|
+
if (frames.length > 1)
|
|
180
|
+
cycle = frames;
|
|
181
|
+
}
|
|
182
|
+
const index = cycle === undefined ? base + position : cycle[0];
|
|
183
|
+
const topLeft = cellTopLeft(env.layout, rr, cc, tileset.cell.width, tileset.cell.height);
|
|
184
|
+
const op = { l: layerIndex, r: w.r, c: w.c, t: index, x: Math.round(topLeft.x), y: Math.round(topLeft.y), a: 1 };
|
|
185
|
+
if (cycle !== undefined)
|
|
186
|
+
op.cycle = cycle;
|
|
187
|
+
return [op];
|
|
188
|
+
}
|
|
189
|
+
/** A2 拼块:一格 4 条 op(TL/TR/BL/BR),各自指向「该象限形状 + 变体」的那个小格。 */
|
|
190
|
+
function a2Ops(env, layerIndex, tileset, base, at, w, rr, cc) {
|
|
191
|
+
const halfWidth = tileset.cell.width / 2;
|
|
192
|
+
const halfHeight = tileset.cell.height / 2;
|
|
193
|
+
const shapes = a2QuadrantShapes(at, w.r, w.c);
|
|
194
|
+
// 小格列数必须与 `maptiles.buildTiles` 的切分一致(只切完整逻辑块)。
|
|
195
|
+
const smallCols = Math.max(1, Math.floor(tileset.imageWidth / Math.max(1, tileset.cell.width)) * 2);
|
|
196
|
+
const declared = Math.max(1, Math.round(tileset.variants) || 1);
|
|
197
|
+
const variants = Math.min(declared, Math.max(1, Math.floor(smallCols / 2)));
|
|
198
|
+
if (declared > variants)
|
|
199
|
+
env.sink.a2Range.add(tileset.id);
|
|
200
|
+
// 格子左上角:拿「一格大小」的方块问 `cellTopLeft`,得到的正好是格子左上角,
|
|
201
|
+
// 与贴图锚点怎么定义无关;四个象限再从它加上半格偏移。
|
|
202
|
+
const origin = cellTopLeft(env.layout, rr, cc, env.layout.stepX, env.layout.stepY);
|
|
203
|
+
const ops = [];
|
|
204
|
+
for (let quad = 0; quad < 4; quad++) {
|
|
205
|
+
const shape = shapes[quad];
|
|
206
|
+
const smallRow = shape * 2 + Math.floor(quad / 2);
|
|
207
|
+
const cycle = [];
|
|
208
|
+
for (let variant = 0; variant < variants; variant++) {
|
|
209
|
+
const index = a2SmallIndex(tileset, smallCols, variant, shape, quad);
|
|
210
|
+
if (index < 0 || index >= tileset.tiles.length) {
|
|
211
|
+
env.sink.a2Range.add(tileset.id);
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
const rect = tileset.tiles[index].rect;
|
|
215
|
+
if (rect.x !== (variant * 2 + (quad % 2)) * halfWidth || rect.y !== smallRow * halfHeight || rect.width !== halfWidth || rect.height !== halfHeight) {
|
|
216
|
+
// 仍然按行优先公式取块(换别的算法也未必对),但要说出来 —— 错位是看得见的。
|
|
217
|
+
env.sink.a2Layout.add(tileset.id);
|
|
218
|
+
}
|
|
219
|
+
cycle.push(base + index);
|
|
220
|
+
}
|
|
221
|
+
if (cycle.length === 0)
|
|
222
|
+
continue;
|
|
223
|
+
const op = {
|
|
224
|
+
l: layerIndex,
|
|
225
|
+
r: w.r,
|
|
226
|
+
c: w.c,
|
|
227
|
+
t: cycle[0],
|
|
228
|
+
x: Math.round(origin.x + (quad % 2) * halfWidth),
|
|
229
|
+
y: Math.round(origin.y + Math.floor(quad / 2) * halfHeight),
|
|
230
|
+
a: 1
|
|
231
|
+
};
|
|
232
|
+
// 变体差异就是动画:四个象限各带一条**等长**的 cycle,帧号因此是全局同一个。
|
|
233
|
+
if (cycle.length > 1)
|
|
234
|
+
op.cycle = cycle;
|
|
235
|
+
ops.push(op);
|
|
236
|
+
}
|
|
237
|
+
return ops;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* 生成绘制计划。
|
|
241
|
+
*
|
|
242
|
+
* - `opts.range` 缺省 = 整张地图(`0..cols` / `0..rows`)—— **导出走这条**,不环绕。
|
|
243
|
+
* 给了就越界也算(环绕):同一格会在多个像素位置出现,读格一律 `wrapCell`。
|
|
244
|
+
* - `opts.layers` 只算这些层号(单层预览);`op.l` 仍是真实层号。
|
|
245
|
+
* - chunk 之间按行优先的 chunk 序拼接,chunk 内按 行 → 列 → 层 稳定排序:
|
|
246
|
+
* 客户端照序 `drawImage` 即可与宿主逐像素同构。
|
|
247
|
+
*/
|
|
248
|
+
export function buildPlan(context, doc, opts = {}) {
|
|
249
|
+
const layout = layoutOf(context.grid, doc.cols, doc.rows);
|
|
250
|
+
const range = opts.range ?? { r0: 0, c0: 0, r1: doc.rows, c1: doc.cols };
|
|
251
|
+
const keepLayers = opts.layers === undefined ? undefined : new Set(opts.layers);
|
|
252
|
+
const legend = opts.includeLegend === false
|
|
253
|
+
? []
|
|
254
|
+
: flatTiles(context.tilesets).map((flat) => ({ index: flat.index, tilesetId: flat.tilesetId, rect: flat.tile.rect }));
|
|
255
|
+
const env = {
|
|
256
|
+
context,
|
|
257
|
+
doc,
|
|
258
|
+
layout,
|
|
259
|
+
baseIndex: tilesetBaseIndex(context.tilesets),
|
|
260
|
+
tilesetById: new Map(context.tilesets.map((tileset) => [tileset.id, tileset])),
|
|
261
|
+
sink: { missing: new Set(), fallback: new Map(), unmasked: new Set(), a2Layout: new Set(), a2Range: new Set() }
|
|
262
|
+
};
|
|
263
|
+
// 每个 op 额外记下**未 wrap** 的 (rr,cc):op 上的 r/c 是规范坐标,用来排序的话
|
|
264
|
+
// 环绕视口里会把地图的另一头排到前面(屏幕上的行序就乱了)。
|
|
265
|
+
const buckets = new Map();
|
|
266
|
+
for (let layerIndex = 0; layerIndex < doc.layers.length; layerIndex++) {
|
|
267
|
+
if (keepLayers !== undefined && !keepLayers.has(layerIndex))
|
|
268
|
+
continue;
|
|
269
|
+
const layer = doc.layers[layerIndex];
|
|
270
|
+
if (layer === undefined)
|
|
271
|
+
continue;
|
|
272
|
+
for (let rr = range.r0; rr < range.r1; rr++) {
|
|
273
|
+
for (let cc = range.c0; cc < range.c1; cc++) {
|
|
274
|
+
const ops = cellOpsAt(env, layer.id, layerIndex, rr, cc);
|
|
275
|
+
if (ops.length === 0)
|
|
276
|
+
continue;
|
|
277
|
+
const key = chunkKey(chunkOf(rr, cc));
|
|
278
|
+
let list = buckets.get(key);
|
|
279
|
+
if (list === undefined) {
|
|
280
|
+
list = [];
|
|
281
|
+
buckets.set(key, list);
|
|
282
|
+
}
|
|
283
|
+
for (const op of ops)
|
|
284
|
+
list.push({ op, rr, cc });
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
const warnings = [];
|
|
289
|
+
const nameOf = (id) => env.tilesetById.get(id)?.name ?? id;
|
|
290
|
+
if (context.tilesets.length === 0)
|
|
291
|
+
warnings.push("还没有导入任何图集。");
|
|
292
|
+
if (env.sink.missing.size > 0) {
|
|
293
|
+
warnings.push(`地图里用到了 ${env.sink.missing.size} 个在图集里找不到的图块序号(素材被删过或改过切分?),这些格子没有画出来。`);
|
|
294
|
+
}
|
|
295
|
+
for (const row of env.sink.fallback.values()) {
|
|
296
|
+
warnings.push(`图集「${nameOf(row.tilesetId)}」没有掩码 ${row.wanted} 的块,${row.cells} 个格子用了最近的掩码 ${row.used} 顶上。`);
|
|
297
|
+
}
|
|
298
|
+
for (const id of env.sink.unmasked)
|
|
299
|
+
warnings.push(`图集「${nameOf(id)}」是自动连接形态,但没有一块带掩码,统一按第一块绘制。`);
|
|
300
|
+
for (const id of env.sink.a2Layout)
|
|
301
|
+
warnings.push(`图集「${nameOf(id)}」的 A2 小格排列与预期(小格行优先)不一致,拼出来的地块可能错位。`);
|
|
302
|
+
for (const id of env.sink.a2Range)
|
|
303
|
+
warnings.push(`图集「${nameOf(id)}」的变体 / 形状数超过了素材里的小格,画不出来的帧被丢掉了。`);
|
|
304
|
+
const chunks = [];
|
|
305
|
+
const cycles = [];
|
|
306
|
+
const seenCycles = new Set();
|
|
307
|
+
let frameCount = 1;
|
|
308
|
+
const ordered = [...buckets.entries()].sort((a, b) => {
|
|
309
|
+
const pa = parseChunkKey(a[0]) ?? { cx: 0, cy: 0 };
|
|
310
|
+
const pb = parseChunkKey(b[0]) ?? { cx: 0, cy: 0 };
|
|
311
|
+
return pa.cy - pb.cy || pa.cx - pb.cx;
|
|
312
|
+
});
|
|
313
|
+
for (const [key, list] of ordered) {
|
|
314
|
+
list.sort((a, b) => a.rr - b.rr || a.cc - b.cc || a.op.l - b.op.l);
|
|
315
|
+
for (const item of list) {
|
|
316
|
+
const cycle = item.op.cycle;
|
|
317
|
+
if (cycle === undefined)
|
|
318
|
+
continue;
|
|
319
|
+
if (cycle.length > frameCount)
|
|
320
|
+
frameCount = cycle.length;
|
|
321
|
+
const id = cycle.join(",");
|
|
322
|
+
if (seenCycles.has(id))
|
|
323
|
+
continue;
|
|
324
|
+
seenCycles.add(id);
|
|
325
|
+
cycles.push(cycle);
|
|
326
|
+
}
|
|
327
|
+
chunks.push({ key, ops: list.map((item) => item.op) });
|
|
328
|
+
}
|
|
329
|
+
return {
|
|
330
|
+
chunks,
|
|
331
|
+
bounds: planBounds(context, doc),
|
|
332
|
+
legend,
|
|
333
|
+
fps: DEFAULT_FPS,
|
|
334
|
+
frameCount,
|
|
335
|
+
cycles,
|
|
336
|
+
markers: buildMarkers(context, doc, range),
|
|
337
|
+
warnings
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* 落在 `range` 里的标记。
|
|
342
|
+
*
|
|
343
|
+
* 与 `cellOpsAt` 同一套走法:按**未 wrap** 的格坐标遍历视口,读格时 `wrapCell` ——
|
|
344
|
+
* 于是环绕接缝处的标记会以多个像素位置各出一条,界面不需要知道环绕。
|
|
345
|
+
*
|
|
346
|
+
* 一格最多一个标记(宿主 `applyOps` 已经拦住重复放置),所以这里是 1:1 的查表。
|
|
347
|
+
*/
|
|
348
|
+
export function buildMarkers(context, doc, range) {
|
|
349
|
+
if (doc.markers.length === 0)
|
|
350
|
+
return [];
|
|
351
|
+
const layout = layoutOf(context.grid, doc.cols, doc.rows);
|
|
352
|
+
const byCell = new Map();
|
|
353
|
+
doc.markers.forEach((marker, index) => {
|
|
354
|
+
byCell.set(marker.r * doc.cols + marker.c, { n: index + 1, marker });
|
|
355
|
+
});
|
|
356
|
+
// 圆别比格子还大(小网格 + 大标记会互相盖住),字号跟着半径走。
|
|
357
|
+
const radius = Math.max(5, Math.round(Math.min(layout.stepX, layout.stepY) * 0.42));
|
|
358
|
+
const font = Math.max(8, Math.round(radius * 1.2));
|
|
359
|
+
const out = [];
|
|
360
|
+
for (let rr = range.r0; rr < range.r1; rr++) {
|
|
361
|
+
for (let cc = range.c0; cc < range.c1; cc++) {
|
|
362
|
+
const w = wrapCell(layout, rr, cc);
|
|
363
|
+
// 非环绕时越界就是越界(`wrapCell` 原样返回)。
|
|
364
|
+
if (w.r < 0 || w.c < 0 || w.r >= doc.rows || w.c >= doc.cols)
|
|
365
|
+
continue;
|
|
366
|
+
const hit = byCell.get(w.r * doc.cols + w.c);
|
|
367
|
+
if (hit === undefined)
|
|
368
|
+
continue;
|
|
369
|
+
const center = cellCenter(layout, rr, cc);
|
|
370
|
+
out.push({
|
|
371
|
+
id: hit.marker.id,
|
|
372
|
+
n: hit.n,
|
|
373
|
+
name: hit.marker.name,
|
|
374
|
+
r: w.r,
|
|
375
|
+
c: w.c,
|
|
376
|
+
x: Math.round(center.x),
|
|
377
|
+
y: Math.round(center.y),
|
|
378
|
+
radius,
|
|
379
|
+
font
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return out;
|
|
384
|
+
}
|
|
385
|
+
// ── 光栅化(导出 PNG 用)──────────────────────────────────────────────────
|
|
386
|
+
/** source-over 合成(与浏览器 canvas 的默认混合一致)。 */
|
|
387
|
+
function blendPixel(dst, index, r, g, b, a) {
|
|
388
|
+
if (a <= 0)
|
|
389
|
+
return;
|
|
390
|
+
const sa = Math.min(1, a);
|
|
391
|
+
const da = dst[index + 3] / 255;
|
|
392
|
+
const outA = sa + da * (1 - sa);
|
|
393
|
+
if (outA <= 0)
|
|
394
|
+
return;
|
|
395
|
+
const mix = (s, d) => Math.round((s * sa + d * da * (1 - sa)) / outA);
|
|
396
|
+
dst[index] = mix(r, dst[index]);
|
|
397
|
+
dst[index + 1] = mix(g, dst[index + 1]);
|
|
398
|
+
dst[index + 2] = mix(b, dst[index + 2]);
|
|
399
|
+
dst[index + 3] = Math.round(outA * 255);
|
|
400
|
+
}
|
|
401
|
+
/** 最近邻整数缩放贴一块图(像素画专用,不做插值)。 */
|
|
402
|
+
function blitScaled(dst, source, rect, dx, dy, scale, alpha) {
|
|
403
|
+
const x0 = Math.max(0, -dx);
|
|
404
|
+
const y0 = Math.max(0, -dy);
|
|
405
|
+
const x1 = Math.min(rect.width, Math.ceil((dst.width - dx) / scale));
|
|
406
|
+
const y1 = Math.min(rect.height, Math.ceil((dst.height - dy) / scale));
|
|
407
|
+
for (let sy = y0; sy < y1; sy++) {
|
|
408
|
+
const srcRow = (rect.y + sy) * source.width;
|
|
409
|
+
if (rect.y + sy < 0 || rect.y + sy >= source.height)
|
|
410
|
+
continue;
|
|
411
|
+
for (let sx = x0; sx < x1; sx++) {
|
|
412
|
+
const srcX = rect.x + sx;
|
|
413
|
+
if (srcX < 0 || srcX >= source.width)
|
|
414
|
+
continue;
|
|
415
|
+
const si = (srcRow + srcX) * 4;
|
|
416
|
+
const a = (source.rgba[si + 3] / 255) * alpha;
|
|
417
|
+
if (a <= 0)
|
|
418
|
+
continue;
|
|
419
|
+
const r = source.rgba[si];
|
|
420
|
+
const g = source.rgba[si + 1];
|
|
421
|
+
const b = source.rgba[si + 2];
|
|
422
|
+
for (let oy = 0; oy < scale; oy++) {
|
|
423
|
+
const ty = dy + sy * scale + oy;
|
|
424
|
+
if (ty < 0 || ty >= dst.height)
|
|
425
|
+
continue;
|
|
426
|
+
for (let ox = 0; ox < scale; ox++) {
|
|
427
|
+
const tx = dx + sx * scale + ox;
|
|
428
|
+
if (tx < 0 || tx >= dst.width)
|
|
429
|
+
continue;
|
|
430
|
+
blendPixel(dst.rgba, (ty * dst.width + tx) * 4, r, g, b, a);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* 把计划光栅化成位图(导出 `map.png` 用)。
|
|
438
|
+
*
|
|
439
|
+
* `lookup` 是**已解码的图集**(tilesetId → 位图);解码由调用方做一次,
|
|
440
|
+
* 一张地图里同一张图集只解一次,不要每块贴图解一遍。
|
|
441
|
+
*
|
|
442
|
+
* 只画**第 0 帧**(`op.t`):PNG 是静帧,动画信息留在 `map.json` 与计划里。
|
|
443
|
+
*/
|
|
444
|
+
export function rasterize(context, doc, lookup, options = {}) {
|
|
445
|
+
const scale = Math.max(1, Math.round(options.scale ?? 1));
|
|
446
|
+
// 不传 range = 整张地图、不环绕(导出就是这条路)。
|
|
447
|
+
const plan = buildPlan(context, doc, { includeLegend: true });
|
|
448
|
+
const width = Math.ceil(plan.bounds.width * scale);
|
|
449
|
+
const height = Math.ceil(plan.bounds.height * scale);
|
|
450
|
+
if (width < 1 || height < 1)
|
|
451
|
+
throw new Error("这张地图还没有任何内容,先画点东西再导出。");
|
|
452
|
+
if (width * height > MAX_RENDER_PIXELS) {
|
|
453
|
+
throw new Error(`导出图会到 ${width}×${height}(${((width * height) / 1e6).toFixed(1)}M 像素),超过 ${MAX_RENDER_PIXELS / 1e6}M 上限。` +
|
|
454
|
+
`请把缩放调小,或把地图拆小。`);
|
|
455
|
+
}
|
|
456
|
+
const bitmap = { width, height, rgba: Buffer.alloc(width * height * 4) };
|
|
457
|
+
const legend = new Map(plan.legend.map((entry) => [entry.index, entry]));
|
|
458
|
+
const warnings = [...plan.warnings];
|
|
459
|
+
const missingBitmaps = new Set();
|
|
460
|
+
let drawn = 0;
|
|
461
|
+
for (const chunk of plan.chunks) {
|
|
462
|
+
for (const op of chunk.ops) {
|
|
463
|
+
const entry = legend.get(op.t);
|
|
464
|
+
if (entry === undefined)
|
|
465
|
+
continue;
|
|
466
|
+
const source = lookup.get(entry.tilesetId);
|
|
467
|
+
if (source === undefined) {
|
|
468
|
+
// 图集没解码出来就是一片透明,界面上完全看不出来 —— 必须报。
|
|
469
|
+
missingBitmaps.add(entry.tilesetId);
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
blitScaled(bitmap, source, entry.rect, Math.round((op.x - plan.bounds.originX) * scale), Math.round((op.y - plan.bounds.originY) * scale), scale, op.a);
|
|
473
|
+
drawn++;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
for (const id of missingBitmaps) {
|
|
477
|
+
const name = context.tilesets.find((tileset) => tileset.id === id)?.name ?? id;
|
|
478
|
+
warnings.push(`图集「${name}」的图片没有解码出来,它上面的格子没有画进 PNG。`);
|
|
479
|
+
}
|
|
480
|
+
return { bitmap, bounds: plan.bounds, warnings, drawn };
|
|
481
|
+
}
|
|
482
|
+
export function bitmapToPng(bitmap) {
|
|
483
|
+
return encodePng(bitmap.rgba, bitmap.width, bitmap.height);
|
|
484
|
+
}
|
package/lib/mapsample.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 地图编辑器(模块六)· **内置示例素材的纹理覆盖表**(接口占位 + 文档)。
|
|
3
|
+
*
|
|
4
|
+
* ## 这个文件是干什么的
|
|
5
|
+
*
|
|
6
|
+
* 内置示例素材(草地 / 土路 / 水 / 石墙 / 道具)默认**纯程序化**生成,零成本、
|
|
7
|
+
* 确定性、像素精确 —— 实现在 `src/mapsamplegen.ts` 的 `renderLocalTexture()`。
|
|
8
|
+
*
|
|
9
|
+
* 如果想让内置素材更有「手绘感」,可以用火山方舟 Seedream 生成纹理替换掉程序化纹理:
|
|
10
|
+
*
|
|
11
|
+
* ```bash
|
|
12
|
+
* node scripts/make-map-sample-tileset.mjs --ark --yes # ★真实计费
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* 那个脚本会为每种地貌调一次生图(无缝平铺、俯视、无光照方向的像素画纹理),
|
|
16
|
+
* 把结果**降采样成 32×32** 后写回本文件的 `SAMPLE_TEXTURE_OVERRIDE`(base64 PNG)。
|
|
17
|
+
* 运行时 `textureFor()` 见到某个地貌有覆盖就用它,没有就回退程序化 ——
|
|
18
|
+
* 也就是说即使只覆盖了「水」,其余三张图照旧程序化生成,不会因为缺一张就跑不起来。
|
|
19
|
+
*
|
|
20
|
+
* ## 约定
|
|
21
|
+
*
|
|
22
|
+
* - 键 = 地貌种类:`grass` / `dirt` / `water` / `stone`。
|
|
23
|
+
* - 值 = PNG 的 base64(不带 `data:` 前缀),32×32 的纹理(多则居中裁剪、少则平铺)。
|
|
24
|
+
* 解码走 `bitmap.ts` 的统一入口 `decodeFile`(ffmpeg),所以**色深 / 调色板 / 隔行都无所谓**。
|
|
25
|
+
* - 值非空时,调用方必须先 `await primeSampleTextures()` 一次(`src/mapsamplegen.ts`)——
|
|
26
|
+
* 解码是异步的,而 `renderSampleTilesets()` 是同步契约。忘了 prime 会**明确抛错**,
|
|
27
|
+
* 不会悄悄回退成程序化纹理(那样会出现「界面看着能用、纹理却不是他生成的那张」)。
|
|
28
|
+
* - `SAMPLE_VERSION`:覆盖表的版本号。改了程序化纹理的画法、或者重新生成了覆盖图,
|
|
29
|
+
* 就把它 +1,方便排查「用户机器上是旧素材」这一类问题。
|
|
30
|
+
*
|
|
31
|
+
* 依赖:无。本文件不 import 任何东西,也不做任何计算 —— 它只是**数据占位**。
|
|
32
|
+
*/
|
|
33
|
+
/** 由 `scripts/make-map-sample-tileset.mjs --ark` 生成;空对象 = 全部用程序化纹理。 */
|
|
34
|
+
export const SAMPLE_TEXTURE_OVERRIDE = {};
|
|
35
|
+
/** 内置示例素材的版本号;纹理画法或覆盖表变了就 +1。 */
|
|
36
|
+
export const SAMPLE_VERSION = 1;
|