dsh-game-material-master 0.11.0 → 0.11.2
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 +2 -0
- package/README.md +2 -0
- package/lib/chroma.js +74 -15
- package/lib/client.js +233 -48
- package/lib/config.js +6 -3
- package/lib/customimage.js +43 -0
- package/lib/directions.js +69 -8
- package/lib/imagegen.js +18 -4
- package/lib/imageprovider.js +7 -2
- package/lib/index.js +61 -12
- package/lib/isocompose.js +26 -17
- package/lib/isogen.js +7 -3
- package/lib/keycolor.js +251 -0
- package/lib/mapgen.js +11 -7
- package/lib/pipeline.js +10 -3
- package/lib/riggen.js +4 -4
- package/lib/seqgen.js +7 -0
- package/lib/store.js +23 -12
- package/lib/toolguides.js +14 -14
- package/lib/wire.js +13 -5
- package/package.json +1 -1
package/lib/keycolor.js
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 抠像底色(chroma key 背景色)。
|
|
3
|
+
*
|
|
4
|
+
* 这个模块解决两件事:
|
|
5
|
+
*
|
|
6
|
+
* 1. **抠像不再只认绿色**。角色自己穿绿衣服时,绿幕会把衣服一起吃掉;用户可以
|
|
7
|
+
* 改用品红 / 蓝 / 白 / 黑等背景。这里负责把界面 / 配置里的颜色规整成
|
|
8
|
+
* `#RRGGBB`,并给出颜色名与颜色通道方向(去溢色要用)。
|
|
9
|
+
* 2. **提示词里的「绿幕」跟着换**。默认提示词里写死了「纯绿幕 / #00FF00」,
|
|
10
|
+
* 配置成蓝幕后如果还把这些字发给模型,模型会继续画绿幕 —— 于是抠像参数
|
|
11
|
+
* 与画面背景对不上。`applyKeyColorToPrompt` / `backgroundClause` 就是这层的
|
|
12
|
+
* 唯一真源:服务端默认提示词与浏览器半区的 AI 素材提示词都走它。
|
|
13
|
+
*
|
|
14
|
+
* 颜色计算只做纯函数,不读配置、不碰 IO —— 这样 chroma / directions / client
|
|
15
|
+
* 都能安全引用,也方便自检逐条断言。
|
|
16
|
+
*/
|
|
17
|
+
/** 默认抠像底色:标准 chroma key 绿。 */
|
|
18
|
+
export const DEFAULT_KEY_COLOR = "#00FF00";
|
|
19
|
+
/**
|
|
20
|
+
* 内置可选底色。
|
|
21
|
+
*
|
|
22
|
+
* 只放**饱和、均匀**的纯色:抠像最怕背景本身有纹理 / 渐变,模型也最容易画准
|
|
23
|
+
* 这种颜色。白 / 黑放在最后:它们适合「绿、蓝、品红都不能用」的极端衣服配色,
|
|
24
|
+
* 但边缘溢色不如彩色幕好处理。
|
|
25
|
+
*/
|
|
26
|
+
export const KEY_COLOR_PRESETS = [
|
|
27
|
+
{ hex: "#00FF00", zh: "绿幕", en: "green screen", zhColor: "绿", enColor: "green" },
|
|
28
|
+
{ hex: "#0000FF", zh: "蓝幕", en: "blue screen", zhColor: "蓝", enColor: "blue" },
|
|
29
|
+
{ hex: "#FF00FF", zh: "品红幕", en: "magenta screen", zhColor: "品红", enColor: "magenta" },
|
|
30
|
+
{ hex: "#00FFFF", zh: "青幕", en: "cyan screen", zhColor: "青", enColor: "cyan" },
|
|
31
|
+
{ hex: "#FFFF00", zh: "黄幕", en: "yellow screen", zhColor: "黄", enColor: "yellow" },
|
|
32
|
+
{ hex: "#FF0000", zh: "红幕", en: "red screen", zhColor: "红", enColor: "red" },
|
|
33
|
+
{ hex: "#FFFFFF", zh: "白幕", en: "white screen", zhColor: "白", enColor: "white" },
|
|
34
|
+
{ hex: "#000000", zh: "黑幕", en: "black screen", zhColor: "黑", enColor: "black" }
|
|
35
|
+
];
|
|
36
|
+
const NAMED_COLORS = {
|
|
37
|
+
green: "#00FF00",
|
|
38
|
+
blue: "#0000FF",
|
|
39
|
+
magenta: "#FF00FF",
|
|
40
|
+
cyan: "#00FFFF",
|
|
41
|
+
yellow: "#FFFF00",
|
|
42
|
+
red: "#FF0000",
|
|
43
|
+
white: "#FFFFFF",
|
|
44
|
+
black: "#000000",
|
|
45
|
+
gray: "#808080",
|
|
46
|
+
grey: "#808080",
|
|
47
|
+
绿: "#00FF00",
|
|
48
|
+
绿幕: "#00FF00",
|
|
49
|
+
蓝: "#0000FF",
|
|
50
|
+
蓝幕: "#0000FF",
|
|
51
|
+
品红: "#FF00FF",
|
|
52
|
+
品红幕: "#FF00FF",
|
|
53
|
+
青: "#00FFFF",
|
|
54
|
+
青幕: "#00FFFF",
|
|
55
|
+
黄: "#FFFF00",
|
|
56
|
+
黄幕: "#FFFF00",
|
|
57
|
+
红: "#FF0000",
|
|
58
|
+
红幕: "#FF0000",
|
|
59
|
+
白: "#FFFFFF",
|
|
60
|
+
白幕: "#FFFFFF",
|
|
61
|
+
黑: "#000000",
|
|
62
|
+
黑幕: "#000000"
|
|
63
|
+
};
|
|
64
|
+
/** 任意输入 → 规范的 `#RRGGBB`;认不出来回退到 `fallback`。 */
|
|
65
|
+
export function normalizeKeyColor(value, fallback = DEFAULT_KEY_COLOR) {
|
|
66
|
+
const text = typeof value === "string" ? value.trim().toLowerCase() : "";
|
|
67
|
+
if (text === "")
|
|
68
|
+
return fallback;
|
|
69
|
+
const named = NAMED_COLORS[text];
|
|
70
|
+
if (named !== undefined)
|
|
71
|
+
return named;
|
|
72
|
+
const short = /^#([0-9a-f]{3})$/.exec(text);
|
|
73
|
+
if (short !== null) {
|
|
74
|
+
const [r, g, b] = short[1].split("");
|
|
75
|
+
return `#${r}${r}${g}${g}${b}${b}`.toUpperCase();
|
|
76
|
+
}
|
|
77
|
+
const full = /^#?([0-9a-f]{6})$/.exec(text);
|
|
78
|
+
if (full !== null)
|
|
79
|
+
return `#${full[1]}`.toUpperCase();
|
|
80
|
+
return fallback;
|
|
81
|
+
}
|
|
82
|
+
/** `#RRGGBB` → 0~255 三个通道。 */
|
|
83
|
+
export function keyColorRgb(hex) {
|
|
84
|
+
const color = normalizeKeyColor(hex);
|
|
85
|
+
return {
|
|
86
|
+
r: Number.parseInt(color.slice(1, 3), 16),
|
|
87
|
+
g: Number.parseInt(color.slice(3, 5), 16),
|
|
88
|
+
b: Number.parseInt(color.slice(5, 7), 16)
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/** 是不是「绿幕」那一档。绿色走原有的绿色优势判据(对实拍 / 视频帧更稳)。 */
|
|
92
|
+
export function isGreenKeyColor(hex) {
|
|
93
|
+
const { r, g, b } = keyColorRgb(hex);
|
|
94
|
+
return g > r + 64 && g > b + 64;
|
|
95
|
+
}
|
|
96
|
+
/** 当前底色对应的预设;自定义颜色返回 undefined。 */
|
|
97
|
+
export function keyColorPreset(hex) {
|
|
98
|
+
const color = normalizeKeyColor(hex);
|
|
99
|
+
return KEY_COLOR_PRESETS.find((preset) => preset.hex === color);
|
|
100
|
+
}
|
|
101
|
+
/** 颜色字的语言版本。自定义颜色退回 `#RRGGBB`。 */
|
|
102
|
+
export function keyColorWord(hex, lang = "zh") {
|
|
103
|
+
const preset = keyColorPreset(hex);
|
|
104
|
+
if (preset !== undefined)
|
|
105
|
+
return lang === "en" ? preset.enColor : preset.zhColor;
|
|
106
|
+
return normalizeKeyColor(hex);
|
|
107
|
+
}
|
|
108
|
+
/** 「绿幕 / 蓝幕 / 品红幕…」;自定义颜色说「纯色背景」。 */
|
|
109
|
+
export function keyScreenName(hex, lang = "zh") {
|
|
110
|
+
const preset = keyColorPreset(hex);
|
|
111
|
+
if (preset !== undefined)
|
|
112
|
+
return lang === "en" ? preset.en : preset.zh;
|
|
113
|
+
return lang === "en" ? "solid background" : "纯色背景";
|
|
114
|
+
}
|
|
115
|
+
/** 是否标准 `#00FF00`:默认提示词与旧项目都按这个判断。 */
|
|
116
|
+
export function isDefaultKeyColor(hex) {
|
|
117
|
+
return normalizeKeyColor(hex) === DEFAULT_KEY_COLOR;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* 去溢色要压制的通道。
|
|
121
|
+
*
|
|
122
|
+
* 绿幕只压 G;蓝幕只压 B;品红幕同时压 R / B。返回**主色通道的下标**,
|
|
123
|
+
* 其余通道取最大值作为「其它颜色」的基准 —— 这样品红幕的白色边缘(R≈G≈B)
|
|
124
|
+
* 不会被误压,而偏品红的软边会被拉回中性。
|
|
125
|
+
*/
|
|
126
|
+
export function keyDominantChannels(hex) {
|
|
127
|
+
const { r, g, b } = keyColorRgb(hex);
|
|
128
|
+
const channels = [r, g, b];
|
|
129
|
+
const out = [];
|
|
130
|
+
for (let i = 0; i < 3; i++) {
|
|
131
|
+
const others = channels.filter((_, index) => index !== i);
|
|
132
|
+
const otherAverage = (others[0] + others[1]) / 2;
|
|
133
|
+
if (channels[i] - otherAverage >= 48)
|
|
134
|
+
out.push(i);
|
|
135
|
+
}
|
|
136
|
+
return out;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* 默认提示词里「整幅背景」那句话。
|
|
140
|
+
*
|
|
141
|
+
* 这一段会**真的发给模型**,所以绿色必须保持与旧版逐字一致(有一批老项目、
|
|
142
|
+
* 自检与模型行为都按那几句验证过);其它颜色按同一句式生成。
|
|
143
|
+
*/
|
|
144
|
+
export function backgroundClause(hex, lang = "zh", variant = "image") {
|
|
145
|
+
const color = normalizeKeyColor(hex);
|
|
146
|
+
if (color === DEFAULT_KEY_COLOR) {
|
|
147
|
+
return variant === "turn"
|
|
148
|
+
? lang === "en"
|
|
149
|
+
? "Solid green screen, the color is standard chroma key green #00FF00, the whole background is a completely uniform single color, pure green throughout, with no shadows, gradients, ground or cast shadow."
|
|
150
|
+
: "纯色绿幕,颜色是标准 chroma key 绿 #00FF00,整幅背景颜色完全均匀一致,全程保持纯绿,不要任何阴影、渐变、地面或投影。"
|
|
151
|
+
: lang === "en"
|
|
152
|
+
? "Solid green screen, the color is standard chroma key green #00FF00, the whole background is a completely uniform single color, with no shadows, gradients or noise of any kind."
|
|
153
|
+
: "纯色绿幕,颜色是标准 chroma key 绿 #00FF00,整幅背景颜色完全均匀一致,不要任何阴影、渐变或噪点。";
|
|
154
|
+
}
|
|
155
|
+
const screen = keyScreenName(color, lang);
|
|
156
|
+
const word = keyColorWord(color, lang);
|
|
157
|
+
const screenClause = keyColorPreset(color) === undefined ? (lang === "en" ? "Solid background" : "纯色背景") : (lang === "en" ? `Solid ${screen}` : `纯色${screen}`);
|
|
158
|
+
if (variant === "turn") {
|
|
159
|
+
return lang === "en"
|
|
160
|
+
? `${screenClause} (${color}), the whole background is a completely uniform single color, pure ${word} throughout, with no shadows, gradients, ground or cast shadow.`
|
|
161
|
+
: `${screenClause},颜色是 ${color},整幅背景颜色完全均匀一致,全程保持纯${word},不要任何阴影、渐变、地面或投影。`;
|
|
162
|
+
}
|
|
163
|
+
return lang === "en"
|
|
164
|
+
? `${screenClause} (${color}), the whole background is a completely uniform single color, with no shadows, gradients or noise of any kind.`
|
|
165
|
+
: `${screenClause},颜色是 ${color},整幅背景颜色完全均匀一致,不要任何阴影、渐变或噪点。`;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* 把默认提示词里的背景描述换成目标颜色。
|
|
169
|
+
*
|
|
170
|
+
* 只替换**内置默认句式**(对所有内置颜色 + 目标自定义颜色都认),用户自己改写过的
|
|
171
|
+
* 句子一个字都不动。这样:
|
|
172
|
+
* · 老项目里存着「纯绿幕 #00FF00」的默认提示词,切到蓝幕后仍会自动更新;
|
|
173
|
+
* · 用户手写「我要一个绿色背景」不会被误替换(不匹配默认句式)。
|
|
174
|
+
*/
|
|
175
|
+
export function applyKeyColorToPrompt(text, hex, lang = "zh") {
|
|
176
|
+
if (typeof text !== "string" || text === "")
|
|
177
|
+
return text;
|
|
178
|
+
const target = normalizeKeyColor(hex);
|
|
179
|
+
let out = text;
|
|
180
|
+
// 1) 整句背景:内置颜色 + 目标自定义颜色都替换成目标句式。
|
|
181
|
+
const colors = new Set([target, ...KEY_COLOR_PRESETS.map((preset) => preset.hex)]);
|
|
182
|
+
const variants = ["image", "turn"];
|
|
183
|
+
for (const variant of variants) {
|
|
184
|
+
const replacement = backgroundClause(target, lang, variant);
|
|
185
|
+
for (const color of colors) {
|
|
186
|
+
const old = backgroundClause(color, lang, variant);
|
|
187
|
+
if (old !== replacement)
|
|
188
|
+
out = out.split(old).join(replacement);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
// 2) 句中的短称呼:`绿幕背景` / `干净的绿幕` / `同一片绿` / `抠绿幕` …
|
|
192
|
+
// 也覆盖「目标颜色是绿」时从蓝幕 / 品红幕默认值改回来的情况。
|
|
193
|
+
const screenNames = new Set(KEY_COLOR_PRESETS.map((preset) => (lang === "en" ? preset.en : preset.zh)));
|
|
194
|
+
const targetScreen = keyScreenName(target, lang);
|
|
195
|
+
const targetWord = keyColorWord(target, lang);
|
|
196
|
+
const targetShort = lang === "en"
|
|
197
|
+
? keyColorPreset(target) === undefined ? targetWord : targetScreen.replace(/\s+screen$/u, "")
|
|
198
|
+
: targetScreen.endsWith("幕") ? targetScreen.slice(0, -1) : targetWord;
|
|
199
|
+
const targetBackground = lang === "en"
|
|
200
|
+
? keyColorPreset(target) === undefined ? "solid background" : `${targetScreen} background`
|
|
201
|
+
: targetScreen.endsWith("幕") ? `${targetScreen}背景` : "纯色背景";
|
|
202
|
+
for (const screen of screenNames) {
|
|
203
|
+
const short = lang === "en" ? screen.replace(/\s+screen$/u, "") : screen.replace(/幕$/u, "");
|
|
204
|
+
if (screen === targetScreen && short === targetShort)
|
|
205
|
+
continue;
|
|
206
|
+
out = out.split(`${screen}背景`).join(targetBackground);
|
|
207
|
+
out = out.split(`干净的${screen}`).join(`干净的${targetScreen}`);
|
|
208
|
+
out = out.split(`没检测到${screen}`).join(`没检测到${targetScreen}`);
|
|
209
|
+
out = out.split(`抠${screen}`).join(`抠${targetScreen}`);
|
|
210
|
+
out = out.split(`抠${short}`).join(`抠${targetShort}`);
|
|
211
|
+
out = out.split(`抠掉${screen}背景`).join(`抠掉${targetBackground}`);
|
|
212
|
+
out = out.split(`同一片${short}`).join(`同一片${targetShort}`);
|
|
213
|
+
out = out.split(`纯${short}`).join(`纯${targetShort}`);
|
|
214
|
+
out = out.split(`每一片${short}`).join(`每一片${targetShort}`);
|
|
215
|
+
out = out.split(`${short}色描边`).join(`${targetWord}色描边`);
|
|
216
|
+
out = out.split(`${short}色外发光`).join(`${targetWord}色外发光`);
|
|
217
|
+
if (lang === "en") {
|
|
218
|
+
out = out.split(`${screen} background`).join(targetBackground);
|
|
219
|
+
out = out.split(`clean ${screen}`).join(`clean ${targetScreen}`);
|
|
220
|
+
out = out.split(`the same ${short} background`).join(`the same ${targetShort} background`);
|
|
221
|
+
out = out.split(`${short} background`).join(targetBackground);
|
|
222
|
+
out = out.split(`pure ${short}`).join(`pure ${targetWord}`);
|
|
223
|
+
out = out.split(`${short} outline`).join(`${targetWord} outline`);
|
|
224
|
+
out = out.split(`${short} glow`).join(`${targetWord} glow`);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
// 3) 自定义旧颜色(不是任何内置预设)的整句背景:把 `颜色是 #XXXXXX` 所在句替换成目标句。
|
|
228
|
+
// 只匹配「颜色是 #RRGGBB」到句号,避免误伤其它提到色值的文字。
|
|
229
|
+
if (lang === "zh") {
|
|
230
|
+
out = out.replace(/纯色[^,。\n]*,颜色是 #[0-9A-Fa-f]{6},整幅背景颜色完全均匀一致,不要[^。\n]*。/gu, backgroundClause(target, lang, "image"));
|
|
231
|
+
out = out.replace(/纯色[^,。\n]*,颜色是 #[0-9A-Fa-f]{6},[^。\n]*全程保持[^。\n]*。/gu, backgroundClause(target, lang, "turn"));
|
|
232
|
+
}
|
|
233
|
+
if (lang === "en") {
|
|
234
|
+
out = out.replace(/Solid [^,\n]*#[0-9A-Fa-f]{6}[^,\n]*, the whole background is a completely uniform single color, pure [^.]*\./gu, backgroundClause(target, lang, "turn"));
|
|
235
|
+
out = out.replace(/Solid [^,\n]*#[0-9A-Fa-f]{6}[^,\n]*, the whole background is a completely uniform single color, with no [^.]*\./gu, backgroundClause(target, lang, "image"));
|
|
236
|
+
}
|
|
237
|
+
return out;
|
|
238
|
+
}
|
|
239
|
+
/** 一句短背景描述,给「背景保持原样」这类句中位置用。 */
|
|
240
|
+
export function shortBackground(hex, lang = "zh") {
|
|
241
|
+
const color = normalizeKeyColor(hex);
|
|
242
|
+
if (color === DEFAULT_KEY_COLOR)
|
|
243
|
+
return lang === "en" ? "green screen" : "绿幕";
|
|
244
|
+
return lang === "en" ? keyScreenName(color, "en") : `${keyScreenName(color, "zh")}`;
|
|
245
|
+
}
|
|
246
|
+
/** 一行「当前底色」说明,给界面 / 提示词用。 */
|
|
247
|
+
export function describeKeyColor(hex, lang = "zh") {
|
|
248
|
+
const color = normalizeKeyColor(hex);
|
|
249
|
+
const screen = keyScreenName(color, lang);
|
|
250
|
+
return lang === "en" ? `${screen} (${color})` : `${screen}(${color})`;
|
|
251
|
+
}
|
package/lib/mapgen.js
CHANGED
|
@@ -40,6 +40,7 @@ import { appendJobLog, messageOf, readJson, sweepTempFiles, writeJsonAtomic } fr
|
|
|
40
40
|
import { dataRoot } from "./config.js";
|
|
41
41
|
import { decodeFile, encodeBitmap, pngSize, probeImageSize, sniffImageExt } from "./bitmap.js";
|
|
42
42
|
import { keyGreen } from "./chroma.js";
|
|
43
|
+
import { keyScreenName, normalizeKeyColor } from "./keycolor.js";
|
|
43
44
|
import { addLayer, applyOps, cellIndex, clearLayer, cloneDoc, createMapDoc, filledCount, isDocEmpty, moveLayer, normalizeMapDoc, removeLayer, renameLayer, resizeDoc, revertOps } from "./mapdoc.js";
|
|
44
45
|
import { assertGrid, assertMapSize, canonicalChunk, chunkKey, chunksInRange, dirtyChunksFor, layoutOf, normalizeGrid, parseChunkKey, viewCellRange, wrapCell, MAX_MAP_DIM, MAX_PLAN_CHUNKS } from "./mapgeom.js";
|
|
45
46
|
import { bitmapToPng, buildPlan, rasterize, renderMapView, stampCellsOf } from "./maprender.js";
|
|
@@ -807,7 +808,7 @@ export async function importMapTileset(projectId, name, base64, opts = {}) {
|
|
|
807
808
|
id: newTilesetId(),
|
|
808
809
|
name: name.trim() === "" ? `素材 ${project.tilesets.length + 1}` : name.trim(),
|
|
809
810
|
bytes,
|
|
810
|
-
overrides: { cell: opts.cell, shape: opts.shape, animRows: opts.animRows, fps: opts.fps, clarity: opts.clarity, scale: opts.scale, loop: opts.loop, stamp: opts.stamp, stampKey: opts.stampKey },
|
|
811
|
+
overrides: { cell: opts.cell, shape: opts.shape, animRows: opts.animRows, fps: opts.fps, clarity: opts.clarity, scale: opts.scale, loop: opts.loop, stamp: opts.stamp, stampKey: opts.stampKey, keyColor: opts.keyColor },
|
|
811
812
|
projectGrid: { width: project.grid.tileWidth, height: project.grid.tileHeight }
|
|
812
813
|
}, warnings);
|
|
813
814
|
await patchMapProject(projectId, (fresh) => {
|
|
@@ -957,8 +958,10 @@ export async function composeMapAsset(input) {
|
|
|
957
958
|
const frames = [];
|
|
958
959
|
/** 每张源图裁掉了多少边(全部为 0 时说明没白边,不用提)。 */
|
|
959
960
|
const trimmedAmounts = [];
|
|
960
|
-
/**
|
|
961
|
+
/** 整图抠背景的结果(keyed = 被判成背景的像素占比;0 = 没抠)。 */
|
|
961
962
|
const keyedAmounts = [];
|
|
963
|
+
const keyColor = normalizeKeyColor(input.keyColor);
|
|
964
|
+
const screen = keyScreenName(keyColor, "zh");
|
|
962
965
|
for (const file of files) {
|
|
963
966
|
let info;
|
|
964
967
|
try {
|
|
@@ -982,7 +985,7 @@ export async function composeMapAsset(input) {
|
|
|
982
985
|
* 留着会在地图上出现一块绿框,而且把本该空着的角落也涂满。
|
|
983
986
|
* 其它形态不抠(它们的背景就是材质本身)。
|
|
984
987
|
*/
|
|
985
|
-
const keyed = shape === "stamp" ? keyStampBackground(await decodeFile(file), (rgba, width, height) => keyGreen(rgba, width, height, STAMP_KEY_OPTIONS)) : undefined;
|
|
988
|
+
const keyed = shape === "stamp" ? keyStampBackground(await decodeFile(file), (rgba, width, height) => keyGreen(rgba, width, height, { ...STAMP_KEY_OPTIONS, keyColor })) : undefined;
|
|
986
989
|
if (keyed !== undefined)
|
|
987
990
|
keyedAmounts.push(keyed.keyed);
|
|
988
991
|
const trimmed = trimUniformBorder(keyed === undefined ? (keyed === undefined ? await decodeFile(file) : keyed.bitmap) : keyed.bitmap);
|
|
@@ -1020,11 +1023,11 @@ export async function composeMapAsset(input) {
|
|
|
1020
1023
|
const keyedCount = keyedAmounts.filter((fraction) => fraction > 0).length;
|
|
1021
1024
|
if (keyedCount > 0) {
|
|
1022
1025
|
const average = keyedAmounts.reduce((sum, value) => sum + value, 0) / keyedAmounts.length;
|
|
1023
|
-
notes.push(
|
|
1024
|
-
|
|
1026
|
+
notes.push(`已抠掉${screen}背景(平均 ${Math.round(average * 100)}% 的像素变成透明${keyedCount < frames.length ? `,${keyedCount}/${frames.length} 张抠掉了` : ""}):` +
|
|
1027
|
+
`整图是「立在格子上的物件」,背景要透明才看得见地砖;留着背景的话地图上会多出一块色块。`);
|
|
1025
1028
|
}
|
|
1026
1029
|
else {
|
|
1027
|
-
notes.push(
|
|
1030
|
+
notes.push(`**没检测到${screen}背景**:这次按原图用了(画面会连背景一起印到地图上)。想要背景透明,让模型把背景画成**纯色 ${keyColor}**(不要渐变 / 阴影),或者自己用透明 PNG。`);
|
|
1028
1031
|
}
|
|
1029
1032
|
}
|
|
1030
1033
|
{
|
|
@@ -1153,9 +1156,10 @@ async function registerTileset(projectId, input, warnings) {
|
|
|
1153
1156
|
try {
|
|
1154
1157
|
// 「导入时抠绿幕」:手动导一张带绿幕的整图时用(AI 那条路在 composeMapAsset 里已经抠过)。
|
|
1155
1158
|
const keyedSource = input.overrides?.stampKey === true;
|
|
1159
|
+
const keyColor = normalizeKeyColor(input.overrides?.keyColor);
|
|
1156
1160
|
if (stamp !== undefined && stampCell !== undefined) {
|
|
1157
1161
|
const rawSource = await decodeFile(target);
|
|
1158
|
-
const source = keyedSource ? keyStampBackground(rawSource, (rgba, width, height) => keyGreen(rgba, width, height, STAMP_KEY_OPTIONS)).bitmap : rawSource;
|
|
1162
|
+
const source = keyedSource ? keyStampBackground(rawSource, (rgba, width, height) => keyGreen(rgba, width, height, { ...STAMP_KEY_OPTIONS, keyColor })).bitmap : rawSource;
|
|
1159
1163
|
if (keyedSource) {
|
|
1160
1164
|
// 抠完的源图要写回去:下面 resample + 烘焙读的是盘上这份(也便于排查)。
|
|
1161
1165
|
storedBytes = encodeBitmap(source);
|
package/lib/pipeline.js
CHANGED
|
@@ -25,6 +25,7 @@ import { assertImageProviderReady, generateImageWithProvider, imageModelOf, imag
|
|
|
25
25
|
import { extractFrames, mimeOf, sniffVideo, toDataUri, toJpegDataUri } from "./media.js";
|
|
26
26
|
import { assertVideoBackendReady, downloadVideoTask, queryVideoTask, submitVideoTask, videoFailureReason, videoBackendOf } from "./videogen.js";
|
|
27
27
|
import { composeSheet, keyGreen } from "./chroma.js";
|
|
28
|
+
import { applyKeyColorToPrompt } from "./keycolor.js";
|
|
28
29
|
import { encodePng } from "./png.js";
|
|
29
30
|
import { zipEntries } from "./zip.js";
|
|
30
31
|
import { assetPath, freshPicks, log, patchProject, readProject, referenceFiles } from "./store.js";
|
|
@@ -149,7 +150,8 @@ async function generateOne(projectId, key, promptOverride) {
|
|
|
149
150
|
const config = await loadConfig();
|
|
150
151
|
// 后端没配好就不要往下走:这一步会真实计费,早失败比「跑到一半报错」好。
|
|
151
152
|
assertImageProviderReady(config);
|
|
152
|
-
const
|
|
153
|
+
const keyColor = project.settings.keyColorOverride === true ? project.settings.keyColor : config.keyColor;
|
|
154
|
+
const basePrompt = applyKeyColorToPrompt((promptOverride ?? project.prompts.images[key] ?? "").trim(), keyColor, "zh");
|
|
153
155
|
if (basePrompt === "")
|
|
154
156
|
throw new Error("提示词为空,请先填写生图提示词");
|
|
155
157
|
// 统一附加提示词接在每个方向的提示词后面(跨方向的共同注意事项)。
|
|
@@ -326,7 +328,8 @@ async function submitTurnVideo(projectId) {
|
|
|
326
328
|
const first = await turnFirstFrame(projectId, project);
|
|
327
329
|
if (first === undefined)
|
|
328
330
|
throw new Error("还没有源图,无法生成转圈视频");
|
|
329
|
-
const
|
|
331
|
+
const keyColor = project.settings.keyColorOverride === true ? project.settings.keyColor : config.keyColor;
|
|
332
|
+
const base = applyKeyColorToPrompt((project.prompts.turn ?? DEFAULT_TURN_PROMPT).trim(), keyColor, "zh");
|
|
330
333
|
if (base === "")
|
|
331
334
|
throw new Error("提示词为空,请先填写转圈视频提示词");
|
|
332
335
|
const suffix = (project.prompts.suffix ?? "").trim();
|
|
@@ -921,7 +924,8 @@ export function startVideos(projectId, keys) {
|
|
|
921
924
|
try {
|
|
922
925
|
const jpegRel = `videos/${key}-first-frame.jpg`;
|
|
923
926
|
const dataUri = await toJpegDataUri(assetPath(projectId, image.file), assetPath(projectId, jpegRel), 1280, 3, videoBackendOf(config) === "toapis" ? "white" : undefined);
|
|
924
|
-
const
|
|
927
|
+
const keyColor = fresh.settings.keyColorOverride === true ? fresh.settings.keyColor : config.keyColor;
|
|
928
|
+
const prompt = applyKeyColorToPrompt((fresh.prompts.videoPerDirection?.[key] ?? fresh.prompts.video).trim(), keyColor, "zh");
|
|
925
929
|
const submitted = await submitVideoTask(config, {
|
|
926
930
|
prompt,
|
|
927
931
|
firstFrameImage: dataUri,
|
|
@@ -1451,11 +1455,14 @@ async function renderSheet(projectId, options = {}) {
|
|
|
1451
1455
|
if (project === undefined)
|
|
1452
1456
|
throw new Error(`项目不存在:${projectId}`);
|
|
1453
1457
|
const settings = project.settings;
|
|
1458
|
+
const config = await loadConfig();
|
|
1459
|
+
const keyColor = settings.keyColorOverride === true ? settings.keyColor : config.keyColor;
|
|
1454
1460
|
await patchProject(projectId, (current) => {
|
|
1455
1461
|
current.sheet = { ...current.sheet, status: "running", error: undefined };
|
|
1456
1462
|
});
|
|
1457
1463
|
try {
|
|
1458
1464
|
const keyOptions = {
|
|
1465
|
+
keyColor,
|
|
1459
1466
|
keyLow: settings.keyLow,
|
|
1460
1467
|
keyHigh: settings.keyHigh,
|
|
1461
1468
|
despill: settings.despill,
|
package/lib/riggen.js
CHANGED
|
@@ -28,7 +28,7 @@ import { mkdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
|
28
28
|
import { basename, join, resolve, sep } from "node:path";
|
|
29
29
|
import { randomUUID } from "node:crypto";
|
|
30
30
|
import { loadConfig, rigJobsRoot } from "./config.js";
|
|
31
|
-
import { assertImageProviderReady, generateImageWithProvider, imageModelOf, imageProviderBlocker, imageSizeOf, imageTimeoutOf, imageWatermarkOf,
|
|
31
|
+
import { assertImageProviderReady, generateImageWithProvider, imageModelOf, imageProviderBlocker, imageSizeOf, imageTimeoutOf, imageWatermarkOf, imageRedrawModelOf } from "./imageprovider.js";
|
|
32
32
|
import { decodeToRgba, mimeOf, toDataUri } from "./media.js";
|
|
33
33
|
import { encodePng } from "./png.js";
|
|
34
34
|
import { appendJobLog, messageOf, readJson, writeJsonAtomic } from "./jsonio.js";
|
|
@@ -2420,8 +2420,8 @@ export async function generateSheet(jobId) {
|
|
|
2420
2420
|
const prompt = buildSheetRequest(job);
|
|
2421
2421
|
const model = imageModelOf(config);
|
|
2422
2422
|
job.settings.model = model;
|
|
2423
|
-
|
|
2424
|
-
|
|
2423
|
+
// 输出尺寸是全局设置:任务只跟随,不保留会过期的副本(见 imagegen 的同名注释)。
|
|
2424
|
+
job.settings.size = imageSizeOf(config);
|
|
2425
2425
|
job.sheet = { ...job.sheet, status: "running", error: undefined };
|
|
2426
2426
|
appendJobLog(job.log, "info", `开始拆件生图(模型 ${model})`);
|
|
2427
2427
|
await writeRigJob(job);
|
|
@@ -2430,7 +2430,7 @@ export async function generateSheet(jobId) {
|
|
|
2430
2430
|
const result = await generateImageWithProvider({
|
|
2431
2431
|
prompt,
|
|
2432
2432
|
images: [reference],
|
|
2433
|
-
size:
|
|
2433
|
+
size: imageSizeOf(config),
|
|
2434
2434
|
// 与图片生成那条路同口径:全局设置或任务上的旧覆盖位,任一为真就带水印。
|
|
2435
2435
|
watermark: imageWatermarkOf(config) || job.settings.watermark === true,
|
|
2436
2436
|
timeoutMs: imageTimeoutOf(config)
|
package/lib/seqgen.js
CHANGED
|
@@ -26,6 +26,7 @@ import { normalizeDuration, normalizeResolution } from "./minimax.js";
|
|
|
26
26
|
import { assertVideoBackendReady, downloadVideoTask, queryVideoTask, submitVideoTask, videoFailureReason } from "./videogen.js";
|
|
27
27
|
import { extractFrames, fileSize, mimeOf, toJpegDataUri } from "./media.js";
|
|
28
28
|
import { composeSheet, keyGreen } from "./chroma.js";
|
|
29
|
+
import { normalizeKeyColor } from "./keycolor.js";
|
|
29
30
|
import { encodePng } from "./png.js";
|
|
30
31
|
import { appendJobLog, messageOf, readJson, writeJsonAtomic } from "./jsonio.js";
|
|
31
32
|
import { safeName, sniffImage } from "./imagegen.js";
|
|
@@ -77,6 +78,7 @@ export async function createSequenceJob(name) {
|
|
|
77
78
|
pixelSize: config.pixelSize
|
|
78
79
|
},
|
|
79
80
|
keying: {
|
|
81
|
+
keyColor: config.keyColor,
|
|
80
82
|
keyLow: config.keyLow,
|
|
81
83
|
keyHigh: config.keyHigh,
|
|
82
84
|
despill: config.despill,
|
|
@@ -178,6 +180,7 @@ function normalizeSequenceJob(raw) {
|
|
|
178
180
|
pixelSize: clampInt(raw?.settings?.pixelSize, 0, 0, 32)
|
|
179
181
|
},
|
|
180
182
|
keying: {
|
|
183
|
+
keyColor: typeof raw?.keying?.keyColor === "string" ? normalizeKeyColor(raw.keying.keyColor) : "",
|
|
181
184
|
keyLow: clampInt(raw?.keying?.keyLow, 14, 0, 255),
|
|
182
185
|
keyHigh: clampInt(raw?.keying?.keyHigh, 80, 1, 255),
|
|
183
186
|
despill: num(raw?.keying?.despill, 0.65),
|
|
@@ -559,6 +562,7 @@ export async function keySequenceFrames(jobId) {
|
|
|
559
562
|
const job = await readSequenceJob(jobId);
|
|
560
563
|
if (job === undefined)
|
|
561
564
|
throw new Error(`任务不存在:${jobId}`);
|
|
565
|
+
const config = await loadConfig();
|
|
562
566
|
if (job.frames.raw === undefined || job.frames.width === undefined || job.frames.height === undefined) {
|
|
563
567
|
throw new Error("还没有抽过帧,请先抽帧");
|
|
564
568
|
}
|
|
@@ -571,6 +575,7 @@ export async function keySequenceFrames(jobId) {
|
|
|
571
575
|
for (let i = 0; i < total; i++) {
|
|
572
576
|
const slice = Buffer.from(raw.subarray(i * frameBytes, (i + 1) * frameBytes));
|
|
573
577
|
const result = keyGreen(slice, width, height, {
|
|
578
|
+
keyColor: job.keying.keyColor || config.keyColor,
|
|
574
579
|
keyLow: job.keying.keyLow,
|
|
575
580
|
keyHigh: job.keying.keyHigh,
|
|
576
581
|
despill: job.keying.despill,
|
|
@@ -593,6 +598,7 @@ export async function composeSequenceSheet(jobId) {
|
|
|
593
598
|
const job = await readSequenceJob(jobId);
|
|
594
599
|
if (job === undefined)
|
|
595
600
|
throw new Error(`任务不存在:${jobId}`);
|
|
601
|
+
const config = await loadConfig();
|
|
596
602
|
if (job.frames.raw === undefined || job.frames.width === undefined || job.frames.height === undefined) {
|
|
597
603
|
throw new Error("还没有抽过帧,请先抽帧");
|
|
598
604
|
}
|
|
@@ -605,6 +611,7 @@ export async function composeSequenceSheet(jobId) {
|
|
|
605
611
|
for (let i = 0; i < total; i++) {
|
|
606
612
|
const slice = Buffer.from(raw.subarray(i * frameBytes, (i + 1) * frameBytes));
|
|
607
613
|
const result = keyGreen(slice, width, height, {
|
|
614
|
+
keyColor: job.keying.keyColor || config.keyColor,
|
|
608
615
|
keyLow: job.keying.keyLow,
|
|
609
616
|
keyHigh: job.keying.keyHigh,
|
|
610
617
|
despill: job.keying.despill,
|
package/lib/store.js
CHANGED
|
@@ -16,8 +16,9 @@ import { mkdir, readFile, readdir, rm, stat } from "node:fs/promises";
|
|
|
16
16
|
import { join, resolve, sep } from "node:path";
|
|
17
17
|
import { randomUUID } from "node:crypto";
|
|
18
18
|
import { writeJsonAtomic } from "./jsonio.js";
|
|
19
|
-
import {
|
|
19
|
+
import { DIRECTION_KEYS, DEFAULT_ROW_ORDER, TURN_FRAME_COUNT_DEFAULT, TURN_DIRECTION_DEFAULT, TURN_FRAME_COUNT_MAX, TURN_FRAME_COUNT_MIN, applyKeyColorToPrompts, defaultPrompts, defaultTurnPicks } from "./directions.js";
|
|
20
20
|
import { DEFAULT_CONFIG, ROW_ORDER_VERSION, isLegacyRowOrder, loadConfig, projectsRoot } from "./config.js";
|
|
21
|
+
import { normalizeKeyColor } from "./keycolor.js";
|
|
21
22
|
/** 每个项目一条读-改-写串行链,见 patchProject。 */
|
|
22
23
|
const locks = new Map();
|
|
23
24
|
export function projectDir(id) {
|
|
@@ -47,6 +48,8 @@ export function settingsFromConfig(config, rowOrder) {
|
|
|
47
48
|
fillRatio: config.fillRatio,
|
|
48
49
|
bottomMargin: config.bottomMargin,
|
|
49
50
|
cropInset: config.cropInset,
|
|
51
|
+
keyColor: config.keyColor,
|
|
52
|
+
keyColorOverride: false,
|
|
50
53
|
keyLow: config.keyLow,
|
|
51
54
|
keyHigh: config.keyHigh,
|
|
52
55
|
despill: config.despill,
|
|
@@ -168,6 +171,7 @@ export async function createProject(name) {
|
|
|
168
171
|
const id = `p${Date.now().toString(36)}${randomUUID().slice(0, 6)}`;
|
|
169
172
|
const now = Date.now();
|
|
170
173
|
const settings = settingsFromConfig(config);
|
|
174
|
+
const defaults = defaultPrompts("zh", config.keyColor);
|
|
171
175
|
const project = {
|
|
172
176
|
id,
|
|
173
177
|
name: name.trim() === "" ? "未命名项目" : name.trim(),
|
|
@@ -175,9 +179,9 @@ export async function createProject(name) {
|
|
|
175
179
|
updatedAt: now,
|
|
176
180
|
source: null,
|
|
177
181
|
prompts: {
|
|
178
|
-
images:
|
|
179
|
-
video:
|
|
180
|
-
turn:
|
|
182
|
+
images: defaults.images,
|
|
183
|
+
video: defaults.video,
|
|
184
|
+
turn: defaults.turn,
|
|
181
185
|
videoPerDirection: {},
|
|
182
186
|
suffix: ""
|
|
183
187
|
},
|
|
@@ -291,6 +295,8 @@ function normalizeProject(raw) {
|
|
|
291
295
|
fillRatio: num(settings.fillRatio, defaults.fillRatio),
|
|
292
296
|
bottomMargin: num(settings.bottomMargin, defaults.bottomMargin),
|
|
293
297
|
cropInset: num(settings.cropInset, defaults.cropInset),
|
|
298
|
+
keyColor: normalizeKeyColor(settings.keyColor, defaults.keyColor),
|
|
299
|
+
keyColorOverride: settings.keyColorOverride === true,
|
|
294
300
|
keyLow: num(settings.keyLow, defaults.keyLow),
|
|
295
301
|
keyHigh: num(settings.keyHigh, defaults.keyHigh),
|
|
296
302
|
despill: num(settings.despill, defaults.despill),
|
|
@@ -338,12 +344,19 @@ function normalizeProject(raw) {
|
|
|
338
344
|
};
|
|
339
345
|
}
|
|
340
346
|
}
|
|
341
|
-
const
|
|
347
|
+
const promptDefaults = defaultPrompts("zh", merged.keyColor);
|
|
342
348
|
const images_prompts = {};
|
|
343
349
|
for (const key of DIRECTION_KEYS) {
|
|
344
350
|
const value = raw?.prompts?.images?.[key];
|
|
345
|
-
images_prompts[key] = typeof value === "string" && value.trim() !== "" ? value :
|
|
351
|
+
images_prompts[key] = typeof value === "string" && value.trim() !== "" ? value : promptDefaults.images[key];
|
|
346
352
|
}
|
|
353
|
+
const videoPrompt = typeof raw?.prompts?.video === "string" && raw.prompts.video.trim() !== "" ? raw.prompts.video : promptDefaults.video;
|
|
354
|
+
const turnPrompt = typeof raw?.prompts?.turn === "string" && raw.prompts.turn.trim() !== "" ? raw.prompts.turn : promptDefaults.turn;
|
|
355
|
+
const perDirectionPrompts = raw?.prompts?.videoPerDirection !== null && typeof raw?.prompts?.videoPerDirection === "object"
|
|
356
|
+
? { ...raw.prompts.videoPerDirection }
|
|
357
|
+
: {};
|
|
358
|
+
const normalizedPrompts = { images: images_prompts, video: videoPrompt, turn: turnPrompt, videoPerDirection: perDirectionPrompts, suffix: "" };
|
|
359
|
+
applyKeyColorToPrompts(normalizedPrompts, merged.keyColor);
|
|
347
360
|
const imageMode = deriveImageMode(raw);
|
|
348
361
|
return {
|
|
349
362
|
id: String(raw?.id ?? ""),
|
|
@@ -352,12 +365,10 @@ function normalizeProject(raw) {
|
|
|
352
365
|
updatedAt: num(raw?.updatedAt, now),
|
|
353
366
|
source: raw?.source?.file ? { file: String(raw.source.file), name: String(raw.source.name ?? "source") } : null,
|
|
354
367
|
prompts: {
|
|
355
|
-
images:
|
|
356
|
-
video:
|
|
357
|
-
turn:
|
|
358
|
-
videoPerDirection:
|
|
359
|
-
? { ...raw.prompts.videoPerDirection }
|
|
360
|
-
: {},
|
|
368
|
+
images: normalizedPrompts.images,
|
|
369
|
+
video: normalizedPrompts.video,
|
|
370
|
+
turn: normalizedPrompts.turn,
|
|
371
|
+
videoPerDirection: normalizedPrompts.videoPerDirection,
|
|
361
372
|
suffix: typeof raw?.prompts?.suffix === "string" ? raw.prompts.suffix : ""
|
|
362
373
|
},
|
|
363
374
|
images,
|