pipeline.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. """可被网站/命令行复用的生成管线。
  2. 读 manifest -> 生成各类资产 -> 写 library.json(供网站可视化列出与预览)。
  3. """
  4. import json
  5. import os
  6. import providers
  7. import spine_builder
  8. import particle_builder
  9. import tween_builder
  10. import baidu_segment
  11. HERE = os.path.dirname(os.path.abspath(__file__))
  12. def run(manifest, out_root, creds=None, log=print, merge_existing=False):
  13. """manifest: dict; out_root: 输出根目录; creds: {provider,api_key,base_url,model,size}
  14. 返回 (library_dict, base_out)。"""
  15. creds = creds or {}
  16. game = manifest.get("game", "game")
  17. base_out = os.path.join(out_root, game)
  18. chars_out = os.path.join(base_out, "characters")
  19. vfx_out = os.path.join(base_out, "vfx")
  20. ui_out = os.path.join(base_out, "ui")
  21. style = manifest.get("style", "")
  22. library = {
  23. "game": game,
  24. "slot_config": manifest.get("slot_config", {}),
  25. "characters": [],
  26. "vfx": [],
  27. "ui": [],
  28. "ui_art": [],
  29. }
  30. if merge_existing:
  31. library_path = os.path.join(base_out, "library.json")
  32. if os.path.isfile(library_path):
  33. with open(library_path, encoding="utf-8") as f:
  34. existing = json.load(f)
  35. for key in ("characters", "vfx", "ui", "ui_art"):
  36. library[key] = existing.get(key, [])
  37. library["slot_config"] = manifest.get("slot_config") or existing.get("slot_config", {})
  38. def upsert(section, item):
  39. item_id = item.get("id")
  40. if not item_id:
  41. library[section].append(item)
  42. return
  43. rows = library.setdefault(section, [])
  44. for idx, old in enumerate(rows):
  45. if old.get("id") == item_id:
  46. rows[idx] = item
  47. return
  48. rows.append(item)
  49. total_steps = len(manifest.get("characters", [])) + len(manifest.get("ui_art", [])) + len(manifest.get("vfx", [])) + (1 if manifest.get("ui", []) else 0)
  50. done_steps = 0
  51. def progress(label):
  52. nonlocal done_steps
  53. done_steps += 1
  54. log(f"进度 {done_steps}/{max(1, total_steps)} · {label}")
  55. def transparent_prompt(extra):
  56. return ", ".join([
  57. extra,
  58. "生成纯透明背景 PNG,真实 Alpha 通道,不要棋盘格,不要白底,不要阴影。",
  59. ])
  60. def alpha_report(img):
  61. img = img.convert("RGBA")
  62. alpha = img.getchannel("A")
  63. mn, mx = alpha.getextrema()
  64. transparent = sum(1 for v in alpha.getdata() if v == 0)
  65. ratio = transparent / max(1, img.width * img.height)
  66. return mn, mx, ratio
  67. def has_alpha(img):
  68. return alpha_report(img)[0] == 0
  69. def log_alpha(label, img, required):
  70. if not required:
  71. return
  72. mn, mx, ratio = alpha_report(img)
  73. if mn == 0:
  74. log(f"✅ [{label}] Alpha 透明通道有效:透明像素 {ratio:.1%}")
  75. else:
  76. log(f"⚠️ [{label}] 模型返回 PNG 但没有透明 Alpha:alpha={mn}-{mx},请重新生成或换支持透明输出的图像模型")
  77. def generate_checked(label, prompt, size, require_alpha):
  78. img = providers.generate(creds["provider"], prompt, creds["api_key"],
  79. creds.get("base_url", "https://api.openai.com/v1"),
  80. creds.get("model", "gpt-image-2"),
  81. size)
  82. log_alpha(label, img, require_alpha)
  83. if not require_alpha or has_alpha(img):
  84. return img
  85. log(f"🧠 [{label}] 模型没有真实 Alpha,直接改用百度智能抠图兜底…")
  86. try:
  87. fixed = baidu_segment.remove_background(img, label=label, log=log)
  88. except Exception as e:
  89. raise RuntimeError(f"模型没有返回真实 Alpha,百度智能抠图也失败:{e}")
  90. log_alpha(label, fixed, True)
  91. if has_alpha(fixed):
  92. return fixed
  93. raise RuntimeError("百度智能抠图返回结果仍没有真实 Alpha 透明通道")
  94. def boss_config():
  95. boss = manifest.get("slot_config", {}).get("boss", {})
  96. return boss if boss.get("enabled", False) else {}
  97. def required_boss_id():
  98. boss = boss_config()
  99. if not boss:
  100. return ""
  101. return boss.get("id") or "boss_demon_lord"
  102. def is_required_boss(c):
  103. boss_id = required_boss_id()
  104. return bool(boss_id and (c.get("role") == "boss" or c.get("id") == boss_id))
  105. boss_required_in_this_run = any(is_required_boss(c) for c in manifest.get("characters", []))
  106. required_failures = []
  107. def part_sheet_prompt(c, parts, cols, rows):
  108. ordered = ", ".join(f"{idx + 1}. {p['id']} ({p.get('prompt', '')})" for idx, p in enumerate(parts))
  109. return ", ".join(x for x in [
  110. c.get("prompt", ""),
  111. style,
  112. (
  113. f"create one transparent PNG boss rigging parts sprite sheet, exactly {cols} columns and {rows} rows, "
  114. "one isolated part per cell, no labels, no numbers, no grid lines, no text, no shadows, no background, "
  115. "parts must not overlap cell borders, all parts from the same character, same lighting and style, "
  116. "center each part inside its own cell, leave unused cells fully transparent"
  117. ),
  118. "cell order left to right, top to bottom: " + ordered,
  119. transparent_prompt("sprite sheet of separated rigging parts only")
  120. ] if x)
  121. def split_part_sheet(sheet_img, parts, cols, rows):
  122. sheet = sheet_img.convert("RGBA")
  123. cell_w = max(1, sheet.width // cols)
  124. cell_h = max(1, sheet.height // rows)
  125. part_images = {}
  126. major_ids = {
  127. "torso", "head", "left_arm", "right_arm", "greatsword",
  128. "left_leg", "right_leg", "cape",
  129. }
  130. for idx, part in enumerate(parts):
  131. col = idx % cols
  132. row = idx // cols
  133. box = (col * cell_w, row * cell_h, (col + 1) * cell_w, (row + 1) * cell_h)
  134. cell = sheet.crop(box)
  135. bbox = cell.getchannel("A").point(lambda v: 255 if v > 16 else 0).getbbox()
  136. if not bbox:
  137. raise RuntimeError(f"拆件表第 {idx + 1} 格 {part['id']} 是空的")
  138. bw, bh = bbox[2] - bbox[0], bbox[3] - bbox[1]
  139. if part["id"] in major_ids and (bw < cell_w * 0.16 or bh < cell_h * 0.16):
  140. raise RuntimeError(
  141. f"拆件表第 {idx + 1} 格 {part['id']} 太小:{bw}x{bh},需要填满单元格"
  142. )
  143. part_images[part["id"]] = cell
  144. return part_images
  145. # ---- A. 角色(Spine)----
  146. for i, c in enumerate(manifest.get("characters", [])):
  147. cid = c.get("id", f"char_{i}")
  148. anims = c.get("animations", ["idle"])
  149. if not creds.get("api_key"):
  150. log(f"⚠️ 未填 key,跳过角色 {cid}")
  151. continue
  152. try:
  153. if c.get("type") == "spine_parts":
  154. parts = c.get("parts") or []
  155. part_images = {}
  156. sheet_cfg = c.get("spriteSheet") or {}
  157. use_sheet = c.get("partGeneration") == "sprite_sheet" or sheet_cfg.get("enabled")
  158. if use_sheet:
  159. cols = int(sheet_cfg.get("cols") or 4)
  160. rows = int(sheet_cfg.get("rows") or 4)
  161. if len(parts) > cols * rows:
  162. raise RuntimeError(f"Boss 拆件数 {len(parts)} 超过 sprite sheet 容量 {cols}x{rows}")
  163. try:
  164. log(f"🎨 [{cid}] 生成 Boss 拆件表 {cols}×{rows}…")
  165. sheet = generate_checked(f"{cid}/parts_sheet",
  166. part_sheet_prompt(c, parts, cols, rows),
  167. sheet_cfg.get("size", c.get("size", "1024x1024")),
  168. True)
  169. part_images = split_part_sheet(sheet, parts, cols, rows)
  170. log(f"✂️ [{cid}] 已按固定网格切出 {len(parts)} 个 Boss 拆件")
  171. except Exception as e:
  172. log(f"⚠️ [{cid}] 拆件表生成/切图失败,回退逐部件生成:{e}")
  173. part_images = {}
  174. if not part_images:
  175. for part in parts:
  176. part_id = part["id"]
  177. part_prompt = ", ".join(x for x in [
  178. c.get("prompt", ""),
  179. part.get("prompt", ""),
  180. style,
  181. transparent_prompt("single separated rigging part only, centered, no text, no other body parts")
  182. ] if x)
  183. log(f"🎨 [{cid}/{part_id}] 生成 Boss 拆件…")
  184. pimg = generate_checked(f"{cid}/{part_id}", part_prompt,
  185. part.get("size", c.get("size", creds.get("size", "1024x1024"))),
  186. True)
  187. part_images[part_id] = pimg
  188. spine_builder.build_parts_character(cid, part_images, chars_out, anims, parts)
  189. w, h = 1000, 1000
  190. files = [f"characters/{cid}.json", f"characters/{cid}.atlas", f"characters/{cid}.png",
  191. f"characters/{cid}_preview.png"]
  192. preview = f"characters/{cid}_preview.png"
  193. else:
  194. full_prompt = ", ".join(x for x in [
  195. c.get("prompt", ""), style,
  196. transparent_prompt("single game icon character or slot symbol, centered, full body in frame, no text, not a boss, not a demon lord, not dark armor"),
  197. ] if x)
  198. log(f"🎨 [{cid}] 生成角色图…")
  199. img = generate_checked(cid, full_prompt, c.get("size", creds.get("size", "1024x1024")), True)
  200. spine_builder.build_character(cid, img, chars_out, anims)
  201. w, h = spine_builder.trim_to_content(img).size
  202. files = [f"characters/{cid}.json", f"characters/{cid}.atlas", f"characters/{cid}.png"]
  203. preview = ""
  204. upsert("characters", {
  205. "id": cid,
  206. "png": f"characters/{cid}.png",
  207. "w": w, "h": h,
  208. "type": c.get("type", "spine"),
  209. "role": c.get("role", ""),
  210. "preview": preview,
  211. "animations": spine_builder.anim_data(anims),
  212. "files": files,
  213. })
  214. log(f"✅ [{cid}] 完成 ({anims})")
  215. progress(f"{cid}")
  216. except Exception as e:
  217. log(f"❌ [{cid}] 失败: {e}")
  218. if is_required_boss(c):
  219. required_failures.append(f"{cid}: {e}")
  220. progress(f"{cid}")
  221. boss_id = required_boss_id()
  222. boss_missing_after_chars = (
  223. boss_id and boss_required_in_this_run
  224. and not any(c.get("id") == boss_id for c in library["characters"])
  225. )
  226. # ---- A2. UI 美术(背景 / Logo / 卷轴框 / 按钮 等整图)----
  227. ui_art_out = os.path.join(base_out, "ui_art")
  228. for a in manifest.get("ui_art", []):
  229. aid = a.get("id", "art")
  230. if not creds.get("api_key"):
  231. log(f"⚠️ 未填 key,跳过 UI 美术 {aid}")
  232. continue
  233. try:
  234. transparent = a.get("transparent", True)
  235. extra = (transparent_prompt("single clean UI element, no text")
  236. if transparent
  237. else "full-bleed illustration, no text, no UI elements")
  238. full_prompt = ", ".join(x for x in [a.get("prompt", ""), style if a.get("use_style") else "", extra] if x)
  239. log(f"🖼 [{aid}] 生成 UI 美术…")
  240. img = generate_checked(aid, full_prompt, a.get("size", creds.get("size", "1024x1024")), transparent)
  241. os.makedirs(ui_art_out, exist_ok=True)
  242. img.save(os.path.join(ui_art_out, f"{aid}.png"))
  243. upsert("ui_art", {"id": aid, "file": f"ui_art/{aid}.png",
  244. "w": img.width, "h": img.height,
  245. "transparent": transparent})
  246. log(f"✅ [{aid}] UI 美术完成")
  247. progress(f"{aid}")
  248. except Exception as e:
  249. log(f"❌ [{aid}] UI 美术失败: {e}")
  250. progress(f"{aid}")
  251. # ---- B. 粒子 VFX ----
  252. for v in manifest.get("vfx", []):
  253. vid = v.get("id", "vfx")
  254. try:
  255. path = particle_builder.build_particle(
  256. vid, v.get("template", "burst"), v.get("color", [255, 255, 255]), vfx_out)
  257. cfg = json.load(open(path, encoding="utf-8"))
  258. upsert("vfx", {"id": vid, "template": v.get("template"),
  259. "file": f"vfx/{vid}.particle.json", "config": cfg})
  260. log(f"✨ [{vid}] 粒子配置完成")
  261. progress(f"{vid}")
  262. except Exception as e:
  263. log(f"❌ [{vid}] 粒子失败: {e}")
  264. progress(f"{vid}")
  265. # ---- C. UI Tween ----
  266. ui = manifest.get("ui", [])
  267. if ui:
  268. used = [u.get("preset") for u in ui if u.get("preset")]
  269. try:
  270. tween_builder.build_tweens(used, ui_out)
  271. for u in ui:
  272. upsert("ui", {"id": u.get("id"), "preset": u.get("preset"),
  273. "params": u.get("params", {})})
  274. log(f"🎛 TweenPresets.ts 完成 ({used})")
  275. progress("TweenPresets")
  276. except Exception as e:
  277. log(f"❌ Tween 失败: {e}")
  278. progress("TweenPresets")
  279. os.makedirs(base_out, exist_ok=True)
  280. with open(os.path.join(base_out, "library.json"), "w", encoding="utf-8") as f:
  281. json.dump(library, f, ensure_ascii=False, indent=2)
  282. log("—— 完成 ——")
  283. if boss_missing_after_chars:
  284. detail = ";".join(required_failures) if required_failures else "生成结果中没有关主资源"
  285. raise RuntimeError(
  286. f"关主大魔王资源缺失:{boss_id}。已保存其他成功资源;请在任务卡片里继续补生成 boss。原因:{detail}"
  287. )
  288. return library, base_out