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
|
@@ -0,0 +1,816 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 自包含骨骼动画预览(HTML)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么不用官方 Spine Web Player:它要从 CDN 拉 `spine-player.js`,而验收页
|
|
5
|
+
* 可能在没有外网的环境里打开(本机离线、内网、或者 CDN 被墙)。这个预览把
|
|
6
|
+
* 部件图片与骨架 JSON 全部以 base64 内联,**零外部依赖**,双击就能看。
|
|
7
|
+
*
|
|
8
|
+
* 渲染器实现的是 spine-core 的那套语义:
|
|
9
|
+
* - 骨骼局部量 = setup(x, y, rotation) + 动画增量;子骨骼的位移要先用
|
|
10
|
+
* 父骨骼的旋转去转,再叠加到父骨骼世界坐标上。
|
|
11
|
+
* - 挂点 (x, y) 是图片中心相对骨骼原点的位移,处在骨骼局部坐标系里;
|
|
12
|
+
* `rotation` 是挂点自身的局部旋转(我们用它在初始姿态里抵消骨骼朝向,
|
|
13
|
+
* 让装配结果逐像素还原)。
|
|
14
|
+
* - 动画曲线是 **Spine 4.2 的绝对贝塞尔**:4 个数给单属性(rotate 的 value),
|
|
15
|
+
* 8 个数给双属性(translate 的 x、y 各占 4 个)。控制点是 (时间, 值) 空间里
|
|
16
|
+
* 的绝对坐标,因此要在 x(u)=t 上反解 u,再取 y(u)。
|
|
17
|
+
*/
|
|
18
|
+
function safeJson(value) {
|
|
19
|
+
// JSON 里可能出现 `</script>`,直接内联会提前结束脚本块。
|
|
20
|
+
return JSON.stringify(value).replace(/<\//g, "<\\/");
|
|
21
|
+
}
|
|
22
|
+
export function buildPreviewHtml(options) {
|
|
23
|
+
const title = options.title ?? "骨骼动画预览";
|
|
24
|
+
const images = Object.fromEntries(options.images.map((image) => [image.name, image.dataUri]));
|
|
25
|
+
const animations = Object.keys(options.spine?.animations ?? {});
|
|
26
|
+
const defaultAnimation = options.defaultAnimation !== undefined && animations.includes(options.defaultAnimation)
|
|
27
|
+
? options.defaultAnimation
|
|
28
|
+
: animations[0] ?? "";
|
|
29
|
+
const background = options.background ?? "#12121c";
|
|
30
|
+
/** 没有变形的部件不必内联任何东西——预览里靠 DEFORMS[name] 是否存在来分流。 */
|
|
31
|
+
const deforms = options.deforms ?? {};
|
|
32
|
+
const paths = options.paths ?? {};
|
|
33
|
+
return `<!DOCTYPE html>
|
|
34
|
+
<html lang="zh-CN">
|
|
35
|
+
<head>
|
|
36
|
+
<meta charset="UTF-8">
|
|
37
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
38
|
+
<title>${title}</title>
|
|
39
|
+
<style>
|
|
40
|
+
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
41
|
+
body {
|
|
42
|
+
background: ${background};
|
|
43
|
+
color: #e6e8f0;
|
|
44
|
+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
|
|
45
|
+
display: flex; flex-direction: column; align-items: center; gap: 14px; padding: 18px;
|
|
46
|
+
}
|
|
47
|
+
h1 { font-size: 16px; font-weight: 600; letter-spacing: .04em; color: #aab4d4; }
|
|
48
|
+
#stage { border-radius: 12px; overflow: hidden; box-shadow: 0 10px 40px rgba(0,0,0,.55); background: #1b1b28; }
|
|
49
|
+
canvas { display: block; }
|
|
50
|
+
.bar { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; align-items: center; }
|
|
51
|
+
button {
|
|
52
|
+
background: #262a3d; color: #cfd6ea; border: 1px solid #363c56; border-radius: 8px;
|
|
53
|
+
padding: 6px 12px; font-size: 13px; cursor: pointer; transition: .15s;
|
|
54
|
+
}
|
|
55
|
+
button:hover { background: #31374f; }
|
|
56
|
+
button.active { background: #3f6fff; border-color: #3f6fff; color: #fff; }
|
|
57
|
+
.meta { font-size: 12px; color: #7c86a6; }
|
|
58
|
+
input[type=range] { width: 220px; }
|
|
59
|
+
</style>
|
|
60
|
+
</head>
|
|
61
|
+
<body>
|
|
62
|
+
<h1>${escapeHtml(title)}</h1>
|
|
63
|
+
<div id="stage"><canvas id="cv"></canvas></div>
|
|
64
|
+
<div class="bar" id="anims"></div>
|
|
65
|
+
<div class="bar">
|
|
66
|
+
<button id="play">⏸ 暂停</button>
|
|
67
|
+
<input type="range" id="scrub" min="0" max="1000" value="0" />
|
|
68
|
+
<button id="bones">骨骼</button>
|
|
69
|
+
<button id="grid">网格</button>
|
|
70
|
+
<span class="meta" id="tip"></span>
|
|
71
|
+
</div>
|
|
72
|
+
<div class="meta">内联渲染,无外部依赖;切换动画即时生效。</div>
|
|
73
|
+
<script>
|
|
74
|
+
(function () {
|
|
75
|
+
var SPINE = ${safeJson(options.spine)};
|
|
76
|
+
var IMAGE_DATA = ${safeJson(images)};
|
|
77
|
+
var ANIMS = ${safeJson(animations)};
|
|
78
|
+
// FFD 变形参数(部件名 → 波形)。没有它就不会走 mesh 渲染那条路。
|
|
79
|
+
var DEFORMS = ${safeJson(deforms)};
|
|
80
|
+
// Path 约束(名字 → 路径 + 骨链)。点已经是骨骼世界坐标。
|
|
81
|
+
var PATHS = ${safeJson(paths)};
|
|
82
|
+
var current = ${safeJson(defaultAnimation)};
|
|
83
|
+
// 允许用 URL 片段指定初始动画(#walk):插件的界面用它做「切动画」按钮,
|
|
84
|
+
// 不必反复重载整个预览页。
|
|
85
|
+
var hashAnim = (location.hash || '').replace('#', '');
|
|
86
|
+
if (hashAnim && ANIMS.indexOf(hashAnim) >= 0) current = hashAnim;
|
|
87
|
+
|
|
88
|
+
var canvas = document.getElementById('cv');
|
|
89
|
+
var ctx = canvas.getContext('2d');
|
|
90
|
+
var images = {};
|
|
91
|
+
var loaded = 0;
|
|
92
|
+
var total = Object.keys(IMAGE_DATA).length;
|
|
93
|
+
|
|
94
|
+
var bones = {};
|
|
95
|
+
var boneList = [];
|
|
96
|
+
var slots = (SPINE.slots || []).slice();
|
|
97
|
+
var skin = (SPINE.skins && SPINE.skins[0] && SPINE.skins[0].attachments) || {};
|
|
98
|
+
|
|
99
|
+
// ── 骨骼树 ──────────────────────────────────────────────────────────
|
|
100
|
+
(SPINE.bones || []).forEach(function (b) {
|
|
101
|
+
bones[b.name] = {
|
|
102
|
+
name: b.name, parent: b.parent || null, children: [],
|
|
103
|
+
setupX: b.x || 0, setupY: b.y || 0, setupRot: b.rotation || 0, length: b.length || 0,
|
|
104
|
+
animX: 0, animY: 0, animRot: 0, worldX: 0, worldY: 0, worldRot: 0
|
|
105
|
+
};
|
|
106
|
+
boneList.push(bones[b.name]);
|
|
107
|
+
});
|
|
108
|
+
boneList.forEach(function (b) {
|
|
109
|
+
if (b.parent && bones[b.parent]) { b.parentBone = bones[b.parent]; bones[b.parent].children.push(b); }
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
function updateWorld(b) {
|
|
113
|
+
var lx = b.setupX + b.animX;
|
|
114
|
+
var ly = b.setupY + b.animY;
|
|
115
|
+
var lr = (b.setupRot + b.animRot) * Math.PI / 180;
|
|
116
|
+
if (!b.parentBone) { b.worldX = lx; b.worldY = ly; b.worldRot = lr; }
|
|
117
|
+
else {
|
|
118
|
+
var pr = b.parentBone.worldRot, c = Math.cos(pr), s = Math.sin(pr);
|
|
119
|
+
b.worldX = b.parentBone.worldX + lx * c - ly * s;
|
|
120
|
+
b.worldY = b.parentBone.worldY + lx * s + ly * c;
|
|
121
|
+
b.worldRot = pr + lr;
|
|
122
|
+
}
|
|
123
|
+
for (var i = 0; i < b.children.length; i++) updateWorld(b.children[i]);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ── IK 约束(两骨余弦定理) ──────────────────────────────────────────
|
|
127
|
+
//
|
|
128
|
+
// Spine 的 ik 里,bones 是「从根到末端」的骨骼名,target 是目标骨。
|
|
129
|
+
// **只解最后两根**:三骨以上的 IK 需要另一套解法(解析解或 FABRIK),
|
|
130
|
+
// 不是「多迭代几遍」就能对的——宁可不做,也不要给一个看起来能动、
|
|
131
|
+
// 实际姿势错的结果。
|
|
132
|
+
var IKS = (SPINE.ik || []).filter(function (c) { return c && c.bones && c.bones.length >= 2; });
|
|
133
|
+
|
|
134
|
+
function shortestAngle(from, to) {
|
|
135
|
+
var d = to - from;
|
|
136
|
+
while (d > Math.PI) d -= Math.PI * 2;
|
|
137
|
+
while (d < -Math.PI) d += Math.PI * 2;
|
|
138
|
+
return d;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function solveIk() {
|
|
142
|
+
for (var i = 0; i < IKS.length; i++) {
|
|
143
|
+
var c = IKS[i];
|
|
144
|
+
var chain = c.bones;
|
|
145
|
+
var child = bones[chain[chain.length - 1]];
|
|
146
|
+
var parent = bones[chain[chain.length - 2]];
|
|
147
|
+
var target = bones[c.target];
|
|
148
|
+
if (!child || !parent || !target) continue;
|
|
149
|
+
// **第一段长度与方向都按「子骨原点在父骨局部系里的实际位置」算**,不用 parent.length。
|
|
150
|
+
//
|
|
151
|
+
// IK 的经典推导假设链上骨骼首尾相接(子骨原点落在父骨的局部 +y,即骨尖)。
|
|
152
|
+
// 而我们的骨骼原点取的是**部件近端锚点**,部件摆得不够首尾相接时这个假设就破了:
|
|
153
|
+
// 实测举起的右臂两段被摆在同一小块区域,实际间距 41.9px、而上臂长度 140px;
|
|
154
|
+
// 更关键的是子骨原点在父骨局部系的**方向**(-160°)与父骨朝向(+90°)差了 250°。
|
|
155
|
+
// 只把长度改对、方向还按父骨朝向算,解出来的角度依然不满足几何(骨尖离目标 208px)。
|
|
156
|
+
//
|
|
157
|
+
// 父骨旋转不改变这个局部偏移,所以它稳定等于绑定姿势下的值。
|
|
158
|
+
var l1 = Math.sqrt(child.setupX * child.setupX + child.setupY * child.setupY);
|
|
159
|
+
// 子骨原点在父骨局部系里的方向角;父骨旋转 θ 时,它的世界方向 = θ + phi。
|
|
160
|
+
var phi = Math.atan2(child.setupY, child.setupX);
|
|
161
|
+
var l2 = child.length;
|
|
162
|
+
if (l1 <= 0.01 || l2 <= 0.01) continue;
|
|
163
|
+
var dx = target.worldX - parent.worldX;
|
|
164
|
+
var dy = target.worldY - parent.worldY;
|
|
165
|
+
var dist = Math.sqrt(dx * dx + dy * dy);
|
|
166
|
+
if (dist < 1e-6) continue;
|
|
167
|
+
// 够不到就伸直、太近就折到极限:夹进 [|l1-l2|, l1+l2],
|
|
168
|
+
// 否则 acos 的参数会跑出 [-1,1],NaN 顺着矩阵扩散、整只骨架消失。
|
|
169
|
+
var d = Math.min(l1 + l2 - 1e-4, Math.max(Math.abs(l1 - l2) + 1e-4, dist));
|
|
170
|
+
var base = Math.atan2(dy, dx);
|
|
171
|
+
var cosA = (l1 * l1 + d * d - l2 * l2) / (2 * l1 * d);
|
|
172
|
+
var cosB = (l1 * l1 + l2 * l2 - d * d) / (2 * l1 * l2);
|
|
173
|
+
var a = Math.acos(Math.max(-1, Math.min(1, cosA)));
|
|
174
|
+
var b = Math.acos(Math.max(-1, Math.min(1, cosB)));
|
|
175
|
+
// bendPositive 决定取哪一支解(肘/膝往哪边弯)。
|
|
176
|
+
var sign = c.bendPositive === false ? -1 : 1;
|
|
177
|
+
var mix = typeof c.mix === 'number' ? Math.max(0, Math.min(1, c.mix)) : 1;
|
|
178
|
+
var pOrig = parent.worldRot, cOrig = child.worldRot;
|
|
179
|
+
// 两骨余弦定理的直接结论:
|
|
180
|
+
// 父骨那一段的朝向 = base − a
|
|
181
|
+
// 子骨的朝向 = **父骨朝向 + (π − b)**
|
|
182
|
+
// 子骨那一项必须相对**父骨朝向**算,不能相对 base——P1 并不在 P0→target 的
|
|
183
|
+
// 直线上,两者差一个 a。写错这一项的表现是「IK 看起来转了、但骨尖就是不到目标」
|
|
184
|
+
// (实测差了整整 91°)。
|
|
185
|
+
var pTheta = base - a * sign;
|
|
186
|
+
var cTheta = pTheta + (Math.PI - b) * sign;
|
|
187
|
+
// 骨骼沿**局部 +y** 生长,朝向角 = 世界旋转 + 90°。
|
|
188
|
+
// 父骨那一段用的是「子骨原点方向」(θ + phi),不是父骨朝向,所以减 phi。
|
|
189
|
+
var pWant = pTheta - phi;
|
|
190
|
+
var cWant = cTheta - Math.PI / 2;
|
|
191
|
+
// 软 IK:在「不约束」与「对齐目标」之间按最短角差插值。
|
|
192
|
+
var pNew = pOrig + shortestAngle(pOrig, pWant) * mix;
|
|
193
|
+
var cNew = cOrig + shortestAngle(cOrig, cWant) * mix;
|
|
194
|
+
var pBase = parent.parentBone ? parent.parentBone.worldRot : 0;
|
|
195
|
+
parent.animRot = pNew * 180 / Math.PI - parent.setupRot - pBase * 180 / Math.PI;
|
|
196
|
+
child.animRot = cNew * 180 / Math.PI - child.setupRot - pNew * 180 / Math.PI;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// ── 贝塞尔(绝对控制点,4.2 语义) ──────────────────────────────────
|
|
201
|
+
// c = [cx1, cy1, cx2, cy2],控制点与 time / value 同单位。
|
|
202
|
+
function bezierValue(c, t0, v0, t1, v1, t) {
|
|
203
|
+
if (!c || c.length < 4) {
|
|
204
|
+
var lin = (t1 - t0) > 1e-9 ? (t - t0) / (t1 - t0) : 0;
|
|
205
|
+
return v0 + (v1 - v0) * lin;
|
|
206
|
+
}
|
|
207
|
+
var x1 = c[0], y1 = c[1], x2 = c[2], y2 = c[3];
|
|
208
|
+
var lo = 0, hi = 1, u = 0.5;
|
|
209
|
+
for (var i = 0; i < 24; i++) {
|
|
210
|
+
u = (lo + hi) / 2;
|
|
211
|
+
var mu = 1 - u;
|
|
212
|
+
var x = mu * mu * mu * t0 + 3 * mu * mu * u * x1 + 3 * mu * u * u * x2 + u * u * u * t1;
|
|
213
|
+
if (x < t) lo = u; else hi = u;
|
|
214
|
+
}
|
|
215
|
+
var v = 1 - u;
|
|
216
|
+
return v * v * v * v0 + 3 * v * v * u * y1 + 3 * v * u * u * y2 + u * u * u * v1;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function sampleTrack(frames, t, kind) {
|
|
220
|
+
if (!frames || !frames.length) return null;
|
|
221
|
+
if (frames.length === 1) return frames[0];
|
|
222
|
+
// 末帧之外保持末帧值;时长等于末帧时间时由外层取模,循环自然闭合。
|
|
223
|
+
if (t <= frames[0].time) return frames[0];
|
|
224
|
+
if (t >= frames[frames.length - 1].time) return frames[frames.length - 1];
|
|
225
|
+
for (var i = 0; i < frames.length - 1; i++) {
|
|
226
|
+
var a = frames[i], b = frames[i + 1];
|
|
227
|
+
if (t >= a.time && t <= b.time) {
|
|
228
|
+
if (a.curve === 'stepped') return a;
|
|
229
|
+
if (kind === 'rotate') {
|
|
230
|
+
return { value: bezierValue(a.curve, a.time, a.value || 0, b.time, b.value || 0, t) };
|
|
231
|
+
}
|
|
232
|
+
var cx = a.curve && a.curve.length >= 8 ? a.curve.slice(0, 4) : null;
|
|
233
|
+
var cy = a.curve && a.curve.length >= 8 ? a.curve.slice(4, 8) : null;
|
|
234
|
+
return {
|
|
235
|
+
x: bezierValue(cx, a.time, a.x || 0, b.time, b.x || 0, t),
|
|
236
|
+
y: bezierValue(cy, a.time, a.y || 0, b.time, b.y || 0, t)
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return frames[frames.length - 1];
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function durationOf(anim) {
|
|
244
|
+
var max = 0.0001;
|
|
245
|
+
var b = anim.bones || {};
|
|
246
|
+
Object.keys(b).forEach(function (name) {
|
|
247
|
+
Object.keys(b[name]).forEach(function (kind) {
|
|
248
|
+
(b[name][kind] || []).forEach(function (f) { if (f.time > max) max = f.time; });
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
return max;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function applyAnimation(t) {
|
|
255
|
+
boneList.forEach(function (b) { b.animX = 0; b.animY = 0; b.animRot = 0; });
|
|
256
|
+
var anim = SPINE.animations && SPINE.animations[current];
|
|
257
|
+
if (anim) {
|
|
258
|
+
var tracks = anim.bones || {};
|
|
259
|
+
Object.keys(tracks).forEach(function (name) {
|
|
260
|
+
var bone = bones[name];
|
|
261
|
+
if (!bone) return;
|
|
262
|
+
var tl = tracks[name];
|
|
263
|
+
if (tl.rotate) { var r = sampleTrack(tl.rotate, t, 'rotate'); bone.animRot = (r && r.value) || 0; }
|
|
264
|
+
if (tl.translate) { var v = sampleTrack(tl.translate, t, 'translate'); bone.animX = (v && v.x) || 0; bone.animY = (v && v.y) || 0; }
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
// 拖动过的 IK 目标点覆盖动画值:**拖动是用户的即时意图**,不该被动画轨道盖掉
|
|
268
|
+
// (目标骨一般没有自己的轨道,但「手被动画驱动、目标点没动」时必须以手为准)。
|
|
269
|
+
boneList.forEach(function (b) {
|
|
270
|
+
if (b.dragX === undefined) return;
|
|
271
|
+
b.animX = b.dragX - b.setupX;
|
|
272
|
+
b.animY = b.dragY - b.setupY;
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// ── 蒙皮网格 + FFD 变形(M5 的 L1 / L3)─────────────────────────────
|
|
277
|
+
//
|
|
278
|
+
// 有网格的部件走「逐三角形裁剪绘制」,没有的仍是一整块 drawImage。
|
|
279
|
+
// 分流的判据是 att.type === 'mesh'(骨架里写死的),不额外维护一张表。
|
|
280
|
+
//
|
|
281
|
+
// 顶点坐标在骨架里是「部件中心为原点」,与 region attachment 的 -w/2,-h/2 同一套系,
|
|
282
|
+
// 所以两种附件的变换链完全一样,这里只是把 drawImage 换成按三角形贴。
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* 当前时刻的顶点位移(FFD)。
|
|
286
|
+
*
|
|
287
|
+
* 与宿主 rigmesh.ts 的 buildWaveDeform **同一个公式**:越靠近固定端越不动,
|
|
288
|
+
* 相位按时间推进。两处必须一致,否则「预览里看到的」和「导出的」不是一回事。
|
|
289
|
+
*/
|
|
290
|
+
function deformOffsetsOf(name, att, t) {
|
|
291
|
+
var spec = DEFORMS[name];
|
|
292
|
+
if (!spec) return null;
|
|
293
|
+
var verts = att.vertices || [];
|
|
294
|
+
var height = att.height || 1;
|
|
295
|
+
var duration = spec.duration > 0 ? spec.duration : 1;
|
|
296
|
+
var phase = ((t / duration) % 1 + 1) % 1;
|
|
297
|
+
var cycles = spec.cycles || 1;
|
|
298
|
+
var amplitude = spec.amplitude || 0;
|
|
299
|
+
var dirX = Math.cos(spec.direction || 0);
|
|
300
|
+
var dirY = Math.sin(spec.direction || 0);
|
|
301
|
+
var out = new Array(verts.length);
|
|
302
|
+
for (var i = 0; i < verts.length; i += 2) {
|
|
303
|
+
// 顶点是中心坐标,换回「左上角为原点」才能算"离固定端多远"。
|
|
304
|
+
var ly = verts[i + 1] + height / 2;
|
|
305
|
+
var along = spec.anchor === 'bottom' ? 1 - ly / height : spec.anchor === 'none' ? 1 : ly / height;
|
|
306
|
+
// 相位只随时间推进;沿 y 变化的是权重。写成 sin(along·cycles + phase) 会让
|
|
307
|
+
// 不同高度在同一时刻朝相反方向走,整条裙子被横向扯开(第一版就是这个毛病)。
|
|
308
|
+
var sway = Math.sin(phase * cycles * Math.PI * 2);
|
|
309
|
+
var magnitude = amplitude * along * sway;
|
|
310
|
+
out[i] = dirX * magnitude;
|
|
311
|
+
out[i + 1] = dirY * magnitude;
|
|
312
|
+
}
|
|
313
|
+
return out;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** 三个源点(图片像素)→ 三个目标点(当前坐标系)的仿射矩阵。 */
|
|
317
|
+
function affineFromTriangles(s0, s1, s2, d0, d1, d2) {
|
|
318
|
+
var denom = (s1.x - s0.x) * (s2.y - s0.y) - (s2.x - s0.x) * (s1.y - s0.y);
|
|
319
|
+
if (Math.abs(denom) < 1e-9) return null;
|
|
320
|
+
var a = ((d1.x - d0.x) * (s2.y - s0.y) - (d2.x - d0.x) * (s1.y - s0.y)) / denom;
|
|
321
|
+
var b = ((d1.y - d0.y) * (s2.y - s0.y) - (d2.y - d0.y) * (s1.y - s0.y)) / denom;
|
|
322
|
+
var c = ((d2.x - d0.x) * (s1.x - s0.x) - (d1.x - d0.x) * (s2.x - s0.x)) / denom;
|
|
323
|
+
var d = ((d2.y - d0.y) * (s1.x - s0.x) - (d1.y - d0.y) * (s2.x - s0.x)) / denom;
|
|
324
|
+
return { a: a, b: b, c: c, d: d, e: d0.x - a * s0.x - c * s0.y, f: d0.y - b * s0.x - d * s0.y };
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* 把附件的顶点解成「**部件中心坐标系**里该画在哪」(也就是 drawMesh 用的那套)。
|
|
329
|
+
*
|
|
330
|
+
* 两种格式要分开处理,而它们只能靠**长度**区分:
|
|
331
|
+
* - 非加权:vertices 就是坐标对,直接可用(长度等于 uvs);
|
|
332
|
+
* - 加权(LBS):Spine 的交错流 [骨骼数, (骨骼下标, x, y, 权重) × 骨骼数, …],
|
|
333
|
+
* x/y 是**骨骼局部**坐标,得先用各骨骼的**当前**世界变换做线性混合,
|
|
334
|
+
* 再逐级逆回部件坐标系。
|
|
335
|
+
*
|
|
336
|
+
* 不分开的后果不是报错,是**画出一团乱线**:骨骼下标会被当成坐标用。
|
|
337
|
+
*/
|
|
338
|
+
function meshLocalVertices(att, bone) {
|
|
339
|
+
const raw = att.vertices || [];
|
|
340
|
+
const uvs = att.uvs || [];
|
|
341
|
+
if (raw.length === 0) return [];
|
|
342
|
+
if (raw.length === uvs.length) return raw;
|
|
343
|
+
|
|
344
|
+
const out = [];
|
|
345
|
+
const attRad = -((att.rotation || 0) * Math.PI) / 180;
|
|
346
|
+
const cosA = Math.cos(attRad);
|
|
347
|
+
const sinA = Math.sin(attRad);
|
|
348
|
+
const attX = att.x || 0;
|
|
349
|
+
const attY = att.y || 0;
|
|
350
|
+
let cursor = 0;
|
|
351
|
+
while (cursor < raw.length) {
|
|
352
|
+
const count = raw[cursor++];
|
|
353
|
+
let wx = 0;
|
|
354
|
+
let wy = 0;
|
|
355
|
+
for (let n = 0; n < count; n++) {
|
|
356
|
+
const index = raw[cursor++];
|
|
357
|
+
const x = raw[cursor++];
|
|
358
|
+
const y = raw[cursor++];
|
|
359
|
+
const weight = raw[cursor++];
|
|
360
|
+
const owner = bones[(SPINE.bones || [])[index]?.name];
|
|
361
|
+
if (!owner) continue;
|
|
362
|
+
wx += (Math.cos(owner.worldRot) * x - Math.sin(owner.worldRot) * y + owner.worldX) * weight;
|
|
363
|
+
wy += (Math.sin(owner.worldRot) * x + Math.cos(owner.worldRot) * y + owner.worldY) * weight;
|
|
364
|
+
}
|
|
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);
|
|
373
|
+
}
|
|
374
|
+
return out;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function drawMesh(bone, att, img, name, t) {
|
|
378
|
+
const verts = meshLocalVertices(att, bone);
|
|
379
|
+
const uvs = att.uvs || [];
|
|
380
|
+
const tris = att.triangles || [];
|
|
381
|
+
if (verts.length === 0 || tris.length === 0) return; // 加权流已在上一步解码成坐标
|
|
382
|
+
var width = att.width || 1;
|
|
383
|
+
var height = att.height || 1;
|
|
384
|
+
var offsets = deformOffsetsOf(name, att, t);
|
|
385
|
+
var halfW = width / 2;
|
|
386
|
+
var halfH = height / 2;
|
|
387
|
+
|
|
388
|
+
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
|
+
for (var k = 0; k + 2 < tris.length; k += 3) {
|
|
397
|
+
var i0 = tris[k] * 2, i1 = tris[k + 1] * 2, i2 = tris[k + 2] * 2;
|
|
398
|
+
var p0 = { x: verts[i0] + (offsets ? offsets[i0] : 0), y: -(verts[i0 + 1] + (offsets ? offsets[i0 + 1] : 0)) };
|
|
399
|
+
var p1 = { x: verts[i1] + (offsets ? offsets[i1] : 0), y: -(verts[i1 + 1] + (offsets ? offsets[i1 + 1] : 0)) };
|
|
400
|
+
var p2 = { x: verts[i2] + (offsets ? offsets[i2] : 0), y: -(verts[i2 + 1] + (offsets ? offsets[i2 + 1] : 0)) };
|
|
401
|
+
// 源点用**图片像素**:顶点是中心坐标,加上半宽半高就是图片里的位置。
|
|
402
|
+
var s0 = { x: verts[i0] + halfW, y: verts[i0 + 1] + halfH };
|
|
403
|
+
var s1 = { x: verts[i1] + halfW, y: verts[i1 + 1] + halfH };
|
|
404
|
+
var s2 = { x: verts[i2] + halfW, y: verts[i2 + 1] + halfH };
|
|
405
|
+
var m = affineFromTriangles(s0, s1, s2, p0, p1, p2);
|
|
406
|
+
if (m === null) continue;
|
|
407
|
+
// 三个顶点以重心为中心**外扩一点点**。
|
|
408
|
+
//
|
|
409
|
+
// 逐三角形 clip 之后,每条边的抗锯齿像素都是半透明的,相邻三角形拼起来
|
|
410
|
+
// 就会在共享边上留下一条亮白细线(实测裙子上能看出一整张网格)。外扩之后
|
|
411
|
+
// 三角形彼此重叠,细线被后画的三角形盖掉。源点不跟着扩——那会让纹理整体
|
|
412
|
+
// 放大一圈;这里只挪目标点,代价是每边约 0.5px 的轻微拉伸,看不出来。
|
|
413
|
+
var gx = (p0.x + p1.x + p2.x) / 3;
|
|
414
|
+
var gy = (p0.y + p1.y + p2.y) / 3;
|
|
415
|
+
var grow = 1 + 1.2 / Math.max(1, Math.min(width, height) * view.scale);
|
|
416
|
+
p0 = { x: gx + (p0.x - gx) * grow, y: gy + (p0.y - gy) * grow };
|
|
417
|
+
p1 = { x: gx + (p1.x - gx) * grow, y: gy + (p1.y - gy) * grow };
|
|
418
|
+
p2 = { x: gx + (p2.x - gx) * grow, y: gy + (p2.y - gy) * grow };
|
|
419
|
+
ctx.save();
|
|
420
|
+
ctx.beginPath();
|
|
421
|
+
ctx.moveTo(p0.x, p0.y);
|
|
422
|
+
ctx.lineTo(p1.x, p1.y);
|
|
423
|
+
ctx.lineTo(p2.x, p2.y);
|
|
424
|
+
ctx.closePath();
|
|
425
|
+
ctx.clip();
|
|
426
|
+
ctx.transform(m.a, m.b, m.c, m.d, m.e, m.f);
|
|
427
|
+
ctx.drawImage(img, 0, 0);
|
|
428
|
+
ctx.restore();
|
|
429
|
+
}
|
|
430
|
+
ctx.restore();
|
|
431
|
+
void uvs;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// ── Path 约束(M5)─────────────────────────────────────────────────
|
|
435
|
+
//
|
|
436
|
+
// 把一串骨骼沿一条折线**按弧长**铺开。用弧长而不是"按段索引",是因为段长不等时
|
|
437
|
+
// 按索引分会让骨骼在长段上稀疏、短段上挤成一团。
|
|
438
|
+
//
|
|
439
|
+
// 与宿主 rigpath.ts 是同一套公式(那边是 TS、这边是内联的经典脚本,无法共享模块),
|
|
440
|
+
// 所以两边的形状必须保持一样;差别只在宿主多做了「图像坐标 → 骨骼世界坐标」那一步。
|
|
441
|
+
|
|
442
|
+
function pathSegmentLengths(path) {
|
|
443
|
+
var count = path.points.length / 2;
|
|
444
|
+
var total = path.closed === true ? count : count - 1;
|
|
445
|
+
var out = [];
|
|
446
|
+
for (var i = 0; i < total; i++) {
|
|
447
|
+
var j = (i + 1) % count;
|
|
448
|
+
var dx = path.points[j * 2] - path.points[i * 2];
|
|
449
|
+
var dy = path.points[j * 2 + 1] - path.points[i * 2 + 1];
|
|
450
|
+
out.push(Math.sqrt(dx * dx + dy * dy));
|
|
451
|
+
}
|
|
452
|
+
return out;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
function samplePath(path, lengths, total, distance) {
|
|
456
|
+
var count = path.points.length / 2;
|
|
457
|
+
var remaining = Math.max(0, Math.min(total, distance));
|
|
458
|
+
for (var i = 0; i < lengths.length; i++) {
|
|
459
|
+
var length = lengths[i];
|
|
460
|
+
if (remaining <= length || i === lengths.length - 1) {
|
|
461
|
+
var t = length > 1e-9 ? remaining / length : 0;
|
|
462
|
+
var j = (i + 1) % count;
|
|
463
|
+
var x0 = path.points[i * 2], y0 = path.points[i * 2 + 1];
|
|
464
|
+
var x1 = path.points[j * 2], y1 = path.points[j * 2 + 1];
|
|
465
|
+
return { x: x0 + (x1 - x0) * t, y: y0 + (y1 - y0) * t, angle: Math.atan2(y1 - y0, x1 - x0) };
|
|
466
|
+
}
|
|
467
|
+
remaining -= length;
|
|
468
|
+
}
|
|
469
|
+
return { x: path.points[0], y: path.points[1], angle: 0 };
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/** 把一根骨搬到一个目标世界位置/朝向上(local = setup + anim,就近插值到目标)。 */
|
|
473
|
+
function placeBone(bone, targetX, targetY, targetRot, translateMix, rotateMix) {
|
|
474
|
+
var parent = bone.parentBone;
|
|
475
|
+
var px = parent ? parent.worldX : 0;
|
|
476
|
+
var py = parent ? parent.worldY : 0;
|
|
477
|
+
var pr = parent ? parent.worldRot : 0;
|
|
478
|
+
var dx = targetX - px, dy = targetY - py;
|
|
479
|
+
var c = Math.cos(-pr), s = Math.sin(-pr);
|
|
480
|
+
var wantX = dx * c - dy * s;
|
|
481
|
+
var wantY = dx * s + dy * c;
|
|
482
|
+
var wantRot = targetRot - pr;
|
|
483
|
+
var haveX = bone.setupX + bone.animX;
|
|
484
|
+
var haveY = bone.setupY + bone.animY;
|
|
485
|
+
var haveRot = (bone.setupRot + bone.animRot) * Math.PI / 180;
|
|
486
|
+
var mixT = typeof translateMix === 'number' ? translateMix : 1;
|
|
487
|
+
var mixR = typeof rotateMix === 'number' ? rotateMix : 1;
|
|
488
|
+
var nextX = haveX + (wantX - haveX) * mixT;
|
|
489
|
+
var nextY = haveY + (wantY - haveY) * mixT;
|
|
490
|
+
// 角度按**最短差**插值:不然「-179° → 179°」会绕一大圈。
|
|
491
|
+
var delta = wantRot - haveRot;
|
|
492
|
+
while (delta > Math.PI) delta -= Math.PI * 2;
|
|
493
|
+
while (delta < -Math.PI) delta += Math.PI * 2;
|
|
494
|
+
var nextRot = haveRot + delta * mixR;
|
|
495
|
+
bone.animX = nextX - bone.setupX;
|
|
496
|
+
bone.animY = nextY - bone.setupY;
|
|
497
|
+
bone.animRot = nextRot * 180 / Math.PI - bone.setupRot;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
function solvePaths() {
|
|
501
|
+
for (var name in PATHS) {
|
|
502
|
+
var spec = PATHS[name];
|
|
503
|
+
var chain = spec.bones || [];
|
|
504
|
+
if (chain.length === 0) continue;
|
|
505
|
+
var lengths = pathSegmentLengths(spec);
|
|
506
|
+
var total = 0;
|
|
507
|
+
for (var i = 0; i < lengths.length; i++) total += lengths[i];
|
|
508
|
+
if (total <= 1e-6) continue;
|
|
509
|
+
var spacing = spec.spacing > 0 ? spec.spacing : total / Math.max(1, chain.length - 1);
|
|
510
|
+
for (var k = 0; k < chain.length; k++) {
|
|
511
|
+
var bone = bones[chain[k]];
|
|
512
|
+
if (!bone) continue;
|
|
513
|
+
var at = samplePath(spec, lengths, total, k * spacing);
|
|
514
|
+
// 骨骼沿**局部 +y** 生长,朝向角 = 世界旋转 + 90°,所以反解减 π/2
|
|
515
|
+
// (与 IK 那条约定一致)。
|
|
516
|
+
placeBone(bone, at.x, at.y, at.angle - Math.PI / 2, spec.translateMix, spec.rotateMix);
|
|
517
|
+
// **放好一根就立刻刷新它的世界变换**:placeBone 是拿父级的当前世界位置
|
|
518
|
+
// 反算 local 的,而链上下一根骨的父级正是刚放好的这根——不刷新的话它读到的
|
|
519
|
+
// 还是旧位置,误差会沿着链累积(实测三根骨的末端偏了 39px)。
|
|
520
|
+
updateWorld(bone);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// ── 视图 ────────────────────────────────────────────────────────────
|
|
526
|
+
var view = { scale: 1, cx: 0, cy: 0 };
|
|
527
|
+
function fitView() {
|
|
528
|
+
var w = SPINE.skeleton.width || 512, h = SPINE.skeleton.height || 512;
|
|
529
|
+
var box = { minX: -w / 2, maxX: w / 2, minY: 0, maxY: h };
|
|
530
|
+
slots.forEach(function (slot) {
|
|
531
|
+
var att = skin[slot.name] && skin[slot.name][slot.attachment];
|
|
532
|
+
var bone = bones[slot.bone];
|
|
533
|
+
if (!att || !bone) return;
|
|
534
|
+
var rad = bone.worldRot + ((att.rotation || 0) * Math.PI / 180);
|
|
535
|
+
var hw = att.width / 2, hh = att.height / 2;
|
|
536
|
+
for (var sx = -1; sx <= 1; sx += 2) for (var sy = -1; sy <= 1; sy += 2) {
|
|
537
|
+
var lx = att.x + (sx * hw * Math.cos(rad) - sy * hh * Math.sin(rad));
|
|
538
|
+
var ly = att.y + (sx * hw * Math.sin(rad) + sy * hh * Math.cos(rad));
|
|
539
|
+
var pr = bone.worldRot, c = Math.cos(pr), s = Math.sin(pr);
|
|
540
|
+
var wx = bone.worldX + lx * c - ly * s;
|
|
541
|
+
var wy = bone.worldY + lx * s + ly * c;
|
|
542
|
+
if (wx < box.minX) box.minX = wx;
|
|
543
|
+
if (wx > box.maxX) box.maxX = wx;
|
|
544
|
+
if (wy < box.minY) box.minY = wy;
|
|
545
|
+
if (wy > box.maxY) box.maxY = wy;
|
|
546
|
+
}
|
|
547
|
+
});
|
|
548
|
+
var bw = Math.max(32, box.maxX - box.minX);
|
|
549
|
+
var bh = Math.max(32, box.maxY - box.minY);
|
|
550
|
+
var cw = Math.min(760, Math.max(300, Math.round(bw * 1.9)));
|
|
551
|
+
var ch = Math.round(cw * (bh / bw) * 1.12);
|
|
552
|
+
canvas.width = cw;
|
|
553
|
+
canvas.height = Math.min(820, Math.max(260, ch));
|
|
554
|
+
var margin = 26;
|
|
555
|
+
var sc = Math.min((canvas.width - margin * 2) / bw, (canvas.height - margin * 2) / bh);
|
|
556
|
+
view.scale = sc;
|
|
557
|
+
view.cx = canvas.width / 2 - ((box.minX + box.maxX) / 2) * sc;
|
|
558
|
+
view.cy = canvas.height / 2 + ((box.minY + box.maxY) / 2) * sc;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
var showBones = false, showGrid = false;
|
|
562
|
+
|
|
563
|
+
function render(t) {
|
|
564
|
+
applyAnimation(t);
|
|
565
|
+
boneList.forEach(function (b) { if (!b.parentBone) updateWorld(b); });
|
|
566
|
+
// IK 要用**未约束**的姿态当基准(软 IK 的插值起点),所以先算一遍世界变换、
|
|
567
|
+
// 求解、再算一遍让约束结果生效。
|
|
568
|
+
solveIk();
|
|
569
|
+
boneList.forEach(function (b) { if (!b.parentBone) updateWorld(b); });
|
|
570
|
+
// Path 同理:它读父级的世界位置来反算 local,所以必须排在 updateWorld 之后;
|
|
571
|
+
// 链上父在前,就地算完即可,最后统一再刷一次世界变换。
|
|
572
|
+
solvePaths();
|
|
573
|
+
boneList.forEach(function (b) { if (!b.parentBone) updateWorld(b); });
|
|
574
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
575
|
+
|
|
576
|
+
if (showGrid) {
|
|
577
|
+
ctx.save();
|
|
578
|
+
ctx.strokeStyle = 'rgba(120,140,200,.18)';
|
|
579
|
+
ctx.lineWidth = 1;
|
|
580
|
+
for (var x = 0; x < canvas.width; x += 24) { ctx.beginPath(); ctx.moveTo(x, 0); ctx.lineTo(x, canvas.height); ctx.stroke(); }
|
|
581
|
+
for (var y = 0; y < canvas.height; y += 24) { ctx.beginPath(); ctx.moveTo(0, y); ctx.lineTo(canvas.width, y); ctx.stroke(); }
|
|
582
|
+
ctx.restore();
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
ctx.save();
|
|
586
|
+
ctx.translate(view.cx, view.cy);
|
|
587
|
+
ctx.scale(view.scale, -view.scale);
|
|
588
|
+
|
|
589
|
+
slots.forEach(function (slot) {
|
|
590
|
+
var bone = bones[slot.bone];
|
|
591
|
+
if (!bone) return;
|
|
592
|
+
var atts = skin[slot.name];
|
|
593
|
+
if (!atts) return;
|
|
594
|
+
var att = atts[slot.attachment];
|
|
595
|
+
if (!att) return;
|
|
596
|
+
var img = images[slot.name];
|
|
597
|
+
if (!img || !img.complete || !img.naturalWidth) return;
|
|
598
|
+
if (att.type === 'mesh') { drawMesh(bone, att, img, slot.name, t); return; }
|
|
599
|
+
ctx.save();
|
|
600
|
+
ctx.translate(bone.worldX, bone.worldY);
|
|
601
|
+
ctx.rotate(bone.worldRot);
|
|
602
|
+
ctx.translate(att.x || 0, att.y || 0);
|
|
603
|
+
ctx.rotate((att.rotation || 0) * Math.PI / 180);
|
|
604
|
+
ctx.scale(1, -1);
|
|
605
|
+
ctx.drawImage(img, -(att.width / 2), -(att.height / 2), att.width, att.height);
|
|
606
|
+
ctx.restore();
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
if (showBones) {
|
|
610
|
+
boneList.forEach(function (b) {
|
|
611
|
+
ctx.save();
|
|
612
|
+
ctx.translate(b.worldX, b.worldY);
|
|
613
|
+
ctx.rotate(b.worldRot);
|
|
614
|
+
if (b.length > 0) {
|
|
615
|
+
ctx.beginPath();
|
|
616
|
+
ctx.moveTo(0, 0);
|
|
617
|
+
ctx.lineTo(0, b.length);
|
|
618
|
+
ctx.strokeStyle = 'rgba(80,230,140,.85)';
|
|
619
|
+
ctx.lineWidth = 1.6 / view.scale;
|
|
620
|
+
ctx.stroke();
|
|
621
|
+
}
|
|
622
|
+
ctx.beginPath();
|
|
623
|
+
ctx.arc(0, 0, 3.2 / view.scale, 0, Math.PI * 2);
|
|
624
|
+
ctx.fillStyle = 'rgba(255,90,90,.95)';
|
|
625
|
+
ctx.fill();
|
|
626
|
+
ctx.restore();
|
|
627
|
+
ctx.save();
|
|
628
|
+
ctx.translate(b.worldX, b.worldY);
|
|
629
|
+
ctx.scale(1, -1);
|
|
630
|
+
ctx.fillStyle = 'rgba(255,255,255,.62)';
|
|
631
|
+
ctx.font = (9 / view.scale) + 'px sans-serif';
|
|
632
|
+
ctx.fillText(b.name, 5 / view.scale, -3 / view.scale);
|
|
633
|
+
ctx.restore();
|
|
634
|
+
});
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
// IK 目标点:画成显眼的菱形——它是**可拖的**,也就是验收判据里那个「目标点」。
|
|
638
|
+
IKS.forEach(function (c) {
|
|
639
|
+
var t = bones[c.target];
|
|
640
|
+
if (!t) return;
|
|
641
|
+
ctx.save();
|
|
642
|
+
ctx.translate(t.worldX, t.worldY);
|
|
643
|
+
ctx.scale(1, -1); // 抵消全局的 y 翻转,形状才是正立的
|
|
644
|
+
var r = 6 / view.scale;
|
|
645
|
+
ctx.beginPath();
|
|
646
|
+
ctx.moveTo(0, -r); ctx.lineTo(r, 0); ctx.lineTo(0, r); ctx.lineTo(-r, 0);
|
|
647
|
+
ctx.closePath();
|
|
648
|
+
ctx.fillStyle = 'rgba(90,220,255,.92)';
|
|
649
|
+
ctx.fill();
|
|
650
|
+
ctx.strokeStyle = 'rgba(16,58,88,.95)';
|
|
651
|
+
ctx.lineWidth = 1.2 / view.scale;
|
|
652
|
+
ctx.stroke();
|
|
653
|
+
ctx.restore();
|
|
654
|
+
});
|
|
655
|
+
|
|
656
|
+
ctx.restore();
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
// ── 播放控制 ────────────────────────────────────────────────────────
|
|
660
|
+
var playing = true, animTime = 0, last = 0, scrubLock = false;
|
|
661
|
+
var scrub = document.getElementById('scrub');
|
|
662
|
+
var tip = document.getElementById('tip');
|
|
663
|
+
|
|
664
|
+
function duration() {
|
|
665
|
+
var anim = SPINE.animations && SPINE.animations[current];
|
|
666
|
+
return anim ? durationOf(anim) : 1;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
function tick(ts) {
|
|
670
|
+
if (!last) last = ts;
|
|
671
|
+
var dt = Math.min(0.05, (ts - last) / 1000);
|
|
672
|
+
last = ts;
|
|
673
|
+
var dur = duration();
|
|
674
|
+
if (playing) {
|
|
675
|
+
animTime = (animTime + dt) % dur;
|
|
676
|
+
if (!scrubLock) scrub.value = String(Math.round((animTime / dur) * 1000));
|
|
677
|
+
}
|
|
678
|
+
render(animTime);
|
|
679
|
+
requestAnimationFrame(tick);
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
function updateTip() {
|
|
683
|
+
var anim = SPINE.animations && SPINE.animations[current];
|
|
684
|
+
if (!anim) { tip.textContent = '(没有动画数据)'; return; }
|
|
685
|
+
var count = Object.keys(anim.bones || {}).length;
|
|
686
|
+
tip.textContent = '时长 ' + durationOf(anim).toFixed(2) + ' 秒 · 驱动 ' + count + ' 根骨骼 · 共 ' + ANIMS.length + ' 个动作';
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
Object.entries(IMAGE_DATA).forEach(function (entry) {
|
|
690
|
+
var img = new Image();
|
|
691
|
+
img.onload = img.onerror = function () {
|
|
692
|
+
loaded++;
|
|
693
|
+
if (loaded === total) { fitView(); updateTip(); requestAnimationFrame(tick); }
|
|
694
|
+
};
|
|
695
|
+
img.src = entry[1];
|
|
696
|
+
images[entry[0]] = img;
|
|
697
|
+
});
|
|
698
|
+
if (total === 0) { fitView(); updateTip(); requestAnimationFrame(tick); }
|
|
699
|
+
|
|
700
|
+
var box = document.getElementById('anims');
|
|
701
|
+
ANIMS.forEach(function (name) {
|
|
702
|
+
var btn = document.createElement('button');
|
|
703
|
+
btn.textContent = name;
|
|
704
|
+
if (name === current) btn.className = 'active';
|
|
705
|
+
btn.onclick = function () {
|
|
706
|
+
current = name;
|
|
707
|
+
animTime = 0;
|
|
708
|
+
Array.prototype.forEach.call(box.children, function (c) { c.className = ''; });
|
|
709
|
+
btn.className = 'active';
|
|
710
|
+
updateTip();
|
|
711
|
+
};
|
|
712
|
+
box.appendChild(btn);
|
|
713
|
+
});
|
|
714
|
+
|
|
715
|
+
document.getElementById('play').onclick = function () {
|
|
716
|
+
playing = !playing;
|
|
717
|
+
this.textContent = playing ? '⏸ 暂停' : '▶ 播放';
|
|
718
|
+
};
|
|
719
|
+
document.getElementById('bones').onclick = function () {
|
|
720
|
+
showBones = !showBones;
|
|
721
|
+
this.className = showBones ? 'active' : '';
|
|
722
|
+
};
|
|
723
|
+
document.getElementById('grid').onclick = function () {
|
|
724
|
+
showGrid = !showGrid;
|
|
725
|
+
this.className = showGrid ? 'active' : '';
|
|
726
|
+
};
|
|
727
|
+
scrub.addEventListener('input', function () {
|
|
728
|
+
animTime = duration() * (Number(this.value) / 1000);
|
|
729
|
+
});
|
|
730
|
+
scrub.addEventListener('pointerdown', function () { scrubLock = true; });
|
|
731
|
+
scrub.addEventListener('pointerup', function () { scrubLock = false; });
|
|
732
|
+
// ── 拖动 IK 目标点 ──────────────────────────────────────────────────
|
|
733
|
+
//
|
|
734
|
+
// 验收判据就是这一条:「把一只手约束到目标点,拖动目标点时手跟随」。
|
|
735
|
+
// 所以预览页必须能直接拖——只能去编辑器里改数字的话,这条判据等于没验。
|
|
736
|
+
var draggingTarget = null;
|
|
737
|
+
function toWorldPoint(event) {
|
|
738
|
+
var rect = canvas.getBoundingClientRect();
|
|
739
|
+
var sx = (event.clientX - rect.left) * (canvas.width / Math.max(1, rect.width));
|
|
740
|
+
var sy = (event.clientY - rect.top) * (canvas.height / Math.max(1, rect.height));
|
|
741
|
+
// 与 render 里的变换严格互逆:屏幕 = (cx + wx·s, cy − wy·s)
|
|
742
|
+
return { x: (sx - view.cx) / view.scale, y: (view.cy - sy) / view.scale };
|
|
743
|
+
}
|
|
744
|
+
canvas.addEventListener('mousedown', function (event) {
|
|
745
|
+
if (IKS.length === 0) return;
|
|
746
|
+
var point = toWorldPoint(event);
|
|
747
|
+
var best = null;
|
|
748
|
+
var bestDist = 26 / view.scale;
|
|
749
|
+
IKS.forEach(function (c) {
|
|
750
|
+
var t = bones[c.target];
|
|
751
|
+
if (!t) return;
|
|
752
|
+
var d = Math.sqrt(Math.pow(t.worldX - point.x, 2) + Math.pow(t.worldY - point.y, 2));
|
|
753
|
+
if (d < bestDist) { bestDist = d; best = t; }
|
|
754
|
+
});
|
|
755
|
+
if (best !== null) {
|
|
756
|
+
draggingTarget = best;
|
|
757
|
+
canvas.style.cursor = 'grabbing';
|
|
758
|
+
event.preventDefault();
|
|
759
|
+
}
|
|
760
|
+
});
|
|
761
|
+
window.addEventListener('mousemove', function (event) {
|
|
762
|
+
if (draggingTarget === null) return;
|
|
763
|
+
var point = toWorldPoint(event);
|
|
764
|
+
draggingTarget.dragX = point.x;
|
|
765
|
+
draggingTarget.dragY = point.y;
|
|
766
|
+
event.preventDefault();
|
|
767
|
+
});
|
|
768
|
+
window.addEventListener('mouseup', function () {
|
|
769
|
+
if (draggingTarget === null) return;
|
|
770
|
+
draggingTarget = null;
|
|
771
|
+
canvas.style.cursor = '';
|
|
772
|
+
});
|
|
773
|
+
|
|
774
|
+
// 只读验收钩子:自动化测试要能问「链末端的骨尖现在在哪」,
|
|
775
|
+
// 否则「拖动目标点时手跟随」这条判据只能靠肉眼看截图。
|
|
776
|
+
// 只暴露读接口,不提供任何写入口——验收用的东西不该能改状态。
|
|
777
|
+
window.__gmmPreview = {
|
|
778
|
+
bones: function () {
|
|
779
|
+
var out = {};
|
|
780
|
+
boneList.forEach(function (b) {
|
|
781
|
+
out[b.name] = { x: b.worldX, y: b.worldY, rot: b.worldRot, length: b.length };
|
|
782
|
+
});
|
|
783
|
+
return out;
|
|
784
|
+
},
|
|
785
|
+
iks: function () {
|
|
786
|
+
return IKS.map(function (c) { return { name: c.name, target: c.target, bones: c.bones.slice(), mix: c.mix }; });
|
|
787
|
+
},
|
|
788
|
+
/** 骨骼在 canvas 里的像素坐标(拖动验收要用它算出鼠标该落在哪)。 */
|
|
789
|
+
screen: function (name) {
|
|
790
|
+
var b = bones[name];
|
|
791
|
+
if (!b) return null;
|
|
792
|
+
return { x: view.cx + b.worldX * view.scale, y: view.cy - b.worldY * view.scale };
|
|
793
|
+
},
|
|
794
|
+
/** 链末端的**骨尖**世界坐标——它应该落在目标点上。 */
|
|
795
|
+
tip: function (constraintName) {
|
|
796
|
+
for (var i = 0; i < IKS.length; i++) {
|
|
797
|
+
if (constraintName !== undefined && IKS[i].name !== constraintName) continue;
|
|
798
|
+
var chain = IKS[i].bones;
|
|
799
|
+
var last = bones[chain[chain.length - 1]];
|
|
800
|
+
if (!last) continue;
|
|
801
|
+
return { x: last.worldX - Math.sin(last.worldRot) * last.length, y: last.worldY + Math.cos(last.worldRot) * last.length };
|
|
802
|
+
}
|
|
803
|
+
return null;
|
|
804
|
+
}
|
|
805
|
+
};
|
|
806
|
+
|
|
807
|
+
window.addEventListener('resize', function () { fitView(); render(animTime); });
|
|
808
|
+
})();
|
|
809
|
+
</script>
|
|
810
|
+
</body>
|
|
811
|
+
</html>
|
|
812
|
+
`;
|
|
813
|
+
}
|
|
814
|
+
function escapeHtml(value) {
|
|
815
|
+
return value.replace(/[&<>"']/g, (char) => char === "&" ? "&" : char === "<" ? "<" : char === ">" ? ">" : char === '"' ? """ : "'");
|
|
816
|
+
}
|