Last active
August 29, 2015 13:59
-
-
Save amosl/10592871 to your computer and use it in GitHub Desktop.
Shared Material
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment