Fixed TexturePacker Importer

2 votes · 0 comments

Trimming wasn't working properly on the standard importer in samples.

This code will make trimming work and also center the images to be consistent with the rest of moai.

~Muzz

raw ·
copy
· download
function loadAtlas(lua, png) local frames = dofile ( lua ).frames local tex = MOAITexture.new () tex:load ( png ) local xtex, ytex = tex:getSize () -- 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. --MUZZEDIT. Trimming now works, and images are centered local cr = frame.spriteColorRect local r = {} r.x0 = cr.x r.y0 = cr.y + cr.height r.x1 = cr.x + cr.width r.y1 = cr.y r.x0 = r.x0 - frame.spriteSourceSize.width/2 r.y0 = -r.y0 + frame.spriteSourceSize.height/2 r.x1 = r.x1 -frame.spriteSourceSize.width/2 r.y1 = -r.y1 + frame.spriteSourceSize.height/2 frame.geomRect = r end -- Construct the deck local deck = MOAIGfxQuadDeck2D.new () deck:setTexture ( tex ) deck:reserve ( #frames ) local names = {} for i, frame in ipairs ( frames ) do local q = frame.uvQuad local r = frame.geomRect names[frame.name] = i deck:setUVQuad ( i, q.x0,q.y0, q.x1,q.y1, q.x2,q.y2, q.x3,q.y3 ) deck:setRect ( i, r.x0,r.y0, r.x1,r.y1 ) end return deck, names end

Be the first to comment

Sign in with OpenID