dsh-game-material-master 0.1.1 → 0.2.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.md +31 -5
- package/lib/chroma.js +128 -21
- package/lib/client.js +506 -17
- package/lib/directions.js +61 -0
- package/lib/index.js +127 -5
- package/lib/pipeline.js +431 -6
- package/lib/store.js +105 -6
- package/lib/tools.js +121 -9
- package/lib/wire.js +39 -1
- package/package.json +2 -2
package/lib/pipeline.js
CHANGED
|
@@ -8,19 +8,25 @@
|
|
|
8
8
|
* 3. frames 每段视频按时长平均抽 N 帧 (ffmpeg)
|
|
9
9
|
* 4. sheet 全部帧抠绿幕 → 拼成一张 8×8 整图 (内置抠像 + PNGu 编码)
|
|
10
10
|
*
|
|
11
|
+
* 阶段 1 有两种生成方式(`Project.imageMode`):
|
|
12
|
+
* - `turn`(默认)先让视频模型绕竖轴转一整圈,再按时长匀抽候选帧,
|
|
13
|
+
* 按八个截帧位置切出八张绿幕图——八个方向出自同一段视频,一致性最好;
|
|
14
|
+
* - `direct` 逐个方向 Seedream 生图(原来那条路,按依赖顺序生成)。
|
|
15
|
+
* 两条路产出的都是同一批 `images/<方向>.png`,所以阶段 2~4 完全不必区分。
|
|
16
|
+
*
|
|
11
17
|
* 所有耗时操作都通过 kick() 丢到后台,远程调用只负责「启动」和「读状态」,
|
|
12
18
|
* 界面靠自己轮询 getState 看进度——一次 Hailuo 生成要几分钟,绝不能同步阻塞。
|
|
13
19
|
*/
|
|
14
20
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
15
21
|
import { dirname } from "node:path";
|
|
16
22
|
import { loadConfig } from "./config.js";
|
|
17
|
-
import { DIRECTIONS, DIRECTION_KEYS, directionOf } from "./directions.js";
|
|
23
|
+
import { DEFAULT_TURN_PROMPT, DIRECTIONS, DIRECTION_KEYS, TURN_DIRECTION_DEFAULT, TURN_FRAME_COUNT_MAX, TURN_FRAME_COUNT_MIN, directionOf, turnOrder } from "./directions.js";
|
|
18
24
|
import { generateImage } from "./ark.js";
|
|
19
25
|
import { downloadVideo, queryVideo, retrieveFile, submitVideo } from "./minimax.js";
|
|
20
26
|
import { extractFrames, mimeOf, toDataUri, toJpegDataUri } from "./media.js";
|
|
21
27
|
import { composeSheet, keyGreen } from "./chroma.js";
|
|
22
28
|
import { encodePng } from "./png.js";
|
|
23
|
-
import { assetPath, log, patchProject, readProject, referenceFiles } from "./store.js";
|
|
29
|
+
import { assetPath, freshPicks, log, patchProject, readProject, referenceFiles } from "./store.js";
|
|
24
30
|
/** 生图的依赖分层:同一层内可并发,层与层之间必须按序。 */
|
|
25
31
|
const IMAGE_STAGES = [
|
|
26
32
|
["front"],
|
|
@@ -242,6 +248,400 @@ export function startAllImages(projectId, force = false) {
|
|
|
242
248
|
});
|
|
243
249
|
return started ? { started: true } : { started: false, reason: "批量生成已在进行中" };
|
|
244
250
|
}
|
|
251
|
+
// ── 阶段 1b:转圈截帧(八方向绿幕图的默认生成方式)──────────────────────
|
|
252
|
+
//
|
|
253
|
+
// 一次生成、多次截取:整圈只有一段视频,八个方向是它时间轴上的八个截帧位置。
|
|
254
|
+
// 好处是八个方向天生一致(同一个角色、同一段光线、同一套画风),
|
|
255
|
+
// 代价是分辨率取决于视频、以及「转速是否真的均匀」——所以位置必须可拖。
|
|
256
|
+
function clampInt(value, min, max) {
|
|
257
|
+
if (!Number.isFinite(value))
|
|
258
|
+
return min;
|
|
259
|
+
return Math.min(max, Math.max(min, Math.round(value)));
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* 转圈视频的首帧。
|
|
263
|
+
*
|
|
264
|
+
* 优先用已经有的正面绿幕图,没有就退回源图:源图不一定是绿幕,但提示词里
|
|
265
|
+
* 已经明确要求「纯色绿幕 #00FF00」,模型会照着重画背景。把实际用的那张记进
|
|
266
|
+
* `turn.video.firstFrame`,排查「第一帧就不是正面」时才有据可依。
|
|
267
|
+
*/
|
|
268
|
+
function turnFirstFrame(project) {
|
|
269
|
+
const front = project.images["front"]?.file;
|
|
270
|
+
if (front !== undefined)
|
|
271
|
+
return { file: front, label: "正面绿幕图" };
|
|
272
|
+
if (project.source !== null)
|
|
273
|
+
return { file: project.source.file, label: "源图" };
|
|
274
|
+
return undefined;
|
|
275
|
+
}
|
|
276
|
+
export function startTurnVideo(projectId) {
|
|
277
|
+
if (jobs.get(projectId)?.has("turn:video") === true)
|
|
278
|
+
return { started: false, reason: "转圈视频正在生成中" };
|
|
279
|
+
const started = kick(projectId, "turn:video", "生成转圈视频", () => submitTurnVideo(projectId));
|
|
280
|
+
return started ? { started: true } : { started: false, reason: "任务已在进行中" };
|
|
281
|
+
}
|
|
282
|
+
async function submitTurnVideo(projectId) {
|
|
283
|
+
const project = await readProject(projectId);
|
|
284
|
+
if (project === undefined)
|
|
285
|
+
throw new Error(`项目不存在:${projectId}`);
|
|
286
|
+
const config = await loadConfig();
|
|
287
|
+
if (config.minimaxApiKey.trim() === "")
|
|
288
|
+
throw new Error("尚未配置 MiniMax API Key");
|
|
289
|
+
const first = turnFirstFrame(project);
|
|
290
|
+
if (first === undefined)
|
|
291
|
+
throw new Error("还没有源图,无法生成转圈视频");
|
|
292
|
+
const base = (project.prompts.turn ?? DEFAULT_TURN_PROMPT).trim();
|
|
293
|
+
if (base === "")
|
|
294
|
+
throw new Error("提示词为空,请先填写转圈视频提示词");
|
|
295
|
+
const suffix = (project.prompts.suffix ?? "").trim();
|
|
296
|
+
const prompt = suffix === "" ? base : `${base}\n${suffix}`;
|
|
297
|
+
await patchProject(projectId, (current) => {
|
|
298
|
+
current.turn.video = {
|
|
299
|
+
...current.turn.video,
|
|
300
|
+
status: "running",
|
|
301
|
+
remoteStatus: "提交中",
|
|
302
|
+
error: undefined,
|
|
303
|
+
firstFrame: first.file
|
|
304
|
+
};
|
|
305
|
+
// 旧视频的候选帧还在,但已经和这次的新视频对不上了:标成「需重抽」,
|
|
306
|
+
// 而不是直接清空——用户还能对着上一版继续调位置,直到新视频落地。
|
|
307
|
+
if (current.turn.frames.frames.length > 0)
|
|
308
|
+
current.turn.frames.stale = true;
|
|
309
|
+
log(current, "info", `开始生成转圈视频(首帧:${first.label})`);
|
|
310
|
+
});
|
|
311
|
+
const startedAt = Date.now();
|
|
312
|
+
try {
|
|
313
|
+
const jpegRel = "videos/turn-first-frame.jpg";
|
|
314
|
+
const dataUri = await toJpegDataUri(assetPath(projectId, first.file), assetPath(projectId, jpegRel));
|
|
315
|
+
const taskId = await submitVideo({
|
|
316
|
+
...miniMaxRequest(config),
|
|
317
|
+
prompt,
|
|
318
|
+
firstFrameImage: dataUri,
|
|
319
|
+
duration: config.minimaxDuration,
|
|
320
|
+
resolution: config.minimaxResolution,
|
|
321
|
+
promptOptimizer: config.minimaxPromptOptimizer
|
|
322
|
+
});
|
|
323
|
+
await patchProject(projectId, (current) => {
|
|
324
|
+
current.turn.video = {
|
|
325
|
+
...current.turn.video,
|
|
326
|
+
status: "running",
|
|
327
|
+
taskId,
|
|
328
|
+
remoteStatus: "已提交",
|
|
329
|
+
elapsedMs: Date.now() - startedAt,
|
|
330
|
+
updatedAt: Date.now()
|
|
331
|
+
};
|
|
332
|
+
log(current, "info", `转圈视频任务已提交(${taskId})`);
|
|
333
|
+
});
|
|
334
|
+
ensurePoller(projectId);
|
|
335
|
+
}
|
|
336
|
+
catch (error) {
|
|
337
|
+
const message = messageOf(error);
|
|
338
|
+
await patchProject(projectId, (current) => {
|
|
339
|
+
current.turn.video = { ...current.turn.video, status: "error", error: message, remoteStatus: "提交失败", updatedAt: Date.now() };
|
|
340
|
+
log(current, "error", `转圈视频提交失败:${message}`);
|
|
341
|
+
});
|
|
342
|
+
throw error;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
/** 轮询一次转圈视频任务(由 ensurePoller 的定时器驱动)。 */
|
|
346
|
+
async function pollTurnVideoOnce(projectId, config) {
|
|
347
|
+
const project = await readProject(projectId);
|
|
348
|
+
if (project === undefined)
|
|
349
|
+
return;
|
|
350
|
+
const taskId = project.turn?.video?.taskId;
|
|
351
|
+
if (project.turn?.video?.status !== "running" || taskId === undefined)
|
|
352
|
+
return;
|
|
353
|
+
const request = miniMaxRequest(config);
|
|
354
|
+
try {
|
|
355
|
+
const query = await queryVideo({ ...request, taskId });
|
|
356
|
+
if (query.status === "failed") {
|
|
357
|
+
const reason = query.error ?? "MiniMax 报告该任务失败";
|
|
358
|
+
await patchProject(projectId, (current) => {
|
|
359
|
+
current.turn.video = { ...current.turn.video, status: "error", error: reason, remoteStatus: query.remoteStatus, updatedAt: Date.now() };
|
|
360
|
+
log(current, "error", `转圈视频生成失败:${reason}`);
|
|
361
|
+
});
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
if (query.status !== "succeeded") {
|
|
365
|
+
await patchProject(projectId, (current) => {
|
|
366
|
+
current.turn.video = { ...current.turn.video, remoteStatus: query.remoteStatus, updatedAt: Date.now() };
|
|
367
|
+
});
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
const url = query.videoUrl ??
|
|
371
|
+
(query.fileId !== undefined ? await retrieveFile({ ...request, fileId: query.fileId }) : undefined);
|
|
372
|
+
if (url === undefined)
|
|
373
|
+
throw new Error("任务已成功,但响应里既没有视频地址也没有 file_id");
|
|
374
|
+
const bytes = await downloadVideo(url, config.minimaxTimeoutMs);
|
|
375
|
+
const relative = "videos/turn.mp4";
|
|
376
|
+
await writeFile(assetPath(projectId, relative), bytes);
|
|
377
|
+
await patchProject(projectId, (current) => {
|
|
378
|
+
current.turn.video = {
|
|
379
|
+
...current.turn.video,
|
|
380
|
+
status: "ready",
|
|
381
|
+
file: relative,
|
|
382
|
+
remoteStatus: query.remoteStatus,
|
|
383
|
+
error: undefined,
|
|
384
|
+
updatedAt: Date.now()
|
|
385
|
+
};
|
|
386
|
+
current.turn.frames.stale = true;
|
|
387
|
+
log(current, "info", `转圈视频已下载(${(bytes.length / 1024 / 1024).toFixed(1)} MB),接着抽取候选帧`);
|
|
388
|
+
});
|
|
389
|
+
// 视频到手就把候选帧抽出来:抽帧是本机 ffmpeg,不花钱,让链路自己往下走
|
|
390
|
+
// 比「再点一次按钮」更省事。用户拖圆圈之前本来也要先有候选帧。
|
|
391
|
+
startTurnFrames(projectId);
|
|
392
|
+
}
|
|
393
|
+
catch (error) {
|
|
394
|
+
// 单次轮询失败不等于任务失败(网络抖动很常见),保留 running 让下一轮重试。
|
|
395
|
+
await patchProject(projectId, (current) => {
|
|
396
|
+
const node = current.turn?.video;
|
|
397
|
+
if (node !== undefined)
|
|
398
|
+
node.remoteStatus = `查询异常:${messageOf(error)}`;
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
export function startTurnFrames(projectId, count) {
|
|
403
|
+
if (jobs.get(projectId)?.has("turn:frames") === true)
|
|
404
|
+
return { started: false, reason: "抽帧已在进行中" };
|
|
405
|
+
const started = kick(projectId, "turn:frames", "抽取转圈候选帧", async () => {
|
|
406
|
+
// 第一个 await 之前公布覆盖面:抽完候选帧才会逐个切出八张方向图,
|
|
407
|
+
// 那段空窗里八个格子全靠这张表盖着(与批量抽帧同一套约定)。
|
|
408
|
+
setJobTargets(projectId, "turn:frames", DIRECTION_KEYS);
|
|
409
|
+
await extractTurnFrames(projectId, count);
|
|
410
|
+
});
|
|
411
|
+
return started ? { started: true } : { started: false, reason: "任务已在进行中" };
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* 候选帧数变了之后,把用户拖过的位置按比例换算过去。
|
|
415
|
+
*
|
|
416
|
+
* 直接保留下标是不行的:32 帧里的第 17 帧和 64 帧里的第 17 帧朝向完全不同,
|
|
417
|
+
* 相当于把用户调好的八个位置整体打乱。按比例缩放至少保持「相对位置」不变。
|
|
418
|
+
*/
|
|
419
|
+
function rescalePicks(picks, from, to) {
|
|
420
|
+
const out = {};
|
|
421
|
+
const ratio = from > 0 ? to / from : 1;
|
|
422
|
+
for (const key of DIRECTION_KEYS) {
|
|
423
|
+
out[key] = clampInt((picks[key] ?? 0) * ratio, 0, Math.max(0, to - 1));
|
|
424
|
+
}
|
|
425
|
+
return out;
|
|
426
|
+
}
|
|
427
|
+
async function extractTurnFrames(projectId, count) {
|
|
428
|
+
const project = await readProject(projectId);
|
|
429
|
+
if (project === undefined)
|
|
430
|
+
throw new Error(`项目不存在:${projectId}`);
|
|
431
|
+
if (project.turn?.video?.file === undefined)
|
|
432
|
+
throw new Error("还没有转圈视频,请先生成转圈视频");
|
|
433
|
+
const frameCount = clampInt(count ?? project.settings.turnFrameCount, TURN_FRAME_COUNT_MIN, TURN_FRAME_COUNT_MAX);
|
|
434
|
+
const previous = project.turn.frames;
|
|
435
|
+
await patchProject(projectId, (current) => {
|
|
436
|
+
current.turn.frames = { ...current.turn.frames, status: "running", error: undefined };
|
|
437
|
+
current.settings.turnFrameCount = frameCount;
|
|
438
|
+
log(current, "info", `开始抽取转圈候选帧:${frameCount} 张`);
|
|
439
|
+
});
|
|
440
|
+
try {
|
|
441
|
+
const settings = project.settings;
|
|
442
|
+
const extracted = await extractFrames({
|
|
443
|
+
video: assetPath(projectId, project.turn.video.file),
|
|
444
|
+
frameCount,
|
|
445
|
+
longEdge: settings.workingLongEdge,
|
|
446
|
+
cropInset: settings.cropInset
|
|
447
|
+
});
|
|
448
|
+
const rawRelative = "turn/raw.bin";
|
|
449
|
+
await mkdir(dirname(assetPath(projectId, rawRelative)), { recursive: true });
|
|
450
|
+
await writeFile(assetPath(projectId, rawRelative), Buffer.concat(extracted.frames));
|
|
451
|
+
const files = [];
|
|
452
|
+
const times = [];
|
|
453
|
+
// 候选帧写在 turn/frames/ 下,ensureLayout 只建到 turn/ 这一层,这里补建。
|
|
454
|
+
await mkdir(dirname(assetPath(projectId, "turn/frames/f00.png")), { recursive: true });
|
|
455
|
+
for (let i = 0; i < extracted.frames.length; i++) {
|
|
456
|
+
const relative = `turn/frames/f${String(i).padStart(2, "0")}.png`;
|
|
457
|
+
await writeFile(assetPath(projectId, relative), encodePng(extracted.frames[i], extracted.width, extracted.height));
|
|
458
|
+
files.push(relative);
|
|
459
|
+
// 与 ffmpeg 的 `fps=张数/时长` 一致:第 i 帧取在 i * 时长/张数 秒处。
|
|
460
|
+
times.push((i * extracted.duration) / Math.max(1, extracted.frames.length));
|
|
461
|
+
}
|
|
462
|
+
// 轴上的圆圈是「对着整圈缩略条带」拖的,所以条带必须和候选帧一一对应。
|
|
463
|
+
const strip = composeSheet([{ key: "turn", frames: extracted.frames, width: extracted.width, height: extracted.height }], {
|
|
464
|
+
cellWidth: 96,
|
|
465
|
+
cellHeight: Math.max(24, Math.round((96 * extracted.height) / Math.max(1, extracted.width))),
|
|
466
|
+
frameCount: extracted.frames.length,
|
|
467
|
+
autoCrop: false,
|
|
468
|
+
fillRatio: 1,
|
|
469
|
+
pixelSize: 0,
|
|
470
|
+
fitMode: "stretch",
|
|
471
|
+
bottomMargin: 0
|
|
472
|
+
});
|
|
473
|
+
const stripRelative = "turn/strip.png";
|
|
474
|
+
await writeFile(assetPath(projectId, stripRelative), encodePng(strip.rgba, strip.width, strip.height));
|
|
475
|
+
// 位置怎么来:
|
|
476
|
+
// · 从来没有抽过候选帧(rawFrameCount 还没有)→ 这八个位置只是占位,
|
|
477
|
+
// 按**这次的**张数重新等分。否则第一次抽 16 张时,那套按 32 张算的
|
|
478
|
+
// 默认位置会有四个方向一起被夹到最后一帧(实测过)。
|
|
479
|
+
// · 抽过、这次张数变了 → 按比例换算(保留用户拖过的相对位置)。
|
|
480
|
+
// · 抽过、张数没变 → 原样保留。
|
|
481
|
+
const picks = previous.rawFrameCount !== undefined && previous.rawFrameCount > 0
|
|
482
|
+
? previous.rawFrameCount !== files.length
|
|
483
|
+
? rescalePicks(previous.picks, previous.rawFrameCount, files.length)
|
|
484
|
+
: previous.picks
|
|
485
|
+
: freshPicks(files.length, project.turn?.direction ?? TURN_DIRECTION_DEFAULT);
|
|
486
|
+
await patchProject(projectId, (current) => {
|
|
487
|
+
current.turn.frames = {
|
|
488
|
+
status: "ready",
|
|
489
|
+
frames: files,
|
|
490
|
+
times,
|
|
491
|
+
raw: rawRelative,
|
|
492
|
+
rawWidth: extracted.width,
|
|
493
|
+
rawHeight: extracted.height,
|
|
494
|
+
rawFrameCount: files.length,
|
|
495
|
+
duration: extracted.duration,
|
|
496
|
+
picks,
|
|
497
|
+
strip: stripRelative,
|
|
498
|
+
approved: false,
|
|
499
|
+
stale: false,
|
|
500
|
+
updatedAt: Date.now()
|
|
501
|
+
};
|
|
502
|
+
log(current, "info", `候选帧抽取完成:${files.length} 张 / ${extracted.width}×${extracted.height} / 视频时长 ${extracted.duration.toFixed(2)} 秒`);
|
|
503
|
+
});
|
|
504
|
+
await applyTurnPicks(projectId);
|
|
505
|
+
}
|
|
506
|
+
catch (error) {
|
|
507
|
+
const message = messageOf(error);
|
|
508
|
+
await patchProject(projectId, (current) => {
|
|
509
|
+
current.turn.frames = { ...current.turn.frames, status: "error", error: message };
|
|
510
|
+
log(current, "error", `候选帧抽取失败:${message}`);
|
|
511
|
+
});
|
|
512
|
+
throw error;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* 按 `picks` 把八个方向的绿幕图从候选帧里切出来。
|
|
517
|
+
*
|
|
518
|
+
* 这一步是本机 CPU:读 `turn/raw.bin` 的第 N 帧 → 编码成 PNG → 覆盖
|
|
519
|
+
* `images/<方向>.png`。所以拖动圆圈可以随便试,改一个位置不必重新抽帧、
|
|
520
|
+
* 更不必重新生成视频。
|
|
521
|
+
*
|
|
522
|
+
* 换了朝向 = 这个方向的行走视频与它的序列帧都作废(它们拍的是另一个朝向),
|
|
523
|
+
* 整图同样作废——不然后面几步会拿着旧朝向的帧合成。
|
|
524
|
+
*/
|
|
525
|
+
export async function applyTurnPicks(projectId, keys) {
|
|
526
|
+
const project = await readProject(projectId);
|
|
527
|
+
if (project === undefined)
|
|
528
|
+
throw new Error(`项目不存在:${projectId}`);
|
|
529
|
+
const node = project.turn?.frames;
|
|
530
|
+
if (node?.raw === undefined || node.rawWidth === undefined || node.rawWidth <= 0) {
|
|
531
|
+
throw new Error("还没有候选帧,请先抽取转圈候选帧");
|
|
532
|
+
}
|
|
533
|
+
const width = node.rawWidth;
|
|
534
|
+
const height = node.rawHeight;
|
|
535
|
+
const raw = await readFile(assetPath(projectId, node.raw));
|
|
536
|
+
const frameBytes = width * height * 4;
|
|
537
|
+
const total = Math.max(0, Math.floor(raw.length / frameBytes));
|
|
538
|
+
if (total === 0)
|
|
539
|
+
throw new Error("候选帧缓存是空的,请重新抽取候选帧");
|
|
540
|
+
const targets = (keys ?? DIRECTION_KEYS).filter((key) => directionOf(key) !== undefined);
|
|
541
|
+
for (const key of targets) {
|
|
542
|
+
const index = clampInt(node.picks[key] ?? 0, 0, total - 1);
|
|
543
|
+
const slice = Buffer.from(raw.subarray(index * frameBytes, (index + 1) * frameBytes));
|
|
544
|
+
const relative = `images/${key}.png`;
|
|
545
|
+
await writeFile(assetPath(projectId, relative), encodePng(slice, width, height));
|
|
546
|
+
const label = directionOf(key)?.label ?? key;
|
|
547
|
+
await patchProject(projectId, (current) => {
|
|
548
|
+
current.images[key] = {
|
|
549
|
+
status: "ready",
|
|
550
|
+
file: relative,
|
|
551
|
+
approved: false,
|
|
552
|
+
stale: false,
|
|
553
|
+
model: "转圈截帧",
|
|
554
|
+
updatedAt: Date.now()
|
|
555
|
+
};
|
|
556
|
+
if (current.videos[key]?.file !== undefined || current.videos[key]?.status !== "empty") {
|
|
557
|
+
current.videos[key] = { status: "empty", approved: false };
|
|
558
|
+
}
|
|
559
|
+
if (current.frames[key]?.raw !== undefined || current.frames[key]?.status !== "empty") {
|
|
560
|
+
current.frames[key] = { status: "empty", frames: [], keyed: [], approved: false };
|
|
561
|
+
}
|
|
562
|
+
current.sheet = { status: "empty", approved: false };
|
|
563
|
+
log(current, "info", `截出「${label}」:第 ${index + 1}/${total} 帧(${(node.times[index] ?? 0).toFixed(2)} 秒)`);
|
|
564
|
+
});
|
|
565
|
+
// 这一格的方向图已经写出来了,把它从「抽帧中」的覆盖面里摘掉。
|
|
566
|
+
dropJobTarget(projectId, "turn:frames", key);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
/** 改一个方向的截帧位置(拖动圆圈松手时调用),只重切那一张。 */
|
|
570
|
+
export async function setTurnPick(projectId, key, index) {
|
|
571
|
+
const project = await readProject(projectId);
|
|
572
|
+
if (project === undefined)
|
|
573
|
+
throw new Error(`项目不存在:${projectId}`);
|
|
574
|
+
if (directionOf(key) === undefined)
|
|
575
|
+
throw new Error(`未知方向:${key}`);
|
|
576
|
+
const total = project.turn?.frames?.frames?.length ?? 0;
|
|
577
|
+
const next = clampInt(index, 0, Math.max(0, total - 1));
|
|
578
|
+
const currentPick = project.turn?.frames?.picks?.[key];
|
|
579
|
+
const unchanged = currentPick === next && project.images[key]?.file !== undefined;
|
|
580
|
+
await patchProject(projectId, (current) => {
|
|
581
|
+
current.turn.frames.picks = { ...current.turn.frames.picks, [key]: next };
|
|
582
|
+
});
|
|
583
|
+
// 位置没变、图也已经在,就别白重切一遍——更要紧的是**不要**因此把下游作废。
|
|
584
|
+
if (!unchanged)
|
|
585
|
+
await applyTurnPicks(projectId, [key]);
|
|
586
|
+
return { index: next, changed: !unchanged };
|
|
587
|
+
}
|
|
588
|
+
/** 重新铺一遍八个位置(用一个转圈方向的默认等分位置)。 */
|
|
589
|
+
export async function resetTurnPicks(projectId, direction) {
|
|
590
|
+
const project = await readProject(projectId);
|
|
591
|
+
if (project === undefined)
|
|
592
|
+
throw new Error(`项目不存在:${projectId}`);
|
|
593
|
+
const round = direction ?? project.turn?.direction ?? TURN_DIRECTION_DEFAULT;
|
|
594
|
+
const count = project.turn?.frames?.frames?.length || project.settings.turnFrameCount;
|
|
595
|
+
await patchProject(projectId, (current) => {
|
|
596
|
+
current.turn.direction = round;
|
|
597
|
+
current.turn.frames.picks = freshPicks(count, round);
|
|
598
|
+
log(current, "info", `八个截帧位置已重置为${round === "ccw" ? "逆时针" : "顺时针"}的默认等分位置(依次:${turnOrder(round)
|
|
599
|
+
.map((key) => directionOf(key)?.label ?? key)
|
|
600
|
+
.join(" → ")})`);
|
|
601
|
+
});
|
|
602
|
+
if ((project.turn?.frames?.raw ?? undefined) !== undefined)
|
|
603
|
+
await applyTurnPicks(projectId);
|
|
604
|
+
}
|
|
605
|
+
/**
|
|
606
|
+
* 一次写多个截帧位置(agent 看完条带后整份写回,与 `setRigLayoutHints` 同一角色)。
|
|
607
|
+
*
|
|
608
|
+
* 只看单张方向图判断不了「是不是转反了」——必须对着整圈条带看。所以这条路的
|
|
609
|
+
* 正确用法是:`read_image` 看 `turn.stripUrl` → 判断八个朝向各落在第几帧 →
|
|
610
|
+
* 一次写回,而不是逐个点方向图去猜。
|
|
611
|
+
*/
|
|
612
|
+
export async function setTurnPicks(projectId, picks) {
|
|
613
|
+
const project = await readProject(projectId);
|
|
614
|
+
if (project === undefined)
|
|
615
|
+
throw new Error(`项目不存在:${projectId}`);
|
|
616
|
+
const total = project.turn?.frames?.frames?.length ?? 0;
|
|
617
|
+
const next = { ...(project.turn?.frames?.picks ?? {}) };
|
|
618
|
+
const keys = [];
|
|
619
|
+
for (const [key, value] of Object.entries(picks)) {
|
|
620
|
+
if (directionOf(key) === undefined)
|
|
621
|
+
throw new Error(`未知方向:${key}`);
|
|
622
|
+
if (!Number.isFinite(Number(value)))
|
|
623
|
+
throw new Error(`${key} 的截帧位置必须是数字`);
|
|
624
|
+
next[key] = clampInt(Number(value), 0, Math.max(0, total - 1));
|
|
625
|
+
keys.push(key);
|
|
626
|
+
}
|
|
627
|
+
if (keys.length === 0)
|
|
628
|
+
throw new Error("没有要改的方向");
|
|
629
|
+
await patchProject(projectId, (current) => {
|
|
630
|
+
current.turn.frames.picks = next;
|
|
631
|
+
});
|
|
632
|
+
if (total > 0)
|
|
633
|
+
await applyTurnPicks(projectId, keys);
|
|
634
|
+
return { picks: next };
|
|
635
|
+
}
|
|
636
|
+
/** 切换阶段①的生成方式。只换「怎么产出八张图」,产物一律不动。 */
|
|
637
|
+
export async function setImageMode(projectId, mode) {
|
|
638
|
+
await patchProject(projectId, (project) => {
|
|
639
|
+
if (project.imageMode === mode)
|
|
640
|
+
return;
|
|
641
|
+
project.imageMode = mode;
|
|
642
|
+
log(project, "info", mode === "turn" ? "阶段①改用「转圈截帧」" : "阶段①改用「逐方向生图」");
|
|
643
|
+
});
|
|
644
|
+
}
|
|
245
645
|
// ── 阶段 2:八段视频 ──────────────────────────────────────────────────────
|
|
246
646
|
export function startVideos(projectId, keys) {
|
|
247
647
|
const started = kick(projectId, "videos:submit", "提交视频任务", async () => {
|
|
@@ -355,7 +755,10 @@ export async function pollVideosOnce(projectId) {
|
|
|
355
755
|
}
|
|
356
756
|
const config = await loadConfig();
|
|
357
757
|
const pending = DIRECTION_KEYS.filter((key) => project.videos[key]?.status === "running" && typeof project.videos[key]?.taskId === "string");
|
|
358
|
-
|
|
758
|
+
// 转圈视频走同一个轮询器:它和八段行走视频是同一类任务(同一个模型、同一套
|
|
759
|
+
// query/download),单独再起一个定时器只多一份「谁负责停」的账。
|
|
760
|
+
const turnPending = project.turn?.video?.status === "running" && typeof project.turn?.video?.taskId === "string";
|
|
761
|
+
if (pending.length === 0 && !turnPending) {
|
|
359
762
|
stopPoller(projectId);
|
|
360
763
|
return;
|
|
361
764
|
}
|
|
@@ -408,8 +811,13 @@ export async function pollVideosOnce(projectId) {
|
|
|
408
811
|
});
|
|
409
812
|
}
|
|
410
813
|
}));
|
|
814
|
+
if (turnPending) {
|
|
815
|
+
await pollTurnVideoOnce(projectId, config).catch(() => undefined);
|
|
816
|
+
}
|
|
411
817
|
const after = await readProject(projectId);
|
|
412
|
-
if (after !== undefined &&
|
|
818
|
+
if (after !== undefined &&
|
|
819
|
+
!DIRECTION_KEYS.some((key) => after.videos[key]?.status === "running") &&
|
|
820
|
+
after.turn?.video?.status !== "running") {
|
|
413
821
|
stopPoller(projectId);
|
|
414
822
|
}
|
|
415
823
|
}
|
|
@@ -538,6 +946,12 @@ async function renderSheet(projectId, options = {}) {
|
|
|
538
946
|
const rows = [];
|
|
539
947
|
let hasFrames = false;
|
|
540
948
|
let working = { width: 0, height: 0 };
|
|
949
|
+
// 抠像诊断:背景占比能看出「是不是没抠干净」,被排除的边框采样点能看出
|
|
950
|
+
// 「角色是不是贴到了画面边缘」(贴边时那一小撮角色色会被挡在调色板之外)。
|
|
951
|
+
let backgroundSum = 0;
|
|
952
|
+
let keyedSamples = 0;
|
|
953
|
+
let borderSamples = 0;
|
|
954
|
+
let borderSamplesDropped = 0;
|
|
541
955
|
for (const key of order) {
|
|
542
956
|
const node = project.frames[key];
|
|
543
957
|
if (node?.rawWidth !== undefined && node.rawWidth > 0)
|
|
@@ -557,6 +971,10 @@ async function renderSheet(projectId, options = {}) {
|
|
|
557
971
|
for (let i = 0; i < count; i++) {
|
|
558
972
|
const slice = Buffer.from(raw.subarray(i * frameBytes, (i + 1) * frameBytes));
|
|
559
973
|
const result = keyGreen(slice, width, height, keyOptions);
|
|
974
|
+
backgroundSum += result.backgroundFraction;
|
|
975
|
+
keyedSamples++;
|
|
976
|
+
borderSamples += result.borderSamples;
|
|
977
|
+
borderSamplesDropped += result.borderSamplesDropped;
|
|
560
978
|
row.frames.push(result.rgba);
|
|
561
979
|
keyedBuffers.push(result.rgba);
|
|
562
980
|
const relative = `keyed/${key}/f${String(i).padStart(2, "0")}.png`;
|
|
@@ -613,6 +1031,7 @@ async function renderSheet(projectId, options = {}) {
|
|
|
613
1031
|
const relative = "out/sheet.png";
|
|
614
1032
|
await writeFile(assetPath(projectId, relative), encodePng(sheet.rgba, sheet.width, sheet.height));
|
|
615
1033
|
await patchProject(projectId, (current) => {
|
|
1034
|
+
const backgroundFraction = keyedSamples > 0 ? backgroundSum / keyedSamples : undefined;
|
|
616
1035
|
current.sheet = {
|
|
617
1036
|
status: "ready",
|
|
618
1037
|
file: relative,
|
|
@@ -624,9 +1043,15 @@ async function renderSheet(projectId, options = {}) {
|
|
|
624
1043
|
rowOrder: [...order],
|
|
625
1044
|
cellWidth: settings.cellWidth,
|
|
626
1045
|
cellHeight: settings.cellHeight,
|
|
627
|
-
frameCount: settings.frameCount
|
|
1046
|
+
frameCount: settings.frameCount,
|
|
1047
|
+
backgroundFraction,
|
|
1048
|
+
borderSamples,
|
|
1049
|
+
borderSamplesDropped
|
|
628
1050
|
};
|
|
629
|
-
|
|
1051
|
+
const droppedNote = borderSamplesDropped > 0
|
|
1052
|
+
? `;边框采样 ${borderSamples} 个里排除了 ${borderSamplesDropped} 个不属于背景主色的点(通常是角色贴到了画面边缘,已自动排除)`
|
|
1053
|
+
: "";
|
|
1054
|
+
log(current, "info", `整图合成完成:${sheet.width}×${sheet.height}(${sheet.columns} 列 × ${sheet.rows} 行,裁剪框 ${sheet.bbox.width}×${sheet.bbox.height},背景占比 ${backgroundFraction === undefined ? "?" : (backgroundFraction * 100).toFixed(1)}%${droppedNote})`);
|
|
630
1055
|
});
|
|
631
1056
|
}
|
|
632
1057
|
catch (error) {
|