-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite.go
138 lines (107 loc) · 2.74 KB
/
sprite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package spritenik
import (
"fmt"
"image"
_ "image/jpeg"
_ "image/png"
"sort"
"golang.org/x/image/draw"
)
func GenerateSprite(textures []Texture, pixelRatio int, renderImage bool) (map[string]*TextureSprite, image.Image, error) {
set := map[string]*TextureSprite{}
chDone := make(chan struct{})
defer close(chDone)
blocks := make([]*Node, 0)
for i := range textures {
t := textures[i]
img := t.TextureImage()
tm := &TextureMeta{
Name: t.TextureName(),
PixelRatio: t.TexturePixelRatio(),
Width: t.TextureWidth(),
Height: t.TextureHeight(),
}
ts := &TextureSprite{
TextureMeta: tm.ScaleTo(pixelRatio),
OriginTextureMeta: tm,
Image: img,
}
blocks = append(blocks, NewNode(ts.Name, ts.Width, ts.Height))
set[t.TextureName()] = ts
}
sort.Slice(blocks, func(i, j int) bool {
return blocks[j].Width*blocks[j].Height < blocks[i].Width*blocks[i].Height
})
(&GrowingPacker{}).Fit(blocks)
w := 0
h := 0
for _, b := range blocks {
set[b.Key].X = b.X
set[b.Key].Y = b.Y
if renderImage {
mayMaxX := b.Width + b.X
mayMaxY := b.Height + b.Y
if mayMaxX > w {
w = mayMaxX
}
if mayMaxY > h {
h = mayMaxY
}
}
}
if renderImage {
if w == 0 {
w = 1
}
if h == 0 {
h = 1
}
dst := image.NewRGBA(image.Rect(0, 0, w, h))
for _, ts := range set {
img := ts.Image
if ts.OriginTextureMeta != nil && ts.OriginTextureMeta.PixelRatio != pixelRatio {
d := image.NewRGBA(image.Rect(0, 0, ts.Width, ts.Height))
draw.BiLinear.Scale(d, d.Bounds(), ts.Image, ts.Image.Bounds(), draw.Over, nil)
img = d
}
draw.Draw(dst, dst.Bounds(), img, image.Pt(-ts.X, -ts.Y), draw.Src)
}
return set, dst, nil
}
return set, nil, nil
}
type Texture interface {
TextureName() string
TextureWidth() int
TextureHeight() int
TexturePixelRatio() int
TextureImage() image.Image
}
type TextureSprite struct {
*TextureMeta
X int `json:"x"`
Y int `json:"y"`
OriginTextureMeta *TextureMeta `json:"-"`
Image image.Image `json:"-"`
}
func (s TextureSprite) String() string {
return fmt.Sprintf("(%d,%d) %s", s.X, s.Y, s.TextureMeta)
}
type TextureMeta struct {
Name string `json:"-"`
Width int `json:"width"`
Height int `json:"height"`
PixelRatio int `json:"pixelRatio"`
}
func (t TextureMeta) ScaleTo(pixelRatio int) *TextureMeta {
scale := float64(pixelRatio) / float64(t.PixelRatio)
return &TextureMeta{
Name: t.Name,
Width: int(float64(t.Width) * scale),
Height: int(float64(t.Height) * scale),
PixelRatio: pixelRatio,
}
}
func (t TextureMeta) String() string {
return fmt.Sprintf("%s#%d,%d@%dx", t.Name, t.Width, t.Height, t.PixelRatio)
}