dsh-game-material-master 0.4.0 → 0.6.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 +54 -9
- package/README.md +45 -9
- package/lib/{tilemedia.js → bitmap.js} +21 -3
- package/lib/client.js +2635 -153
- package/lib/index.js +235 -2
- package/lib/jsonio.js +24 -3
- package/lib/links.js +11 -2
- package/lib/mapauto.js +126 -0
- package/lib/mapdoc.js +444 -0
- package/lib/mapexport.js +177 -0
- package/lib/mapgen.js +1579 -0
- package/lib/mapgeom.js +308 -0
- package/lib/maprender.js +430 -0
- package/lib/mapsample.js +36 -0
- package/lib/mapsamplegen.js +760 -0
- package/lib/maptiles.js +253 -0
- package/lib/rigexport.js +29 -98
- package/lib/riggen.js +6 -4
- 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/tilegen.js +1 -1
- package/lib/tilegeom.js +1 -1
- package/lib/tools.js +186 -15
- package/lib/wire.js +179 -3
- package/lib/zip.js +127 -0
- package/package.json +10 -4
package/lib/maptiles.js
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 地图编辑器(模块六)的**图集(tileset)模型与切分**。
|
|
3
|
+
*
|
|
4
|
+
* 一个项目可以导入多张图集;每张图集按**自动识别出来的形态**切成图块:
|
|
5
|
+
*
|
|
6
|
+
* · `plain` 正方形网格:按 `cell` 行优先逐格切;`animRows` 时**同一列的每一行
|
|
7
|
+
* 是同一块的一帧**(地形动画,AsLib 的「逐行动画」)
|
|
8
|
+
* · `corner16` 整格自动连接:切法与 plain 完全相同,只是每块会挂一个 4 位掩码
|
|
9
|
+
* · `a2quad` A2 拼块:一格的四个象限从素材里各取一个小格(详见 `buildTiles`)
|
|
10
|
+
*
|
|
11
|
+
* ## 两条铁律
|
|
12
|
+
*
|
|
13
|
+
* 1. **图块 id 由矩形派生**(`t{x}_{y}`),不是数组下标。重新切分(改格尺寸、改形态)
|
|
14
|
+
* 之后,同一个矩形的元数据仍然挂在同一个 id 上;用下标的话会静默漂移。
|
|
15
|
+
* 2. **单元格里存的是「全局图块序号」(1-based,0 = 空)**,不是图块 id ——
|
|
16
|
+
* 绘制计划、脏 chunk、`map.json` 都靠它。序号由 `flatTiles()` 唯一决定,
|
|
17
|
+
* 顺序 = 图集顺序 → `tiles` 数组顺序,别的地方不许按下标自己算。
|
|
18
|
+
*
|
|
19
|
+
* ## A2 的真实几何(照 AsLib,`AsMap.hpp` 的 `drawMapBranch` / `map20n_Number`)
|
|
20
|
+
*
|
|
21
|
+
* 一张 A2 图:小格边长 `S = imageHeight / 10`(10 行小格),横向 `imageWidth % S === 0`。
|
|
22
|
+
* 一个「逻辑块」(= 屏幕上一格)= 2×2 个小格。于是:
|
|
23
|
+
* · **块行号 0..4 = 形状号**(5 种),**块列号 = 变体号**(素材里横着排了几种)
|
|
24
|
+
* · 逻辑块尺寸 = 2S(32px 的块 = 16px 的小格),逻辑块列数 = `imageWidth / (2S)`
|
|
25
|
+
* · `detectLayout(64, 160)` → S=16、块 32×32、2 变体 × 5 形状
|
|
26
|
+
* (AsLib 要求「小格列数(= NumX)是偶数」,否则变体凑不成对)
|
|
27
|
+
* 小格的行列号与小格内的象限是一一对应的:
|
|
28
|
+
* `smallRow = 2 * shape + (0 上 / 1 下)`,`smallCol = 2 * variant + (0 左 / 1 右)`
|
|
29
|
+
* 这跟 AsLib 的 chip 下标 `NumX * 2 * shape + tm_id(+1)(+NumX)` 逐项相等 ——
|
|
30
|
+
* 我们把 `tiles` 按小格行优先展开,于是 **1-based 序号 − 1 = AsLib 的 chip 下标**。
|
|
31
|
+
* 渲染端(`maprender.ts`)按这个对应关系挑象限块,别自己另立一套顺序。
|
|
32
|
+
*/
|
|
33
|
+
import { pngSize } from "./bitmap.js";
|
|
34
|
+
import { A2_SHAPES } from "./mapauto.js";
|
|
35
|
+
export const COMMON_TILE_SIZES = [8, 16, 24, 32, 48, 64];
|
|
36
|
+
export function tileIdFor(rect) {
|
|
37
|
+
return `t${rect.x}_${rect.y}`;
|
|
38
|
+
}
|
|
39
|
+
export function rectKey(rect) {
|
|
40
|
+
return `${rect.x},${rect.y},${rect.width},${rect.height}`;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* plain 兜底的试算顺序:**先 ≤32 的降序**(32 → 24 → 16 → 8),再 >32 的升序(48 → 64)。
|
|
44
|
+
*
|
|
45
|
+
* 为什么不直接取「能整除的最大尺寸」:像素画工作流的默认口径就是 32px,而「最大」
|
|
46
|
+
* 会把 2×2 个 32px 道具并成一个 64px 格 —— 那是**静默改掉用户素材的语义**(一块里
|
|
47
|
+
* 装四个道具),比报错难查得多。48 / 64 只在前面的都试不出来时才轮到。
|
|
48
|
+
*
|
|
49
|
+
* 举例(都是 32px 网格):`256×192` = 8 列 × 6 行 —— 若取最大会得到 64px / 4×3;
|
|
50
|
+
* `96×96` = 3×3 —— 若取最大会得到 48px。注意 `256×160`(8×5)**轮不到这一分支**:
|
|
51
|
+
* 它会先命中上面的 A2 判据(高 = 5 × 32、宽能被 32 整除),那正是内置道具图走
|
|
52
|
+
* `importSampleTileset` 的 `known` 通道显式声明形态、不经自动识别的原因。
|
|
53
|
+
*/
|
|
54
|
+
const PLAIN_PREFERRED_SIZES = [
|
|
55
|
+
...COMMON_TILE_SIZES.filter((size) => size <= 32).reverse(),
|
|
56
|
+
...COMMON_TILE_SIZES.filter((size) => size > 32)
|
|
57
|
+
];
|
|
58
|
+
/**
|
|
59
|
+
* 自动识别切分方式。
|
|
60
|
+
*
|
|
61
|
+
* **先试 A2**(`imageHeight === 5 × 块边长` 且 `imageWidth` 能被块边长整除,从小到大),
|
|
62
|
+
* 免得 64×160 的草地被当成普通网格;再按 `PLAIN_PREFERRED_SIZES` 找能整除两者的
|
|
63
|
+
* 正方形网格;两个都不成立时退回 32×32,并在 reason 里说清为什么要裁像素。
|
|
64
|
+
*
|
|
65
|
+
* ⚠️ **尺寸判据天生有二义性,这里不是 bug**:`256×160` 既可能是「8 变体 × 5 形状的
|
|
66
|
+
* 拼块图」,也可能是「8×5 的 32px 普通网格」(AsLib 靠清单文件里的 `type` 列区分,
|
|
67
|
+
* 光看尺寸分不开)。这里的取舍是**优先给拼块结论**;内置素材那条路不吃这个判据
|
|
68
|
+
* (`mapgen.importSampleTileset` 带显式的 `shape` / `cell` / `animRows`),
|
|
69
|
+
* 用户自己导入的图靠界面上「这块素材怎么用」的下拉改(`saveMapTileset`)。
|
|
70
|
+
*/
|
|
71
|
+
export function detectLayout(imageWidth, imageHeight) {
|
|
72
|
+
for (const size of COMMON_TILE_SIZES) {
|
|
73
|
+
if (imageHeight !== size * A2_SHAPES || imageWidth % size !== 0)
|
|
74
|
+
continue;
|
|
75
|
+
return {
|
|
76
|
+
...layoutFor("a2quad", { width: size, height: size }, imageWidth, imageHeight),
|
|
77
|
+
reason: `识别到:拼块自动连接(${imageWidth / size} 种变体 × ${A2_SHAPES} 种形状)`
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
for (const size of PLAIN_PREFERRED_SIZES) {
|
|
81
|
+
if (imageWidth < size || imageHeight < size)
|
|
82
|
+
continue;
|
|
83
|
+
if (imageWidth % size !== 0 || imageHeight % size !== 0)
|
|
84
|
+
continue;
|
|
85
|
+
return {
|
|
86
|
+
...layoutFor("plain", { width: size, height: size }, imageWidth, imageHeight),
|
|
87
|
+
reason: `识别到:正方形网格 ${size}×${size}(${imageWidth / size} 列 × ${imageHeight / size} 行)`
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
...layoutFor("plain", { width: 32, height: 32 }, imageWidth, imageHeight),
|
|
92
|
+
reason: `没找到能整除 ${imageWidth}×${imageHeight} 的常见尺寸,先按 32×32 切:右侧 / 底部不足一格的部分会被裁掉。`
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* 按指定的形态与格尺寸算切分结果(用户在图集设置里改形态 / 格尺寸时走这条)。
|
|
97
|
+
*
|
|
98
|
+
* 只取**完整的块**:横向 / 纵向不足一个块的像素被裁掉(`Math.floor`),
|
|
99
|
+
* 因为半个块既挑不出象限也对不上邻接。A2 还要求格尺寸是偶数(小格 = 块 / 2)。
|
|
100
|
+
*/
|
|
101
|
+
export function layoutFor(shape, cell, imageWidth, imageHeight) {
|
|
102
|
+
if (!Number.isInteger(cell.width) || !Number.isInteger(cell.height) || cell.width < 1 || cell.height < 1) {
|
|
103
|
+
throw new Error(`图块尺寸必须是正整数,收到 ${cell.width}×${cell.height}`);
|
|
104
|
+
}
|
|
105
|
+
if (shape === "a2quad") {
|
|
106
|
+
if (cell.width % 2 !== 0 || cell.height % 2 !== 0) {
|
|
107
|
+
throw new Error(`A2 拼块要求逻辑块尺寸是偶数(小格 = 逻辑块 / 2),收到 ${cell.width}×${cell.height}`);
|
|
108
|
+
}
|
|
109
|
+
const cols = Math.floor(imageWidth / cell.width);
|
|
110
|
+
// 形状只有 0..4:第 6 行以后永远选不到,切出来只会让序号表里多出用不上的块。
|
|
111
|
+
const rows = Math.min(A2_SHAPES, Math.floor(imageHeight / cell.height));
|
|
112
|
+
if (cols < 1 || rows < 1) {
|
|
113
|
+
return {
|
|
114
|
+
cell: { width: cell.width, height: cell.height },
|
|
115
|
+
shape,
|
|
116
|
+
cols: 0,
|
|
117
|
+
rows: 0,
|
|
118
|
+
count: 0,
|
|
119
|
+
reason: `素材 ${imageWidth}×${imageHeight} 比一个逻辑块 ${cell.width}×${cell.height} 还小,切不出图块。`
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
cell: { width: cell.width, height: cell.height },
|
|
124
|
+
shape,
|
|
125
|
+
cols,
|
|
126
|
+
rows,
|
|
127
|
+
count: cols * 2 * rows * 2,
|
|
128
|
+
reason: `已设为:拼块自动连接(${cols} 种变体 × ${rows} 种形状)`
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
const cols = Math.floor(imageWidth / cell.width);
|
|
132
|
+
const rows = Math.floor(imageHeight / cell.height);
|
|
133
|
+
if (cols < 1 || rows < 1) {
|
|
134
|
+
return {
|
|
135
|
+
cell: { width: cell.width, height: cell.height },
|
|
136
|
+
shape,
|
|
137
|
+
cols: 0,
|
|
138
|
+
rows: 0,
|
|
139
|
+
count: 0,
|
|
140
|
+
reason: `素材 ${imageWidth}×${imageHeight} 比一个图块 ${cell.width}×${cell.height} 还小,切不出图块。`
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
const count = cols * rows;
|
|
144
|
+
const reason = shape === "corner16"
|
|
145
|
+
? `已设为:整格自动连接(${cell.width}×${cell.height} 切 ${cols} 列 × ${rows} 行,16 种邻接)`
|
|
146
|
+
: `已设为:正方形网格 ${cell.width}×${cell.height}(${cols} 列 × ${rows} 行)`;
|
|
147
|
+
return { cell: { width: cell.width, height: cell.height }, shape, cols, rows, count, reason };
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* 按切分结果生成图块表,并**保留已有元数据**。
|
|
151
|
+
*
|
|
152
|
+
* 保留的判据是矩形派生的 id(`tileIdFor`):改格尺寸后仍然存在的矩形**把旧 tile 上的
|
|
153
|
+
* 字段原样带过来** —— 界面上按位序配好的 16 张连接块、改过的名字不会因为一次
|
|
154
|
+
* 「改格尺寸」全被抹掉。消失的 id 丢弃,新出现的给默认值。
|
|
155
|
+
*
|
|
156
|
+
* ⚠️ 这里**不做白名单拷贝**,只覆盖由几何算出来的那几项(`id` / `rect` / `shape` /
|
|
157
|
+
* `variant` / `frame`)。白名单是旧实现踩过的坑:`MapTile` 以后加一个字段
|
|
158
|
+
* (锚点、通行、标签……),白名单没跟上就会在每次重切时静默清空 —— 而且只有
|
|
159
|
+
* 「改过那个字段、又改了格尺寸」的用户才会遇到。反过来,几何派生项**必须**覆盖:
|
|
160
|
+
* 沿用旧值会让块号与 rect 对不上,静默错位。
|
|
161
|
+
*
|
|
162
|
+
* 顺序(= `flatTiles` 的 1-based 序号顺序,改了就等同于换了一套语义值):
|
|
163
|
+
* · plain / corner16:行优先逐格;`animRows` 时 `frame` = 行号(同列共用一个逻辑块)
|
|
164
|
+
* · a2quad:小格行优先;`shape` = 块行号、`variant` = 块列号、`frame` = 0
|
|
165
|
+
*/
|
|
166
|
+
export function buildTiles(input, previous = []) {
|
|
167
|
+
const { shape, cell, imageWidth, imageHeight } = input;
|
|
168
|
+
const previousById = new Map(previous.map((tile) => [tile.id, tile]));
|
|
169
|
+
const tileOf = (rect, shapeAt, variant, frame) => {
|
|
170
|
+
const id = tileIdFor(rect);
|
|
171
|
+
const old = previousById.get(id);
|
|
172
|
+
// 先整份继承旧 tile(含以后新增的字段),再覆盖几何派生项。
|
|
173
|
+
const tile = old === undefined ? { id, rect, name: id } : { ...old };
|
|
174
|
+
tile.id = id;
|
|
175
|
+
tile.rect = rect;
|
|
176
|
+
if (typeof tile.name !== "string" || tile.name === "")
|
|
177
|
+
tile.name = id;
|
|
178
|
+
if (shapeAt === undefined)
|
|
179
|
+
delete tile.shape;
|
|
180
|
+
else
|
|
181
|
+
tile.shape = shapeAt;
|
|
182
|
+
if (variant === undefined)
|
|
183
|
+
delete tile.variant;
|
|
184
|
+
else
|
|
185
|
+
tile.variant = variant;
|
|
186
|
+
tile.frame = frame;
|
|
187
|
+
return tile;
|
|
188
|
+
};
|
|
189
|
+
if (shape === "a2quad") {
|
|
190
|
+
if (cell.width % 2 !== 0 || cell.height % 2 !== 0) {
|
|
191
|
+
throw new Error(`A2 拼块要求逻辑块尺寸是偶数(小格 = 逻辑块 / 2),收到 ${cell.width}×${cell.height}`);
|
|
192
|
+
}
|
|
193
|
+
const smallWidth = cell.width / 2;
|
|
194
|
+
const smallHeight = cell.height / 2;
|
|
195
|
+
// 只切完整的逻辑块:横向留半块的话,那一列凑不齐左右象限(AsLib 要求小格列数是偶数)。
|
|
196
|
+
const smallCols = Math.floor(imageWidth / cell.width) * 2;
|
|
197
|
+
const smallRows = Math.min(A2_SHAPES, Math.floor(imageHeight / cell.height)) * 2;
|
|
198
|
+
const tiles = [];
|
|
199
|
+
for (let smallRow = 0; smallRow < smallRows; smallRow++) {
|
|
200
|
+
for (let smallCol = 0; smallCol < smallCols; smallCol++) {
|
|
201
|
+
tiles.push(tileOf({ x: smallCol * smallWidth, y: smallRow * smallHeight, width: smallWidth, height: smallHeight }, Math.floor(smallRow / 2), Math.floor(smallCol / 2), 0));
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return tiles;
|
|
205
|
+
}
|
|
206
|
+
const cols = Math.floor(imageWidth / cell.width);
|
|
207
|
+
const rows = Math.floor(imageHeight / cell.height);
|
|
208
|
+
const animRows = input.animRows === true;
|
|
209
|
+
const tiles = [];
|
|
210
|
+
for (let row = 0; row < rows; row++) {
|
|
211
|
+
for (let col = 0; col < cols; col++) {
|
|
212
|
+
tiles.push(tileOf({ x: col * cell.width, y: row * cell.height, width: cell.width, height: cell.height }, undefined, undefined, animRows ? row : 0));
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return tiles;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* 展平成一个全项目通用的索引。
|
|
219
|
+
*
|
|
220
|
+
* 顺序 = 图集顺序 → `tileset.tiles` 数组顺序,**因此只要不动图集顺序与切分参数,
|
|
221
|
+
* 序号就稳定**;这也是 `applyOps` 里存的序号能跨会话读回来的前提。
|
|
222
|
+
* 序号的唯一真源是这里,别的任何地方都不许自己按数组下标算。
|
|
223
|
+
*/
|
|
224
|
+
export function flatTiles(tilesets) {
|
|
225
|
+
const out = [];
|
|
226
|
+
let index = 1;
|
|
227
|
+
for (const tileset of tilesets) {
|
|
228
|
+
for (const tile of tileset.tiles) {
|
|
229
|
+
out.push({ index, tilesetId: tileset.id, tile, value: index });
|
|
230
|
+
index++;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return out;
|
|
234
|
+
}
|
|
235
|
+
export function flatIndexByValue(tilesets) {
|
|
236
|
+
return new Map(flatTiles(tilesets).map((flat) => [flat.value, flat]));
|
|
237
|
+
}
|
|
238
|
+
// ── 图像尺寸探测 / 裁剪 ──────────────────────────────────────────────────
|
|
239
|
+
/** 从文件头拿尺寸;PNG 之外的类型回落到 ffmpeg(见 `bitmap.probeImageSize`)。 */
|
|
240
|
+
export function sizeFromBytes(bytes) {
|
|
241
|
+
return pngSize(bytes);
|
|
242
|
+
}
|
|
243
|
+
/** 给验收 / 界面用的图块位图(裁剪出一块)。 */
|
|
244
|
+
export function cropTile(source, rect) {
|
|
245
|
+
const width = Math.max(1, Math.min(rect.width, source.width - rect.x));
|
|
246
|
+
const height = Math.max(1, Math.min(rect.height, source.height - rect.y));
|
|
247
|
+
const rgba = Buffer.alloc(width * height * 4);
|
|
248
|
+
for (let y = 0; y < height; y++) {
|
|
249
|
+
const from = ((rect.y + y) * source.width + rect.x) * 4;
|
|
250
|
+
source.rgba.copy(rgba, y * width * 4, from, from + width * 4);
|
|
251
|
+
}
|
|
252
|
+
return { width, height, rgba };
|
|
253
|
+
}
|
package/lib/rigexport.js
CHANGED
|
@@ -21,8 +21,9 @@
|
|
|
21
21
|
* 处(`dragonBones/armature/Slot.ts:409-461`)。所以「图片中心落在骨骼原点」
|
|
22
22
|
* 这件事要靠 `pivot = 0.5/0.5` + `frameX = 裁剪偏移` 一起凑出来,裁剪过的部件
|
|
23
23
|
* 少给 `frame*` 就会整体偏掉。
|
|
24
|
-
* - **坐标系是 y 向下**(`DragonBones.yDown` 默认 `true
|
|
25
|
-
*
|
|
24
|
+
* - **坐标系是 y 向下**(`DragonBones.yDown` 默认 `true`),Spine 输入却是 y 向上。
|
|
25
|
+
* 骨骼、图片挂点及动画的 y/旋转必须取反,不能直接复制 Spine 的数值。
|
|
26
|
+
* - 当前 DragonBones 只保证刚体贴图与骨骼动画;网格/FFD/Path 明确降级并报告限制。
|
|
26
27
|
*/
|
|
27
28
|
import { animationPresetOf } from "./spine.js";
|
|
28
29
|
export const DRAGONBONES_VERSION = "5.5";
|
|
@@ -64,11 +65,11 @@ export function encodeDragonBonesCurve(control) {
|
|
|
64
65
|
const TIMELINE_KINDS = {
|
|
65
66
|
rotate: {
|
|
66
67
|
frameKey: "rotateFrame",
|
|
67
|
-
valueOf: (frame) => ({ rotate: round(num(frame.angle, 0), 4) })
|
|
68
|
+
valueOf: (frame) => ({ rotate: round(-num(frame.angle, 0), 4) })
|
|
68
69
|
},
|
|
69
70
|
translate: {
|
|
70
71
|
frameKey: "translateFrame",
|
|
71
|
-
valueOf: (frame) => ({ x: round(num(frame.x, 0), 4), y: round(num(frame.y, 0), 4) })
|
|
72
|
+
valueOf: (frame) => ({ x: round(num(frame.x, 0), 4), y: round(-num(frame.y, 0), 4) })
|
|
72
73
|
},
|
|
73
74
|
scale: {
|
|
74
75
|
frameKey: "scaleFrame",
|
|
@@ -124,20 +125,24 @@ export function buildDragonBonesTimeline(kind, frames, frameRate) {
|
|
|
124
125
|
export function buildDragonBonesSkeleton(options) {
|
|
125
126
|
const frameRate = options.frameRate ?? DRAGONBONES_FRAME_RATE;
|
|
126
127
|
const spine = options.spine;
|
|
128
|
+
const exportWarnings = [];
|
|
129
|
+
for (const constraint of spine?.path ?? []) {
|
|
130
|
+
exportWarnings.push(`Path「${constraint.name}」未导出到 DragonBones:请使用 Spine 导出保留路径约束`);
|
|
131
|
+
}
|
|
127
132
|
const bones = (spine?.bones ?? []).map((bone) => {
|
|
128
|
-
const rotation = round(num(bone.rotation, 0), 4);
|
|
133
|
+
const rotation = round(-num(bone.rotation, 0), 4);
|
|
129
134
|
return {
|
|
130
135
|
name: bone.name,
|
|
131
136
|
...(bone.parent === undefined ? {} : { parent: bone.parent }),
|
|
132
137
|
length: round(num(bone.length, 0), 4),
|
|
133
138
|
transform: {
|
|
134
139
|
x: round(num(bone.x, 0), 4),
|
|
135
|
-
y: round(num(bone.y, 0), 4),
|
|
140
|
+
y: round(-num(bone.y, 0), 4),
|
|
136
141
|
// `skX === skY` 时 `skew === 0`;故意不引入双角度,见方案 §7.3。
|
|
137
142
|
skX: rotation,
|
|
138
143
|
skY: rotation,
|
|
139
|
-
scX: 1,
|
|
140
|
-
scY: 1
|
|
144
|
+
scX: num(bone.scaleX, 1),
|
|
145
|
+
scY: num(bone.scaleY, 1)
|
|
141
146
|
},
|
|
142
147
|
inheritTranslation: true,
|
|
143
148
|
inheritRotation: true,
|
|
@@ -145,55 +150,31 @@ export function buildDragonBonesSkeleton(options) {
|
|
|
145
150
|
inheritReflection: true
|
|
146
151
|
};
|
|
147
152
|
});
|
|
148
|
-
const
|
|
153
|
+
const attachments = spine?.skins?.[0]?.attachments ?? {};
|
|
154
|
+
const imageSlots = (spine?.slots ?? []).filter((slot) => attachments[slot.name]?.[slot.attachment]?.type !== "path");
|
|
155
|
+
const slots = imageSlots.map((slot) => ({
|
|
149
156
|
name: slot.name,
|
|
150
157
|
parent: slot.bone,
|
|
151
158
|
displayIndex: 0
|
|
152
159
|
}));
|
|
153
|
-
const
|
|
154
|
-
const skinSlots = (spine?.slots ?? []).map((slot) => {
|
|
160
|
+
const skinSlots = imageSlots.map((slot) => {
|
|
155
161
|
const attachment = attachments[slot.name]?.[slot.attachment] ?? {};
|
|
156
|
-
const rotation = round(num(attachment.rotation, 0), 4);
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
const isMesh = attachment.type === "mesh" && Array.isArray(attachment.vertices);
|
|
161
|
-
// **加权网格在 DragonBones 里是另一套编码**(顶点流 + `weights` + `slotPose` +
|
|
162
|
-
// `bonePose` 四件套),不能直接吃 Spine 的交错流——喂错了不会报错,只会把
|
|
163
|
-
// 「骨骼下标」当成坐标画出一团乱线。所以这里只处理**非加权**网格;
|
|
164
|
-
// 检测到加权就退回 `image`(贴图仍然正确显示,只是这一块不参与 LBS)。
|
|
165
|
-
// 判定方式:加权的顶点流比 uvs 长(每顶点额外带骨骼下标与权重)。
|
|
166
|
-
const isWeighted = isMesh && Array.isArray(attachment.uvs) && attachment.vertices.length !== attachment.uvs.length;
|
|
167
|
-
const useMesh = isMesh && !isWeighted;
|
|
168
|
-
// `path` 附件也不是贴图:它是一串顶点 + 闭合标记,给 Path 约束当轨道用。
|
|
169
|
-
const isPath = attachment.type === "path";
|
|
162
|
+
const rotation = round(-num(attachment.rotation, 0), 4);
|
|
163
|
+
if (attachment.type === "mesh") {
|
|
164
|
+
exportWarnings.push(`网格「${slot.name}」在 DragonBones 中退回刚体贴图,未保留蒙皮或 FFD:请使用 Spine 导出保留这些效果`);
|
|
165
|
+
}
|
|
170
166
|
return {
|
|
171
167
|
name: slot.name,
|
|
172
168
|
display: [
|
|
173
169
|
{
|
|
174
|
-
type:
|
|
170
|
+
type: "image",
|
|
175
171
|
name: slot.attachment,
|
|
176
172
|
// 图集里的区域名就是部件名,`path` 与之一致才能取到贴图。
|
|
177
173
|
path: slot.attachment,
|
|
178
|
-
...(useMesh
|
|
179
|
-
? {
|
|
180
|
-
vertices: attachment.vertices,
|
|
181
|
-
uvs: attachment.uvs,
|
|
182
|
-
triangles: attachment.triangles,
|
|
183
|
-
width: round(num(attachment.width, 0), 4),
|
|
184
|
-
height: round(num(attachment.height, 0), 4)
|
|
185
|
-
}
|
|
186
|
-
: {}),
|
|
187
|
-
...(isPath
|
|
188
|
-
? {
|
|
189
|
-
vertices: attachment.vertices,
|
|
190
|
-
closed: attachment.closed === true
|
|
191
|
-
}
|
|
192
|
-
: {}),
|
|
193
174
|
pivot: { x: 0.5, y: 0.5 },
|
|
194
175
|
transform: {
|
|
195
176
|
x: round(num(attachment.x, 0), 4),
|
|
196
|
-
y: round(num(attachment.y, 0), 4),
|
|
177
|
+
y: round(-num(attachment.y, 0), 4),
|
|
197
178
|
skX: rotation,
|
|
198
179
|
skY: rotation,
|
|
199
180
|
scX: 1,
|
|
@@ -220,36 +201,12 @@ export function buildDragonBonesSkeleton(options) {
|
|
|
220
201
|
// 我们的 `chain` 字段是「链长」(两骨 = 1);Spine 那边写的是骨骼数组长度,
|
|
221
202
|
// 所以这里优先用显式字段,缺了才从数组长度反推。
|
|
222
203
|
chain: Math.max(1, num(constraint.chain, chain.length - 1)),
|
|
223
|
-
|
|
204
|
+
// y 反射同时反转两骨 IK 的弯曲方向。
|
|
205
|
+
bendPositive: constraint.bendPositive === false,
|
|
224
206
|
weight: Math.max(0, Math.min(1, num(constraint.mix, 1))),
|
|
225
207
|
scale: false
|
|
226
208
|
});
|
|
227
209
|
}
|
|
228
|
-
// ── Path 约束 ────────────────────────────────────────────────────────
|
|
229
|
-
//
|
|
230
|
-
// Spine 用的是字符串枚举,DragonBones 用数字:不转的话运行时会把
|
|
231
|
-
// `"percent"` 当成 0(fixed),于是「均匀铺满」悄悄变成「只铺开头一段」。
|
|
232
|
-
const PATH_POSITION_MODE = { fixed: 0, percent: 1 };
|
|
233
|
-
const PATH_SPACING_MODE = { length: 0, fixed: 1, percent: 2, proportional: 3 };
|
|
234
|
-
const PATH_ROTATE_MODE = { tangent: 0, chain: 1, chainScale: 2 };
|
|
235
|
-
const pathConstraints = [];
|
|
236
|
-
for (const constraint of Array.isArray(spine?.path) ? spine.path : []) {
|
|
237
|
-
const chain = Array.isArray(constraint?.bones) ? constraint.bones.filter((name) => typeof name === "string") : [];
|
|
238
|
-
if (chain.length === 0)
|
|
239
|
-
continue;
|
|
240
|
-
pathConstraints.push({
|
|
241
|
-
name: String(constraint.name ?? `path-${pathConstraints.length}`),
|
|
242
|
-
// DragonBones 用「链的**第一根**骨 + 目标骨骼」,与 Spine 的「整条数组」不同。
|
|
243
|
-
bone: chain[0],
|
|
244
|
-
target: String(constraint.target ?? "root"),
|
|
245
|
-
positionMode: PATH_POSITION_MODE[String(constraint.positionMode ?? "percent")] ?? 1,
|
|
246
|
-
spacingMode: PATH_SPACING_MODE[String(constraint.spacingMode ?? "length")] ?? 0,
|
|
247
|
-
rotateMode: PATH_ROTATE_MODE[String(constraint.rotateMode ?? "tangent")] ?? 0,
|
|
248
|
-
rotation: num(constraint.rotation, 0),
|
|
249
|
-
translateMix: Math.max(0, Math.min(1, num(constraint.translateMix, 1))),
|
|
250
|
-
rotateMix: Math.max(0, Math.min(1, num(constraint.rotateMix, 1)))
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
210
|
const animation = [];
|
|
254
211
|
for (const [id, value] of Object.entries(options.animations ?? {})) {
|
|
255
212
|
const timelines = value?.bones;
|
|
@@ -276,37 +233,12 @@ export function buildDragonBonesSkeleton(options) {
|
|
|
276
233
|
const explicit = num(value?.duration, 0);
|
|
277
234
|
const seconds = explicit > 0 ? explicit : durationSecondsOf(value, preset?.duration ?? 1);
|
|
278
235
|
const loop = typeof value?.loop === "boolean" ? value.loop : preset?.loop !== false;
|
|
279
|
-
// FFD 变形:Spine 的 `deform`(按秒)转成 DragonBones 的 `ffd`(按帧)。
|
|
280
|
-
//
|
|
281
|
-
// 顺序必须与骨架里 mesh 的 `vertices` 完全一致——两边都是同一份规则网格算出来的,
|
|
282
|
-
// 一旦这里改了顶点的排列或数量,位移就会对到别的顶点上,表现是「裙摆乱扭」而不是报错。
|
|
283
|
-
const ffd = [];
|
|
284
|
-
const spineDeform = spine?.animations?.[id]?.deform;
|
|
285
|
-
if (spineDeform !== null && typeof spineDeform === "object") {
|
|
286
|
-
for (const [slotName, bySkin] of Object.entries(spineDeform)) {
|
|
287
|
-
const frames = Array.isArray(bySkin?.default) ? bySkin.default : [];
|
|
288
|
-
if (frames.length === 0)
|
|
289
|
-
continue;
|
|
290
|
-
ffd.push({
|
|
291
|
-
name: slotName,
|
|
292
|
-
skin: "default",
|
|
293
|
-
slot: slotName,
|
|
294
|
-
frame: frames.map((frame, index) => ({
|
|
295
|
-
// 末帧的 duration 被解析器忽略(与骨骼时间轴同一条规则)。
|
|
296
|
-
duration: index === frames.length - 1 ? 0 : Math.max(1, Math.round(((frames[index + 1].time ?? 0) - (frame.time ?? 0)) * frameRate)),
|
|
297
|
-
offset: num(frame.offset, 0),
|
|
298
|
-
vertices: Array.isArray(frame.vertices) ? frame.vertices.map((n) => num(n, 0)) : []
|
|
299
|
-
}))
|
|
300
|
-
});
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
236
|
animation.push({
|
|
304
237
|
duration: Math.max(1, Math.round(seconds * frameRate)),
|
|
305
238
|
name: id,
|
|
306
239
|
// 0 = 无限循环。六个预设都是首尾闭合的(`validateAnimationLoops` 盯着)。
|
|
307
240
|
playTimes: loop ? 0 : 1,
|
|
308
|
-
bone: boneTimelines
|
|
309
|
-
...(ffd.length === 0 ? {} : { ffd })
|
|
241
|
+
bone: boneTimelines
|
|
310
242
|
});
|
|
311
243
|
}
|
|
312
244
|
const width = Math.round(options.canvasWidth);
|
|
@@ -316,18 +248,17 @@ export function buildDragonBonesSkeleton(options) {
|
|
|
316
248
|
name: options.name,
|
|
317
249
|
version: DRAGONBONES_VERSION,
|
|
318
250
|
compatibleVersion: DRAGONBONES_COMPATIBLE_VERSION,
|
|
251
|
+
...(exportWarnings.length === 0 ? {} : { exportWarnings }),
|
|
319
252
|
armature: [
|
|
320
253
|
{
|
|
321
254
|
type: "Armature",
|
|
322
255
|
frameRate,
|
|
323
256
|
name: options.name,
|
|
324
|
-
aabb: { x:
|
|
325
|
-
|
|
326
|
-
canvas: { x: -width / 2, y: 0, width, height },
|
|
257
|
+
aabb: { x: -width / 2, y: -height, width, height },
|
|
258
|
+
canvas: { x: -width / 2, y: -height, width, height },
|
|
327
259
|
bone: bones,
|
|
328
260
|
slot: slots,
|
|
329
261
|
...(ik.length === 0 ? {} : { ik }),
|
|
330
|
-
...(pathConstraints.length === 0 ? {} : { path: pathConstraints }),
|
|
331
262
|
skin: [{ name: "default", slot: skinSlots }],
|
|
332
263
|
animation,
|
|
333
264
|
defaultActions: []
|
package/lib/riggen.js
CHANGED
|
@@ -261,7 +261,7 @@ function normalizeRigJob(raw) {
|
|
|
261
261
|
error: typeof part.error === "string" ? part.error : undefined,
|
|
262
262
|
updatedAt: Number.isFinite(part.updatedAt) ? part.updatedAt : undefined,
|
|
263
263
|
role: typeof part.role === "string" && part.role !== "" ? part.role : undefined,
|
|
264
|
-
parent: typeof part.parent === "string" && part.parent !== "" ? part.parent : undefined,
|
|
264
|
+
parent: part.parent === null ? null : typeof part.parent === "string" && part.parent !== "" ? part.parent : undefined,
|
|
265
265
|
proximal: Array.isArray(part.proximal) && part.proximal.length === 2 ? [num(part.proximal[0], 0.5), num(part.proximal[1], 0)] : undefined,
|
|
266
266
|
distal: Array.isArray(part.distal) && part.distal.length === 2 ? [num(part.distal[0], 0.5), num(part.distal[1], 1)] : undefined,
|
|
267
267
|
tags: Array.isArray(part.tags) ? part.tags.filter((tag) => typeof tag === "string") : undefined,
|
|
@@ -1950,12 +1950,14 @@ export async function setRigConstraints(jobId, patches, options = {}) {
|
|
|
1950
1950
|
name: patch.name,
|
|
1951
1951
|
bone: patch.bone ?? (index >= 0 ? current[index].bone : ""),
|
|
1952
1952
|
target: patch.target ?? (index >= 0 ? current[index].target : `${patch.name}-target`),
|
|
1953
|
-
chain:
|
|
1953
|
+
chain: num(patch.chain, index >= 0 ? current[index].chain : 1),
|
|
1954
1954
|
bendPositive: patch.bendPositive ?? (index >= 0 ? current[index].bendPositive : true),
|
|
1955
1955
|
weight: Math.max(0, Math.min(1, num(patch.weight, index >= 0 ? current[index].weight : 1)))
|
|
1956
1956
|
};
|
|
1957
1957
|
if (merged.bone === "")
|
|
1958
1958
|
throw new Error(`IK「${merged.name}」缺少被约束的骨骼`);
|
|
1959
|
+
if (merged.chain !== 1)
|
|
1960
|
+
throw new Error(`IK「${merged.name}」目前只支持两根骨骼(chain = 1)`);
|
|
1959
1961
|
if (index >= 0)
|
|
1960
1962
|
current[index] = merged;
|
|
1961
1963
|
else
|
|
@@ -3307,8 +3309,8 @@ export async function buildRigOutput(jobId) {
|
|
|
3307
3309
|
scale: item.scale,
|
|
3308
3310
|
rotation: item.rotation,
|
|
3309
3311
|
z: item.z,
|
|
3310
|
-
//
|
|
3311
|
-
parent: node?.parent,
|
|
3312
|
+
// 语义已补齐:没有语义父级也属于已确定的 root,不再按名字兜底。
|
|
3313
|
+
parent: node?.parent ?? null,
|
|
3312
3314
|
proximal: node?.proximal,
|
|
3313
3315
|
distal: node?.distal,
|
|
3314
3316
|
// 三通道的中间那一层:人的手工偏置,任何自动重跑都不碰它。
|
package/lib/rigpath.js
CHANGED
|
@@ -5,9 +5,8 @@
|
|
|
5
5
|
* DragonBones / 我们的预览器)都能表达的最小共同集:路径是一串折线点,
|
|
6
6
|
* 骨骼沿弧长均匀(或按固定间距)铺上去,每根骨取所在位置的切线。
|
|
7
7
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* 少一层「曲线 → 折线」的采样,也就少一个「预览和导出不一样」的可能。
|
|
8
|
+
* 编辑数据保留折线;Spine 的 PathAttachment 实际存三次贝塞尔控制点,
|
|
9
|
+
* 导出时把每条直线段编码为两个共线控制点,形状与弧长不变。
|
|
11
10
|
*
|
|
12
11
|
* 坐标系与部件一致:**参考图像素、Y 向下**。转成骨骼世界坐标由调用方负责
|
|
13
12
|
* (`toWorld` 那一步),这里只做纯几何。
|
|
@@ -20,7 +19,7 @@ export function normalizePath(raw) {
|
|
|
20
19
|
const source = Array.isArray(value.points) ? value.points : [];
|
|
21
20
|
// 扁平数组 `[x0,y0,x1,y1,…]`:**内部表示就是这个形状**(`RigPathEntry.points` 是扁平的),
|
|
22
21
|
// wire 上传过来时也是最省事的一种。另外两种写法是给人手写 JSON 用的。
|
|
23
|
-
if (source.length >= 4 && source.every((entry) => typeof entry === "number" && Number.isFinite(entry))) {
|
|
22
|
+
if (source.length >= 4 && source.length % 2 === 0 && source.every((entry) => typeof entry === "number" && Number.isFinite(entry))) {
|
|
24
23
|
return { points: source.map((n) => Number(n.toFixed(3))), closed: value.closed === true };
|
|
25
24
|
}
|
|
26
25
|
const flat = [];
|
|
@@ -42,7 +41,7 @@ export function pathSegmentCount(path) {
|
|
|
42
41
|
const count = path.points.length / 2;
|
|
43
42
|
return path.closed === true ? count : count - 1;
|
|
44
43
|
}
|
|
45
|
-
/**
|
|
44
|
+
/** 每段的长度;Spine 导出另行编成累计弧长表。 */
|
|
46
45
|
export function pathSegmentLengths(path) {
|
|
47
46
|
const count = path.points.length / 2;
|
|
48
47
|
const segments = [];
|
|
@@ -59,21 +58,59 @@ export function pathTotalLength(path, lengths) {
|
|
|
59
58
|
const segments = lengths ?? pathSegmentLengths(path);
|
|
60
59
|
return Number(segments.reduce((sum, value) => sum + value, 0).toFixed(4));
|
|
61
60
|
}
|
|
61
|
+
/** 折线 → Spine 三次曲线顶点。输入已是槽位局部坐标(Y 向上)。 */
|
|
62
|
+
export function spinePathGeometry(path) {
|
|
63
|
+
const points = [];
|
|
64
|
+
for (let i = 0; i + 1 < path.points.length; i += 2) {
|
|
65
|
+
if (points.length > 0 && Math.hypot(path.points[i] - points[points.length - 2], path.points[i + 1] - points[points.length - 1]) <= 1e-6)
|
|
66
|
+
continue;
|
|
67
|
+
points.push(path.points[i], path.points[i + 1]);
|
|
68
|
+
}
|
|
69
|
+
if (path.closed && points.length > 4 && Math.hypot(points[0] - points[points.length - 2], points[1] - points[points.length - 1]) <= 1e-6)
|
|
70
|
+
points.splice(-2);
|
|
71
|
+
const count = points.length / 2;
|
|
72
|
+
const segments = path.closed ? count : count - 1;
|
|
73
|
+
// 每段 [起点, 1/3 控制点, 2/3 控制点, 终点],相邻段共用端点。
|
|
74
|
+
const curve = [points[0], points[1]];
|
|
75
|
+
const lengths = [];
|
|
76
|
+
let total = 0;
|
|
77
|
+
for (let i = 0; i < segments; i++) {
|
|
78
|
+
const j = (i + 1) % count;
|
|
79
|
+
const x = points[i * 2], y = points[i * 2 + 1];
|
|
80
|
+
const dx = points[j * 2] - x, dy = points[j * 2 + 1] - y;
|
|
81
|
+
curve.push(x + dx / 3, y + dy / 3, x + dx * 2 / 3, y + dy * 2 / 3, points[j * 2], points[j * 2 + 1]);
|
|
82
|
+
total += Math.hypot(dx, dy);
|
|
83
|
+
lengths.push(Number(total.toFixed(4)));
|
|
84
|
+
}
|
|
85
|
+
// 开放路径的首尾额外控制点不参与曲线;闭合路径把末段第二控制点放在首位。
|
|
86
|
+
const vertices = path.closed
|
|
87
|
+
? [...curve.slice(-4, -2), ...curve.slice(0, -4)]
|
|
88
|
+
: [points[0], points[1], ...curve, points[points.length - 2], points[points.length - 1]];
|
|
89
|
+
if (!path.closed)
|
|
90
|
+
lengths.push(Number(total.toFixed(4)));
|
|
91
|
+
return { vertices: vertices.map((n) => Number(n.toFixed(6))), vertexCount: vertices.length / 2, lengths };
|
|
92
|
+
}
|
|
62
93
|
/**
|
|
63
94
|
* 按**弧长**采样:给出沿路径走 `distance` 之后的位置与切线角(弧度)。
|
|
64
95
|
*
|
|
65
|
-
* 用弧长而不是"按段索引"
|
|
66
|
-
*
|
|
67
|
-
* 调用方(排列骨链)本来就会算出越界值,那时"停在端点"才是想要的行为。
|
|
96
|
+
* 用弧长而不是"按段索引"让骨骼等距。与 Spine 一致:闭合路径按总长循环,
|
|
97
|
+
* 开放路径越界时沿首/末段切线外推,固定间距不会在端点挤成一团。
|
|
68
98
|
*/
|
|
69
99
|
export function samplePath(path, distance, lengths, total) {
|
|
70
100
|
const segments = lengths ?? pathSegmentLengths(path);
|
|
71
101
|
const sum = total ?? segments.reduce((acc, value) => acc + value, 0);
|
|
72
102
|
const count = path.points.length / 2;
|
|
73
|
-
|
|
103
|
+
if (sum <= 1e-6)
|
|
104
|
+
return { x: path.points[0], y: path.points[1], angle: 0 };
|
|
105
|
+
let remaining = path.closed ? ((distance % sum) + sum) % sum : distance;
|
|
106
|
+
let lastSegment = segments.length - 1;
|
|
107
|
+
while (lastSegment >= 0 && segments[lastSegment] <= 1e-6)
|
|
108
|
+
lastSegment--;
|
|
74
109
|
for (let i = 0; i < segments.length; i++) {
|
|
75
110
|
const length = segments[i];
|
|
76
|
-
if (
|
|
111
|
+
if (length <= 1e-6)
|
|
112
|
+
continue;
|
|
113
|
+
if (remaining <= length || i === lastSegment) {
|
|
77
114
|
const t = length > 1e-9 ? remaining / length : 0;
|
|
78
115
|
const j = (i + 1) % count;
|
|
79
116
|
const x0 = path.points[i * 2];
|