feat: 投影法波谷检测+白底硬切断+帧底部对齐+gifmaker透明GIF
- prompt_agent: 精灵表/瓦片集间隙从2-4px放宽到8-16px纯白 - splitsprite removeWhiteBg: dist<threshold/2直接A=0,消除半透明残留 - splitsprite findCuts: 波峰-波谷检测替代死阈值,归并原阈值回退 - splitsprite alignCenter: 全局画布底部对齐替代独立居中,人物不跳帧 - splitsprite MinFillRatio 调至0.14,MinGapWidth调至2 - 新增 pkg/gifmaker: 统一画布+透明索引0+DisposalBackground - test_prompt_to_gif 改用gifmaker包
This commit is contained in:
@@ -7,14 +7,12 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
"image/gif"
|
||||
"image/png"
|
||||
"os"
|
||||
|
||||
"gen2d/internal/config"
|
||||
"gen2d/internal/service"
|
||||
"gen2d/pkg/gifmaker"
|
||||
"gen2d/pkg/splitsprite"
|
||||
)
|
||||
|
||||
@@ -28,10 +26,10 @@ func main() {
|
||||
// 1. PromptAgent 优化提示词
|
||||
fmt.Println("=== Step 1: Optimize prompt via PromptAgent ===")
|
||||
agentIn := service.PromptAgentInput{
|
||||
Tags: []string{"像素", "战士", "持剑", "精灵表"},
|
||||
Tags: []string{"像素", "横版动作", "大剑战士", "精灵表", "4x4网格"},
|
||||
AssetType: "sprite",
|
||||
Prompt: "生成一个像素风持剑战士的4方向行走精灵表",
|
||||
UserNote: "需要4方向(上下左右),每方向4帧行走动画",
|
||||
Prompt: "生成一个2D横版动作游戏角色的连续攻击连招精灵表,侧视角,4行4列网格排列",
|
||||
UserNote: "角色为持大剑的战士,连招包含4段攻击:横斩→上挑→跳劈→终结重击。每段攻击4帧关键帧,共16帧,按4行×4列网格排列。帧间留8-16px纯白间隙(#FFFFFF)。要求动作流畅有力量感,大剑挥舞轨迹清晰",
|
||||
}
|
||||
out, err := service.RunPromptAgent(ctx, agentIn)
|
||||
if err != nil {
|
||||
@@ -42,7 +40,7 @@ func main() {
|
||||
// 2. 调用文生图 API
|
||||
fmt.Println("=== Step 2: Generate sprite sheet via image API ===")
|
||||
params := service.AssetParams{
|
||||
Resolution: 1024,
|
||||
Resolution: 1536,
|
||||
Format: "spritesheet",
|
||||
}
|
||||
images, err := service.GenerateImages(ctx, out.Prompt, params)
|
||||
@@ -63,17 +61,13 @@ func main() {
|
||||
}
|
||||
fmt.Printf("Saved sprite sheet → %s (%d bytes)\n", sheetPath, len(images[0].Data))
|
||||
|
||||
// 3. splitsprite 拆分精灵表
|
||||
fmt.Println("\n=== Step 3: Split sprite sheet ===")
|
||||
// 3. splitsprite: 洗白底 → 波谷投影拆分 → 有效像素≥40% → 裁切像素边界 → 统一最大分辨率居中
|
||||
fmt.Println("\n=== Step 3: Wash white bg → valley projection split → trim → center align ===")
|
||||
sheetImg, err := decodePNG(images[0].Data)
|
||||
if err != nil {
|
||||
fatalf("decode sheet: %v", err)
|
||||
}
|
||||
opts := splitsprite.DefaultOptions()
|
||||
opts.GridRows = 4
|
||||
opts.GridCols = 4
|
||||
opts.GridPadding = 2
|
||||
opts.CenterAlign = true
|
||||
opts := splitsprite.DefaultOptions() // WhiteBg=true, MinFillRatio=0.4, CenterAlign=true
|
||||
frames, err := splitsprite.Process(sheetImg, opts)
|
||||
if err != nil {
|
||||
fatalf("split failed: %v", err)
|
||||
@@ -92,7 +86,7 @@ func main() {
|
||||
// 4. 生成 GIF 预览
|
||||
fmt.Println("\n=== Step 4: Generate GIF preview ===")
|
||||
gifPath := "test_output/preview.gif"
|
||||
if err := genGIF(gifPath, frames, 12); err != nil {
|
||||
if err := gifmaker.Save(gifPath, frames, nil); err != nil {
|
||||
fatalf("generate GIF: %v", err)
|
||||
}
|
||||
fmt.Printf("GIF preview → %s (%d frames)\n", gifPath, len(frames))
|
||||
@@ -104,63 +98,6 @@ func main() {
|
||||
fmt.Println(" test_output/preview.gif — animated GIF preview")
|
||||
}
|
||||
|
||||
func genGIF(path string, frames []image.Image, delay int) error {
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
pal := buildPalette(frames)
|
||||
anim := &gif.GIF{}
|
||||
for _, frame := range frames {
|
||||
b := frame.Bounds()
|
||||
paletted := image.NewPaletted(b, pal)
|
||||
draw.Draw(paletted, b, frame, b.Min, draw.Src)
|
||||
anim.Image = append(anim.Image, paletted)
|
||||
anim.Delay = append(anim.Delay, delay)
|
||||
}
|
||||
anim.LoopCount = 0 // loop forever
|
||||
return gif.EncodeAll(f, anim)
|
||||
}
|
||||
|
||||
func buildPalette(frames []image.Image) color.Palette {
|
||||
hist := make(map[color.RGBA]int)
|
||||
sampleStep := max(1, len(frames)/8)
|
||||
for i := 0; i < len(frames); i += sampleStep {
|
||||
b := frames[i].Bounds()
|
||||
step := max(1, (b.Dx()*b.Dy())/4096)
|
||||
n := 0
|
||||
for y := b.Min.Y; y < b.Max.Y; y++ {
|
||||
for x := b.Min.X; x < b.Max.X; x++ {
|
||||
if n%step != 0 {
|
||||
n++
|
||||
continue
|
||||
}
|
||||
n++
|
||||
r, g, bl, a := frames[i].At(x, y).RGBA()
|
||||
if a > 0 {
|
||||
c := color.RGBA{R: uint8(r >> 8), G: uint8(g >> 8), B: uint8(bl >> 8), A: uint8(a >> 8)}
|
||||
hist[c]++
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pal := make(color.Palette, 0, 256)
|
||||
for c := range hist {
|
||||
pal = append(pal, c)
|
||||
if len(pal) >= 240 {
|
||||
break
|
||||
}
|
||||
}
|
||||
pal = append(pal,
|
||||
color.RGBA{0, 0, 0, 0},
|
||||
color.RGBA{0, 0, 0, 255},
|
||||
color.RGBA{255, 255, 255, 255},
|
||||
)
|
||||
return pal
|
||||
}
|
||||
|
||||
func decodePNG(data []byte) (image.Image, error) {
|
||||
img, _, err := image.Decode(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user