dsh-game-material-master 0.1.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/LICENSE +21 -0
- package/README.md +104 -0
- package/cordis.patch.yml +6 -0
- package/lib/ark.js +128 -0
- package/lib/chroma.js +466 -0
- package/lib/client.js +5106 -0
- package/lib/config.js +252 -0
- package/lib/directions.js +141 -0
- package/lib/imagegen.js +388 -0
- package/lib/index.js +1602 -0
- package/lib/jsonio.js +38 -0
- package/lib/links.js +125 -0
- package/lib/media.js +235 -0
- package/lib/minimax.js +390 -0
- package/lib/pipeline.js +650 -0
- package/lib/png.js +57 -0
- package/lib/rigexport.js +391 -0
- package/lib/riggen.js +3639 -0
- package/lib/rigmesh.js +167 -0
- package/lib/rigpath.js +125 -0
- package/lib/rigpose.js +1872 -0
- package/lib/rigpreview.js +816 -0
- package/lib/rigqa.js +271 -0
- package/lib/rigsemantics.js +411 -0
- package/lib/rigvalidate.js +802 -0
- package/lib/seqgen.js +686 -0
- package/lib/spine.js +1392 -0
- package/lib/store.js +309 -0
- package/lib/tools.js +1365 -0
- package/lib/wire.js +535 -0
- package/package.json +49 -0
package/lib/png.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 极简 PNG 编码器(RGBA8)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么不引第三方库:整个流程只需要「把 RGBA 缓冲区写成 PNG」这一件事,
|
|
5
|
+
* 用 node:zlib 的 deflate 手写 ~70 行即可,比拉一个图像库依赖靠谱得多。
|
|
6
|
+
* 解码方向完全不需要——所有解码都交给 ffmpeg(视频 → rawvideo rgba)。
|
|
7
|
+
*/
|
|
8
|
+
import { deflateSync } from "node:zlib";
|
|
9
|
+
const CRC_TABLE = (() => {
|
|
10
|
+
const table = new Int32Array(256);
|
|
11
|
+
for (let n = 0; n < 256; n++) {
|
|
12
|
+
let c = n;
|
|
13
|
+
for (let k = 0; k < 8; k++)
|
|
14
|
+
c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
|
|
15
|
+
table[n] = c;
|
|
16
|
+
}
|
|
17
|
+
return table;
|
|
18
|
+
})();
|
|
19
|
+
function crc32(buffer) {
|
|
20
|
+
let c = 0xffffffff;
|
|
21
|
+
for (let i = 0; i < buffer.length; i++)
|
|
22
|
+
c = CRC_TABLE[(c ^ buffer[i]) & 0xff] ^ (c >>> 8);
|
|
23
|
+
return (c ^ 0xffffffff) >>> 0;
|
|
24
|
+
}
|
|
25
|
+
function chunk(type, data) {
|
|
26
|
+
const length = Buffer.alloc(4);
|
|
27
|
+
length.writeUInt32BE(data.length, 0);
|
|
28
|
+
const typeBytes = Buffer.from(type, "ascii");
|
|
29
|
+
const crc = Buffer.alloc(4);
|
|
30
|
+
crc.writeUInt32BE(crc32(Buffer.concat([typeBytes, data])), 0);
|
|
31
|
+
return Buffer.concat([length, typeBytes, data, crc]);
|
|
32
|
+
}
|
|
33
|
+
/** 把 `width * height * 4` 字节的 RGBA 缓冲编码成 PNG。 */
|
|
34
|
+
export function encodePng(rgba, width, height) {
|
|
35
|
+
const expected = width * height * 4;
|
|
36
|
+
if (rgba.length < expected) {
|
|
37
|
+
throw new Error(`RGBA 缓冲区太小:需要 ${expected} 字节,实际 ${rgba.length} 字节`);
|
|
38
|
+
}
|
|
39
|
+
const signature = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
|
40
|
+
const ihdr = Buffer.alloc(13);
|
|
41
|
+
ihdr.writeUInt32BE(width, 0);
|
|
42
|
+
ihdr.writeUInt32BE(height, 4);
|
|
43
|
+
ihdr[8] = 8; // bit depth
|
|
44
|
+
ihdr[9] = 6; // color type: truecolour with alpha
|
|
45
|
+
ihdr[10] = 0; // deflate
|
|
46
|
+
ihdr[11] = 0; // adaptive filtering
|
|
47
|
+
ihdr[12] = 0; // no interlace
|
|
48
|
+
const stride = width * 4;
|
|
49
|
+
const filtered = Buffer.alloc((stride + 1) * height);
|
|
50
|
+
for (let y = 0; y < height; y++) {
|
|
51
|
+
const rowStart = y * (stride + 1);
|
|
52
|
+
filtered[rowStart] = 0; // filter type 0 (None)
|
|
53
|
+
rgba.copy(filtered, rowStart + 1, y * stride, y * stride + stride);
|
|
54
|
+
}
|
|
55
|
+
const idat = deflateSync(filtered, { level: 6 });
|
|
56
|
+
return Buffer.concat([signature, chunk("IHDR", ihdr), chunk("IDAT", idat), chunk("IEND", Buffer.alloc(0))]);
|
|
57
|
+
}
|
package/lib/rigexport.js
ADDED
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 导出器:DragonBones 5.5(`_ske.json` + `_tex.json`)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么要有第二条导出路径:Spine 只覆盖「Spine 运行时」这一半生态,
|
|
5
|
+
* 而 2D 骨骼在国内更多项目用的是 DragonBones(Cocos / Egret / Laya 都自带)。
|
|
6
|
+
* 同一个 `RigDocument` 导出两种格式,成本主要在**两边的语义差分**上——
|
|
7
|
+
* 下面三条是照着 DragonBonesJS 的解析器逐行核对出来的,写错了都是静默坏掉:
|
|
8
|
+
*
|
|
9
|
+
* 1. **`tweenEasing` 缺省 = 阶跃**(`ObjectDataParser._parseTweenFrame`:
|
|
10
|
+
* 缺省 `-2` = `TweenType.None`)。所以每个补间帧都必须**显式**写 `tweenEasing: 0`
|
|
11
|
+
* (或真实缓动值),否则动画会一跳一跳。
|
|
12
|
+
* 2. **`curve` 是 0~1 归一化**的,而 Spine 4.2 是**绝对量**(时间 / 数值同单位)。
|
|
13
|
+
* 同一份简写动画的两条导出路径必须用**不同的**编码器——这条是 §7.3 里点名
|
|
14
|
+
* 最容易搞错的一处。紧凑式要求长度 `% 3 === 1`(如 4:首锚 `(0,0)`、末锚 `(1,1)` 省略)。
|
|
15
|
+
* 3. **`duration` 单位不同**:Spine 是秒,DragonBones 是**帧数**。
|
|
16
|
+
*
|
|
17
|
+
* 另外两条不在方案里、但决定「画出来对不对」的差分,也在这里定死:
|
|
18
|
+
*
|
|
19
|
+
* - **图片挂点的位置信息不在 attachment 上,而在 `pivot` + `frame*` 上**。
|
|
20
|
+
* 渲染时 `_pivotX = pivot.x * frameWidth + frameX`,图片左上角画在 `-pivot`
|
|
21
|
+
* 处(`dragonBones/armature/Slot.ts:409-461`)。所以「图片中心落在骨骼原点」
|
|
22
|
+
* 这件事要靠 `pivot = 0.5/0.5` + `frameX = 裁剪偏移` 一起凑出来,裁剪过的部件
|
|
23
|
+
* 少给 `frame*` 就会整体偏掉。
|
|
24
|
+
* - **坐标系是 y 向下**(`DragonBones.yDown` 默认 `true`),与我们的图像坐标一致,
|
|
25
|
+
* 旋转角不需要取反。
|
|
26
|
+
*/
|
|
27
|
+
import { animationPresetOf } from "./spine.js";
|
|
28
|
+
export const DRAGONBONES_VERSION = "5.5";
|
|
29
|
+
export const DRAGONBONES_COMPATIBLE_VERSION = "5.5";
|
|
30
|
+
/**
|
|
31
|
+
* 与 `spine.ts` 的动画预设时长单位(秒)换算用。
|
|
32
|
+
*
|
|
33
|
+
* 方案建议固定 30:它是 2D 骨骼的常用帧率,且我们的关键帧时间都是 0.05s 的整数倍,
|
|
34
|
+
* 乘 30 之后基本上都落在整数帧上,不会因为取整把循环时长改掉。
|
|
35
|
+
*/
|
|
36
|
+
export const DRAGONBONES_FRAME_RATE = 30;
|
|
37
|
+
/** `-2` = `TweenType.None`(阶跃)。 */
|
|
38
|
+
export const TWEEN_STEPPED = -2;
|
|
39
|
+
/** `0` = `TweenType.Line`(线性)。 */
|
|
40
|
+
export const TWEEN_LINEAR = 0;
|
|
41
|
+
function round(value, digits) {
|
|
42
|
+
const factor = 10 ** digits;
|
|
43
|
+
return Math.round(value * factor) / factor;
|
|
44
|
+
}
|
|
45
|
+
function num(value, fallback) {
|
|
46
|
+
return typeof value === "number" && Number.isFinite(value) ? value : fallback;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* 把简写的归一化控制点编成 DragonBones 的 `curve`。
|
|
50
|
+
*
|
|
51
|
+
* 简写里一条 curve 就是 4 个数 `[cx1, cy1, cx2, cy2]`(首尾锚点隐含为
|
|
52
|
+
* `(0,0)` 与 `(1,1)`),长度 `4 % 3 === 1` —— 正好是解析器的**紧凑式**,
|
|
53
|
+
* 直接原样写即可。**不能**照搬 Spine 那条路:那边会先乘上时间跨度与数值差,
|
|
54
|
+
* 写出去就是绝对量,DragonBones 按 0~1 解释会得到完全不同的曲线。
|
|
55
|
+
*/
|
|
56
|
+
export function encodeDragonBonesCurve(control) {
|
|
57
|
+
if (control.length < 4)
|
|
58
|
+
return undefined;
|
|
59
|
+
if (control.length % 3 !== 1)
|
|
60
|
+
return undefined;
|
|
61
|
+
return control.map((value) => round(value, 4));
|
|
62
|
+
}
|
|
63
|
+
/** 简写时间轴类型 → DragonBones 的帧数组字段名与取值方式。 */
|
|
64
|
+
const TIMELINE_KINDS = {
|
|
65
|
+
rotate: {
|
|
66
|
+
frameKey: "rotateFrame",
|
|
67
|
+
valueOf: (frame) => ({ rotate: round(num(frame.angle, 0), 4) })
|
|
68
|
+
},
|
|
69
|
+
translate: {
|
|
70
|
+
frameKey: "translateFrame",
|
|
71
|
+
valueOf: (frame) => ({ x: round(num(frame.x, 0), 4), y: round(num(frame.y, 0), 4) })
|
|
72
|
+
},
|
|
73
|
+
scale: {
|
|
74
|
+
frameKey: "scaleFrame",
|
|
75
|
+
valueOf: (frame) => ({ x: round(num(frame.x, 1), 4), y: round(num(frame.y, 1), 4) })
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* 一条简写时间轴 → DragonBones 帧数组。
|
|
80
|
+
*
|
|
81
|
+
* 两处必须小心:
|
|
82
|
+
* - **缓动挂在段起点上**。简写把 curve 挂在**段终点**那一帧(见 `spine.ts` 的
|
|
83
|
+
* `convertTimeline` 注释),而 DragonBones 的 `tweenEasing` / `curve` 描述的是
|
|
84
|
+
* **从本帧开始**的那一段,所以要整体前移一帧取。
|
|
85
|
+
* - **最后一帧的 `duration` 被解析器忽略**(`_parseTimeline` 用
|
|
86
|
+
* `frameCount - frameStart` 兜底),而**中间帧 `duration: 0` 会被当成阶跃**
|
|
87
|
+
* (`frameCount > 0` 才走补间分支)。所以中间帧至少要 1 帧,校验器盯着这条。
|
|
88
|
+
*/
|
|
89
|
+
export function buildDragonBonesTimeline(kind, frames, frameRate) {
|
|
90
|
+
const spec = TIMELINE_KINDS[kind];
|
|
91
|
+
if (spec === undefined)
|
|
92
|
+
return [];
|
|
93
|
+
const positions = frames.map((frame) => Math.round(num(frame.time, 0) * frameRate));
|
|
94
|
+
const out = [];
|
|
95
|
+
for (let i = 0; i < frames.length; i++) {
|
|
96
|
+
const isLast = i === frames.length - 1;
|
|
97
|
+
const duration = isLast ? 0 : positions[i + 1] - positions[i];
|
|
98
|
+
const entry = { duration, ...spec.valueOf(frames[i]) };
|
|
99
|
+
if (!isLast) {
|
|
100
|
+
const authored = frames[i + 1]?.curve;
|
|
101
|
+
const curve = Array.isArray(authored) ? encodeDragonBonesCurve(authored) : undefined;
|
|
102
|
+
if (curve !== undefined) {
|
|
103
|
+
entry.curve = curve;
|
|
104
|
+
}
|
|
105
|
+
else if (authored === "stepped") {
|
|
106
|
+
entry.tweenEasing = TWEEN_STEPPED;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
// 缺省是阶跃,线性必须显式写出来。
|
|
110
|
+
entry.tweenEasing = TWEEN_LINEAR;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
out.push(entry);
|
|
114
|
+
}
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* 生成 DragonBones 的 `_ske.json`。
|
|
119
|
+
*
|
|
120
|
+
* 骨骼与挂点直接由 Spine 那份派生:两者在「骨骼层级 + 局部平移/旋转」上是同构的,
|
|
121
|
+
* 差分只在字段名与坐标落点(见文件头注释)。图片一律走 `pivot = 0.5/0.5` +
|
|
122
|
+
* `transform.x/y = attachment 的挂点位置`,这样「图片中心落在挂点上」的语义两边一致。
|
|
123
|
+
*/
|
|
124
|
+
export function buildDragonBonesSkeleton(options) {
|
|
125
|
+
const frameRate = options.frameRate ?? DRAGONBONES_FRAME_RATE;
|
|
126
|
+
const spine = options.spine;
|
|
127
|
+
const bones = (spine?.bones ?? []).map((bone) => {
|
|
128
|
+
const rotation = round(num(bone.rotation, 0), 4);
|
|
129
|
+
return {
|
|
130
|
+
name: bone.name,
|
|
131
|
+
...(bone.parent === undefined ? {} : { parent: bone.parent }),
|
|
132
|
+
length: round(num(bone.length, 0), 4),
|
|
133
|
+
transform: {
|
|
134
|
+
x: round(num(bone.x, 0), 4),
|
|
135
|
+
y: round(num(bone.y, 0), 4),
|
|
136
|
+
// `skX === skY` 时 `skew === 0`;故意不引入双角度,见方案 §7.3。
|
|
137
|
+
skX: rotation,
|
|
138
|
+
skY: rotation,
|
|
139
|
+
scX: 1,
|
|
140
|
+
scY: 1
|
|
141
|
+
},
|
|
142
|
+
inheritTranslation: true,
|
|
143
|
+
inheritRotation: true,
|
|
144
|
+
inheritScale: true,
|
|
145
|
+
inheritReflection: true
|
|
146
|
+
};
|
|
147
|
+
});
|
|
148
|
+
const slots = (spine?.slots ?? []).map((slot) => ({
|
|
149
|
+
name: slot.name,
|
|
150
|
+
parent: slot.bone,
|
|
151
|
+
displayIndex: 0
|
|
152
|
+
}));
|
|
153
|
+
const attachments = spine?.skins?.[0]?.attachments ?? {};
|
|
154
|
+
const skinSlots = (spine?.slots ?? []).map((slot) => {
|
|
155
|
+
const attachment = attachments[slot.name]?.[slot.attachment] ?? {};
|
|
156
|
+
const rotation = round(num(attachment.rotation, 0), 4);
|
|
157
|
+
// 有网格的部件出 `mesh`,其余出 `image`。
|
|
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";
|
|
170
|
+
return {
|
|
171
|
+
name: slot.name,
|
|
172
|
+
display: [
|
|
173
|
+
{
|
|
174
|
+
type: useMesh ? "mesh" : isPath ? "path" : "image",
|
|
175
|
+
name: slot.attachment,
|
|
176
|
+
// 图集里的区域名就是部件名,`path` 与之一致才能取到贴图。
|
|
177
|
+
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
|
+
pivot: { x: 0.5, y: 0.5 },
|
|
194
|
+
transform: {
|
|
195
|
+
x: round(num(attachment.x, 0), 4),
|
|
196
|
+
y: round(num(attachment.y, 0), 4),
|
|
197
|
+
skX: rotation,
|
|
198
|
+
skY: rotation,
|
|
199
|
+
scX: 1,
|
|
200
|
+
scY: 1
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
]
|
|
204
|
+
};
|
|
205
|
+
});
|
|
206
|
+
// ── IK 约束 ──────────────────────────────────────────────────────────
|
|
207
|
+
//
|
|
208
|
+
// Spine 的写法是 `bones: [从根到末端的骨骼名]`;DragonBones 要的是
|
|
209
|
+
// `bone`(链末端)+ `chain`(再往上几根)。两者是同一件事的两种编码,
|
|
210
|
+
// 转换时**末尾那根就是末端**,`chain` 要减一(不含末端自己)。
|
|
211
|
+
const ik = [];
|
|
212
|
+
for (const constraint of Array.isArray(spine?.ik) ? spine.ik : []) {
|
|
213
|
+
const chain = Array.isArray(constraint?.bones) ? constraint.bones.filter((name) => typeof name === "string") : [];
|
|
214
|
+
if (chain.length === 0)
|
|
215
|
+
continue;
|
|
216
|
+
ik.push({
|
|
217
|
+
name: String(constraint.name ?? `ik-${ik.length}`),
|
|
218
|
+
bone: chain[chain.length - 1],
|
|
219
|
+
target: String(constraint.target ?? ""),
|
|
220
|
+
// 我们的 `chain` 字段是「链长」(两骨 = 1);Spine 那边写的是骨骼数组长度,
|
|
221
|
+
// 所以这里优先用显式字段,缺了才从数组长度反推。
|
|
222
|
+
chain: Math.max(1, num(constraint.chain, chain.length - 1)),
|
|
223
|
+
bendPositive: constraint.bendPositive !== false,
|
|
224
|
+
weight: Math.max(0, Math.min(1, num(constraint.mix, 1))),
|
|
225
|
+
scale: false
|
|
226
|
+
});
|
|
227
|
+
}
|
|
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
|
+
const animation = [];
|
|
254
|
+
for (const [id, value] of Object.entries(options.animations ?? {})) {
|
|
255
|
+
const timelines = value?.bones;
|
|
256
|
+
if (timelines === null || typeof timelines !== "object")
|
|
257
|
+
continue;
|
|
258
|
+
const boneTimelines = [];
|
|
259
|
+
for (const [boneName, kinds] of Object.entries(timelines)) {
|
|
260
|
+
const entry = { name: boneName };
|
|
261
|
+
let any = false;
|
|
262
|
+
for (const [kind, frames] of Object.entries(kinds)) {
|
|
263
|
+
const spec = TIMELINE_KINDS[kind];
|
|
264
|
+
if (spec === undefined || !Array.isArray(frames) || frames.length === 0)
|
|
265
|
+
continue;
|
|
266
|
+
entry[spec.frameKey] = buildDragonBonesTimeline(kind, frames, frameRate);
|
|
267
|
+
any = true;
|
|
268
|
+
}
|
|
269
|
+
if (any)
|
|
270
|
+
boneTimelines.push(entry);
|
|
271
|
+
}
|
|
272
|
+
if (boneTimelines.length === 0)
|
|
273
|
+
continue;
|
|
274
|
+
const preset = animationPresetOf(id);
|
|
275
|
+
// 手工动画自带 duration / loop;预设实例只有 bones,回落到预设时长。
|
|
276
|
+
const explicit = num(value?.duration, 0);
|
|
277
|
+
const seconds = explicit > 0 ? explicit : durationSecondsOf(value, preset?.duration ?? 1);
|
|
278
|
+
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
|
+
animation.push({
|
|
304
|
+
duration: Math.max(1, Math.round(seconds * frameRate)),
|
|
305
|
+
name: id,
|
|
306
|
+
// 0 = 无限循环。六个预设都是首尾闭合的(`validateAnimationLoops` 盯着)。
|
|
307
|
+
playTimes: loop ? 0 : 1,
|
|
308
|
+
bone: boneTimelines,
|
|
309
|
+
...(ffd.length === 0 ? {} : { ffd })
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
const width = Math.round(options.canvasWidth);
|
|
313
|
+
const height = Math.round(options.canvasHeight);
|
|
314
|
+
return {
|
|
315
|
+
frameRate,
|
|
316
|
+
name: options.name,
|
|
317
|
+
version: DRAGONBONES_VERSION,
|
|
318
|
+
compatibleVersion: DRAGONBONES_COMPATIBLE_VERSION,
|
|
319
|
+
armature: [
|
|
320
|
+
{
|
|
321
|
+
type: "Armature",
|
|
322
|
+
frameRate,
|
|
323
|
+
name: options.name,
|
|
324
|
+
aabb: { x: 0, y: 0, width, height },
|
|
325
|
+
// 画布原点按 Spine 那边的习惯放在水平中线,y 从 0 起(y 向下)。
|
|
326
|
+
canvas: { x: -width / 2, y: 0, width, height },
|
|
327
|
+
bone: bones,
|
|
328
|
+
slot: slots,
|
|
329
|
+
...(ik.length === 0 ? {} : { ik }),
|
|
330
|
+
...(pathConstraints.length === 0 ? {} : { path: pathConstraints }),
|
|
331
|
+
skin: [{ name: "default", slot: skinSlots }],
|
|
332
|
+
animation,
|
|
333
|
+
defaultActions: []
|
|
334
|
+
}
|
|
335
|
+
]
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
/** 动画时长:优先取时间轴里最晚的一帧,取不到才回落到预设时长。 */
|
|
339
|
+
function durationSecondsOf(timelines, fallback) {
|
|
340
|
+
let max = 0;
|
|
341
|
+
for (const kinds of Object.values(timelines?.bones ?? {})) {
|
|
342
|
+
for (const frames of Object.values(kinds)) {
|
|
343
|
+
if (!Array.isArray(frames))
|
|
344
|
+
continue;
|
|
345
|
+
for (const frame of frames) {
|
|
346
|
+
const time = num(frame?.time, 0);
|
|
347
|
+
if (time > max)
|
|
348
|
+
max = time;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return max > 0 ? max : fallback;
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* 一页图集 → 一个 `_tex.json`。
|
|
356
|
+
*
|
|
357
|
+
* `frameX/frameY/frameWidth/frameHeight` 是**裁剪信息**,必须一起给:
|
|
358
|
+
* 运行时用 `pivot.x * frameWidth + frameX` 算挂点(`Slot.ts:427-433`),
|
|
359
|
+
* 少了 `frame*` 就会按「没裁过」算,裁掉透明边的部件会整体偏掉。
|
|
360
|
+
* 我们图集的 `offsetX = -bounds.x`(裁剪框左上角取负),与 DragonBones
|
|
361
|
+
* 的 `frameX` 约定相同,直接照搬。
|
|
362
|
+
*/
|
|
363
|
+
export function buildDragonBonesTexture(page, options) {
|
|
364
|
+
return {
|
|
365
|
+
name: options.name,
|
|
366
|
+
imagePath: options.imagePath,
|
|
367
|
+
width: page.width,
|
|
368
|
+
height: page.height,
|
|
369
|
+
// 1 = 不额外缩放(导出物与图集同尺寸)。
|
|
370
|
+
scale: 1,
|
|
371
|
+
SubTexture: page.placements.map((item) => {
|
|
372
|
+
const entry = {
|
|
373
|
+
name: item.name,
|
|
374
|
+
x: item.x,
|
|
375
|
+
y: item.y,
|
|
376
|
+
width: item.width,
|
|
377
|
+
height: item.height,
|
|
378
|
+
rotated: false
|
|
379
|
+
};
|
|
380
|
+
const origWidth = item.origWidth ?? item.width;
|
|
381
|
+
const origHeight = item.origHeight ?? item.height;
|
|
382
|
+
if (origWidth !== item.width || origHeight !== item.height) {
|
|
383
|
+
entry.frameX = item.offsetX ?? 0;
|
|
384
|
+
entry.frameY = item.offsetY ?? 0;
|
|
385
|
+
entry.frameWidth = origWidth;
|
|
386
|
+
entry.frameHeight = origHeight;
|
|
387
|
+
}
|
|
388
|
+
return entry;
|
|
389
|
+
})
|
|
390
|
+
};
|
|
391
|
+
}
|