Skip to content

Instantly share code, notes, and snippets.

@amosl
Last active August 29, 2015 13:59
Show Gist options
  • Save amosl/10592871 to your computer and use it in GitHub Desktop.
Save amosl/10592871 to your computer and use it in GitHub Desktop.
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