Create a gist now

Instantly share code, notes, and snippets.

@amosl /AwaySheet.as
Last active Aug 29, 2015

What would you like to do?
Shared Material
package
{
import away3d.core.base.SubMesh;
import away3d.entities.Mesh;
import away3d.materials.TextureMaterial;
import away3d.textures.Texture2DBase;
import flash.geom.Rectangle;
import starling.textures.Texture;
import starling.textures.TextureAtlas;
/**
* ...
*/
public class AwaySheet
{
protected var _material:TextureMaterial;
private var _baseSheet:TextureAtlas;
public function AwaySheet(atlas:TextureAtlas)
{
_baseSheet = atlas;
createAwayTexture(_baseSheet.texture);
}
private function createAwayTexture(tex:Texture):void
{
var sharedTex:SharedTexture = new SharedTexture(tex);
_material = new TextureMaterial(sharedTex, true, false, false);
_material.animateUVs = true; // this has to be on for UV manipulation to work
_material.alphaBlending = true;
}
public function get atlas():TextureAtlas { return _baseSheet; }
public function get awayTexture():Texture2DBase { return _material.texture; }
public function get sharedMaterial():TextureMaterial { return _material; }
public function setupUV(mesh:Mesh, name:String):void
{
if(mesh == null)
throw new ArgumentError("submesh cannot be null");
var regionRc:Rectangle = _baseSheet.getRegion(name);
if(regionRc == null)
throw new ArgumentError("given region does not exist: " + name);
var submesh:SubMesh = mesh.subMeshes[0];
var w:Number = _baseSheet.texture.width;
var h:Number = _baseSheet.texture.height;
submesh.offsetU = regionRc.x / w;
submesh.offsetV = regionRc.y / h;
submesh.scaleU = regionRc.width / w;
submesh.scaleV = regionRc.height / h;
}
}
}
package
{
import away3d.textures.Texture2DBase;
import flash.display3D.textures.TextureBase;
import starling.textures.Texture;
/**
* ...
*/
public class SharedTexture extends Texture2DBase
{
private var refTexture:Texture;
public function SharedTexture(tex:Texture)
{
super();
refTexture = tex;
}
override protected function uploadContent(texture:TextureBase):void
{
// Leave empty
}
override protected function createTexture(context:Context3D):TextureBase
{
var baseTex:TextureBase = refTexture.base;
return baseTex;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment