Files
ngzz-mc/internal/atlas/atlas_test.go

60 lines
1.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package atlas
import (
"image"
"path/filepath"
"testing"
)
// testResolve 从真实素材包解析纹理。
func testResolve(key string) (string, bool) {
p := filepath.Join("..", "..", "resourcepacks", "texture-pack-default1.20.5-26.2",
"assets", "minecraft", "textures", key+".png")
if _, err := filepath.Abs(p); err != nil {
return "", false
}
if _, err := testStat(p); err != nil {
return "", false
}
return p, true
}
func testStat(p string) (interface{}, error) {
f, err := filepath.Glob(p)
if err != nil || len(f) == 0 {
return nil, err
}
return f[0], nil
}
// TestBuild 验证图集打包:UV 矩形齐全、图像尺寸正确。
func TestBuild(t *testing.T) {
keys := []string{"block/stone", "block/dirt", "block/grass_block_top"}
img, uv, err := Build(testResolve, keys, 16)
if err != nil {
t.Fatalf("构建图集失败: %v", err)
}
b := img.Bounds()
if b.Dx() != 32 || b.Dy() != 32 {
t.Fatalf("图集尺寸期望 32×32,实际 %dx%d", b.Dx(), b.Dy())
}
for _, k := range keys {
if _, ok := uv[k]; !ok {
t.Fatalf("纹理 %s 缺 UV", k)
}
}
// 三个键的 UV 不重叠且各不相同
if uv[keys[0]] == uv[keys[1]] {
t.Fatal("UV 矩形重叠")
}
}
// TestScaleNearest 验证最近邻缩放尺寸。
func TestScaleNearest(t *testing.T) {
src := image.NewRGBA(image.Rect(0, 0, 32, 32))
dst := scaleNearest(src, 16)
if db := dst.Bounds(); db.Dx() != 16 || db.Dy() != 16 {
t.Fatalf("缩放后尺寸期望 16×16,实际 %dx%d", db.Dx(), db.Dy())
}
}