-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTKTexturePackerUtil.lua
57 lines (51 loc) · 1.76 KB
/
TKTexturePackerUtil.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
module(..., package.seeall)
function load( lua, png )
local textureTable = {}
local frames = dofile ( lua ).frames
textureTable.texture = MOAITexture.new ()
textureTable.texture:load ( png )
-- Annotate the frame array with uv quads and geometry rects
for i, frame in ipairs ( frames ) do
-- convert frame.uvRect to frame.uvQuad to handle rotation
local uv = frame.uvRect
local q = {}
if not frame.textureRotated then
-- From Moai docs: "Vertex order is clockwise from upper left (xMin, yMax)"
q.x0, q.y0 = uv.u0, uv.v0
q.x1, q.y1 = uv.u1, uv.v0
q.x2, q.y2 = uv.u1, uv.v1
q.x3, q.y3 = uv.u0, uv.v1
else
-- Sprite data is rotated 90 degrees CW on the texture
-- u0v0 is still the upper-left
q.x3, q.y3 = uv.u0, uv.v0
q.x0, q.y0 = uv.u1, uv.v0
q.x1, q.y1 = uv.u1, uv.v1
q.x2, q.y2 = uv.u0, uv.v1
end
frame.uvQuad = q
-- convert frame.spriteColorRect and frame.spriteSourceSize
-- to frame.geomRect. Origin is at x0,y0 of original sprite
local cr = frame.spriteColorRect
local r = {}
r.x0 = cr.x - cr.width/2
r.y0 = cr.y - cr.height/2
r.x1 = cr.x + cr.width/2
r.y1 = cr.y + cr.height/2
frame.geomRect = r
end
-- Construct the deck
textureTable.deck = MOAIGfxQuadDeck2D.new ()
textureTable.deck:setTexture ( textureTable.texture )
textureTable.deck:reserve ( #frames )
local names = {}
for i, frame in ipairs ( frames ) do
local q = frame.uvQuad
local r = frame.geomRect
names[frame.name] = i
textureTable.deck:setUVQuad ( i, q.x0,q.y0, q.x1,q.y1, q.x2,q.y2, q.x3,q.y3 )
textureTable.deck:setRect ( i, r.x0,r.y0, r.x1,r.y1 )
end
textureTable.spriteNames = names
return textureTable
end