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/rigmesh.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 蒙皮网格(M5 的 L1)与自动权重(L0)。
|
|
3
|
+
*
|
|
4
|
+
* 纯计算,不碰文件系统——网格与权重是「算出来的」,能离线断言就别放到运行时里试。
|
|
5
|
+
*
|
|
6
|
+
* 为什么需要它:**裙摆、披风、长发这类部件用刚体骨骼是动不好的**。一根骨骼只能
|
|
7
|
+
* 让整块贴图绕原点转,而裙摆要的是「上缘几乎不动、下缘甩出去」——必须在部件内部
|
|
8
|
+
* 细分顶点、让不同顶点受不同骨骼影响(或者被 FFD 直接搬走)。
|
|
9
|
+
*
|
|
10
|
+
* 网格用**规则三角化**而不是自适应三角化:规则网格的顶点是确定的(同样的输入永远
|
|
11
|
+
* 同样的坐标),FFD 编辑器里「用户拖的是哪个顶点」才有稳定含义;自适应三角化每次
|
|
12
|
+
* 跑出来的拓扑可能不同,存下来的 deform 会直接对不上号。
|
|
13
|
+
*
|
|
14
|
+
* 输出契约抄 DragonBones 的自包含设计:只要产出
|
|
15
|
+
* `{vertices, uvs, triangles, weights, slotPose, bonePose}` 这六样,蒙皮就闭环,
|
|
16
|
+
* 运行时不需要外部绑定姿势。这里的 `vertices/uvs/triangles` 都在**部件局部坐标系**
|
|
17
|
+
* (左上角为原点),`slotPose/bonePose` 由导出器按当时的骨骼世界矩阵现算。
|
|
18
|
+
*/
|
|
19
|
+
/** 网格密度上限:16×16 已经 289 个顶点,再多预览里的逐三角形裁剪会明显掉帧。 */
|
|
20
|
+
export const MESH_MAX_DIVISIONS = 16;
|
|
21
|
+
/** 裙摆这类部件用 6×6 就够软;太密只会让 FFD 变难拖。 */
|
|
22
|
+
export const MESH_DEFAULT_DIVISIONS = 6;
|
|
23
|
+
/** 每个顶点最多受几根骨骼影响(DragonBones 的常见上限是 4)。 */
|
|
24
|
+
export const MESH_MAX_INFLUENCES = 3;
|
|
25
|
+
function clampDivisions(value, fallback) {
|
|
26
|
+
const parsed = typeof value === "number" && Number.isFinite(value) ? Math.round(value) : fallback;
|
|
27
|
+
return Math.max(1, Math.min(MESH_MAX_DIVISIONS, parsed));
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 生成规则网格:`cols × rows` 个格子、`(cols+1) × (rows+1)` 个顶点、`cols × rows × 2` 个三角形。
|
|
31
|
+
*
|
|
32
|
+
* 顶点顺序是**逐行**的(先第一行从左到右),所以「第 i 个顶点」在编辑器里位置确定。
|
|
33
|
+
*/
|
|
34
|
+
export function buildRigMesh(options) {
|
|
35
|
+
const width = Math.max(1, options.width);
|
|
36
|
+
const height = Math.max(1, options.height);
|
|
37
|
+
const cols = clampDivisions(options.cols, MESH_DEFAULT_DIVISIONS);
|
|
38
|
+
const rows = clampDivisions(options.rows, MESH_DEFAULT_DIVISIONS);
|
|
39
|
+
const maxInfluences = Math.max(1, Math.min(MESH_MAX_INFLUENCES, options.maxInfluences ?? MESH_MAX_INFLUENCES));
|
|
40
|
+
const falloff = options.falloff ?? 2;
|
|
41
|
+
const vertices = [];
|
|
42
|
+
const uvs = [];
|
|
43
|
+
const weights = [];
|
|
44
|
+
const influenceCount = Math.min(maxInfluences, Math.max(1, options.bones.length));
|
|
45
|
+
for (let row = 0; row <= rows; row++) {
|
|
46
|
+
const v = row / rows;
|
|
47
|
+
for (let col = 0; col <= cols; col++) {
|
|
48
|
+
const u = col / cols;
|
|
49
|
+
const localX = u * width;
|
|
50
|
+
const localY = v * height;
|
|
51
|
+
vertices.push(Number(localX.toFixed(4)), Number(localY.toFixed(4)));
|
|
52
|
+
uvs.push(Number(u.toFixed(6)), Number(v.toFixed(6)));
|
|
53
|
+
if (options.bones.length === 0) {
|
|
54
|
+
weights.push([]);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
// 顶点在画布上的位置:离哪根骨骼近就多受谁影响。
|
|
58
|
+
const worldX = options.x + localX;
|
|
59
|
+
const worldY = options.y + localY;
|
|
60
|
+
const scored = options.bones
|
|
61
|
+
.map((bone, index) => {
|
|
62
|
+
const dx = worldX - bone.x;
|
|
63
|
+
const dy = worldY - bone.y;
|
|
64
|
+
return { bone: index, distance: Math.sqrt(dx * dx + dy * dy) };
|
|
65
|
+
})
|
|
66
|
+
.sort((a, b) => a.distance - b.distance)
|
|
67
|
+
.slice(0, influenceCount);
|
|
68
|
+
// 反距离加权:恰好落在骨骼上时退化成「只受这一根影响」。
|
|
69
|
+
let total = 0;
|
|
70
|
+
const raw = scored.map((entry) => {
|
|
71
|
+
const value = entry.distance <= 1e-3 ? 1e6 : 1 / Math.pow(entry.distance, falloff);
|
|
72
|
+
total += value;
|
|
73
|
+
return { bone: entry.bone, weight: value };
|
|
74
|
+
});
|
|
75
|
+
weights.push(raw
|
|
76
|
+
.map((entry) => ({ bone: entry.bone, weight: Number((entry.weight / total).toFixed(6)) }))
|
|
77
|
+
.filter((entry) => entry.weight > 0));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const triangles = [];
|
|
81
|
+
for (let row = 0; row < rows; row++) {
|
|
82
|
+
for (let col = 0; col < cols; col++) {
|
|
83
|
+
const topLeft = row * (cols + 1) + col;
|
|
84
|
+
const topRight = topLeft + 1;
|
|
85
|
+
const bottomLeft = topLeft + (cols + 1);
|
|
86
|
+
const bottomRight = bottomLeft + 1;
|
|
87
|
+
triangles.push(topLeft, topRight, bottomLeft);
|
|
88
|
+
triangles.push(topRight, bottomRight, bottomLeft);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
cols,
|
|
93
|
+
rows,
|
|
94
|
+
vertices,
|
|
95
|
+
uvs,
|
|
96
|
+
triangles,
|
|
97
|
+
bones: options.bones.map((bone) => bone.name),
|
|
98
|
+
weights
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
/** 顶点个数。 */
|
|
102
|
+
export function meshVertexCount(mesh) {
|
|
103
|
+
return mesh.vertices.length / 2;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* 把一份顶点位移(FFD)按双线性插值到一个任意局部坐标上。
|
|
107
|
+
*
|
|
108
|
+
* 预览器要把「顶点位移」变成「整张贴图的形变」,最常见的做法是逐三角形裁剪绘制;
|
|
109
|
+
* 这个函数给的是**任意点**的位移,用于反查某个坐标被搬到了哪里(测试与调试用)。
|
|
110
|
+
*/
|
|
111
|
+
export function sampleDeform(mesh, offsets, localX, localY, width, height) {
|
|
112
|
+
if (offsets === undefined || offsets.length !== mesh.vertices.length)
|
|
113
|
+
return { x: localX, y: localY };
|
|
114
|
+
const u = Math.max(0, Math.min(1, localX / Math.max(1e-6, width))) * mesh.cols;
|
|
115
|
+
const v = Math.max(0, Math.min(1, localY / Math.max(1e-6, height))) * mesh.rows;
|
|
116
|
+
const col = Math.min(mesh.cols - 1, Math.floor(u));
|
|
117
|
+
const row = Math.min(mesh.rows - 1, Math.floor(v));
|
|
118
|
+
const fx = u - col;
|
|
119
|
+
const fy = v - row;
|
|
120
|
+
const at = (c, r) => {
|
|
121
|
+
const index = (r * (mesh.cols + 1) + c) * 2;
|
|
122
|
+
return { x: offsets[index], y: offsets[index + 1] };
|
|
123
|
+
};
|
|
124
|
+
const a = at(col, row);
|
|
125
|
+
const b = at(col + 1, row);
|
|
126
|
+
const c = at(col, row + 1);
|
|
127
|
+
const d = at(col + 1, row + 1);
|
|
128
|
+
const top = { x: a.x + (b.x - a.x) * fx, y: a.y + (b.y - a.y) * fx };
|
|
129
|
+
const bottom = { x: c.x + (d.x - c.x) * fx, y: c.y + (d.y - c.y) * fx };
|
|
130
|
+
return { x: localX + top.x + (bottom.x - top.x) * fy, y: localY + top.y + (bottom.y - top.y) * fy };
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* 生成一段"飘动"的 FFD 位移:越靠近**远端**(远离固定端的那一侧)位移越大。
|
|
134
|
+
*
|
|
135
|
+
* 用正弦而不是随机:循环动画的首尾必须闭合(与动画预设同一个要求),
|
|
136
|
+
* 而且「摆动」这件事本来就该是周期函数。
|
|
137
|
+
*
|
|
138
|
+
* ⚠️ **相位只随时间推进,不随高度变化**。第一版写成 `sin(along·cycles + phase)`,
|
|
139
|
+
* 于是同一时刻不同高度的位移方向不同——相邻行被反向拉扯,整条裙子会**横向撑开**
|
|
140
|
+
* 变成一个气球,而不是左右摆。现在 `along` 只作为**权重**(上缘 0、下缘 1),
|
|
141
|
+
* `cycles` 变成「一个循环里摆几次」。
|
|
142
|
+
*/
|
|
143
|
+
export function buildWaveDeform(options) {
|
|
144
|
+
const { mesh, width, height } = options;
|
|
145
|
+
const amplitude = options.amplitude ?? Math.max(6, Math.min(width, height) * 0.06);
|
|
146
|
+
const phase = options.phase ?? 0;
|
|
147
|
+
const direction = options.direction ?? 0;
|
|
148
|
+
const cycles = options.cycles ?? 1;
|
|
149
|
+
const offsets = [];
|
|
150
|
+
let maxDisplacement = 0;
|
|
151
|
+
const count = meshVertexCount(mesh);
|
|
152
|
+
for (let i = 0; i < count; i++) {
|
|
153
|
+
const x = mesh.vertices[i * 2];
|
|
154
|
+
const y = mesh.vertices[i * 2 + 1];
|
|
155
|
+
// 权重:离"固定端"越远越自由(0 = 完全不动)。
|
|
156
|
+
const along = options.anchor === "bottom" ? 1 - y / Math.max(1e-6, height) : options.anchor === "none" ? 1 : y / Math.max(1e-6, height);
|
|
157
|
+
// **相位只跟时间有关**(见函数头的注释):沿 y 变化的是权重,不是相位。
|
|
158
|
+
const sway = Math.sin((phase * cycles) * Math.PI * 2);
|
|
159
|
+
const magnitude = amplitude * along * sway;
|
|
160
|
+
const dx = Math.cos(direction) * magnitude;
|
|
161
|
+
const dy = Math.sin(direction) * magnitude;
|
|
162
|
+
offsets.push(Number(dx.toFixed(4)), Number(dy.toFixed(4)));
|
|
163
|
+
maxDisplacement = Math.max(maxDisplacement, Math.abs(magnitude));
|
|
164
|
+
void x;
|
|
165
|
+
}
|
|
166
|
+
return { offsets, maxDisplacement: Number(maxDisplacement.toFixed(4)) };
|
|
167
|
+
}
|
package/lib/rigpath.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Path 约束的几何层(M5)。
|
|
3
|
+
*
|
|
4
|
+
* **只做弧长表这一档**,这是方案 §8.2 明确划的范围,也是三条导出路径(Spine /
|
|
5
|
+
* DragonBones / 我们的预览器)都能表达的最小共同集:路径是一串折线点,
|
|
6
|
+
* 骨骼沿弧长均匀(或按固定间距)铺上去,每根骨取所在位置的切线。
|
|
7
|
+
*
|
|
8
|
+
* 不做贝塞尔路径:Spine 的 PathAttachment 存的本来就是**采样后的折线**
|
|
9
|
+
* (`vertices` + `lengths`),编辑器里的曲线在导出前也会被压平。我们直接收折线,
|
|
10
|
+
* 少一层「曲线 → 折线」的采样,也就少一个「预览和导出不一样」的可能。
|
|
11
|
+
*
|
|
12
|
+
* 坐标系与部件一致:**参考图像素、Y 向下**。转成骨骼世界坐标由调用方负责
|
|
13
|
+
* (`toWorld` 那一步),这里只做纯几何。
|
|
14
|
+
*/
|
|
15
|
+
/** 至少要两个点才谈得上"路径"。 */
|
|
16
|
+
export function normalizePath(raw) {
|
|
17
|
+
if (raw === null || typeof raw !== "object")
|
|
18
|
+
return undefined;
|
|
19
|
+
const value = raw;
|
|
20
|
+
const source = Array.isArray(value.points) ? value.points : [];
|
|
21
|
+
// 扁平数组 `[x0,y0,x1,y1,…]`:**内部表示就是这个形状**(`RigPathEntry.points` 是扁平的),
|
|
22
|
+
// wire 上传过来时也是最省事的一种。另外两种写法是给人手写 JSON 用的。
|
|
23
|
+
if (source.length >= 4 && source.every((entry) => typeof entry === "number" && Number.isFinite(entry))) {
|
|
24
|
+
return { points: source.map((n) => Number(n.toFixed(3))), closed: value.closed === true };
|
|
25
|
+
}
|
|
26
|
+
const flat = [];
|
|
27
|
+
for (const entry of source) {
|
|
28
|
+
// 接受 `[x,y]` 与 `{x,y}` 两种写法:前者给人看,后者给 JSON 手改时更清楚。
|
|
29
|
+
if (Array.isArray(entry) && entry.length >= 2 && Number.isFinite(entry[0]) && Number.isFinite(entry[1])) {
|
|
30
|
+
flat.push(Number(entry[0]), Number(entry[1]));
|
|
31
|
+
}
|
|
32
|
+
else if (entry !== null && typeof entry === "object" && Number.isFinite(entry.x) && Number.isFinite(entry.y)) {
|
|
33
|
+
flat.push(Number(entry.x), Number(entry.y));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (flat.length < 4)
|
|
37
|
+
return undefined;
|
|
38
|
+
return { points: flat.map((n) => Number(n.toFixed(3))), closed: value.closed === true };
|
|
39
|
+
}
|
|
40
|
+
/** 折线的段数(闭合时多一段)。 */
|
|
41
|
+
export function pathSegmentCount(path) {
|
|
42
|
+
const count = path.points.length / 2;
|
|
43
|
+
return path.closed === true ? count : count - 1;
|
|
44
|
+
}
|
|
45
|
+
/** 每段的长度——**这就是 Spine 的 `PathAttachment.lengths`**。 */
|
|
46
|
+
export function pathSegmentLengths(path) {
|
|
47
|
+
const count = path.points.length / 2;
|
|
48
|
+
const segments = [];
|
|
49
|
+
const total = pathSegmentCount(path);
|
|
50
|
+
for (let i = 0; i < total; i++) {
|
|
51
|
+
const j = (i + 1) % count;
|
|
52
|
+
const dx = path.points[j * 2] - path.points[i * 2];
|
|
53
|
+
const dy = path.points[j * 2 + 1] - path.points[i * 2 + 1];
|
|
54
|
+
segments.push(Number(Math.hypot(dx, dy).toFixed(4)));
|
|
55
|
+
}
|
|
56
|
+
return segments;
|
|
57
|
+
}
|
|
58
|
+
export function pathTotalLength(path, lengths) {
|
|
59
|
+
const segments = lengths ?? pathSegmentLengths(path);
|
|
60
|
+
return Number(segments.reduce((sum, value) => sum + value, 0).toFixed(4));
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* 按**弧长**采样:给出沿路径走 `distance` 之后的位置与切线角(弧度)。
|
|
64
|
+
*
|
|
65
|
+
* 用弧长而不是"按段索引"是因为骨骼要**等距**铺开:段长不等时按索引分会让骨骼在
|
|
66
|
+
* 长段上稀疏、短段上挤成一团。`distance` 会被夹进 `[0, 总长]`,越界不报错——
|
|
67
|
+
* 调用方(排列骨链)本来就会算出越界值,那时"停在端点"才是想要的行为。
|
|
68
|
+
*/
|
|
69
|
+
export function samplePath(path, distance, lengths, total) {
|
|
70
|
+
const segments = lengths ?? pathSegmentLengths(path);
|
|
71
|
+
const sum = total ?? segments.reduce((acc, value) => acc + value, 0);
|
|
72
|
+
const count = path.points.length / 2;
|
|
73
|
+
let remaining = Math.max(0, Math.min(sum, distance));
|
|
74
|
+
for (let i = 0; i < segments.length; i++) {
|
|
75
|
+
const length = segments[i];
|
|
76
|
+
if (remaining <= length || i === segments.length - 1) {
|
|
77
|
+
const t = length > 1e-9 ? remaining / length : 0;
|
|
78
|
+
const j = (i + 1) % count;
|
|
79
|
+
const x0 = path.points[i * 2];
|
|
80
|
+
const y0 = path.points[i * 2 + 1];
|
|
81
|
+
const x1 = path.points[j * 2];
|
|
82
|
+
const y1 = path.points[j * 2 + 1];
|
|
83
|
+
return {
|
|
84
|
+
x: Number((x0 + (x1 - x0) * t).toFixed(4)),
|
|
85
|
+
y: Number((y0 + (y1 - y0) * t).toFixed(4)),
|
|
86
|
+
angle: Math.atan2(y1 - y0, x1 - x0)
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
remaining -= length;
|
|
90
|
+
}
|
|
91
|
+
const last = path.points.length - 2;
|
|
92
|
+
return { x: path.points[last], y: path.points[last + 1], angle: 0 };
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* 把 `count` 根骨沿路径铺开,返回每根的弧长位置。
|
|
96
|
+
*
|
|
97
|
+
* `spacing` 是**相邻两根骨之间的距离**(参考图像素)。传 0 或负数表示"均匀铺满整条
|
|
98
|
+
* 路径"——那是 `positionMode: percent` 的语义;给了正值就是固定间距,骨骼可能只覆盖
|
|
99
|
+
* 路径的一段,剩下的空着。
|
|
100
|
+
*/
|
|
101
|
+
export function pathPlacements(path, count, spacing) {
|
|
102
|
+
if (count <= 0)
|
|
103
|
+
return [];
|
|
104
|
+
const lengths = pathSegmentLengths(path);
|
|
105
|
+
const total = lengths.reduce((sum, value) => sum + value, 0);
|
|
106
|
+
if (count === 1)
|
|
107
|
+
return [0];
|
|
108
|
+
if (spacing > 0) {
|
|
109
|
+
return Array.from({ length: count }, (_, index) => Number((index * spacing).toFixed(4)));
|
|
110
|
+
}
|
|
111
|
+
// 均匀铺满:从起点到终点分成 count−1 段,这样最后一根正好落在路径末端。
|
|
112
|
+
const step = total / (count - 1);
|
|
113
|
+
return Array.from({ length: count }, (_, index) => Number((index * step).toFixed(4)));
|
|
114
|
+
}
|
|
115
|
+
/** 路径的包围盒(给宿主渲染验收图用)。 */
|
|
116
|
+
export function pathBounds(path) {
|
|
117
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
118
|
+
for (let i = 0; i < path.points.length; i += 2) {
|
|
119
|
+
minX = Math.min(minX, path.points[i]);
|
|
120
|
+
maxX = Math.max(maxX, path.points[i]);
|
|
121
|
+
minY = Math.min(minY, path.points[i + 1]);
|
|
122
|
+
maxY = Math.max(maxY, path.points[i + 1]);
|
|
123
|
+
}
|
|
124
|
+
return { minX, minY, maxX, maxY };
|
|
125
|
+
}
|