Hey, I wanted to ask how other developers handle textures in general; whether cropping large files is better than wasting hellalot of space by saving each texture individually with a lot of black around them, since textures follow the "power of two" rule?
As an example, my approach to using large texture resources is using SetTexCoord and then finding the starting point in the topleft corner of a given subtexture and then using its width and height to determine the bottomright corner.
I use this to create my own "atlas" of textures, stored in simple tables containing width, height and the topleft starting point (x and y values). I'm not sure whether this is a good approach or not. It definitely works, but is there a better way of doing it that I'm unaware of?
Example:
Lua Code:
local _, db = ...
local path = "Interface\\AddOns\\MyAddon\\Textures\\"
db.Atlas = {}
-- width, height, startX, startY
db.Atlas.Frame = {
Size = {1024, 1024}, -- source texture size
Main = {912, 842, 0, 0},
Ornament = {89, 61, 935, 914},
SplitStack = {237, 115, 0, 842},
Item = {382, 68, 619, 956},
ItemPress = {382, 68, 237, 888},
ItemFocus = {382, 68, 237, 956},
}
-- calculate the fraction of the image size
local function GetCoords(sourceSize, subTexture)
local sourceWidth, sourceHeight = unpack(sourceSize)
local width, height, startX, startY = unpack(subTexture)
return {
(startX) /sourceWidth, -- left
(startX+width) /sourceWidth, -- right
(startY) /sourceHeight, -- top
(startY+height) /sourceHeight, -- bottom
}
end
function MyAddon:CreateAtlasTexture(parent, source, subTexture, layer, name, resizeParent)
local fullTexture = db.Atlas[source]
local textureName = name or subTexture
local subTexture = fullTexture[subTexture]
local width, height = unpack(subTexture)
local texture = parent:CreateTexture(nil, layer)
texture:SetTexture(path..source)
texture:SetTexCoord(unpack(GetCoords(fullTexture.Size, subTexture)))
texture:SetAllPoints(parent)
parent[textureName] = texture
if resizeParent == true then
parent:SetSize(width, height)
end
return texture
end
The info tab in Photoshop gives me all the info I need for calculations on a given subtexture:
..on a source texture file called "Frame.tga", which is 1024x1024:
In-game result: