dsh-game-material-master 0.1.1 → 0.2.1

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 CHANGED
@@ -11,14 +11,15 @@
11
11
  ![游戏素材大师 · 工作台](https://raw.githubusercontent.com/universe-st/dsh-game-material-master/main/docs/images/gmm-00-workbench.png)
12
12
 
13
13
  > **30 秒上手**:`dsh plugin --profile web add dsh-game-material-master` → 重启 DSH →
14
- > 侧栏点「游戏素材大师」→ 设置里填两个 API Key → 新建项目、上传设定图、点「一键生成全部」。
14
+ > 侧栏点「游戏素材大师」→ 设置里填两个 API Key → 新建项目、上传设定图 → 点「生成转圈视频」
15
+ >(默认路径:角色原地匀速转一圈,再自动截出八个方向;要逐方向生图就在阶段①切一下方式)。
15
16
 
16
17
  三个模块共用同一套「生成 → 验收 → 重跑」工作台。每个阶段都能**单独重做**,每份产物都能
17
18
  **逐项打「通过」**;「审核模式」可以选 agent 自动审完就往下走,也可以选每一步停下来等你点头。
18
19
 
19
20
  | 模块 | 做什么 | 走完能拿到 |
20
21
  |---|---|---|
21
- | **① 八方向图生成** | 一张设定图 → 8 方位 × 8 帧 | 可用 WASD 操控预览的行走精灵图 |
22
+ | **① 八方向图生成** | 一张设定图 → 8 方位 × 8 帧(默认「转圈截帧」:一段转圈视频截出八个方向) | 可用 WASD 操控预览的行走精灵图 |
22
23
  | **② 图片生成** | 提示词(可带参考图)→ 出图 → 抠绿幕 | 干净透明的 PNG 立绘 / 道具 |
23
24
  | **③ 序列帧生成** | 首尾帧或参考视频 → 生成视频 → 抽帧 → 抠像 | 横向排布、可循环播放的序列帧 |
24
25
  | ~~④ 骨骼动画生成~~ | 拆件 → 装配 → 骨骼动画 → Spine / DragonBones 图集 | **实验性功能,本文不展示** |
@@ -40,11 +41,29 @@ flowchart LR
40
41
  S(["设定图"]) --> I["① 八方向绿幕图"] --> V["② 行走视频"] --> F["③ 提取序列帧"] --> P["④ 合成整图"] --> W(["⑤ WASD 预览"])
41
42
  ```
42
43
 
44
+ ### ① 的两种生成方式:转圈截帧(默认)/ 逐方向生图
45
+
46
+ 八张方向图有两条产法,产物完全一样(`images/<方位>.png`),所以后面四步不用换做法:
47
+
48
+ | | **转圈截帧(默认)** | 逐方向生图(备选) |
49
+ |---|---|---|
50
+ | 做法 | 先生成**一段**「角色原地匀速转一整圈」的绿幕视频,再按时间轴截出八个方向的帧 | 八个方位各自生图,按依赖顺序 |
51
+ | 花费 | 一次视频调用 | 八次生图调用 |
52
+ | 一致性 | **八个方向同源**:同一个角色、同一段光线、同一套画风 | 模型每次都要重新理解角色,脸型/发色/服装容易漂移 |
53
+ | 调位置 | 时间轴上有**八个可拖动的圆圈**,拖到哪一帧就切哪一帧(本机重切,免费、即时) | 逐张重新生成 |
54
+
55
+ 界面上那一排圆圈就是八个方位的截帧位置,底下的缩略条带是整圈候选帧——对着画面拖,
56
+ 松手就重切那一张(并自动作废这个方位的行走视频与序列帧,避免新旧朝向混在一起)。
57
+ 默认位置是「从正面开始、匀速转满一圈」的等分假设;模型实际转的方向或快慢不一样时,
58
+ 直接拖圆圈,或点一下「转圈方向」重排。改变候选帧数(8~64)会自动重抽,并把拖过的位置按比例换算。
59
+
43
60
  ### 怎么用
44
61
 
45
- 1. **新建项目 → 上传设定图**,写每个方位的生图提示词(可只写一次「统一附加提示词」,八个方向共用)。
46
- 2. **一键生成全部**:八个方位按依赖顺序出绿幕全身图。逐张能看到用时、落到哪个模型;不满意就
47
- **重新生成**这一张,或展开**编辑提示词**只改它。
62
+ 1. **新建项目 → 上传设定图**,写提示词(转圈截帧用的是**一份转圈视频提示词**;逐方向生图是每个方位一份,
63
+ 另有「统一附加提示词」八个方向共用)。
64
+ 2. **生成转圈视频**(默认路径):角色原地匀速转一整圈,绿幕背景。视频到手后插件会自动抽候选帧
65
+ 并切出八个方向,一般不需要你动手。**逐方向生图**(备选)则是**一键生成全部**:八个方位按依赖顺序
66
+ 出绿幕全身图。逐张能看到用时、落到哪个模型;不满意就**重新生成**这一张,或展开**编辑提示词**只改它。
48
67
  3. **生成全部视频**:让角色在原方位原地走三步。这一步最贵、也最慢(单段 1~6 分钟)。
49
68
  4. **提取全部序列帧**:每段视频按时长平均抽帧,这里同时完成统一裁剪、缩放与**像素风量化**。
50
69
  5. **合成整图**:剔除绿幕,按行序拼成一张精灵图;行序可用 ↑↓ 拖动调整,合成前可存参数。
@@ -52,6 +71,13 @@ flowchart LR
52
71
 
53
72
  ### ① 八方向绿幕图
54
73
 
74
+ **转圈截帧(默认)**:一段「原地匀速转一整圈」的绿幕视频,按时间轴截出八个方向;
75
+ 轴上八个圆圈就是各自的截帧位置,拖着改,松手即时重切(本机、免费)。
76
+
77
+ ![转圈截帧](https://raw.githubusercontent.com/universe-st/dsh-game-material-master/main/docs/images/gmm-01b-sprite-turn.png)
78
+
79
+ **逐方向生图(备选)**:八个方位各自生图,单张更清晰,适合只要几个方向或追求单张质量时。
80
+
55
81
  ![八方向绿幕图](https://raw.githubusercontent.com/universe-st/dsh-game-material-master/main/docs/images/gmm-01-sprite-images.png)
56
82
 
57
83
  ### ② 行走动作视频
package/lib/chroma.js CHANGED
@@ -9,6 +9,12 @@
9
9
  * 停下。这一步是必需的:图生视频模型经常会把纯色背景重新打光成渐变,
10
10
  * 只认绿色的做法在那些帧上会整片失效。
11
11
  *
12
+ * ★ 调色板只从**背景主色簇**的边框采样点建:角色一旦贴到画面边缘
13
+ * (尾巴、头发、裙摆被裁到边上),它的颜色也会出现在边框上;不筛掉的话
14
+ * 调色板里就有了角色色,填充会顺着同色的身体部位一路吃进去。实测一张
15
+ * 438×768 的转圈帧:尾巴贴住左边缘,1.6% 的边框采样点是藏青,默认容差
16
+ * 下 7.7% 的掩码变成了角色的裙子与头发(半条裙子直接没了)。
17
+ *
12
18
  * 2. **绿色优势值** `g - max(r, b)`(辅助判据)。只作用在未被判定为背景的
13
19
  * 像素上,用来清理角色轮廓残留的绿色边缘,并提供去绿溢出。
14
20
  *
@@ -34,23 +40,117 @@ function colorDistance(rgba, a, b) {
34
40
  /** 背景调色板的量化格:每通道 16 档。 */
35
41
  const PALETTE_GRID = 16;
36
42
  const PALETTE_CELL = 256 / PALETTE_GRID;
43
+ const PALETTE_CELLS = PALETTE_GRID * PALETTE_GRID * PALETTE_GRID;
44
+ /**
45
+ * 单链聚类时的「同一片背景色」阈值:格心距离 ≤ 2 格(= 32)。
46
+ *
47
+ * 取 2 格是刻意的:沿任一通道相邻(16)、斜一格(22.6)、体对角(27.7)
48
+ * 都算连通,于是**平缓渐变会一路串成一簇**(这正是要保留的能力),而隔了
49
+ * 两格以上的跳跃(35.8 起)断开——角色那种与背景明显不同的颜色因此自成
50
+ * 一小簇,随后被主簇筛掉。
51
+ */
52
+ const CLUSTER_STEP_SQ = (2 * PALETTE_CELL) ** 2;
53
+ /** 聚类邻域(格坐标偏移,只保留距离 ≤ 2 格的)。 */
54
+ const CLUSTER_NEIGHBORS = (() => {
55
+ const out = [];
56
+ for (let dr = -2; dr <= 2; dr++) {
57
+ for (let dg = -2; dg <= 2; dg++) {
58
+ for (let db = -2; db <= 2; db++) {
59
+ if (dr === 0 && dg === 0 && db === 0)
60
+ continue;
61
+ const distSq = (dr * PALETTE_CELL) ** 2 + (dg * PALETTE_CELL) ** 2 + (db * PALETTE_CELL) ** 2;
62
+ if (distSq <= CLUSTER_STEP_SQ + 1e-6)
63
+ out.push([dr, dg, db]);
64
+ }
65
+ }
66
+ }
67
+ return out;
68
+ })();
69
+ /**
70
+ * 一个簇至少要占多少边框采样点才配进调色板。
71
+ *
72
+ * 只留「最大簇」会把「上半绿幕 + 下半蓝幕」这类多色背景的另一半误伤,
73
+ * 所以按占比放行:15% 以上的簇都算背景色(实测角色贴边时那一小撮只有
74
+ * 1.6%,稳稳被挡在门外)。
75
+ */
76
+ const CLUSTER_MIN_SHARE = 0.15;
37
77
  /**
38
78
  * 用四边像素建一个「背景色查找格」。
39
79
  *
40
80
  * 为什么需要它:只用「和相邻像素颜色接近」来生长,会顺着抗锯齿的软边缘
41
- * 一路渗进角色——绿幕到白色围裙的过渡如果跨了 3~4 个像素,每一步的差值都
42
- * 小于局部容差,填充就穿过去了。加上这条全局约束后,像素还必须「本身就像
81
+ * 一路渗进角色——绿幕到白色围裙的过渡如果跨了 3~4 个像素,每一步的差值
82
+ * 都小于局部容差,填充就穿过去了。加上这条全局约束后,像素还必须「本身就像
43
83
  * 背景色」才可能被判为背景,软边缘自然被挡住。
44
84
  *
45
85
  * 采样点覆盖整圈边界,所以平缓渐变的背景也能被完整覆盖。
86
+ *
87
+ * ★ 采样点先按颜色聚类、只保留属于**背景主色**的那些(见 CLUSTER_STEP_SQ)。
88
+ * 这一步是「角色贴边」那个 bug 的正解:贴边会让角色自己的颜色混进边框采样,
89
+ * 主色筛选把它剔除,而渐变背景因为一路相连仍然完整保留。
46
90
  */
47
91
  function buildBackgroundPalette(rgba, width, height, globalTolerance) {
48
- const grid = new Uint8Array(PALETTE_GRID * PALETTE_GRID * PALETTE_GRID);
49
- const seen = new Uint8Array(PALETTE_GRID * PALETTE_GRID * PALETTE_GRID);
92
+ const grid = new Uint8Array(PALETTE_CELLS);
93
+ const samples = [];
94
+ // 步长控制采样量:长边最多取 128 个点,够覆盖渐变又不至于拖慢。
95
+ const stepX = Math.max(1, Math.floor(width / 128));
96
+ const stepY = Math.max(1, Math.floor(height / 128));
97
+ for (let x = 0; x < width; x += stepX) {
98
+ samples.push(x * 4, ((height - 1) * width + x) * 4);
99
+ }
100
+ for (let y = 0; y < height; y += stepY) {
101
+ samples.push(y * width * 4, (y * width + width - 1) * 4);
102
+ }
103
+ if (samples.length === 0)
104
+ return { grid, samples: 0, kept: 0, dropped: 0 };
105
+ // 1) 边框颜色量化计数。
106
+ const cellOf = (offset) => ((rgba[offset] >> 4) << 8) | ((rgba[offset + 1] >> 4) << 4) | (rgba[offset + 2] >> 4);
107
+ const counts = new Uint32Array(PALETTE_CELLS);
108
+ for (const offset of samples)
109
+ counts[cellOf(offset)]++;
110
+ // 2) 在「有采样的格子」之间做单链聚类(格子最多 4096 个,代价可忽略)。
111
+ const cluster = new Int32Array(PALETTE_CELLS).fill(-1);
112
+ const clusterSize = [];
113
+ for (let seed = 0; seed < PALETTE_CELLS; seed++) {
114
+ if (counts[seed] === 0 || cluster[seed] !== -1)
115
+ continue;
116
+ const id = clusterSize.length;
117
+ let size = 0;
118
+ cluster[seed] = id;
119
+ const queue = [seed];
120
+ while (queue.length > 0) {
121
+ const cell = queue.pop();
122
+ size += counts[cell];
123
+ const r = cell >> 8;
124
+ const g = (cell >> 4) & 15;
125
+ const b = cell & 15;
126
+ for (const [dr, dg, db] of CLUSTER_NEIGHBORS) {
127
+ const nr = r + dr;
128
+ const ng = g + dg;
129
+ const nb = b + db;
130
+ if (nr < 0 || nr >= PALETTE_GRID || ng < 0 || ng >= PALETTE_GRID || nb < 0 || nb >= PALETTE_GRID)
131
+ continue;
132
+ const next = (nr << 8) | (ng << 4) | nb;
133
+ if (counts[next] === 0 || cluster[next] !== -1)
134
+ continue;
135
+ cluster[next] = id;
136
+ queue.push(next);
137
+ }
138
+ }
139
+ clusterSize.push(size);
140
+ }
141
+ // 3) 按占比决定哪些簇算「背景色」(见 CLUSTER_MIN_SHARE)。
142
+ let largest = 0;
143
+ for (let i = 1; i < clusterSize.length; i++)
144
+ if (clusterSize[i] > clusterSize[largest])
145
+ largest = i;
146
+ const keepCluster = clusterSize.map((size, id) => id === largest || size / samples.length >= CLUSTER_MIN_SHARE);
147
+ // 4) 只用保留下来的采样点做球体标记。
50
148
  // 量化误差最多 8*sqrt(3) ≈ 14,补进半径里,避免刚好卡在格边界的颜色被漏掉。
51
149
  const limit = globalTolerance + 14;
52
150
  const limitSq = limit * limit;
53
151
  const radius = Math.max(1, Math.ceil(limit / PALETTE_CELL));
152
+ const seen = new Uint8Array(PALETTE_CELLS);
153
+ let kept = 0;
54
154
  const mark = (index) => {
55
155
  const r = Math.min(PALETTE_GRID - 1, rgba[index] >> 4);
56
156
  const g = Math.min(PALETTE_GRID - 1, rgba[index + 1] >> 4);
@@ -82,18 +182,14 @@ function buildBackgroundPalette(rgba, width, height, globalTolerance) {
82
182
  }
83
183
  }
84
184
  };
85
- // 步长控制采样量:长边最多取 128 个点,够覆盖渐变又不至于拖慢。
86
- const stepX = Math.max(1, Math.floor(width / 128));
87
- const stepY = Math.max(1, Math.floor(height / 128));
88
- for (let x = 0; x < width; x += stepX) {
89
- mark(x * 4);
90
- mark(((height - 1) * width + x) * 4);
91
- }
92
- for (let y = 0; y < height; y += stepY) {
93
- mark(y * width * 4);
94
- mark((y * width + width - 1) * 4);
185
+ for (const offset of samples) {
186
+ const id = cluster[cellOf(offset)];
187
+ if (id < 0 || keepCluster[id] !== true)
188
+ continue;
189
+ kept++;
190
+ mark(offset);
95
191
  }
96
- return grid;
192
+ return { grid, samples: samples.length, kept, dropped: samples.length - kept };
97
193
  }
98
194
  function paletteHas(grid, rgba, index) {
99
195
  const r = Math.min(PALETTE_GRID - 1, rgba[index] >> 4);
@@ -109,6 +205,10 @@ function paletteHas(grid, rgba, index) {
109
205
  * 返回 1 = 背景,0 = 前景。
110
206
  */
111
207
  export function segmentBackground(rgba, width, height, globalTolerance, localTolerance) {
208
+ return segmentBackgroundDetailed(rgba, width, height, globalTolerance, localTolerance).mask;
209
+ }
210
+ /** 与 `segmentBackground` 同一次计算,但把调色板的采样诊断一并带出来。 */
211
+ export function segmentBackgroundDetailed(rgba, width, height, globalTolerance, localTolerance) {
112
212
  // 局部容差故意收得很紧:它只用来跨过背景自身的细微噪声与编码块边界。
113
213
  // 背景的**大范围变化**(渐变、被重新打光)由全局调色板负责——边界采样本来就
114
214
  // 覆盖了整圈颜色,局部判据不需要承担这件事。
@@ -120,7 +220,8 @@ export function segmentBackground(rgba, width, height, globalTolerance, localTol
120
220
  // 12 不会因为噪声而漏抠:4 邻域填充可以从别的方向绕开孤立的噪声像素,
121
221
  // 只有**连续成片**的强边缘(也就是角色轮廓)才能挡住它。
122
222
  const local = Math.max(1, Math.round(localTolerance ?? 12));
123
- const palette = buildBackgroundPalette(rgba, width, height, globalTolerance);
223
+ const built = buildBackgroundPalette(rgba, width, height, globalTolerance);
224
+ const palette = built.grid;
124
225
  const total = width * height;
125
226
  const background = new Uint8Array(total);
126
227
  const stack = new Int32Array(total);
@@ -166,7 +267,7 @@ export function segmentBackground(rgba, width, height, globalTolerance, localTol
166
267
  if (y < height - 1)
167
268
  visit(index + width);
168
269
  }
169
- return background;
270
+ return { mask: background, samples: built.samples, kept: built.kept, dropped: built.dropped };
170
271
  }
171
272
  /** 没启用空间分割时,用颜色判据粗略估一个背景占比,供界面判断可信度。 */
172
273
  function countGreenBackground(rgba, high) {
@@ -192,14 +293,18 @@ export function keyGreen(rgba, width, height, options) {
192
293
  // 几乎没有背景说明四边就是角色本身(贴边裁切)。两种都不可信。
193
294
  let background;
194
295
  let backgroundCount = 0;
296
+ let borderSamples = 0;
297
+ let borderSamplesDropped = 0;
195
298
  if (tolerance > 0) {
196
- const candidate = segmentBackground(rgba, width, height, tolerance);
299
+ const built = segmentBackgroundDetailed(rgba, width, height, tolerance);
300
+ borderSamples = built.samples;
301
+ borderSamplesDropped = built.dropped;
197
302
  let count = 0;
198
303
  for (let i = 0; i < pixels; i++)
199
- count += candidate[i];
304
+ count += built.mask[i];
200
305
  const fraction = count / pixels;
201
306
  if (fraction > 0.12 && fraction < 0.985) {
202
- background = candidate;
307
+ background = built.mask;
203
308
  backgroundCount = count;
204
309
  }
205
310
  }
@@ -237,7 +342,9 @@ export function keyGreen(rgba, width, height, options) {
237
342
  return {
238
343
  rgba: out,
239
344
  backgroundFraction: background !== undefined ? backgroundCount / pixels : countGreenBackground(rgba, high) / pixels,
240
- usedSegmentation: background !== undefined
345
+ usedSegmentation: background !== undefined,
346
+ borderSamples,
347
+ borderSamplesDropped
241
348
  };
242
349
  }
243
350
  /** 对 alpha 通道做可分离的最小值滤波,等价于把前景边界向内收缩 n 像素。 */