-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtexturepack.lua
108 lines (98 loc) · 2.31 KB
/
texturepack.lua
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
local tbinpack = require "tbinpack"
local lfs = require "lfs"
local function parse_opt(...)
local t = { ... }
local ret = {}
local opt
for i = 1, #t do
local v = t[i]
if v:sub(1,1) == "-" then
opt = v:sub(2)
ret[opt] = true
else
if opt then
ret[opt] = v
opt = nil
else
table.insert(ret, v)
end
end
end
return ret
end
local function fetch_source(input_path)
local img = {}
for filename in lfs.dir(input_path) do
local name, ext = filename:match "(.*)%.(%a+)$"
ext = ext and ext:lower()
if ext == "png" or ext == "tga" then
filename = string.format("%s/%s", input_path, filename)
local w,h,x,y,minw,minh =tbinpack.loadimage(filename)
table.insert(img, { name = name, filename = filename, w = minw, h = minh, kx = x, ky = y } )
end
end
return img
end
local function output_altas(rect, filename)
local f
if filename then
filename = filename .. ".altas"
f = assert(io.open(filename, "w"))
io.output(f)
end
for _, v in ipairs(rect) do
local tid = ""
if v.tid ~= 0 then
tid = string.format(" tid=%d", v.tid)
end
local line = string.format("%s kx=%d ky=%d width=%d height=%d x=%d y=%d%s\n",
v.filename, v.kx, v.ky, v.w, v.h, v.x, v.y, tid)
io.write(line)
end
if f then
f:close()
end
end
local function combine_textures(rect, filename, width, height, debugrect)
local t = {}
for _, v in ipairs(rect) do
local tid = v.tid + 1
local texture = t[tid]
if texture == nil then
texture = {}
t[tid] = texture
end
table.insert(texture, v)
end
for index, v in ipairs(t) do
local of = string.format("%s%d.png", filename, index-1)
tbinpack.combine(of, width, height, v, debugrect)
end
end
local USAGE = [[
Options:
-o outputfilename
-image (if enable, output combined image)
-debug (draw debug rect)
-i inputdir
-w width (default is 1024)
-h height (default is width)
Example:
lua textpack.lua -o output -i images -w 1024 -h 1024
]]
local function main(...)
local args = parse_opt(...)
if next(args) == nil then
print(USAGE)
end
local input_path = assert(args.i)
local rect = fetch_source(input_path)
local width = args.w or 1024
local height = args.h or width
tbinpack.binpack(rect, width, height)
output_altas(rect, args.o)
if args.image then
combine_textures(rect, args.o or "output", width, height, args.debug)
end
end
main(...)