dsh-game-material-master 0.3.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 +74 -9
- package/README.md +60 -8
- package/lib/{tilemedia.js → bitmap.js} +21 -3
- package/lib/client.js +2929 -162
- package/lib/config.js +20 -1
- package/lib/index.js +239 -3
- 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 +401 -24
- package/lib/wire.js +179 -3
- package/lib/zip.js +127 -0
- package/package.json +10 -4
package/lib/rigpreview.js
CHANGED
|
@@ -27,8 +27,6 @@ export function buildPreviewHtml(options) {
|
|
|
27
27
|
? options.defaultAnimation
|
|
28
28
|
: animations[0] ?? "";
|
|
29
29
|
const background = options.background ?? "#12121c";
|
|
30
|
-
/** 没有变形的部件不必内联任何东西——预览里靠 DEFORMS[name] 是否存在来分流。 */
|
|
31
|
-
const deforms = options.deforms ?? {};
|
|
32
30
|
const paths = options.paths ?? {};
|
|
33
31
|
return `<!DOCTYPE html>
|
|
34
32
|
<html lang="zh-CN">
|
|
@@ -75,8 +73,6 @@ export function buildPreviewHtml(options) {
|
|
|
75
73
|
var SPINE = ${safeJson(options.spine)};
|
|
76
74
|
var IMAGE_DATA = ${safeJson(images)};
|
|
77
75
|
var ANIMS = ${safeJson(animations)};
|
|
78
|
-
// FFD 变形参数(部件名 → 波形)。没有它就不会走 mesh 渲染那条路。
|
|
79
|
-
var DEFORMS = ${safeJson(deforms)};
|
|
80
76
|
// Path 约束(名字 → 路径 + 骨链)。点已经是骨骼世界坐标。
|
|
81
77
|
var PATHS = ${safeJson(paths)};
|
|
82
78
|
var current = ${safeJson(defaultAnimation)};
|
|
@@ -148,7 +144,7 @@ export function buildPreviewHtml(options) {
|
|
|
148
144
|
if (!child || !parent || !target) continue;
|
|
149
145
|
// **第一段长度与方向都按「子骨原点在父骨局部系里的实际位置」算**,不用 parent.length。
|
|
150
146
|
//
|
|
151
|
-
// IK 的经典推导假设链上骨骼首尾相接(子骨原点落在父骨的局部 +
|
|
147
|
+
// IK 的经典推导假设链上骨骼首尾相接(子骨原点落在父骨的局部 +x,即骨尖)。
|
|
152
148
|
// 而我们的骨骼原点取的是**部件近端锚点**,部件摆得不够首尾相接时这个假设就破了:
|
|
153
149
|
// 实测举起的右臂两段被摆在同一小块区域,实际间距 41.9px、而上臂长度 140px;
|
|
154
150
|
// 更关键的是子骨原点在父骨局部系的**方向**(-160°)与父骨朝向(+90°)差了 250°。
|
|
@@ -184,16 +180,18 @@ export function buildPreviewHtml(options) {
|
|
|
184
180
|
// (实测差了整整 91°)。
|
|
185
181
|
var pTheta = base - a * sign;
|
|
186
182
|
var cTheta = pTheta + (Math.PI - b) * sign;
|
|
187
|
-
//
|
|
188
|
-
// 父骨那一段用的是「子骨原点方向」(θ + phi
|
|
183
|
+
// Spine 骨骼沿局部 +x 生长,子骨朝向就是世界旋转。
|
|
184
|
+
// 父骨那一段用的是「子骨原点方向」(θ + phi),所以仍减 phi。
|
|
189
185
|
var pWant = pTheta - phi;
|
|
190
|
-
var cWant = cTheta
|
|
186
|
+
var cWant = cTheta;
|
|
191
187
|
// 软 IK:在「不约束」与「对齐目标」之间按最短角差插值。
|
|
192
188
|
var pNew = pOrig + shortestAngle(pOrig, pWant) * mix;
|
|
193
189
|
var cNew = cOrig + shortestAngle(cOrig, cWant) * mix;
|
|
194
190
|
var pBase = parent.parentBone ? parent.parentBone.worldRot : 0;
|
|
195
191
|
parent.animRot = pNew * 180 / Math.PI - parent.setupRot - pBase * 180 / Math.PI;
|
|
196
192
|
child.animRot = cNew * 180 / Math.PI - child.setupRot - pNew * 180 / Math.PI;
|
|
193
|
+
// 后面的约束可能与这条链共享骨骼,必须读到前一条约束已经生效的矩阵。
|
|
194
|
+
updateWorld(parent);
|
|
197
195
|
}
|
|
198
196
|
}
|
|
199
197
|
|
|
@@ -248,6 +246,13 @@ export function buildPreviewHtml(options) {
|
|
|
248
246
|
(b[name][kind] || []).forEach(function (f) { if (f.time > max) max = f.time; });
|
|
249
247
|
});
|
|
250
248
|
});
|
|
249
|
+
Object.values(anim.attachments || {}).forEach(function (skinTracks) {
|
|
250
|
+
Object.values(skinTracks).forEach(function (slotTracks) {
|
|
251
|
+
Object.values(slotTracks).forEach(function (attachmentTracks) {
|
|
252
|
+
(attachmentTracks.deform || []).forEach(function (f) { if (f.time > max) max = f.time; });
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
});
|
|
251
256
|
return max;
|
|
252
257
|
}
|
|
253
258
|
|
|
@@ -278,37 +283,40 @@ export function buildPreviewHtml(options) {
|
|
|
278
283
|
// 有网格的部件走「逐三角形裁剪绘制」,没有的仍是一整块 drawImage。
|
|
279
284
|
// 分流的判据是 att.type === 'mesh'(骨架里写死的),不额外维护一张表。
|
|
280
285
|
//
|
|
281
|
-
//
|
|
282
|
-
//
|
|
286
|
+
// mesh 的非加权顶点是骨骼局部坐标;加权流里的每个影响也都是对应骨骼局部坐标。
|
|
287
|
+
// 不再重用 region 的挂点变换,按 Spine 同一份顶点/FFD/UV 数据绘制。
|
|
283
288
|
|
|
284
289
|
/**
|
|
285
|
-
* 当前时刻的顶点位移(FFD
|
|
286
|
-
*
|
|
287
|
-
* 与宿主 rigmesh.ts 的 buildWaveDeform **同一个公式**:越靠近固定端越不动,
|
|
288
|
-
* 相位按时间推进。两处必须一致,否则「预览里看到的」和「导出的」不是一回事。
|
|
290
|
+
* 当前时刻的顶点位移(FFD),直接采样导出的时间轴。
|
|
291
|
+
* 加权附件的每个骨骼影响都有独立的局部位移,不能把交错顶点流当坐标对。
|
|
289
292
|
*/
|
|
290
293
|
function deformOffsetsOf(name, att, t) {
|
|
291
|
-
var
|
|
292
|
-
|
|
293
|
-
var
|
|
294
|
-
var
|
|
295
|
-
|
|
296
|
-
var
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
var
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
294
|
+
var anim = SPINE.animations && SPINE.animations[current];
|
|
295
|
+
var tracks = anim && anim.attachments && anim.attachments.default;
|
|
296
|
+
var attachmentTracks = tracks && tracks[name] && tracks[name][name];
|
|
297
|
+
var frames = attachmentTracks && attachmentTracks.deform;
|
|
298
|
+
if (!frames || frames.length === 0) return null;
|
|
299
|
+
var length = (att.uvs || []).length;
|
|
300
|
+
if ((att.vertices || []).length !== length) {
|
|
301
|
+
length = 0;
|
|
302
|
+
for (var cursor = 0; cursor < att.vertices.length;) {
|
|
303
|
+
var count = att.vertices[cursor++];
|
|
304
|
+
length += count * 2;
|
|
305
|
+
cursor += count * 4;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
var a = frames[0], b = a;
|
|
309
|
+
if (t >= frames[frames.length - 1].time) a = b = frames[frames.length - 1];
|
|
310
|
+
else if (t > a.time) {
|
|
311
|
+
for (var i = 0; i + 1 < frames.length; i++) {
|
|
312
|
+
if (t >= frames[i].time && t <= frames[i + 1].time) { a = frames[i]; b = frames[i + 1]; break; }
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
var out = new Array(length);
|
|
316
|
+
for (var j = 0; j < length; j++) {
|
|
317
|
+
var av = (a.vertices || [])[j - (a.offset || 0)] || 0;
|
|
318
|
+
var bv = (b.vertices || [])[j - (b.offset || 0)] || 0;
|
|
319
|
+
out[j] = a === b || a.curve === 'stepped' ? av : bezierValue(a.curve, a.time, av, b.time, bv, t);
|
|
312
320
|
}
|
|
313
321
|
return out;
|
|
314
322
|
}
|
|
@@ -325,83 +333,70 @@ export function buildPreviewHtml(options) {
|
|
|
325
333
|
}
|
|
326
334
|
|
|
327
335
|
/**
|
|
328
|
-
*
|
|
336
|
+
* 把附件顶点解成当前的世界坐标,和官方 VertexAttachment.computeWorldVertices 一致。
|
|
329
337
|
*
|
|
330
338
|
* 两种格式要分开处理,而它们只能靠**长度**区分:
|
|
331
|
-
* - 非加权:vertices
|
|
339
|
+
* - 非加权:vertices 是所属骨骼局部坐标(长度等于 uvs);
|
|
332
340
|
* - 加权(LBS):Spine 的交错流 [骨骼数, (骨骼下标, x, y, 权重) × 骨骼数, …],
|
|
333
341
|
* x/y 是**骨骼局部**坐标,得先用各骨骼的**当前**世界变换做线性混合,
|
|
334
|
-
*
|
|
342
|
+
* 然后按权重线性混合。
|
|
335
343
|
*
|
|
336
344
|
* 不分开的后果不是报错,是**画出一团乱线**:骨骼下标会被当成坐标用。
|
|
337
345
|
*/
|
|
338
|
-
function
|
|
346
|
+
function meshWorldVertices(att, bone, offsets) {
|
|
339
347
|
const raw = att.vertices || [];
|
|
340
348
|
const uvs = att.uvs || [];
|
|
341
349
|
if (raw.length === 0) return [];
|
|
342
|
-
if (raw.length === uvs.length) return raw;
|
|
343
|
-
|
|
344
350
|
const out = [];
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
351
|
+
if (raw.length === uvs.length) {
|
|
352
|
+
for (let i = 0; i < raw.length; i += 2) {
|
|
353
|
+
const x = raw[i] + (offsets ? offsets[i] : 0);
|
|
354
|
+
const y = raw[i + 1] + (offsets ? offsets[i + 1] : 0);
|
|
355
|
+
out.push(bone.worldX + Math.cos(bone.worldRot) * x - Math.sin(bone.worldRot) * y,
|
|
356
|
+
bone.worldY + Math.sin(bone.worldRot) * x + Math.cos(bone.worldRot) * y);
|
|
357
|
+
}
|
|
358
|
+
return out;
|
|
359
|
+
}
|
|
350
360
|
let cursor = 0;
|
|
361
|
+
let deformCursor = 0;
|
|
351
362
|
while (cursor < raw.length) {
|
|
352
363
|
const count = raw[cursor++];
|
|
353
364
|
let wx = 0;
|
|
354
365
|
let wy = 0;
|
|
355
366
|
for (let n = 0; n < count; n++) {
|
|
356
367
|
const index = raw[cursor++];
|
|
357
|
-
const x = raw[cursor++];
|
|
358
|
-
const y = raw[cursor++];
|
|
368
|
+
const x = raw[cursor++] + (offsets ? offsets[deformCursor] : 0);
|
|
369
|
+
const y = raw[cursor++] + (offsets ? offsets[deformCursor + 1] : 0);
|
|
370
|
+
deformCursor += 2;
|
|
359
371
|
const weight = raw[cursor++];
|
|
360
372
|
const owner = bones[(SPINE.bones || [])[index]?.name];
|
|
361
373
|
if (!owner) continue;
|
|
362
374
|
wx += (Math.cos(owner.worldRot) * x - Math.sin(owner.worldRot) * y + owner.worldX) * weight;
|
|
363
375
|
wy += (Math.sin(owner.worldRot) * x + Math.cos(owner.worldRot) * y + owner.worldY) * weight;
|
|
364
376
|
}
|
|
365
|
-
|
|
366
|
-
const dx = wx - bone.worldX;
|
|
367
|
-
const dy = wy - bone.worldY;
|
|
368
|
-
const c = Math.cos(-bone.worldRot);
|
|
369
|
-
const s = Math.sin(-bone.worldRot);
|
|
370
|
-
const lx = dx * c - dy * s - attX;
|
|
371
|
-
const ly = dx * s + dy * c - attY;
|
|
372
|
-
out.push(lx * cosA - ly * sinA, lx * sinA + ly * cosA);
|
|
377
|
+
out.push(wx, wy);
|
|
373
378
|
}
|
|
374
379
|
return out;
|
|
375
380
|
}
|
|
376
381
|
|
|
377
382
|
function drawMesh(bone, att, img, name, t) {
|
|
378
|
-
|
|
383
|
+
var offsets = deformOffsetsOf(name, att, t);
|
|
384
|
+
const verts = meshWorldVertices(att, bone, offsets);
|
|
379
385
|
const uvs = att.uvs || [];
|
|
380
386
|
const tris = att.triangles || [];
|
|
381
387
|
if (verts.length === 0 || tris.length === 0) return; // 加权流已在上一步解码成坐标
|
|
382
388
|
var width = att.width || 1;
|
|
383
389
|
var height = att.height || 1;
|
|
384
|
-
var offsets = deformOffsetsOf(name, att, t);
|
|
385
|
-
var halfW = width / 2;
|
|
386
|
-
var halfH = height / 2;
|
|
387
|
-
|
|
388
390
|
ctx.save();
|
|
389
|
-
ctx.translate(bone.worldX, bone.worldY);
|
|
390
|
-
ctx.rotate(bone.worldRot);
|
|
391
|
-
ctx.translate(att.x || 0, att.y || 0);
|
|
392
|
-
ctx.rotate((att.rotation || 0) * Math.PI / 180);
|
|
393
|
-
ctx.scale(1, -1);
|
|
394
|
-
// 到这里坐标系是「部件中心为原点、y 向上」,而顶点是「中心为原点、y 向下」
|
|
395
|
-
// (骨架里按图片坐标算的),所以顶点取负 y。
|
|
396
391
|
for (var k = 0; k + 2 < tris.length; k += 3) {
|
|
397
392
|
var i0 = tris[k] * 2, i1 = tris[k + 1] * 2, i2 = tris[k + 2] * 2;
|
|
398
|
-
var p0 = { x: verts[i0]
|
|
399
|
-
var p1 = { x: verts[i1]
|
|
400
|
-
var p2 = { x: verts[i2]
|
|
401
|
-
//
|
|
402
|
-
var s0 = { x:
|
|
403
|
-
var s1 = { x:
|
|
404
|
-
var s2 = { x:
|
|
393
|
+
var p0 = { x: verts[i0], y: verts[i0 + 1] };
|
|
394
|
+
var p1 = { x: verts[i1], y: verts[i1 + 1] };
|
|
395
|
+
var p2 = { x: verts[i2], y: verts[i2 + 1] };
|
|
396
|
+
// UV 决定固定的源像素;动画只能改目标顶点,不能跟着改纹理采样位置。
|
|
397
|
+
var s0 = { x: uvs[i0] * img.naturalWidth, y: uvs[i0 + 1] * img.naturalHeight };
|
|
398
|
+
var s1 = { x: uvs[i1] * img.naturalWidth, y: uvs[i1 + 1] * img.naturalHeight };
|
|
399
|
+
var s2 = { x: uvs[i2] * img.naturalWidth, y: uvs[i2 + 1] * img.naturalHeight };
|
|
405
400
|
var m = affineFromTriangles(s0, s1, s2, p0, p1, p2);
|
|
406
401
|
if (m === null) continue;
|
|
407
402
|
// 三个顶点以重心为中心**外扩一点点**。
|
|
@@ -428,7 +423,6 @@ export function buildPreviewHtml(options) {
|
|
|
428
423
|
ctx.restore();
|
|
429
424
|
}
|
|
430
425
|
ctx.restore();
|
|
431
|
-
void uvs;
|
|
432
426
|
}
|
|
433
427
|
|
|
434
428
|
// ── Path 约束(M5)─────────────────────────────────────────────────
|
|
@@ -454,10 +448,16 @@ export function buildPreviewHtml(options) {
|
|
|
454
448
|
|
|
455
449
|
function samplePath(path, lengths, total, distance) {
|
|
456
450
|
var count = path.points.length / 2;
|
|
457
|
-
|
|
451
|
+
if (total <= 1e-6) return { x: path.points[0], y: path.points[1], angle: 0 };
|
|
452
|
+
var lastSegment = lengths.length - 1;
|
|
453
|
+
while (lastSegment >= 0 && lengths[lastSegment] <= 1e-6) lastSegment--;
|
|
454
|
+
// 与官方 PathConstraint 相同:闭合路径循环,开放路径沿首尾切线外推。
|
|
455
|
+
var remaining = path.closed && total > 1e-9 ? ((distance % total) + total) % total : distance;
|
|
458
456
|
for (var i = 0; i < lengths.length; i++) {
|
|
459
457
|
var length = lengths[i];
|
|
460
|
-
|
|
458
|
+
// 导出的三次曲线路径移除了相邻重复点;预览也跳过对应的零长度段。
|
|
459
|
+
if (length <= 1e-6) continue;
|
|
460
|
+
if (remaining <= length || i === lastSegment) {
|
|
461
461
|
var t = length > 1e-9 ? remaining / length : 0;
|
|
462
462
|
var j = (i + 1) % count;
|
|
463
463
|
var x0 = path.points[i * 2], y0 = path.points[i * 2 + 1];
|
|
@@ -511,9 +511,8 @@ export function buildPreviewHtml(options) {
|
|
|
511
511
|
var bone = bones[chain[k]];
|
|
512
512
|
if (!bone) continue;
|
|
513
513
|
var at = samplePath(spec, lengths, total, k * spacing);
|
|
514
|
-
//
|
|
515
|
-
|
|
516
|
-
placeBone(bone, at.x, at.y, at.angle - Math.PI / 2, spec.translateMix, spec.rotateMix);
|
|
514
|
+
// Spine 的骨尖沿局部 +x,路径切线角可以直接作为世界旋转。
|
|
515
|
+
placeBone(bone, at.x, at.y, at.angle, spec.translateMix, spec.rotateMix);
|
|
517
516
|
// **放好一根就立刻刷新它的世界变换**:placeBone 是拿父级的当前世界位置
|
|
518
517
|
// 反算 local 的,而链上下一根骨的父级正是刚放好的这根——不刷新的话它读到的
|
|
519
518
|
// 还是旧位置,误差会沿着链累积(实测三根骨的末端偏了 39px)。
|
|
@@ -531,7 +530,7 @@ export function buildPreviewHtml(options) {
|
|
|
531
530
|
var att = skin[slot.name] && skin[slot.name][slot.attachment];
|
|
532
531
|
var bone = bones[slot.bone];
|
|
533
532
|
if (!att || !bone) return;
|
|
534
|
-
var rad =
|
|
533
|
+
var rad = ((att.rotation || 0) * Math.PI / 180);
|
|
535
534
|
var hw = att.width / 2, hh = att.height / 2;
|
|
536
535
|
for (var sx = -1; sx <= 1; sx += 2) for (var sy = -1; sy <= 1; sy += 2) {
|
|
537
536
|
var lx = att.x + (sx * hw * Math.cos(rad) - sy * hh * Math.sin(rad));
|
|
@@ -614,7 +613,7 @@ export function buildPreviewHtml(options) {
|
|
|
614
613
|
if (b.length > 0) {
|
|
615
614
|
ctx.beginPath();
|
|
616
615
|
ctx.moveTo(0, 0);
|
|
617
|
-
ctx.lineTo(
|
|
616
|
+
ctx.lineTo(b.length, 0);
|
|
618
617
|
ctx.strokeStyle = 'rgba(80,230,140,.85)';
|
|
619
618
|
ctx.lineWidth = 1.6 / view.scale;
|
|
620
619
|
ctx.stroke();
|
|
@@ -775,6 +774,14 @@ export function buildPreviewHtml(options) {
|
|
|
775
774
|
// 否则「拖动目标点时手跟随」这条判据只能靠肉眼看截图。
|
|
776
775
|
// 只暴露读接口,不提供任何写入口——验收用的东西不该能改状态。
|
|
777
776
|
window.__gmmPreview = {
|
|
777
|
+
/** 当前 mesh 世界顶点,供验收比较官方运行时,不改变播放状态。 */
|
|
778
|
+
meshVertices: function (name) {
|
|
779
|
+
var slot = slots.find(function (item) { return item.name === name; });
|
|
780
|
+
var att = slot && skin[name] && skin[name][slot.attachment];
|
|
781
|
+
var bone = slot && bones[slot.bone];
|
|
782
|
+
if (!att || att.type !== 'mesh' || !bone) return null;
|
|
783
|
+
return meshWorldVertices(att, bone, deformOffsetsOf(name, att, animTime));
|
|
784
|
+
},
|
|
778
785
|
bones: function () {
|
|
779
786
|
var out = {};
|
|
780
787
|
boneList.forEach(function (b) {
|
|
@@ -798,7 +805,7 @@ export function buildPreviewHtml(options) {
|
|
|
798
805
|
var chain = IKS[i].bones;
|
|
799
806
|
var last = bones[chain[chain.length - 1]];
|
|
800
807
|
if (!last) continue;
|
|
801
|
-
return { x: last.worldX
|
|
808
|
+
return { x: last.worldX + Math.cos(last.worldRot) * last.length, y: last.worldY + Math.sin(last.worldRot) * last.length };
|
|
802
809
|
}
|
|
803
810
|
return null;
|
|
804
811
|
}
|
package/lib/rigsemantics.js
CHANGED
|
@@ -315,11 +315,11 @@ export function validateSemantics(input, options = {}) {
|
|
|
315
315
|
? [...new Set(raw.tags.filter((tag) => typeof tag === "string" && tag.trim() !== "").map((tag) => tag.trim()))]
|
|
316
316
|
: [];
|
|
317
317
|
const source = raw.source === "ai" || raw.source === "human" ? raw.source : "default";
|
|
318
|
-
return { name: raw.name, role, parent: typeof raw.parent === "string" && raw.parent.trim() !== "" ? raw.parent.trim() : undefined, proximal, distal, tags, source };
|
|
318
|
+
return { name: raw.name, role, parent: raw.parent === null ? null : typeof raw.parent === "string" && raw.parent.trim() !== "" ? raw.parent.trim() : undefined, proximal, distal, tags, source };
|
|
319
319
|
});
|
|
320
320
|
// ③ 父级存在性与自环。
|
|
321
321
|
for (const part of parts) {
|
|
322
|
-
if (part.parent
|
|
322
|
+
if (part.parent == null)
|
|
323
323
|
continue;
|
|
324
324
|
if (part.parent === part.name) {
|
|
325
325
|
errors.push({ name: part.name, level: "error", message: "父级指向自己" });
|
|
@@ -337,7 +337,7 @@ export function validateSemantics(input, options = {}) {
|
|
|
337
337
|
for (const part of parts) {
|
|
338
338
|
const path = new Set([part.name]);
|
|
339
339
|
let cursor = part.parent;
|
|
340
|
-
while (cursor
|
|
340
|
+
while (cursor != null) {
|
|
341
341
|
if (path.has(cursor)) {
|
|
342
342
|
errors.push({ name: part.name, level: "error", message: `父级链成环:${[...path].join(" → ")} → ${cursor};已改挂 root` });
|
|
343
343
|
part.parent = undefined;
|
|
@@ -366,7 +366,7 @@ export function validateSemantics(input, options = {}) {
|
|
|
366
366
|
const chain = [];
|
|
367
367
|
let cursor = part.parent;
|
|
368
368
|
let guard = 0;
|
|
369
|
-
while (cursor
|
|
369
|
+
while (cursor != null && guard++ < 64) {
|
|
370
370
|
const parent = byName.get(cursor);
|
|
371
371
|
if (parent === undefined)
|
|
372
372
|
break;
|
package/lib/rigvalidate.js
CHANGED
|
@@ -75,8 +75,8 @@ export function validateSpineWire(spine) {
|
|
|
75
75
|
for (const constraint of Array.isArray(spine?.ik) ? spine.ik : []) {
|
|
76
76
|
const where = `ik/${constraint?.name ?? "?"}`;
|
|
77
77
|
const chain = Array.isArray(constraint?.bones) ? constraint.bones.filter((name) => typeof name === "string") : [];
|
|
78
|
-
if (chain.length
|
|
79
|
-
errors.push({ level: "error", code: "ik-chain", where, message:
|
|
78
|
+
if (chain.length !== 2) {
|
|
79
|
+
errors.push({ level: "error", code: "ik-chain", where, message: `当前只支持两根骨骼的 IK,当前 ${chain.length} 根` });
|
|
80
80
|
continue;
|
|
81
81
|
}
|
|
82
82
|
for (const name of chain) {
|
|
@@ -132,11 +132,12 @@ export function validateSpineWire(spine) {
|
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
134
|
const target = typeof constraint?.target === "string" ? constraint.target : "";
|
|
135
|
-
if (!
|
|
136
|
-
errors.push({ level: "error", code: "path-target", where, message: `Path
|
|
135
|
+
if (!slotNames.has(target)) {
|
|
136
|
+
errors.push({ level: "error", code: "path-target", where, message: `Path 的目标槽位「${target}」不在骨架里` });
|
|
137
137
|
}
|
|
138
|
-
const slotName =
|
|
139
|
-
const
|
|
138
|
+
const slotName = target;
|
|
139
|
+
const slot = (spine.slots ?? []).find((item) => item?.name === slotName);
|
|
140
|
+
const attachment = attachmentOf(slotName, String(slot?.attachment ?? ""));
|
|
140
141
|
if (!slotNames.has(slotName)) {
|
|
141
142
|
errors.push({ level: "error", code: "path-slot", where, message: `缺少路径槽位「${slotName}」,运行时找不到这条路径` });
|
|
142
143
|
}
|
|
@@ -146,22 +147,24 @@ export function validateSpineWire(spine) {
|
|
|
146
147
|
else {
|
|
147
148
|
const vertices = Array.isArray(attachment.vertices) ? attachment.vertices : [];
|
|
148
149
|
const lengths = Array.isArray(attachment.lengths) ? attachment.lengths : [];
|
|
149
|
-
|
|
150
|
-
|
|
150
|
+
const vertexCount = attachment.vertexCount;
|
|
151
|
+
if (!Number.isInteger(vertexCount) || vertexCount < 6 || vertexCount % 3 !== 0 || vertices.length !== vertexCount * 2 || vertices.some((value) => !Number.isFinite(value))) {
|
|
152
|
+
errors.push({ level: "error", code: "path-vertices", where, message: "Path 必须声明 vertexCount,顶点包含三次贝塞尔控制点(点数为 3 的倍数)" });
|
|
151
153
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
const expected = attachment.closed === true ? vertices.length / 2 : vertices.length / 2 - 1;
|
|
155
|
-
if (lengths.length !== expected) {
|
|
154
|
+
const expected = vertexCount / 3;
|
|
155
|
+
if (lengths.length !== expected || lengths.some((value, index) => !Number.isFinite(value) || value <= 0 || (index > 0 && value < lengths[index - 1]))) {
|
|
156
156
|
errors.push({
|
|
157
157
|
level: "error",
|
|
158
158
|
code: "path-lengths",
|
|
159
159
|
where,
|
|
160
|
-
message: `Path 的 lengths
|
|
160
|
+
message: `Path 的 lengths 必须为 ${expected} 项正数累计弧长,当前 ${lengths.length} 项`
|
|
161
161
|
});
|
|
162
162
|
}
|
|
163
163
|
}
|
|
164
|
-
|
|
164
|
+
if (constraint.translateMix !== undefined || constraint.rotateMix !== undefined) {
|
|
165
|
+
errors.push({ level: "error", code: "path-legacy-mix", where, message: "Spine 4.2 Path 必须使用 mixX/mixY/mixRotate,旧混合字段会被忽略" });
|
|
166
|
+
}
|
|
167
|
+
for (const key of ["mixX", "mixY", "mixRotate"]) {
|
|
165
168
|
const value = numOr(constraint?.[key], 1);
|
|
166
169
|
if (value < 0 || value > 1) {
|
|
167
170
|
errors.push({ level: "error", code: "path-mix", where, message: `${key} 必须在 0~1,当前 ${value}` });
|
|
@@ -174,6 +177,41 @@ export function validateSpineWire(spine) {
|
|
|
174
177
|
return report(errors, warnings);
|
|
175
178
|
}
|
|
176
179
|
for (const [animationName, animation] of Object.entries(animations)) {
|
|
180
|
+
if (animation?.deform !== undefined) {
|
|
181
|
+
errors.push({ level: "error", code: "deform-nesting", where: animationName, message: "Spine 4.2 的 deform 必须放在 attachments/皮肤/槽位/附件下,动画根字段会被忽略" });
|
|
182
|
+
}
|
|
183
|
+
for (const [skinName, slots] of Object.entries(animation?.attachments ?? {})) {
|
|
184
|
+
const skin = spine.skins?.find((item) => item?.name === skinName);
|
|
185
|
+
for (const [slotName, byAttachment] of Object.entries(slots ?? {})) {
|
|
186
|
+
for (const [attachmentName, kinds] of Object.entries(byAttachment ?? {})) {
|
|
187
|
+
if (kinds?.deform === undefined)
|
|
188
|
+
continue;
|
|
189
|
+
const where = `${animationName}/attachments/${skinName}/${slotName}/${attachmentName}/deform`;
|
|
190
|
+
const attachment = skin?.attachments?.[slotName]?.[attachmentName];
|
|
191
|
+
if (attachment?.type !== "mesh") {
|
|
192
|
+
errors.push({ level: "error", code: "deform-attachment", where, message: "deform 引用的网格附件不存在" });
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
const expected = meshDeformLength(attachment);
|
|
196
|
+
const frames = kinds.deform;
|
|
197
|
+
if (expected === undefined || !Array.isArray(frames) || frames.length === 0) {
|
|
198
|
+
errors.push({ level: "error", code: "deform-shape", where, message: "deform 的网格顶点流或关键帧数组无效" });
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
for (const [index, frame] of frames.entries()) {
|
|
202
|
+
const time = frame?.time ?? 0;
|
|
203
|
+
const offset = frame?.offset ?? 0;
|
|
204
|
+
const values = frame?.vertices ?? [];
|
|
205
|
+
if (!Number.isFinite(time) || time < 0 || (index > 0 && time <= (frames[index - 1]?.time ?? 0))) {
|
|
206
|
+
errors.push({ level: "error", code: "deform-time", where: `${where}[${index}]`, message: "deform 时间必须非负、严格递增" });
|
|
207
|
+
}
|
|
208
|
+
if (!Number.isInteger(offset) || offset < 0 || !Array.isArray(values) || offset + values.length > expected || values.some((value) => typeof value !== "number" || !Number.isFinite(value))) {
|
|
209
|
+
errors.push({ level: "error", code: "deform-vertices", where: `${where}[${index}]`, message: `deform 位移越界或非有限数:该网格最多 ${expected} 个值(加权网格每影响骨两个值)` });
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
177
215
|
const boneTracks = animation?.bones;
|
|
178
216
|
if (boneTracks === null || typeof boneTracks !== "object")
|
|
179
217
|
continue;
|
|
@@ -283,6 +321,22 @@ export function validateSpineWire(spine) {
|
|
|
283
321
|
}
|
|
284
322
|
return report(errors, warnings);
|
|
285
323
|
}
|
|
324
|
+
function meshDeformLength(attachment) {
|
|
325
|
+
const vertices = attachment?.vertices, uvs = attachment?.uvs;
|
|
326
|
+
if (!Array.isArray(vertices) || !Array.isArray(uvs))
|
|
327
|
+
return undefined;
|
|
328
|
+
if (vertices.length === uvs.length)
|
|
329
|
+
return vertices.length;
|
|
330
|
+
let cursor = 0, count = 0;
|
|
331
|
+
for (let vertex = 0; vertex < uvs.length / 2; vertex++) {
|
|
332
|
+
const influences = vertices[cursor++];
|
|
333
|
+
if (!Number.isInteger(influences) || influences < 1 || cursor + influences * 4 > vertices.length)
|
|
334
|
+
return undefined;
|
|
335
|
+
count += influences * 2;
|
|
336
|
+
cursor += influences * 4;
|
|
337
|
+
}
|
|
338
|
+
return cursor === vertices.length ? count : undefined;
|
|
339
|
+
}
|
|
286
340
|
// ── 循环接缝 ────────────────────────────────────────────────────────────
|
|
287
341
|
/** 数值比较的容差:预设的数值都按 2 位小数 round 过,这里留一点余量。 */
|
|
288
342
|
const VALUE_TOLERANCE = 0.05;
|
|
@@ -511,6 +565,10 @@ const DRAGONBONES_VERSIONS = new Set(["2.3", "3.0", "4.0", "4.5", "5.0", "5.5",
|
|
|
511
565
|
export function validateDragonBones(skeleton) {
|
|
512
566
|
const errors = [];
|
|
513
567
|
const warnings = [];
|
|
568
|
+
for (const message of Array.isArray(skeleton?.exportWarnings) ? skeleton.exportWarnings : []) {
|
|
569
|
+
if (typeof message === "string")
|
|
570
|
+
warnings.push({ level: "warning", code: "db-export-limitation", where: "export", message });
|
|
571
|
+
}
|
|
514
572
|
if (skeleton === null || typeof skeleton !== "object") {
|
|
515
573
|
errors.push({ level: "error", code: "db-empty", where: "skeleton", message: "DragonBones 骨架为空" });
|
|
516
574
|
return report(errors, warnings);
|