/
atlas.go
206 lines (188 loc) · 4.88 KB
/
atlas.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright 2015 Matthew Collins
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package atlas provides a basic texture atlas
package atlas
import (
"errors"
"math"
)
var (
// ErrAtlasFull is returned when an image can't fit in an atlas
ErrAtlasFull = errors.New("atlas full")
)
// Type is a texture atlas for storing quake
// pictures (from the bsp package). The buffer
// is public to allow easy uploading to the
// gpu.
type Type struct {
width, height int
Buffer []byte
pixelSize int
freeSpace []*Rect
padding int
}
// Rect represents a location in a texture
// atlas.
type Rect struct {
X, Y int
Width, Height int
}
// New creates an atlas of the specified size
// with zero padding around the textures.
// pixelSize controls the number of bytes per
// a pixel
func New(width, height, pixelSize int) *Type {
return NewPadded(width, height, pixelSize, 0)
}
// NewPadded creates an atlas of the specified
// size. Textures are padded with the passed
// number of pixels around each size. This is
// useful for filtering textures without other
// textures bleeding through.
// pixelSize controls the number of bytes per
// a pixel
func NewPadded(width, height, pixelSize, padding int) *Type {
a := &Type{
width: width,
height: height,
padding: padding,
pixelSize: pixelSize,
Buffer: make([]byte, width*height*pixelSize),
}
a.freeSpace = append(a.freeSpace, &Rect{
X: 0,
Y: 0,
Width: width,
Height: height,
})
return a
}
// NewLight creates an atlas of the specified
// size. Textures are padded with the passed
// number of pixels around each size. This is
// useful for filtering textures without other
// textures bleeding through.
// pixelSize controls the number of bytes per
// a pixel. This will have no backing buffer.
func NewLight(width, height, padding int) *Type {
a := &Type{
width: width,
height: height,
padding: padding,
}
a.freeSpace = append(a.freeSpace, &Rect{
X: 0,
Y: 0,
Width: width,
Height: height,
})
return a
}
// Add adds the passed texture to the atlas and
// returns the location in the atlas. This method
// panics if the atlas has been baked.
func (a *Type) Add(image []byte, width, height int) (*Rect, error) {
// Double the padding since its for both
// sides
w := width + (a.padding * 2)
h := height + (a.padding * 2)
var target *Rect
targetIndex := 0
priority := math.MaxInt32
// Search through and find the best fit for this texture
for i, free := range a.freeSpace {
if free.Width >= w && free.Height >= h {
currentPriority := (free.Width - w) * (free.Height - h)
if target == nil || currentPriority < priority {
target = free
priority = currentPriority
targetIndex = i
}
// Perfect match, we can break early
if priority == 0 {
break
}
}
}
if target == nil {
return nil, ErrAtlasFull
}
// Copy the image into the atlas
if a.Buffer != nil {
CopyImage(image, a.Buffer, target.X, target.Y, w, h, a.width, a.height, a.pixelSize, a.padding)
}
tx := target.X + a.padding
ty := target.Y + a.padding
if w == target.Width {
target.Y += h
target.Height -= h
if target.Height == 0 {
// Remove empty sections
a.freeSpace = append(a.freeSpace[:targetIndex], a.freeSpace[targetIndex+1:]...)
}
} else {
if target.Height > h {
// Split by height
a.freeSpace = append(
[]*Rect{{
target.X, target.Y + h,
w, target.Height - h,
}},
a.freeSpace...,
)
}
target.X += w
target.Width -= w
}
return &Rect{
X: tx,
Y: ty,
Width: width,
Height: height,
}, nil
}
// helper method that allows for out of bounds access
// to a picture. The coordinates to be changed to the
// nearest edge when out of bounds.
func safeGetPixel(x, y, w, h int) int {
if x < 0 {
x = 0
}
if x >= w {
x = w - 1
}
if y < 0 {
y = 0
}
if y >= h {
y = h - 1
}
return y*w + x
}
// CopyImage copies the passed image data to the
// target buffer, accounting for padding.
func CopyImage(data, buffer []byte, targetX, targetY, w, h, width, height, pixelSize, padding int) {
for y := 0; y < h; y++ {
index := (targetY+y)*width + targetX
for x := 0; x < w; x++ {
px := x - padding
py := y - padding
pw := w - (padding << 1)
ph := h - (padding << 1)
bo := (index + x) * pixelSize
do := safeGetPixel(px, py, pw, ph) * pixelSize
copy(buffer[bo:bo+pixelSize], data[do:do+pixelSize])
}
}
}