Skip to content

Instantly share code, notes, and snippets.

@b005t3r
Created January 30, 2014 15:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b005t3r/8710810 to your computer and use it in GitHub Desktop.
Save b005t3r/8710810 to your computer and use it in GitHub Desktop.
Using Starling's TextureAttlas with Away3D
public class CardAsset extends ObjectContainer3D {
private static const frameGeom:PlaneGeometry = new PlaneGeometry(440, 600, 1, 1, false, true);
private static const titleGeom:PlaneGeometry = new PlaneGeometry(380, 70, 1, 1, false, true);
private static const artGeom:PlaneGeometry = new PlaneGeometry(380, 200, 1, 1, false, true);
private static const textGeom:PlaneGeometry = new PlaneGeometry(380, 200, 1, 1, false, true);
private var _frameMesh:Mesh;
private var _titleMesh:Mesh;
private var _artMesh:Mesh;
private var _textMesh:Mesh;
public function CardAsset(atlas:TextureAtlas) {
_frameMesh = new Mesh(frameGeom, atlas.sharedMaterial);
atlas.setupUV(_frameMesh.subMeshes[0], "frame");
_titleMesh = new Mesh(titleGeom, atlas.sharedMaterial);
atlas.setupUV(_titleMesh.subMeshes[0], "title");
_artMesh = new Mesh(artGeom, atlas.sharedMaterial);
atlas.setupUV(_artMesh.subMeshes[0], "art");
_textMesh = new Mesh(textGeom, atlas.sharedMaterial);
atlas.setupUV(_textMesh.subMeshes[0], "text");
// frame should be drawn before all other layers
//frameMesh.zOffset = -250;
_titleMesh.x = 0;
_titleMesh.y = 230;
_titleMesh.z = -20;
_artMesh.x = 0;
_artMesh.y = 80;
_artMesh.z = -40;
_textMesh.x = 0;
_textMesh.y = -140;
_textMesh.z = -30;
addChild(_frameMesh);
addChild(_titleMesh);
addChild(_artMesh);
addChild(_textMesh);
}
public function get frameMesh():Mesh { return _frameMesh; }
public function get titleMesh():Mesh { return _titleMesh; }
public function get artMesh():Mesh { return _artMesh; }
public function get textMesh():Mesh { return _textMesh; }
}
//setup the scene
var texture:BitmapTexture = Cast.bitmapTexture(ArchonTextures);
_atlas = new TextureAtlas(new XML(new ArchonAtlas()), texture);
_atlas.sharedMaterial.alphaBlending = true;
// crate card
var card:CardAsset = new CardAsset(_atlas);
public class TextureAtlas {
protected var _regions:Object = {};
protected var _material:TextureMaterial;
public function TextureAtlas(atlas:XML, texture:Texture2DBase) {
_material = new TextureMaterial(texture, true, false, false);
_material.animateUVs = true; // this has to be on for UV manipulation to work
parseAtlasXml(atlas);
}
public function get texture():Texture2DBase { return _material.texture; }
public function get sharedMaterial():TextureMaterial { return _material; }
public function setupUV(submesh:SubMesh, region:String):void {
if(submesh == null)
throw new ArgumentError("submesh cannot be null");
var regionData:RegionData = _regions[region];
if(regionData == null)
throw new ArgumentError("given region does not exist: " + region);
var w:Number = _material.texture.width;
var h:Number = _material.texture.height;
submesh.offsetU = regionData.rect.x / w;
submesh.offsetV = regionData.rect.y / h;
submesh.scaleU = regionData.rect.width / w;
submesh.scaleV = regionData.rect.height / h;
}
protected function parseAtlasXml(atlas:XML):void {
const scale:Number = 1; // for scaling to higher DPI displays
for each (var subTexture:XML in atlas.SubTexture) {
var name:String = subTexture.attribute("name");
var x:Number = parseFloat(subTexture.attribute("x")) / scale;
var y:Number = parseFloat(subTexture.attribute("y")) / scale;
var width:Number = parseFloat(subTexture.attribute("width")) / scale;
var height:Number = parseFloat(subTexture.attribute("height")) / scale;
var frameX:Number = parseFloat(subTexture.attribute("frameX")) / scale;
var frameY:Number = parseFloat(subTexture.attribute("frameY")) / scale;
var frameWidth:Number = parseFloat(subTexture.attribute("frameWidth")) / scale;
var frameHeight:Number = parseFloat(subTexture.attribute("frameHeight")) / scale;
var region:Rectangle = new Rectangle(x, y, width, height);
var frame:Rectangle = frameWidth > 0 && frameHeight > 0
? new Rectangle(frameX, frameY, frameWidth, frameHeight)
: null
;
_regions[name] = new RegionData(region, frame);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment