feat: 提示词优化支持多布局模式识别与纯白背景统一
- buildMetaPrompt 按素材类型给出双模式指令(单素材/网格瓦片集),LLM 根据用户意图选择 - 新增 isSheetRequest 自动识别精灵表/瓦片集/tileset 关键字 - 模板回退链路全面支持 isSheet 双模式,默认单素材模式 - 所有素材类型统一纯白色背景(#FFFFFF),由后期 format 节点清洗去背 - 补充 sprite/background/ui/animation 四类素材的完整生成场景覆盖
This commit is contained in:
@@ -31,14 +31,30 @@ func TestRunPromptAgent_Fallback(t *testing.T) {
|
||||
|
||||
func TestRunPromptAgent_Sprite(t *testing.T) {
|
||||
output, err := RunPromptAgent(context.Background(), PromptAgentInput{
|
||||
Tags: []string{"像素", "中世纪", "战士"},
|
||||
Tags: []string{"像素", "中世纪", "战士", "精灵表"},
|
||||
AssetType: "sprite",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("RunPromptAgent failed: %v", err)
|
||||
}
|
||||
if !strings.Contains(output.Prompt, "spritesheet") {
|
||||
t.Errorf("sprite output should mention spritesheet: %s", output.Prompt)
|
||||
t.Errorf("sprite sheet output should mention spritesheet: %s", output.Prompt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunPromptAgent_SpriteSingle(t *testing.T) {
|
||||
output, err := RunPromptAgent(context.Background(), PromptAgentInput{
|
||||
Tags: []string{"像素", "中世纪", "战士"},
|
||||
AssetType: "sprite",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("RunPromptAgent failed: %v", err)
|
||||
}
|
||||
if !strings.Contains(output.Prompt, "纯白色背景") {
|
||||
t.Errorf("single sprite output should mention 纯白色背景: %s", output.Prompt)
|
||||
}
|
||||
if strings.Contains(output.Prompt, "spritesheet") {
|
||||
t.Errorf("single sprite output should not mention spritesheet: %s", output.Prompt)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -251,8 +267,9 @@ func TestBuildMetaPrompt_NoUserNote(t *testing.T) {
|
||||
|
||||
func TestParseTagsFromMeta(t *testing.T) {
|
||||
meta := `用户选择标签: 像素、中世纪、战士
|
||||
素材类型: sprite`
|
||||
tags, assetType := parseTagsFromMeta(meta)
|
||||
素材类型: sprite
|
||||
用户原始描述: 一个持剑角色`
|
||||
tags, assetType, userPrompt := parseTagsFromMeta(meta)
|
||||
if len(tags) != 3 {
|
||||
t.Fatalf("expected 3 tags, got %d: %v", len(tags), tags)
|
||||
}
|
||||
@@ -262,26 +279,29 @@ func TestParseTagsFromMeta(t *testing.T) {
|
||||
if assetType != "sprite" {
|
||||
t.Errorf("expected assetType=sprite, got %s", assetType)
|
||||
}
|
||||
if userPrompt != "一个持剑角色" {
|
||||
t.Errorf("expected userPrompt='一个持剑角色', got %s", userPrompt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTagsFromMeta_SingleTag(t *testing.T) {
|
||||
meta := `用户选择标签: 赛博朋克
|
||||
素材类型: background`
|
||||
tags, _ := parseTagsFromMeta(meta)
|
||||
tags, _, _ := parseTagsFromMeta(meta)
|
||||
if len(tags) != 1 || tags[0] != "赛博朋克" {
|
||||
t.Errorf("expected [赛博朋克], got %v", tags)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTagsFromMeta_Empty(t *testing.T) {
|
||||
tags, assetType := parseTagsFromMeta("no tags here")
|
||||
tags, assetType, _ := parseTagsFromMeta("no tags here")
|
||||
if len(tags) != 0 || assetType != "" {
|
||||
t.Errorf("expected empty, got tags=%v assetType=%s", tags, assetType)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateStructuredPrompt(t *testing.T) {
|
||||
prompt := generateStructuredPrompt([]string{"像素", "战士"}, "sprite")
|
||||
prompt := generateStructuredPrompt([]string{"像素", "战士", "精灵表"}, "sprite", true)
|
||||
if !strings.HasPrefix(prompt, "【主题】") {
|
||||
t.Error("prompt should start with 【主题】")
|
||||
}
|
||||
@@ -293,21 +313,36 @@ func TestGenerateStructuredPrompt(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateStructuredPrompt_Single(t *testing.T) {
|
||||
prompt := generateStructuredPrompt([]string{"像素", "战士"}, "sprite", false)
|
||||
if !strings.Contains(prompt, "独立PNG") {
|
||||
t.Errorf("single sprite should contain 独立PNG: %s", prompt)
|
||||
}
|
||||
if strings.Contains(prompt, "spritesheet") {
|
||||
t.Errorf("single sprite should not contain spritesheet: %s", prompt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildSubject(t *testing.T) {
|
||||
tags := []string{"像素", "战士"}
|
||||
tests := []struct {
|
||||
assetType, want string
|
||||
isSheet bool
|
||||
}{
|
||||
{"sprite", "精灵图"},
|
||||
{"background", "场景背景"},
|
||||
{"ui", "UI元素"},
|
||||
{"animation", "动画帧序列"},
|
||||
{"unknown", "游戏素材"},
|
||||
{"sprite", "精灵图", false},
|
||||
{"sprite", "精灵表", true},
|
||||
{"background", "场景背景", false},
|
||||
{"background", "瓦片集", true},
|
||||
{"ui", "UI元素", false},
|
||||
{"ui", "瓦片集", true},
|
||||
{"animation", "动画帧序列", false},
|
||||
{"animation", "精灵表", true},
|
||||
{"unknown", "游戏素材", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
result := buildSubject(tags, tt.assetType)
|
||||
result := buildSubject(tags, tt.assetType, tt.isSheet)
|
||||
if !strings.Contains(result, tt.want) {
|
||||
t.Errorf("buildSubject(%q) = %s, want containing %q", tt.assetType, result, tt.want)
|
||||
t.Errorf("buildSubject(%q, isSheet=%v) = %s, want containing %q", tt.assetType, tt.isSheet, result, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -327,17 +362,48 @@ func TestBuildStyle(t *testing.T) {
|
||||
func TestBuildTechNotes(t *testing.T) {
|
||||
tests := []struct {
|
||||
assetType, want string
|
||||
isSheet bool
|
||||
}{
|
||||
{"sprite", "spritesheet"},
|
||||
{"background", "1920x1080"},
|
||||
{"ui", "九宫格"},
|
||||
{"animation", "4方向x4帧"},
|
||||
{"unknown", "PNG"},
|
||||
// 默认单人模式
|
||||
{"sprite", "纯白色背景", false},
|
||||
{"background", "1920x1080", false},
|
||||
{"ui", "九宫格", false},
|
||||
{"animation", "4方向x4帧", false},
|
||||
{"unknown", "PNG", false},
|
||||
// 瓦片集/精灵表模式
|
||||
{"sprite", "spritesheet", true},
|
||||
{"background", "tileset", true},
|
||||
{"ui", "瓦片集", true},
|
||||
{"animation", "spritesheet", true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
result := buildTechNotes(tt.assetType)
|
||||
result := buildTechNotes(tt.assetType, tt.isSheet)
|
||||
if !strings.Contains(result, tt.want) {
|
||||
t.Errorf("buildTechNotes(%q) = %s, want containing %q", tt.assetType, result, tt.want)
|
||||
t.Errorf("buildTechNotes(%q, isSheet=%v) = %s, want containing %q", tt.assetType, tt.isSheet, result, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsSheetRequest(t *testing.T) {
|
||||
tests := []struct {
|
||||
tags []string
|
||||
prompt string
|
||||
want bool
|
||||
}{
|
||||
{[]string{"像素", "精灵表"}, "", true},
|
||||
{[]string{"像素", "spritesheet"}, "", true},
|
||||
{[]string{"地形", "瓦片集"}, "", true},
|
||||
{[]string{"UI", "tileset"}, "", true},
|
||||
{[]string{"场景", "tilemap"}, "", true},
|
||||
{[]string{"像素", "战士"}, "", false},
|
||||
{[]string{"像素"}, "生成一个精灵表", true},
|
||||
{[]string{"森林"}, "场景瓦片集", true},
|
||||
{nil, "", false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
got := isSheetRequest(tt.tags, tt.prompt)
|
||||
if got != tt.want {
|
||||
t.Errorf("isSheetRequest(tags=%v, prompt=%q) = %v, want %v", tt.tags, tt.prompt, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user