Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Code by rodrigolopezpeker (aka 7interactive™) on 1/15/14 7:47 PM.
- */
- package ar.com.rodrigolopezpeker.genome.utils {
- import com.genome2d.components.renderables.GSimpleShape;
- import com.genome2d.core.GNode;
- import com.genome2d.core.GNodeFactory;
- import com.genome2d.core.Genome2D;
- import com.genome2d.textures.GTexture;
- import com.genome2d.textures.factories.GTextureFactory;
- import flash.display.BitmapData;
- import flash.display.DisplayObject;
- import flash.display.Graphics;
- import flash.geom.Point;
- import nape.geom.GeomPoly;
- import nape.geom.GeomPolyList;
- import nape.geom.GeomVertexIterator;
- import nape.geom.MarchingSquares;
- import nape.geom.Vec2;
- public class GShapeUtils {
- private static const DEFAULT_POINT:Point = new Point();
- private static const DEFAULT_CONFIGURATION:Object =
- {granularity: 8, quality: 2, simplification: 1.5, alphaThreshold: 0x80, offsetPosition: DEFAULT_POINT };
- public static var DEFAULT_TEXTURE:GTexture;
- /**
- * Utility class, no constructor.
- */
- public function GShapeUtils() {}
- public static function parseGSimpleShape(pDisplay:*, pConf:Object = null, pParentNode:GNode = null):GSimpleShape {
- if (!pConf) pConf = {};
- // create default texture.
- if (!DEFAULT_TEXTURE && Genome2D.getInstance().isInitialized()) {
- DEFAULT_TEXTURE = GTextureFactory.createFromColor('__gshapeutils_tx', 0xffffff, 4, 4);
- }
- // default missing props.
- for (var p:String in DEFAULT_CONFIGURATION) if (!pConf.hasOwnProperty(p)) pConf[p] = DEFAULT_CONFIGURATION[p];
- if (!pConf.hasOwnProperty('texture')) {
- pConf.texture = DEFAULT_TEXTURE;
- } else if (pConf.texture is String) {
- pConf.texture = GTexture.getTextureById(pConf.texture);
- }
- var vert:Vector.<Number>;
- if (pDisplay is DisplayObject) {
- vert = getVerticesDisplayObject(pDisplay as DisplayObject, pConf.offsetPosition, pConf.granularity, pConf.quality, pConf.simplification);
- } else if (pDisplay is BitmapData) {
- vert = getVerticesFromBitmap(pDisplay as BitmapData, pConf.offsetPosition, pConf.alphaThreshold, pConf.granularity, pConf.quality, pConf.simplification);
- } else if (pDisplay is Graphics) {
- }
- var uvs:Vector.<Number> = new Vector.<Number>(vert.length, true);
- var shape:GSimpleShape = GNodeFactory.createNodeWithComponent(GSimpleShape) as GSimpleShape;
- shape.setTexture(pConf.texture);
- shape.init(vert, uvs);
- if (pParentNode) pParentNode.addChild(shape.node);
- return shape;
- }
- public static function getVerticesDisplayObject(mc:DisplayObject, offsetPosition:Point = null, granularity:Number = 8, quality:int = 2, simplification:Number = 1.5):Vector.<Number> {
- var mcIso:DisplayObjectIso = new DisplayObjectIso(mc);
- if (!mc.stage || !mc.parent) {
- trace('Flash requires an object to be on stage for hitTestPoint used by the iso-function to work correctly. Also the displayObject needs a parent! SIGH.');
- return null;
- }
- if (!offsetPosition) offsetPosition = DEFAULT_POINT;
- var granularityVec:Vec2 = Vec2.weak(granularity, granularity);
- var polys:GeomPolyList = MarchingSquares.run(mcIso, mcIso.bounds, granularityVec, quality);
- var vertices:Vector.<Number> = new Vector.<Number>();
- polys.foreach(function (p:GeomPoly):void {
- var qolys:GeomPolyList = p.simplify(simplification).triangularDecomposition(true);
- qolys.foreach(function (p:GeomPoly):void {
- var it:GeomVertexIterator = p.iterator();
- while (it.hasNext()) {
- var v:Vec2 = it.next();
- vertices.push(v.x - offsetPosition.x, v.y - offsetPosition.y);
- }
- });
- });
- polys.clear();
- return vertices;
- }
- public static function getVerticesFromBitmap(bd:BitmapData, offsetPosition:Point = null, alphaThresold:Number = 0x80, granularity:Number = 8, quality:int = 2, simplification:Number = 1.5):Vector.<Number> {
- var bitmapIso:BitmapDataIso = new BitmapDataIso(bd, alphaThresold);
- // offset coordinates to pivot!
- if (!offsetPosition) offsetPosition = DEFAULT_POINT;
- var granularityVec:Vec2 = Vec2.weak(granularity, granularity);
- var polys:GeomPolyList = MarchingSquares.run(bitmapIso, bitmapIso.bounds, granularityVec, quality);
- var vertices:Vector.<Number> = new Vector.<Number>();
- polys.foreach(function (p:GeomPoly):void {
- var qolys:GeomPolyList = p.simplify(simplification).triangularDecomposition(true);
- qolys.foreach(function (p:GeomPoly):void {
- var it:GeomVertexIterator = p.iterator();
- while (it.hasNext()) {
- var v:Vec2 = it.next();
- vertices.push(v.x - offsetPosition.x, v.y - offsetPosition.y);
- }
- });
- });
- polys.clear();
- return vertices;
- }
- }
- }
- import flash.display.Bitmap;
- import flash.display.BitmapData;
- import flash.display.DisplayObject;
- import nape.geom.AABB;
- import nape.geom.IsoFunction;
- class DisplayObjectIso implements IsoFunction {
- public var displayObject:DisplayObject;
- public var bounds:AABB;
- public function DisplayObjectIso(displayObject:DisplayObject):void {
- this.displayObject = displayObject;
- // important to have a parent!
- this.bounds = AABB.fromRect(displayObject.getBounds(displayObject.parent));
- }
- public function iso(x:Number, y:Number):Number {
- // Best we can really do with a generic DisplayObject
- // is to return a binary value {-1, 1} depending on
- // if the sample point is in or out side.
- var hit:Boolean = displayObject.hitTestPoint(x, y, true);
- return hit ? -1 : 1;
- }
- }
- class BitmapDataIso implements IsoFunction {
- public var bitmap:BitmapData;
- public var alphaThreshold:Number;
- public var bounds:AABB;
- public function BitmapDataIso(bitmap:BitmapData, alphaThreshold:Number = 0x80):void {
- this.bitmap = bitmap;
- this.alphaThreshold = alphaThreshold;
- bounds = new AABB(0, 0, bitmap.width, bitmap.height);
- }
- public function iso(x:Number, y:Number):Number {
- // Take 4 nearest pixels to interpolate linearly.
- // This gives us a smooth iso-function for which
- // we can use a lower quality in MarchingSquares for
- // the root finding.
- var ix:int = int(x);
- var iy:int = int(y);
- //clamp in-case of numerical inaccuracies
- if (ix < 0) ix = 0;
- if (iy < 0) iy = 0;
- if (ix >= bitmap.width) ix = bitmap.width - 1;
- if (iy >= bitmap.height) iy = bitmap.height - 1;
- // iso-function values at each pixel centre.
- var a11:Number = alphaThreshold - (bitmap.getPixel32(ix, iy) >>> 24);
- var a12:Number = alphaThreshold - (bitmap.getPixel32(ix + 1, iy) >>> 24);
- var a21:Number = alphaThreshold - (bitmap.getPixel32(ix, iy + 1) >>> 24);
- var a22:Number = alphaThreshold - (bitmap.getPixel32(ix + 1, iy + 1) >>> 24);
- // Bilinear interpolation for sample point (x,y)
- var fx:Number = x - ix;
- var fy:Number = y - iy;
- return a11 * (1 - fx) * (1 - fy) + a12 * fx * (1 - fy) + a21 * (1 - fx) * fy + a22 * fx * fy;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment