Thread Tools Display Modes
08-03-15, 09:06 AM   #1
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 431
Question TexCoord cropping: Best approach?

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:
  1. local _, db = ...
  2. local path = "Interface\\AddOns\\MyAddon\\Textures\\"
  3. db.Atlas = {}
  4.  
  5. -- width, height, startX, startY
  6. db.Atlas.Frame = {
  7.     Size        = {1024, 1024}, -- source texture size
  8.     Main        = {912, 842, 0,   0},
  9.     Ornament    = {89,  61,  935, 914},
  10.     SplitStack  = {237, 115, 0,   842},
  11.     Item        = {382, 68,  619, 956},
  12.     ItemPress   = {382, 68,  237, 888},
  13.     ItemFocus   = {382, 68,  237, 956},
  14. }
  15.  
  16. -- calculate the fraction of the image size
  17. local function GetCoords(sourceSize, subTexture)
  18.     local sourceWidth, sourceHeight = unpack(sourceSize)
  19.     local width, height, startX, startY = unpack(subTexture)
  20.     return {
  21.         (startX)        /sourceWidth,   -- left
  22.         (startX+width)  /sourceWidth,   -- right
  23.         (startY)        /sourceHeight,  -- top
  24.         (startY+height) /sourceHeight,  -- bottom
  25.     }
  26. end
  27.  
  28. function MyAddon:CreateAtlasTexture(parent, source, subTexture, layer, name, resizeParent)
  29.     local fullTexture = db.Atlas[source]
  30.     local textureName = name or subTexture
  31.     local subTexture = fullTexture[subTexture]
  32.     local width, height = unpack(subTexture)
  33.     local texture = parent:CreateTexture(nil, layer)
  34.     texture:SetTexture(path..source)
  35.     texture:SetTexCoord(unpack(GetCoords(fullTexture.Size, subTexture)))
  36.     texture:SetAllPoints(parent)
  37.     parent[textureName] = texture
  38.     if resizeParent == true then
  39.         parent:SetSize(width, height)
  40.     end
  41.     return texture
  42. 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:
__________________

Last edited by MunkDev : 08-03-15 at 09:38 AM.
  Reply With Quote
08-03-15, 11:15 AM   #2
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
It's way to much hassle if you ask me, it can also cause some issue if you later want to rotate/animate those textures. However if you don't plan to edit/rotate/animate the textures later then it's the best method.
  Reply With Quote
08-03-15, 11:15 AM   #3
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
This is what the game does.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
08-03-15, 11:51 AM   #4
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
Originally Posted by Resike View Post
It's way to much hassle if you ask me, it can also cause some issue if you later want to rotate/animate those textures. However if you don't plan to edit/rotate/animate the textures later then it's the best method.
It's substantially better performance-wise to draw the same texture multiple times than it is to draw multiple separate textures on-screen.

As for whether it's worth the hassle of slicing up and defining a texture atlas, that's up to the addon author.
  Reply With Quote
08-04-15, 04:35 AM   #5
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by semlar View Post
It's substantially better performance-wise to draw the same texture multiple times than it is to draw multiple separate textures on-screen.

As for whether it's worth the hassle of slicing up and defining a texture atlas, that's up to the addon author.
I'm pretty sure the game is already collecting the smaller textures and putting them into a bigger square texture by default, to save video memory. However if you do this by yourself then you can save a little bit more space actually.

Last edited by Resike : 08-04-15 at 04:40 AM.
  Reply With Quote
08-04-15, 11:04 AM   #6
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
Whichever way you end up doing, you should have all the coordinates precalculated instead of dynamically calculating the same numbers repeatedly live. Even if you have to do something like this, it'll be a lot better. This also allows more control by allowing you to flip textures by swapping the relevant X or Y values.
Lua Code:
  1. local TexCoords={
  2. --  {Left, Right, Top, Bottom}
  3.     Main=       {0,         0,          912/1024,   842/1024};
  4.     Ornament=   {935/1024914/1024,   1,          975/1024},
  5. --  etc...
  6. }
  7.  
  8. --  Later on
  9. texture:SetTexCoord(unpack(TexCoords.Main));
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 08-04-15 at 11:18 AM.
  Reply With Quote
08-04-15, 04:58 PM   #7
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 431
Originally Posted by SDPhantom View Post
Whichever way you end up doing, you should have all the coordinates precalculated instead of dynamically calculating the same numbers repeatedly live. Even if you have to do something like this, it'll be a lot better. This also allows more control by allowing you to flip textures by swapping the relevant X or Y values.
Damn it, I was hoping to get by without doing simple arithmetic on every texture. Oh well. This solution is indeed cleaner.
__________________
  Reply With Quote
08-05-15, 11:15 AM   #8
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
The solution I posted is a compromise, but it only ends up making the calculation once on load instead of every time it's referenced. It's also simple enough in which you put in point/length instead of percentage values. The point of the matter is the more work you leave the computer to deal with, the less CPU power it has to do everything else it needs to do.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
08-06-15, 03:29 AM   #9
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 431
The consensus seems to be that cropping large textures is the best approach to using a lot of texture resources, instead of splitting them up into separate files. Just looking at the art provided by blizzard, this is the way they do it (both WoW and Diablo).

Is there any proof to any of your claims regarding video memory? There will obviously be less black in a texture file where everything is mashed together, because the sub textures are obviously not always a perfect fit for power of 2 image sizes.
__________________
  Reply With Quote
08-06-15, 05:33 AM   #10
Resike
A Pyroguard Emberseer
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,290
Originally Posted by MunkDev View Post
The consensus seems to be that cropping large textures is the best approach to using a lot of texture resources, instead of splitting them up into separate files. Just looking at the art provided by blizzard, this is the way they do it (both WoW and Diablo).

Is there any proof to any of your claims regarding video memory? There will obviously be less black in a texture file where everything is mashed together, because the sub textures are obviously not always a perfect fit for power of 2 image sizes.
I'm not sure the game still doing it or not. I only know that is used to do with the icons for example.

When you tried to rotate an texture back then it showed all the cached icons/textures around that icon, if there were no cached icons then they weren't visible.

http://i.imgur.com/7zPamH3.jpg

But my guess would be that the game still does this, since sometimes some icons looks really weird like missing 1 line of pixels or have an extra one, which would indicate that the game is reading that icon from such a table and there is an overlapping there. But you really need to have some serious OCD to notice something like that.

Of course all this is only happening, if you don't use any custom icon packs.

Also texture rotation/animation is still fucked up currently, if the texture doesn't have at least a 1 pixel fully transparent border around it.

Last edited by Resike : 08-06-15 at 05:44 AM.
  Reply With Quote
08-06-15, 09:10 AM   #11
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
Originally Posted by Resike View Post
I'm not sure the game still doing it or not. I only know that is used to do with the icons for example.

When you tried to rotate an texture back then it showed all the cached icons/textures around that icon, if there were no cached icons then they weren't visible.

http://i.imgur.com/7zPamH3.jpg
This is strictly a result of the game merging the icon textures, it doesn't do this to anything else.
  Reply With Quote
08-08-15, 09:39 AM   #12
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 431
I'm happy Sublime has support for multiple cursors.
Is it possible to calculate these expressions within a text editor?


Edit: Calculate for Sublime did the trick.
__________________

Last edited by MunkDev : 08-08-15 at 10:24 AM.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » TexCoord cropping: Best approach?


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off