dsh-game-material-master 0.2.0 → 0.3.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 +363 -0
- package/README.md +66 -5
- package/lib/client.js +4358 -873
- package/lib/config.js +25 -4
- package/lib/directions.js +120 -5
- package/lib/index.js +390 -12
- package/lib/jsonio.js +49 -3
- package/lib/links.js +10 -2
- package/lib/pipeline.js +285 -0
- package/lib/store.js +21 -1
- package/lib/tilegen.js +1702 -0
- package/lib/tilegeom.js +1392 -0
- package/lib/tilemap.js +560 -0
- package/lib/tilemedia.js +243 -0
- package/lib/tools.js +191 -15
- package/lib/wire.js +137 -6
- package/package.json +3 -2
package/lib/tilemedia.js
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 地图地块模块的图像编解码与基础像素操作。
|
|
3
|
+
*
|
|
4
|
+
* 这一层只做「字节 ↔ RGBA 缓冲区」和几个原语(PNG 头、颜色统计、菱形遮罩)。
|
|
5
|
+
* 一切几何算法都在 `tilegeom.ts`,拼图在 `tilemap.ts`。
|
|
6
|
+
*
|
|
7
|
+
* 为什么 PNG 解码走 ffmpeg:仓库**没有任何运行时依赖**(见 AGENTS.md),
|
|
8
|
+
* 手写 PNG 解码器要处理 5 种 filter 类型,而 ffmpeg 已经是本插件的既有依赖
|
|
9
|
+
* (抽帧、抠像都用它)。这里把调用集中成一个函数,以后要换只改一处。
|
|
10
|
+
*/
|
|
11
|
+
import { spawn } from "node:child_process";
|
|
12
|
+
import { encodePng } from "./png.js";
|
|
13
|
+
import { ffmpegPath } from "./media.js";
|
|
14
|
+
/** PNG 头里的尺寸(不解码像素,用来做廉价校验)。 */
|
|
15
|
+
export function pngSize(bytes) {
|
|
16
|
+
if (bytes.length < 24)
|
|
17
|
+
return undefined;
|
|
18
|
+
if (bytes[0] !== 0x89 || bytes[1] !== 0x50 || bytes[2] !== 0x4e || bytes[3] !== 0x47)
|
|
19
|
+
return undefined;
|
|
20
|
+
return { width: bytes.readUInt32BE(16), height: bytes.readUInt32BE(20) };
|
|
21
|
+
}
|
|
22
|
+
/** 文件头嗅探(与 ark.ts 同一套判据,但只回扩展名)。 */
|
|
23
|
+
export function sniffImageExt(bytes) {
|
|
24
|
+
if (bytes.length >= 8 && bytes[0] === 0x89 && bytes[1] === 0x50)
|
|
25
|
+
return "png";
|
|
26
|
+
if (bytes.length >= 3 && bytes[0] === 0xff && bytes[1] === 0xd8)
|
|
27
|
+
return "jpg";
|
|
28
|
+
if (bytes.length >= 12 && bytes.toString("ascii", 0, 4) === "RIFF" && bytes.toString("ascii", 8, 12) === "WEBP")
|
|
29
|
+
return "webp";
|
|
30
|
+
if (bytes.length >= 6 && bytes.toString("ascii", 0, 3) === "GIF")
|
|
31
|
+
return "gif";
|
|
32
|
+
return "png";
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* 用 ffmpeg 把任意图片解成 RGBA8。
|
|
36
|
+
*
|
|
37
|
+
* `maxEdge > 0` 时先等比缩到长边不超过它(大图测量时省内存;规整必须用原图)。
|
|
38
|
+
* 解出来的尺寸会和 PNG 头对一次账 —— 管道读二进制最容易出的错就是「少读/多读」,
|
|
39
|
+
* 而那种错只会让几何量出偏差值,不会抛异常。
|
|
40
|
+
*/
|
|
41
|
+
export async function decodeImage(file, maxEdge = 0) {
|
|
42
|
+
const args = ["-hide_banner", "-loglevel", "error", "-i", file];
|
|
43
|
+
if (maxEdge > 0) {
|
|
44
|
+
args.push("-vf", `scale='if(gt(iw,ih),min(iw,${maxEdge}),-2)':'if(gt(iw,ih),-2,min(ih,${maxEdge}))'`);
|
|
45
|
+
}
|
|
46
|
+
args.push("-f", "rawvideo", "-pix_fmt", "rgba", "-");
|
|
47
|
+
const rgba = await runFfmpeg(args);
|
|
48
|
+
// 尺寸取自 ffprobe 语义:先按 raw 长度反推单像素宽度不现实,所以单独探一次。
|
|
49
|
+
const { width, height } = await probeDecodedSize(file, maxEdge);
|
|
50
|
+
const expected = width * height * 4;
|
|
51
|
+
if (rgba.length !== expected) {
|
|
52
|
+
throw new Error(`解码 ${file} 得到 ${rgba.length} 字节,按 ${width}×${height} 应为 ${expected} 字节`);
|
|
53
|
+
}
|
|
54
|
+
return { width, height, rgba };
|
|
55
|
+
}
|
|
56
|
+
/** 解出缩放后的真实尺寸。 */
|
|
57
|
+
async function probeDecodedSize(file, maxEdge) {
|
|
58
|
+
const args = ["-hide_banner", "-loglevel", "error", "-i", file];
|
|
59
|
+
if (maxEdge > 0) {
|
|
60
|
+
args.push("-vf", `scale='if(gt(iw,ih),min(iw,${maxEdge}),-2)':'if(gt(iw,ih),-2,min(ih,${maxEdge}))'`);
|
|
61
|
+
}
|
|
62
|
+
args.push("-frames:v", "1", "-f", "image2pipe", "-vcodec", "png", "-");
|
|
63
|
+
const one = await runFfmpeg(args);
|
|
64
|
+
const size = pngSize(one);
|
|
65
|
+
if (size === undefined)
|
|
66
|
+
throw new Error(`无法确定 ${file} 的尺寸`);
|
|
67
|
+
return size;
|
|
68
|
+
}
|
|
69
|
+
function runFfmpeg(args) {
|
|
70
|
+
return new Promise((resolve, reject) => {
|
|
71
|
+
const child = spawn(ffmpegPath(), args, { stdio: ["ignore", "pipe", "pipe"] });
|
|
72
|
+
const chunks = [];
|
|
73
|
+
const errors = [];
|
|
74
|
+
child.stdout.on("data", (chunk) => chunks.push(chunk));
|
|
75
|
+
child.stderr.on("data", (chunk) => errors.push(chunk));
|
|
76
|
+
child.on("error", (error) => reject(new Error(`无法启动 ffmpeg:${error.message}`)));
|
|
77
|
+
child.on("close", (code) => {
|
|
78
|
+
if (code === 0)
|
|
79
|
+
resolve(Buffer.concat(chunks));
|
|
80
|
+
else
|
|
81
|
+
reject(new Error(`ffmpeg 解码失败(退出码 ${code}):${Buffer.concat(errors).toString("utf8").slice(0, 400)}`));
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/** 读盘 + 解码。 */
|
|
86
|
+
export async function decodeFile(file, maxEdge = 0) {
|
|
87
|
+
return decodeImage(file, maxEdge);
|
|
88
|
+
}
|
|
89
|
+
/** 编码落盘用的 PNG 字节。 */
|
|
90
|
+
export function encodeBitmap(bitmap) {
|
|
91
|
+
return encodePng(bitmap.rgba, bitmap.width, bitmap.height);
|
|
92
|
+
}
|
|
93
|
+
/** 造一张纯色 RGBA 位图。 */
|
|
94
|
+
export function solidBitmap(width, height, color) {
|
|
95
|
+
const rgba = Buffer.alloc(width * height * 4);
|
|
96
|
+
for (let i = 0; i < width * height; i++) {
|
|
97
|
+
rgba[i * 4] = color[0];
|
|
98
|
+
rgba[i * 4 + 1] = color[1];
|
|
99
|
+
rgba[i * 4 + 2] = color[2];
|
|
100
|
+
rgba[i * 4 + 3] = color[3];
|
|
101
|
+
}
|
|
102
|
+
return { width, height, rgba };
|
|
103
|
+
}
|
|
104
|
+
export function pixelAt(bitmap, x, y) {
|
|
105
|
+
const i = (y * bitmap.width + x) * 4;
|
|
106
|
+
return [bitmap.rgba[i], bitmap.rgba[i + 1], bitmap.rgba[i + 2], bitmap.rgba[i + 3]];
|
|
107
|
+
}
|
|
108
|
+
/** 位图里是否至少有一个透明像素(用来判断「这张图到底带不带 alpha」)。 */
|
|
109
|
+
export function hasAlphaChannel(bitmap, threshold = 24) {
|
|
110
|
+
const { rgba } = bitmap;
|
|
111
|
+
for (let i = 3; i < rgba.length; i += 4) {
|
|
112
|
+
if (rgba[i] < threshold)
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* 画布四边的众数颜色 —— 当作“背景色”。
|
|
119
|
+
*
|
|
120
|
+
* 为什么不用「四角平均」:生成图四角常带一点噪声(本次实测边框色有
|
|
121
|
+
* (254,254,254) / (253,253,253) / (251,253,254) 几种),取众数比取平均稳。
|
|
122
|
+
*/
|
|
123
|
+
export function borderColor(bitmap) {
|
|
124
|
+
const counts = new Map();
|
|
125
|
+
const bump = (x, y) => {
|
|
126
|
+
const i = (y * bitmap.width + x) * 4;
|
|
127
|
+
const key = (bitmap.rgba[i] << 16) | (bitmap.rgba[i + 1] << 8) | bitmap.rgba[i + 2];
|
|
128
|
+
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
129
|
+
};
|
|
130
|
+
for (let x = 0; x < bitmap.width; x++) {
|
|
131
|
+
bump(x, 0);
|
|
132
|
+
bump(x, bitmap.height - 1);
|
|
133
|
+
}
|
|
134
|
+
for (let y = 0; y < bitmap.height; y++) {
|
|
135
|
+
bump(0, y);
|
|
136
|
+
bump(bitmap.width - 1, y);
|
|
137
|
+
}
|
|
138
|
+
let best = 0;
|
|
139
|
+
let bestCount = -1;
|
|
140
|
+
for (const [key, count] of counts) {
|
|
141
|
+
if (count > bestCount) {
|
|
142
|
+
bestCount = count;
|
|
143
|
+
best = key;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return [(best >> 16) & 0xff, (best >> 8) & 0xff, best & 0xff];
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* 从四边向内泛洪,标出「与边框连通的背景区域」。
|
|
150
|
+
*
|
|
151
|
+
* 为什么不用「离背景色远 = 前景」:草地地块的表面颜色和白色背景接近,
|
|
152
|
+
* 那个判据会把整张画布当前景 —— 研究期实测拟合出的斜率变成 1.14(实际 2.0),
|
|
153
|
+
* 而且不报任何错。泛洪只吃「与边框连通」的那一片,内部同色区域不会被误伤。
|
|
154
|
+
*/
|
|
155
|
+
export function backgroundMask(bitmap, tolerance = 22) {
|
|
156
|
+
const { width, height, rgba } = bitmap;
|
|
157
|
+
const bg = borderColor(bitmap);
|
|
158
|
+
const near = new Uint8Array(width * height);
|
|
159
|
+
const limit = tolerance * 3;
|
|
160
|
+
for (let i = 0, p = 0; i < near.length; i++, p += 4) {
|
|
161
|
+
const d = Math.abs(rgba[p] - bg[0]) + Math.abs(rgba[p + 1] - bg[1]) + Math.abs(rgba[p + 2] - bg[2]);
|
|
162
|
+
near[i] = d <= limit ? 1 : 0;
|
|
163
|
+
}
|
|
164
|
+
const seen = new Uint8Array(width * height);
|
|
165
|
+
const queue = new Int32Array(width * height);
|
|
166
|
+
let head = 0;
|
|
167
|
+
let tail = 0;
|
|
168
|
+
const push = (index) => {
|
|
169
|
+
if (near[index] === 1 && seen[index] === 0) {
|
|
170
|
+
seen[index] = 1;
|
|
171
|
+
queue[tail++] = index;
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
for (let x = 0; x < width; x++) {
|
|
175
|
+
push(x);
|
|
176
|
+
push((height - 1) * width + x);
|
|
177
|
+
}
|
|
178
|
+
for (let y = 0; y < height; y++) {
|
|
179
|
+
push(y * width);
|
|
180
|
+
push(y * width + width - 1);
|
|
181
|
+
}
|
|
182
|
+
while (head < tail) {
|
|
183
|
+
const index = queue[head++];
|
|
184
|
+
const x = index % width;
|
|
185
|
+
const y = (index - x) / width;
|
|
186
|
+
if (x > 0)
|
|
187
|
+
push(index - 1);
|
|
188
|
+
if (x < width - 1)
|
|
189
|
+
push(index + 1);
|
|
190
|
+
if (y > 0)
|
|
191
|
+
push(index - width);
|
|
192
|
+
if (y < height - 1)
|
|
193
|
+
push(index + width);
|
|
194
|
+
}
|
|
195
|
+
return seen;
|
|
196
|
+
}
|
|
197
|
+
/** 前景掩码:带 alpha 就信 alpha,否则用泛洪找背景。 */
|
|
198
|
+
export function foregroundMask(bitmap, tolerance = 22) {
|
|
199
|
+
const { width, height, rgba } = bitmap;
|
|
200
|
+
if (hasAlphaChannel(bitmap)) {
|
|
201
|
+
const mask = new Uint8Array(width * height);
|
|
202
|
+
for (let i = 0, p = 3; i < mask.length; i++, p += 4)
|
|
203
|
+
mask[i] = rgba[p] >= 24 ? 1 : 0;
|
|
204
|
+
return mask;
|
|
205
|
+
}
|
|
206
|
+
const bg = backgroundMask(bitmap, tolerance);
|
|
207
|
+
const mask = new Uint8Array(width * height);
|
|
208
|
+
for (let i = 0; i < mask.length; i++)
|
|
209
|
+
mask[i] = bg[i] === 1 ? 0 : 1;
|
|
210
|
+
return mask;
|
|
211
|
+
}
|
|
212
|
+
/** 掩码的紧致包围盒;全空时返回 undefined。 */
|
|
213
|
+
export function maskBounds(mask, width, height) {
|
|
214
|
+
let left = width;
|
|
215
|
+
let top = height;
|
|
216
|
+
let right = -1;
|
|
217
|
+
let bottom = -1;
|
|
218
|
+
for (let y = 0; y < height; y++) {
|
|
219
|
+
const row = y * width;
|
|
220
|
+
for (let x = 0; x < width; x++) {
|
|
221
|
+
if (mask[row + x] === 0)
|
|
222
|
+
continue;
|
|
223
|
+
if (x < left)
|
|
224
|
+
left = x;
|
|
225
|
+
if (x > right)
|
|
226
|
+
right = x;
|
|
227
|
+
if (y < top)
|
|
228
|
+
top = y;
|
|
229
|
+
if (y > bottom)
|
|
230
|
+
bottom = y;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (right < 0)
|
|
234
|
+
return undefined;
|
|
235
|
+
return { left, top, right, bottom };
|
|
236
|
+
}
|
|
237
|
+
/** 掩码里 1 的个数。 */
|
|
238
|
+
export function maskCount(mask) {
|
|
239
|
+
let n = 0;
|
|
240
|
+
for (let i = 0; i < mask.length; i++)
|
|
241
|
+
n += mask[i];
|
|
242
|
+
return n;
|
|
243
|
+
}
|
package/lib/tools.js
CHANGED
|
@@ -31,10 +31,11 @@ import { listProjects, readProject } from "./store.js";
|
|
|
31
31
|
import { listImageJobs, readImageJob, sniffImage } from "./imagegen.js";
|
|
32
32
|
import { listSequenceJobs, readSequenceJob, listSequenceTasks } from "./seqgen.js";
|
|
33
33
|
import { listRigJobs, readRigJob, rigSnapshot } from "./riggen.js";
|
|
34
|
+
import { listTileProjects, readTileProject, tileSnapshot } from "./tilegen.js";
|
|
34
35
|
import { TOOL_METHODS } from "./wire.js";
|
|
35
36
|
import { PANEL_KEY, buildOpenLink, originForLinks } from "./links.js";
|
|
36
|
-
/**
|
|
37
|
-
const MODULES = ["sprite", "image", "sequence", "rig"];
|
|
37
|
+
/** 四个模块的 key 与界面里的模块 key 完全一致。 */
|
|
38
|
+
const MODULES = ["sprite", "image", "sequence", "rig", "tile"];
|
|
38
39
|
/** 工具名统一前缀,避免和别家插件撞名。 */
|
|
39
40
|
const PREFIX = "game_material_";
|
|
40
41
|
/** 远程资源路由前缀,与 index.ts 的 ROUTE_PREFIX 一致。 */
|
|
@@ -53,7 +54,7 @@ const clampInt = (value, fallback, min, max) => {
|
|
|
53
54
|
function moduleOf(value) {
|
|
54
55
|
return typeof value === "string" && MODULES.includes(value) ? value : undefined;
|
|
55
56
|
}
|
|
56
|
-
/** 任务 id 前缀决定它属于哪个模块(p… / i… / s… / r…)。 */
|
|
57
|
+
/** 任务 id 前缀决定它属于哪个模块(p… / i… / s… / r… / t…)。 */
|
|
57
58
|
export function moduleForId(id) {
|
|
58
59
|
if (/^p[a-z0-9]+$/.test(id))
|
|
59
60
|
return "sprite";
|
|
@@ -63,6 +64,8 @@ export function moduleForId(id) {
|
|
|
63
64
|
return "sequence";
|
|
64
65
|
if (/^r[a-z0-9]+$/.test(id))
|
|
65
66
|
return "rig";
|
|
67
|
+
if (/^t[a-z0-9]+$/.test(id))
|
|
68
|
+
return "tile";
|
|
66
69
|
return undefined;
|
|
67
70
|
}
|
|
68
71
|
/** 产物相对路径 → 浏览器可直接打开的绝对 URL。 */
|
|
@@ -71,6 +74,56 @@ function assetUrl(assetBase, relative) {
|
|
|
71
74
|
return undefined;
|
|
72
75
|
return `${originForLinks()}${assetBase}${relative}`;
|
|
73
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* 把工具返回值收拾成「能无损过 JSON」的形状。
|
|
79
|
+
*
|
|
80
|
+
* DSH(typert 0.1.7+)对工具返回值做无损 JSON 校验:对象里带一个 `undefined`
|
|
81
|
+
* 的键、或者 `NaN` / `Infinity` / `-0` 这种 JSON 表达不了的数字,整次工具调用
|
|
82
|
+
* 会**直接失败**,界面上只看到 `tool "…" returned invalid output: value is not
|
|
83
|
+
* lossless JSON`——而产物其实好端端躺在磁盘上。
|
|
84
|
+
*
|
|
85
|
+
* 快照里的 `file` / `url` 这类可选字段是按 `file: node.file` 直接写的,节点还没
|
|
86
|
+
* 生成时它就是 `undefined`(键在、值是 undefined)。所以「项目还没跑完」时
|
|
87
|
+
* `status` / `review` / `wait` / `call getProject` 会全部报这个错,项目空空如也
|
|
88
|
+
* 时反而看不出来——很容易误判成「插件装坏了」。
|
|
89
|
+
*
|
|
90
|
+
* 这里做的恰好是 `JSON.stringify` 本来就会做的事:丢掉 `undefined` 键、把
|
|
91
|
+
* 非有限数字变成 `null`、`-0` 变成 `0`。**模型看到的内容与修复前完全一致**
|
|
92
|
+
* (那三种值本来就会在序列化时消失),只是不再让整次调用失败。
|
|
93
|
+
*
|
|
94
|
+
* 只处理普通对象与数组;`Date` / `Buffer` 等非普通对象原样透传,交给各自的
|
|
95
|
+
* 出口去序列化。原地整理:传进来的都是本函数刚组装出来的快照,不是共享状态。
|
|
96
|
+
*/
|
|
97
|
+
function jsonSafe(value, seen = new Set()) {
|
|
98
|
+
if (typeof value === "number") {
|
|
99
|
+
if (Number.isNaN(value) || !Number.isFinite(value))
|
|
100
|
+
return null;
|
|
101
|
+
return (Object.is(value, -0) ? 0 : value);
|
|
102
|
+
}
|
|
103
|
+
if (value === null || typeof value !== "object")
|
|
104
|
+
return value;
|
|
105
|
+
if (seen.has(value))
|
|
106
|
+
return value;
|
|
107
|
+
seen.add(value);
|
|
108
|
+
if (Array.isArray(value)) {
|
|
109
|
+
for (let i = 0; i < value.length; i++)
|
|
110
|
+
value[i] = jsonSafe(value[i], seen);
|
|
111
|
+
return value;
|
|
112
|
+
}
|
|
113
|
+
// 只碰普通对象;类实例(Date / Buffer…)保持原样。
|
|
114
|
+
const proto = Object.getPrototypeOf(value);
|
|
115
|
+
if (proto !== Object.prototype && proto !== null)
|
|
116
|
+
return value;
|
|
117
|
+
const record = value;
|
|
118
|
+
for (const key of Object.keys(record)) {
|
|
119
|
+
const child = record[key];
|
|
120
|
+
if (child === undefined)
|
|
121
|
+
delete record[key];
|
|
122
|
+
else
|
|
123
|
+
record[key] = jsonSafe(child, seen);
|
|
124
|
+
}
|
|
125
|
+
return value;
|
|
126
|
+
}
|
|
74
127
|
function cellsOf(nodes, assetBase, pick) {
|
|
75
128
|
return DIRECTION_KEYS.map((key) => {
|
|
76
129
|
const node = nodes?.[key] ?? {};
|
|
@@ -266,6 +319,12 @@ async function snapshotOf(module, id) {
|
|
|
266
319
|
throw new Error(`找不到序列帧任务:${id}`);
|
|
267
320
|
return sequenceSnapshot(job);
|
|
268
321
|
}
|
|
322
|
+
if (module === "tile") {
|
|
323
|
+
const project = await readTileProject(id);
|
|
324
|
+
if (project === undefined)
|
|
325
|
+
throw new Error(`找不到地图地块项目:${id}`);
|
|
326
|
+
return tileSnapshot(project, originForLinks());
|
|
327
|
+
}
|
|
269
328
|
const job = await readRigJob(id);
|
|
270
329
|
if (job === undefined)
|
|
271
330
|
throw new Error(`找不到骨骼动画任务:${id}`);
|
|
@@ -300,14 +359,18 @@ function intentFromCall(method, payload) {
|
|
|
300
359
|
const projectId = asString(payload.projectId);
|
|
301
360
|
const jobId = asString(payload.jobId);
|
|
302
361
|
const stage = typeof payload.stage === "string" ? payload.stage : undefined;
|
|
303
|
-
if (projectId !== "")
|
|
304
|
-
|
|
362
|
+
if (projectId !== "") {
|
|
363
|
+
const framePick = method === "prepareFramePick" || method === "setFramePick" || method === "setFramePicks" || method === "resetFramePicks";
|
|
364
|
+
return { module: "sprite", projectId, stage: framePick ? "frames" : stage };
|
|
365
|
+
}
|
|
305
366
|
if (jobId !== "") {
|
|
306
367
|
const module = moduleForId(jobId);
|
|
307
368
|
return module === undefined ? undefined : { module, jobId };
|
|
308
369
|
}
|
|
309
370
|
if (method.startsWith("listRigJob") || method.startsWith("createRigJob"))
|
|
310
371
|
return { module: "rig" };
|
|
372
|
+
if (method.startsWith("listTileProject") || method.startsWith("createTileProject"))
|
|
373
|
+
return { module: "tile" };
|
|
311
374
|
if (method.startsWith("listImageJob") || method.startsWith("createImageJob"))
|
|
312
375
|
return { module: "image" };
|
|
313
376
|
if (method.startsWith("listSequenceJob") || method.startsWith("createSequenceJob"))
|
|
@@ -325,7 +388,9 @@ const CALL_DESCRIPTION = [
|
|
|
325
388
|
" uploadSource({projectId,name,data:base64}) / savePrompts({projectId,images?,video?,turn?,videoPerDirection?,suffix?,resetImagesToDefault?,resetVideoToDefault?,resetTurnToDefault?}) /",
|
|
326
389
|
" saveSettings({projectId,settings}) / setApproved({projectId,stage:images|videos|frames|sheet,key?,approved}) / revealProject({projectId}) /",
|
|
327
390
|
" runImage({projectId,key,prompt?}) / runImages({projectId,force?}) / runVideos({projectId,keys?,regenerate?}) / pollVideos({projectId}) /",
|
|
328
|
-
" clearVideos({projectId,keys?}) / runFrames({projectId,keys?}) / rekey({projectId}) / compose({projectId})",
|
|
391
|
+
" clearVideos({projectId,keys?}) / runFrames({projectId,keys?})【整段等分,同一套规则】 / rekey({projectId}) / compose({projectId})",
|
|
392
|
+
" prepareFramePick({projectId,key,count?})【只抽这一个方向的候选帧,免费】/ setFramePick({projectId,key,slot,index})【拖动第 slot 个输出帧到候选 index】/",
|
|
393
|
+
" setFramePicks({projectId,key,picks:[index,…]})【一次写回,长度必须等于每段抽帧数】/ resetFramePicks({projectId,key})【圆圈回到等分】",
|
|
329
394
|
"阶段①「八方向绿幕图」有两种生成方式(默认转圈截帧):",
|
|
330
395
|
" setImageMode({projectId,mode:turn|direct})【turn=转圈截帧(默认):先生成一段「原地匀速转一整圈」的视频,再按时间截出八个方向,一致性好;direct=逐方向生图(备选)】/",
|
|
331
396
|
" runTurnVideo({projectId})【★花钱:一次 MiniMax 调用,整圈只有这一段视频】/ runTurnFrames({projectId,count?})【本地 ffmpeg 抽候选帧并切出八张方向图,免费】/",
|
|
@@ -354,6 +419,18 @@ const CALL_DESCRIPTION = [
|
|
|
354
419
|
" runRigBones({jobId})【本地生成骨架与动画,同时产出 Spine 4.2 skeleton.json 与 DragonBones 5.5 skeleton_ske.json】/",
|
|
355
420
|
" runRigAtlas({jobId})【本地打包图集,同时产出 Spine .atlas 与 DragonBones _tex.json】",
|
|
356
421
|
" ★ 自动摆位不准时用 setRigLayoutHints:先 read_image 看 getRigJob 返回的 partsMontagePath(部件按 partsOrder 顺序排列)",
|
|
422
|
+
"45°地图地块生成(模块五,**实验性**):listTileProjects() / createTileProject({name,style?,lang?,settings?}) / getTileProject({projectId,lang?}) /",
|
|
423
|
+
" deleteTileProject({projectId}) / saveTileProject({projectId,lang?,name?,style?,items?,settings?,resetItemsToDefault?})【改 style / items / settings 会让已生成的地块作废,重跑要花钱】/",
|
|
424
|
+
" runTileItems({projectId,keys?})【★花钱:按清单逐类生成,每个变体一次 Seedream 调用】/ runTileItem({projectId,key,variant?})【★花钱:只重跑某一张】/",
|
|
425
|
+
" setTileApproved({projectId,key?,variant?,approved})【② 验收,只打勾不作废任何东西】/",
|
|
426
|
+
" runTileMap({projectId,rows?,cols?,seed?,fill?,decorDensity?})【③ 本地铺图,免费,同种子 + 同布局 = 逐像素一致】/",
|
|
427
|
+
" saveTileMapCells({projectId,cells?,decor?,buildings?,seed?})【手动改布局,不作废已花钱的地块】/",
|
|
428
|
+
" runTileExport({projectId})【④ 导出地块包 + 地图 PNG + 布局 JSON,免费】/ cancelTileJob({projectId}) / revealTileProject({projectId})",
|
|
429
|
+
" ★ 四个阶段:generate → review → map → export。**没有「先做模板」这一步** —— 等距参考图在 runTileItems 开始时",
|
|
430
|
+
" 按各建筑地块的占格形状自动渲染(形状 / 格子尺寸没变就直接复用,免费、不额外调用)。",
|
|
431
|
+
" ★ 几何交给代码、内容交给 AI:参考图由本机渲染,AI 只填菱形内部;生成后本地量一次、仿射对齐回标准菱形。",
|
|
432
|
+
" 生成的几何报告看 game_material_review 返回的 variants[].geomMode —— measured 是量测成功,template 是回退模板几何。",
|
|
433
|
+
" 出现 template 多半是模型不对:必须用 Seedream 5.0 系列;4.0 会把 2:1 等距菱形画成近正方形(实测比例 1.058)。",
|
|
357
434
|
"说明:key 是方向英文字面量(front/back/downLeft/downRight/upLeft/upRight/left/right);data 是原始文件字节的 base64(不带 data: 前缀);",
|
|
358
435
|
"生成类方法立刻返回 {started:true},接着用 game_material_wait 等它跑完,再用 game_material_review 拿验收包。",
|
|
359
436
|
"调用结果里若带 openUrl,请把它作为 Markdown 链接贴给用户,用户点击即切换到插件对应页面。"
|
|
@@ -368,11 +445,24 @@ const CALL_DESCRIPTION = [
|
|
|
368
445
|
export function registerStudioTools(host, gateway) {
|
|
369
446
|
const disposers = [];
|
|
370
447
|
const tools = host.tools;
|
|
371
|
-
/**
|
|
448
|
+
/**
|
|
449
|
+
* 统一的注册包装:把 output schema 与 render 固定下来,并让**每一个**工具
|
|
450
|
+
* 的返回值都过一遍 `jsonSafe`(DSH 的新版会对工具输出做无损 JSON 校验,
|
|
451
|
+
* 漏掉 `undefined` 键会让整次调用失败,见 jsonSafe 的注释)。
|
|
452
|
+
*/
|
|
372
453
|
const register = (definition) => {
|
|
373
454
|
if (tools === undefined)
|
|
374
455
|
return;
|
|
375
|
-
|
|
456
|
+
const execute = definition.execute;
|
|
457
|
+
const wrapped = typeof execute === "function"
|
|
458
|
+
? {
|
|
459
|
+
...definition,
|
|
460
|
+
async execute(...args) {
|
|
461
|
+
return jsonSafe(await execute.apply(this, args));
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
: definition;
|
|
465
|
+
disposers.push(tools.register(wrapped));
|
|
376
466
|
};
|
|
377
467
|
if (tools !== undefined) {
|
|
378
468
|
register({
|
|
@@ -438,22 +528,29 @@ export function registerStudioTools(host, gateway) {
|
|
|
438
528
|
async execute(args) {
|
|
439
529
|
const id = asString(args?.id);
|
|
440
530
|
if (id === "") {
|
|
441
|
-
const [projects, images, sequences, rigs] = await Promise.all([
|
|
531
|
+
const [projects, images, sequences, rigs, tiles] = await Promise.all([
|
|
532
|
+
listProjects(), listImageJobs(), listSequenceJobs(), listRigJobs(), listTileProjects()
|
|
533
|
+
]);
|
|
442
534
|
return {
|
|
443
535
|
module: moduleOf(args?.module) ?? null,
|
|
444
536
|
projects,
|
|
445
537
|
imageJobs: images,
|
|
446
538
|
sequenceJobs: sequences,
|
|
447
539
|
rigJobs: rigs,
|
|
540
|
+
tileProjects: tiles,
|
|
448
541
|
links: {
|
|
449
542
|
sprite: buildOpenLink({ module: "sprite" }),
|
|
450
543
|
image: buildOpenLink({ module: "image" }),
|
|
451
544
|
sequence: buildOpenLink({ module: "sequence" }),
|
|
452
|
-
rig: buildOpenLink({ module: "rig" })
|
|
545
|
+
rig: buildOpenLink({ module: "rig" }),
|
|
546
|
+
tile: buildOpenLink({ module: "tile" })
|
|
453
547
|
},
|
|
454
|
-
hint: "用 game_material_status({module,id}) 看某个目标的细节;八方向图的四步是 images → videos → frames → sheet。"
|
|
548
|
+
hint: "用 game_material_status({module,id}) 看某个目标的细节;八方向图的四步是 images → videos → frames → sheet,45°地图地块是 generate → review → map → export。"
|
|
455
549
|
};
|
|
456
550
|
}
|
|
551
|
+
// ⚠️ 这里**不能**再按 module 短路:`status({module:'tile', id})` 也走到这里,
|
|
552
|
+
// 短路会把「带 id 的查询」变成「列清单」,assetBase / 几何报告全丢。
|
|
553
|
+
// 只给了 module、没给 id 的情况,上面的 `id === ""` 分支已经覆盖了。
|
|
457
554
|
const module = resolveTarget(args?.module, id);
|
|
458
555
|
const snapshot = await snapshotOf(module, id);
|
|
459
556
|
const stage = typeof args?.stage === "string" ? args.stage : undefined;
|
|
@@ -494,7 +591,11 @@ export function registerStudioTools(host, gateway) {
|
|
|
494
591
|
const config = await gateway.getConfig();
|
|
495
592
|
// 已有目标时先读它的真实状态:源图 / 提示词 / 素材已经在的项目里就不再问一遍。
|
|
496
593
|
const target = id === "" ? undefined : await snapshotOf(module, id);
|
|
497
|
-
|
|
594
|
+
// 带一条深链接:用户点一下就能切到插件对应页面(新建时只带 module)。
|
|
595
|
+
return {
|
|
596
|
+
...intakeFor(module, id, told, config, target),
|
|
597
|
+
...openInfo(intentOf(module, id), "点开可以先看界面,再决定参数")
|
|
598
|
+
};
|
|
498
599
|
}
|
|
499
600
|
});
|
|
500
601
|
register({
|
|
@@ -636,6 +737,7 @@ export function registerStudioTools(host, gateway) {
|
|
|
636
737
|
const direction = typeof args?.direction === "string" ? args.direction : undefined;
|
|
637
738
|
const index = args?.index === undefined ? undefined : clampInt(args.index, 0, 0, 99);
|
|
638
739
|
const step = typeof args?.step === "string" ? args.step : undefined;
|
|
740
|
+
const key = typeof args?.key === "string" && args.key !== "" ? args.key : undefined;
|
|
639
741
|
const packet = {
|
|
640
742
|
module,
|
|
641
743
|
id,
|
|
@@ -672,7 +774,42 @@ export function registerStudioTools(host, gateway) {
|
|
|
672
774
|
packet.steps = steps;
|
|
673
775
|
packet.nextActions = sequenceNextActions(snapshot);
|
|
674
776
|
}
|
|
777
|
+
else if (module === "tile") {
|
|
778
|
+
// ⚠️ 地图地块的 `stages` 是**对象**({generate, review, map, export}),
|
|
779
|
+
// 不是数组。之前它掉进了 `else`(骨骼动画)分支,那边无条件当成数组用
|
|
780
|
+
// `.filter()` —— 于是 game_material_review 对 tile 直接抛
|
|
781
|
+
// `snapshot.stages.filter is not a function`。真机验证才发现。
|
|
782
|
+
const tileStages = snapshot.stages;
|
|
783
|
+
const stageKeys = Object.keys(tileStages ?? {});
|
|
784
|
+
if (stage !== undefined && !stageKeys.includes(stage)) {
|
|
785
|
+
throw new Error(`未知阶段:${stage}(可选:${stageKeys.join(" / ")})`);
|
|
786
|
+
}
|
|
787
|
+
packet.stages = stageKeys
|
|
788
|
+
.filter((key) => stage === undefined || key === stage)
|
|
789
|
+
.map((key) => ({ stage: key, ...tileStages[key] }));
|
|
790
|
+
// 只要某个地块 / 变体:过滤 items,方便「就重跑这一张」的判断
|
|
791
|
+
packet.items = snapshot.items.filter((item) => key === undefined || item.key === key);
|
|
792
|
+
if (packet.items.length === 0)
|
|
793
|
+
throw new Error(`找不到地块:${key}`);
|
|
794
|
+
packet.progress = snapshot.progress;
|
|
795
|
+
packet.map = snapshot.map;
|
|
796
|
+
packet.style = snapshot.style;
|
|
797
|
+
packet.settings = snapshot.settings;
|
|
798
|
+
// 给出「下一步该做什么」:几何回退过的、失败的、没生成的,都要点名
|
|
799
|
+
const fallback = packet.items.flatMap((item) => item.variants.filter((v) => v.geomMode === "template").map((v) => `${item.key} v${v.index + 1}`));
|
|
800
|
+
const failed = packet.items.flatMap((item) => item.variants.filter((v) => v.error !== undefined).map((v) => `${item.key} v${v.index + 1}:${v.error}`));
|
|
801
|
+
const missing = packet.items.filter((item) => item.variants.length === 0).map((item) => item.key);
|
|
802
|
+
packet.nextActions = {
|
|
803
|
+
missing,
|
|
804
|
+
fallback,
|
|
805
|
+
failed,
|
|
806
|
+
hint: fallback.length > 0
|
|
807
|
+
? "这些地块没量出 2:1 菱形,已按模板几何回退。多半是模型不对——请确认生图模型是 Seedream 5.0 系列(4.0 会把等距菱形画成近正方形),然后只重跑这几张:runTileItem({projectId, key, variant})。"
|
|
808
|
+
: "几何都正常。可以继续验收、拼图(runTileMap)或导出(runTileExport)。"
|
|
809
|
+
};
|
|
810
|
+
}
|
|
675
811
|
else {
|
|
812
|
+
// 骨骼动画(模块四)
|
|
676
813
|
const stages = snapshot.stages.filter((entry) => stage === undefined || entry.stage === stage);
|
|
677
814
|
if (stages.length === 0)
|
|
678
815
|
throw new Error(`未知阶段:${stage}`);
|
|
@@ -740,6 +877,15 @@ export function registerStudioTools(host, gateway) {
|
|
|
740
877
|
await gateway.saveImageJob(index === undefined ? { jobId: id, approved } : { jobId: id, approved, index });
|
|
741
878
|
return { module, id, index: index ?? null, approved, count: targets.length, ...openInfo({ module: "image", jobId: id }, "点开看验收结果") };
|
|
742
879
|
}
|
|
880
|
+
if (module === "tile") {
|
|
881
|
+
const key = asString(args?.key) || undefined;
|
|
882
|
+
const variant = args?.variant === undefined ? undefined : clampInt(args.variant, 0, 0, 99);
|
|
883
|
+
await gateway.setTileApproved({ projectId: id, key, variant, approved });
|
|
884
|
+
return {
|
|
885
|
+
module, id, key: key ?? null, variant: variant ?? null, approved,
|
|
886
|
+
...openInfo({ module: "tile", jobId: id }, "点开看验收结果")
|
|
887
|
+
};
|
|
888
|
+
}
|
|
743
889
|
if (module === "sequence") {
|
|
744
890
|
const job = await readSequenceJob(id);
|
|
745
891
|
if (job === undefined)
|
|
@@ -930,6 +1076,25 @@ function intakeFor(module, id, told, config, target) {
|
|
|
930
1076
|
known.默认拆件网格 = `${config ? "" : ""}4 列 × 4 行 = 16 个标准人形部件(头/脖子/躯干/胯/上臂/小臂/手/大腿/小腿/脚),格子位置即部件身份`;
|
|
931
1077
|
known.默认动画 = "idle 待机 / walk 行走 / run 奔跑 / wave 挥手 / jump 跳跃 / attack 攻击";
|
|
932
1078
|
}
|
|
1079
|
+
if (module === "tile") {
|
|
1080
|
+
if (config?.arkApiKeySet !== true) {
|
|
1081
|
+
blockers.push("还没配置火山方舟 API Key(生图必需):设置 → 游戏素材大师 → 火山方舟 API Key,配好点「测试连接」。");
|
|
1082
|
+
}
|
|
1083
|
+
known.实验性 = "45°地图地块生成是实验性功能:几何(参考图/拼图/裁剪)由本地代码算、有像素级自检,可靠;不稳定的是「AI 能否照着占格形状画出对的造型」,非矩形占地经常要重跑。";
|
|
1084
|
+
known.四个阶段 = "① 生成地块(★计费;参考图会按占格形状自动渲染,不用手动准备)→ ② 验收 → ③ 拼成地图(本地免费,可反复)→ ④ 导出";
|
|
1085
|
+
known.几何由代码保证 = "等距参考图由本地代码按「占格形状」渲染(严格 2:1 等距),AI 只负责填内容。实测这样菱形比例达标率 100%";
|
|
1086
|
+
known.默认地块清单 = "9 类地形(草/土/石/灌木,各 1~2 变体)+ 4 个独立装饰(树/松树/巨石)+ 1 栋跨 2×2 格建筑";
|
|
1087
|
+
known.默认单元格 = `${config?.cellWidth ?? 64} 宽,高为 96(菱形占中部,上方留白给高出地面的装饰)`;
|
|
1088
|
+
// ⚠️ 属性名只能用标识符允许的字符:带圈数字 ② 之类不是合法标识符字符,
|
|
1089
|
+
// 写成 `known.只有第②步花钱` 会直接编译不过(Invalid character)。
|
|
1090
|
+
known.只有第1步花钱 = "生成一个变体 = 一次 Seedream 调用;参考图、规整、拼图、导出全在本地,免费且可反复";
|
|
1091
|
+
known.装饰与地面分开生成 = "树/巨石走「白底单图 + 锚点摆放」,不与地面画在同一张图上(同图会被画两遍)";
|
|
1092
|
+
if (target?.reviewMode)
|
|
1093
|
+
known.审核模式 = target.reviewMode;
|
|
1094
|
+
ask("style", "整套地块的画风描述是什么?(例如「像素画风,色彩明快饱和,干净色块,无噪点」;可留空走默认)", "它会追加到每个地块的提示词末尾,改一次整套画风都变。", "用 game_material_call 的 saveTileProject({projectId, style}) 保存。");
|
|
1095
|
+
ask("items", "要做哪几类地块?(默认给 9 类地形 + 4 个装饰 + 1 栋建筑,可以直接用,也可以增删改)", "地块清单决定画面里有什么,也决定要花多少次调用。", "用 saveTileProject({projectId, items:[…]}) 整份写回;每项含 key/label/kind/family/content/mode/variantCount。");
|
|
1096
|
+
ask("mapLayout", "地图要多大?(默认 14×14)要不要指定某几格用别的类别、或者撒装饰?", "布局是本地计算,改多少次都不花钱。", "用 runTileMap({projectId, rows, cols, seed, fill, decorDensity});手动指定格子用 saveTileMapCells。");
|
|
1097
|
+
}
|
|
933
1098
|
if (id === "") {
|
|
934
1099
|
questions.push({
|
|
935
1100
|
key: "target",
|
|
@@ -939,9 +1104,11 @@ function intakeFor(module, id, told, config, target) {
|
|
|
939
1104
|
? "新建一个图片任务,还是在已有任务上继续?"
|
|
940
1105
|
: module === "sequence"
|
|
941
1106
|
? "新建一个序列帧任务,还是在已有任务上继续?"
|
|
942
|
-
: "
|
|
1107
|
+
: module === "tile"
|
|
1108
|
+
? "新建一个地图地块项目,还是在已有项目上继续?"
|
|
1109
|
+
: "新建一个骨骼动画任务,还是在已有任务上继续?",
|
|
943
1110
|
why: "后面的参数都挂在项目 / 任务上。",
|
|
944
|
-
how: "新建:game_material_call({method:'createProject'|'createImageJob'|'createSequenceJob'|'createRigJob', payload:{name}}),返回的 id 就是后续 payload 里的 projectId / jobId。"
|
|
1111
|
+
how: "新建:game_material_call({method:'createProject'|'createImageJob'|'createSequenceJob'|'createRigJob'|'createTileProject', payload:{name}}),返回的 id 就是后续 payload 里的 projectId / jobId。"
|
|
945
1112
|
});
|
|
946
1113
|
}
|
|
947
1114
|
if (module === "sprite") {
|
|
@@ -1020,7 +1187,11 @@ const UPLOAD_KINDS = {
|
|
|
1020
1187
|
sprite: ["source"],
|
|
1021
1188
|
image: ["ref", "item"],
|
|
1022
1189
|
sequence: ["firstFrame", "lastFrame", "referenceImage", "referenceVideo"],
|
|
1023
|
-
rig: ["source", "part"]
|
|
1190
|
+
rig: ["source", "part"],
|
|
1191
|
+
// 地图地块**不需要上传素材**:地块由代码渲染的菱形模板 + 提示词生成。
|
|
1192
|
+
// 留空数组而不是漏掉这一项 —— 漏掉会让类型检查失败,而空数组能让
|
|
1193
|
+
// 「上传」这条路给出明确的报错信息(见 uploadFromPath)。
|
|
1194
|
+
tile: []
|
|
1024
1195
|
};
|
|
1025
1196
|
async function uploadFromPath(gateway, module, id, kind, path, name) {
|
|
1026
1197
|
if (id === "")
|
|
@@ -1065,6 +1236,11 @@ async function uploadFromPath(gateway, module, id, kind, path, name) {
|
|
|
1065
1236
|
else
|
|
1066
1237
|
await call.uploadRigSource({ jobId: id, name: fileName, data: base64 });
|
|
1067
1238
|
}
|
|
1239
|
+
else if (module === "tile") {
|
|
1240
|
+
// 地图地块目前不需要上传素材:地块由模板 + 提示词生成。
|
|
1241
|
+
// 明确报错,而不是掉进 sequence 分支里做一件风马牛不相及的事。
|
|
1242
|
+
throw new Error("地图地块模块不需要上传素材:地块由「代码渲染的菱形模板 + 提示词」生成。要改画风请用 saveTileProject({style})");
|
|
1243
|
+
}
|
|
1068
1244
|
else {
|
|
1069
1245
|
await call.uploadSequenceRef({ jobId: id, kind, name: fileName, data: base64 });
|
|
1070
1246
|
}
|