/
AtlasBuilder.as
69 lines (55 loc) · 2.19 KB
/
AtlasBuilder.as
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
58
59
60
61
62
63
64
65
66
67
68
69
package treefortress.textureutils
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.utils.getTimer;
import starling.textures.Texture;
import starling.textures.TextureAtlas;
public class AtlasBuilder
{
public static var packTime:int;
public static var atlasBitmap:BitmapData;
public static var atlasXml:XML;
public function AtlasBuilder(){
}
public static function build(bitmapList:Vector.<Bitmap>, scale:Number = 1, padding:int = 2, width:int = 2048, height:int = 2048):TextureAtlas {
var t:int = getTimer();
atlasBitmap = new BitmapData(width, height, true, 0x0);
var packer:MaxRectPacker = new MaxRectPacker(width, height);
var atlasText:String = "";
var bitmap:Bitmap, name:String, rect:Rectangle, subText:String, m:Matrix = new Matrix();
for(var i:int = 0; i < bitmapList.length; i++){
bitmap = bitmapList[i];
name = bitmapList[i].name;
rect = packer.quickInsert((bitmap.width * scale) + padding * 2, (bitmap.height * scale) + padding * 2);
//Add padding
rect.x += padding;
rect.y += padding;
rect.width -= padding * 2;
rect.height -= padding * 2;
//Apply scale
if(!rect){ trace("Texture Limit Exceeded"); continue; }
m.identity();
m.scale(scale, scale);
m.translate(rect.x, rect.y);
atlasBitmap.draw(bitmapList[i], m);
//Create XML line item for TextureAtlas
subText = '<SubTexture name="'+name+'" ' +
'x="'+rect.x+'" y="'+rect.y+'" width="'+rect.width+'" height="'+rect.height+'" frameX="0" frameY="0" ' +
'frameWidth="'+rect.width+'" frameHeight="'+rect.height+'"/>';
atlasText = atlasText + subText;
}
//Create XML from text (much faster than working with an actual XML object)
atlasText = '<TextureAtlas imagePath="atlas.png">' + atlasText + "</TextureAtlas>";
atlasXml = new XML(atlasText);
//Create the atlas
var texture:Texture = Texture.fromBitmapData(atlasBitmap, false);
var atlas:TextureAtlas = new TextureAtlas(texture, atlasXml);
//Save elapsed time in case we're curious how long this took
packTime = getTimer() - t;
return atlas;
}
}
}