rodrigolopezpeker

Genome2D > Mesh Transform Demo

Dec 14th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created by rodrigo on 9/23/13.
  3.  */
  4. package test.mesh {
  5.     import com.genome2d.components.GComponent;
  6.     import com.genome2d.components.renderables.GSimpleShape;
  7.     import com.genome2d.core.GNode;
  8.     import com.genome2d.core.GNodeFactory;
  9.     import com.genome2d.g2d;
  10.     import com.genome2d.textures.GTexture;
  11.     import com.greensock.plugins.EndVectorPlugin;
  12.     import com.greensock.plugins.TweenPlugin;
  13.  
  14.     import flash.display.Shape;
  15.  
  16.     use namespace g2d ;
  17.  
  18.     public class GMesh2 extends GComponent {
  19.         private var vert: Vector.<Number>;
  20.         private var uvs: Vector.<Number>;
  21.  
  22.         private var indices: Vector.<uint>;
  23.         private var uvs2: Vector.<Number>;
  24.         public var verts2: Vector.<Number>;
  25.         public var overts: Vector.<Number>;
  26.  
  27.         private var shape: GSimpleShape;
  28.  
  29.         private var _textureId: String ;
  30.         private var texture: GTexture;
  31.         private var stageShape: Shape;
  32.  
  33.         // texture size.
  34.         private var cols: int;
  35.         private var rows: int;
  36.         private var tileW: Number;
  37.         private var tileH: Number;
  38.         private var _currentVertexId: int;
  39.         public var wireframeColor: uint ;
  40.         private var _debug: Boolean = true ;
  41.  
  42.         public function GMesh2(pNode:GNode) {
  43.             super(pNode);
  44.             init()
  45.         }
  46.  
  47.         private function init(): void {
  48.             TweenPlugin.activate([EndVectorPlugin]);
  49.             stageShape = new Shape();
  50.             node.core.stage.addChild(stageShape);
  51.  
  52.             vert = new Vector.<Number>() ;
  53.             uvs = new Vector.<Number>() ;
  54.             verts2 = new Vector.<Number>() ;
  55.             uvs2 = new Vector.<Number>() ;
  56.             indices = new Vector.<uint>() ;
  57.             shape = GNodeFactory.createNodeWithComponent(GSimpleShape) as GSimpleShape ;
  58.             node.addChild(shape.node);
  59.         }
  60.  
  61.         public function get textureId(): String {return _textureId;}
  62.         public function set textureId(value: String): void {
  63.             _textureId = value;
  64.             texture = GTexture.getTextureById( _textureId ) ;
  65.             shape.setTexture( texture ) ;
  66.         }
  67.  
  68.         public function drawMesh(pCols: int, pRows: int): void {
  69.             cols = pCols+1 ;
  70.             rows = pRows+1 ;
  71.             tileW = texture.width / pCols ;
  72.             tileH = texture.height / pRows ;
  73.             var total:int = cols * rows ;
  74.             for (var i: int = 0; i <= total; i++) {
  75.                 var idx:int = i % cols ;
  76.                 var idy:int = i / cols ;
  77.                 verts2.push(tileW * idx, tileH * idy );
  78.                 uvs2.push( idx/pCols, idy/pRows);
  79. //              uvs2.push( idy/pRows, idx/pCols);
  80.             }
  81.             --rows, --cols ;
  82.             var ic:int = 0 ;
  83.             total = rows * cols ;
  84.             for ( i  = 0; i < total; i++) {
  85.                 if( i % cols == 0 && i != 0 ) ic++;
  86.                 //1st triangle
  87.                 indices.push(ic, ic+1, ic+1+cols);
  88.                 //2nd triangle
  89.                 indices.push(ic+1, ic+1+cols+1, ic+1+cols);
  90.                 ic++;
  91.             }
  92.             overts = verts2.concat();
  93.  
  94.             // stretch te middle.
  95. //          idx = getVertexIndex( 3, 1 );
  96. //          verts2[idx] -= 20 ;
  97. //          verts2[idx+1] -= 50 ;
  98.             process() ;
  99.         }
  100.  
  101.         private function getVertexIndex(pCol: int, pRow: int): int {
  102.             pCol *= 2, pRow *= 2 ;
  103.             var idx:int = (cols+1) * pRow + pCol ;
  104.             if( idx >= verts2.length-1 || idx < 0 ) return -1 ;
  105.             return idx ;
  106. //          return 0 ;
  107.         }
  108.  
  109.         public function process( pVerts:Vector.<Number> = null, pUVs:Vector.<Number> = null ): void {
  110.             vert.length = 0 ;
  111.             uvs.length = 0 ;
  112.             var len:int = indices.length ;
  113.             var v2: Vector.<Number> = pVerts || verts2 ;
  114.             var uv2: Vector.<Number> = pUVs || uvs2 ;
  115.             for (var i: int = 0; i < len; i++) {
  116.                 var idX:int = indices[i] * 2 ;
  117.                 var idY:int = idX + 1 ;
  118.                 vert.push( v2[idX], v2[idY] );
  119.                 uvs.push( uv2[idX], uv2[idY]);
  120.             }
  121.             shape.init(vert, uvs ) ;
  122.             if( _debug ) renderGraphics() ;
  123.         }
  124.  
  125.         public function setVerts(pVerts: Vector.<Number>): void {
  126.             verts2 = pVerts ;
  127.             trace('set verts');
  128.             process();
  129.         }
  130.  
  131.         override public function update(p_deltaTime: Number, p_parentTransformUpdate: Boolean, p_parentColorUpdate: Boolean): void {
  132.             if( p_parentTransformUpdate ){
  133.                 stageShape.x = cNode.cTransform.nWorldX //+ cNode.cCore.cDefaultCamera.rViewRectangle.width/2;
  134.                 stageShape.y = cNode.cTransform.nWorldY //+ cNode.cCore.cDefaultCamera.rViewRectangle.height/2;
  135.             }
  136.         }
  137.  
  138.         private function renderGraphics(): void {
  139.             stageShape.graphics.clear();
  140.             stageShape.graphics.lineStyle(0,wireframeColor);
  141. //          stageShape.graphics.beginBitmapFill(texture.bitmapData);
  142.             stageShape.graphics.drawTriangles(vert, null, uvs);
  143.         }
  144.  
  145.         public function getNearestMouseIndex(pX: int, pY: int, pMaxRadio: Number = 30 ): int {
  146.             var total:int = verts2.length ;
  147.             var minDist:Number = Number.POSITIVE_INFINITY ;
  148.             var selIndex: int = -1 ;
  149.             pMaxRadio *= 10 ;
  150.             for (var i: int = 0; i < total; i+=2) {
  151.                 var dx: int = pX - verts2[i] ;
  152.                 var dy: int = pY - verts2[int(i+1)] ;
  153.                 var d:Number = dx * dx + dy * dy ;
  154.                 if( d < minDist && d < pMaxRadio ){
  155.                     selIndex = i ;
  156.                     minDist = d ;
  157.                 }
  158.             }
  159.             return selIndex ;
  160.         }
  161.  
  162.         public function copyVertsMesh(): Vector.<Number> {
  163.             return overts.concat() ;
  164.         }
  165.  
  166.         public function get debug(): Boolean {return _debug;}
  167.         public function set debug(value: Boolean): void {
  168.             _debug = value;
  169.             if(!_debug) {
  170.                 stageShape.graphics.clear();
  171.             } else {
  172.                 renderGraphics() ;
  173.             }
  174.         }
  175.  
  176.     }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment