[lua] Ported rotated mesh region UV loading. See #1327.

This commit is contained in:
badlogic 2019-04-18 17:48:49 +02:00
parent cdd06b22f1
commit 5ad114f671
3 changed files with 57 additions and 26 deletions

View File

@ -186,7 +186,15 @@ function TextureAtlas:parse (atlasContent, imageLoader)
region.name = line
region.page = page
if readValue() == "true" then region.rotate = true end
local rotateValue = readValue()
if rotateValue == "true" then
region.degrees = 90
elseif rotateValue == "false" then
region.degrees = 0
else
region.degrees = tonumber(rotateValue)
end
if region.degrees == 90 then region.rotate = true end
local tuple = readTuple()
local x = parseInt(tuple[1])

View File

@ -44,6 +44,7 @@ function TextureAtlasRegion.new ()
self.y = 0
self.index = 0
self.rotate = false
self.degrees = 0
self.texture = nil
setmetatable(self, TextureAtlasRegion)

View File

@ -61,6 +61,11 @@ function MeshAttachment:updateUVs ()
local v = 0
local width = 0
local height = 0
local regionUVs = self.regionUVs
if not self.uvs or (#self.uvs ~= #regionUVs) then self.uvs = utils.newNumberArray(#regionUVs) end
local uvs = self.uvs
if not self.region then
u = 0
v = 0
@ -70,32 +75,48 @@ function MeshAttachment:updateUVs ()
local region = self.region
local textureWidth = region.page.width
local textureHeight = region.page.height
if region.rotate then
if region.degrees == 90 then
u = region.u - (region.originalHeight - region.offsetY - region.height) / textureWidth
v = region.v - (region.originalWidth - region.offsetX - region.width) / textureHeight
width = region.originalHeight / textureWidth
height = region.originalWidth / textureHeight
local i = 0
local n = #uvs
while i < n do
uvs[i + 1] = u + regionUVs[i + 2] * width;
uvs[i + 2] = v + (1 - regionUVs[i + 1]) * height;
i = i + 2
end
elseif region.degrees == 180 then
u = region.u - (region.originalWidth - region.offsetX - region.width) / textureWidth
v = region.v - region.offsetY / textureHeight
width = region.originalWidth / textureWidth
height = region.originalHeight / textureHeight
local i = 0
local n = #uvs
while i < n do
uvs[i + 1] = u + (1 - regionUVs[i + 1]) * width;
uvs[i + 2] = v + (1 - regionUVs[i + 2]) * height;
i = i + 2
end
elseif region.degrees == 270 then
u = region.u - region.offsetY / textureWidth
v = region.v - region.offsetX / textureHeight
width = region.originalHeight / textureWidth
height = region.originalWidth / textureHeight
local i = 0
local n = #uvs
while i < n do
uvs[i + 1] = u + (1 - regionUVs[i + 2]) * width;
uvs[i + 2] = v + regionUVs[i + 1] * height;
i = i + 2
end
else
u = region.u - region.offsetX / textureWidth;
v = region.v - (region.originalHeight - region.offsetY - region.height) / textureHeight;
width = region.originalWidth / textureWidth;
height = region.originalHeight / textureHeight;
end
end
local regionUVs = self.regionUVs
if not self.uvs or (#self.uvs ~= #regionUVs) then self.uvs = utils.newNumberArray(#regionUVs) end
local uvs = self.uvs
if self.region and self.region.rotate then
local i = 0
local n = #uvs
while i < n do
uvs[i + 1] = u + regionUVs[i + 2] * width;
uvs[i + 2] = v + height - regionUVs[i + 1] * height;
i = i + 2
end
else
local i = 0
local n = #uvs
while i < n do
@ -104,6 +125,7 @@ function MeshAttachment:updateUVs ()
i = i + 2
end
end
end
end
function MeshAttachment:applyDeform (sourceAttachment)