Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Created by rodrigo on 9/23/13.
- */
- package test.mesh {
- import com.genome2d.components.GComponent;
- import com.genome2d.components.renderables.GSimpleShape;
- import com.genome2d.core.GNode;
- import com.genome2d.core.GNodeFactory;
- import com.genome2d.g2d;
- import com.genome2d.textures.GTexture;
- import com.greensock.plugins.EndVectorPlugin;
- import com.greensock.plugins.TweenPlugin;
- import flash.display.Shape;
- use namespace g2d ;
- public class GMesh2 extends GComponent {
- private var vert: Vector.<Number>;
- private var uvs: Vector.<Number>;
- private var indices: Vector.<uint>;
- private var uvs2: Vector.<Number>;
- public var verts2: Vector.<Number>;
- public var overts: Vector.<Number>;
- private var shape: GSimpleShape;
- private var _textureId: String ;
- private var texture: GTexture;
- private var stageShape: Shape;
- // texture size.
- private var cols: int;
- private var rows: int;
- private var tileW: Number;
- private var tileH: Number;
- private var _currentVertexId: int;
- public var wireframeColor: uint ;
- private var _debug: Boolean = true ;
- public function GMesh2(pNode:GNode) {
- super(pNode);
- init()
- }
- private function init(): void {
- TweenPlugin.activate([EndVectorPlugin]);
- stageShape = new Shape();
- node.core.stage.addChild(stageShape);
- vert = new Vector.<Number>() ;
- uvs = new Vector.<Number>() ;
- verts2 = new Vector.<Number>() ;
- uvs2 = new Vector.<Number>() ;
- indices = new Vector.<uint>() ;
- shape = GNodeFactory.createNodeWithComponent(GSimpleShape) as GSimpleShape ;
- node.addChild(shape.node);
- }
- public function get textureId(): String {return _textureId;}
- public function set textureId(value: String): void {
- _textureId = value;
- texture = GTexture.getTextureById( _textureId ) ;
- shape.setTexture( texture ) ;
- }
- public function drawMesh(pCols: int, pRows: int): void {
- cols = pCols+1 ;
- rows = pRows+1 ;
- tileW = texture.width / pCols ;
- tileH = texture.height / pRows ;
- var total:int = cols * rows ;
- for (var i: int = 0; i <= total; i++) {
- var idx:int = i % cols ;
- var idy:int = i / cols ;
- verts2.push(tileW * idx, tileH * idy );
- uvs2.push( idx/pCols, idy/pRows);
- // uvs2.push( idy/pRows, idx/pCols);
- }
- --rows, --cols ;
- var ic:int = 0 ;
- total = rows * cols ;
- for ( i = 0; i < total; i++) {
- if( i % cols == 0 && i != 0 ) ic++;
- //1st triangle
- indices.push(ic, ic+1, ic+1+cols);
- //2nd triangle
- indices.push(ic+1, ic+1+cols+1, ic+1+cols);
- ic++;
- }
- overts = verts2.concat();
- // stretch te middle.
- // idx = getVertexIndex( 3, 1 );
- // verts2[idx] -= 20 ;
- // verts2[idx+1] -= 50 ;
- process() ;
- }
- private function getVertexIndex(pCol: int, pRow: int): int {
- pCol *= 2, pRow *= 2 ;
- var idx:int = (cols+1) * pRow + pCol ;
- if( idx >= verts2.length-1 || idx < 0 ) return -1 ;
- return idx ;
- // return 0 ;
- }
- public function process( pVerts:Vector.<Number> = null, pUVs:Vector.<Number> = null ): void {
- vert.length = 0 ;
- uvs.length = 0 ;
- var len:int = indices.length ;
- var v2: Vector.<Number> = pVerts || verts2 ;
- var uv2: Vector.<Number> = pUVs || uvs2 ;
- for (var i: int = 0; i < len; i++) {
- var idX:int = indices[i] * 2 ;
- var idY:int = idX + 1 ;
- vert.push( v2[idX], v2[idY] );
- uvs.push( uv2[idX], uv2[idY]);
- }
- shape.init(vert, uvs ) ;
- if( _debug ) renderGraphics() ;
- }
- public function setVerts(pVerts: Vector.<Number>): void {
- verts2 = pVerts ;
- trace('set verts');
- process();
- }
- override public function update(p_deltaTime: Number, p_parentTransformUpdate: Boolean, p_parentColorUpdate: Boolean): void {
- if( p_parentTransformUpdate ){
- stageShape.x = cNode.cTransform.nWorldX //+ cNode.cCore.cDefaultCamera.rViewRectangle.width/2;
- stageShape.y = cNode.cTransform.nWorldY //+ cNode.cCore.cDefaultCamera.rViewRectangle.height/2;
- }
- }
- private function renderGraphics(): void {
- stageShape.graphics.clear();
- stageShape.graphics.lineStyle(0,wireframeColor);
- // stageShape.graphics.beginBitmapFill(texture.bitmapData);
- stageShape.graphics.drawTriangles(vert, null, uvs);
- }
- public function getNearestMouseIndex(pX: int, pY: int, pMaxRadio: Number = 30 ): int {
- var total:int = verts2.length ;
- var minDist:Number = Number.POSITIVE_INFINITY ;
- var selIndex: int = -1 ;
- pMaxRadio *= 10 ;
- for (var i: int = 0; i < total; i+=2) {
- var dx: int = pX - verts2[i] ;
- var dy: int = pY - verts2[int(i+1)] ;
- var d:Number = dx * dx + dy * dy ;
- if( d < minDist && d < pMaxRadio ){
- selIndex = i ;
- minDist = d ;
- }
- }
- return selIndex ;
- }
- public function copyVertsMesh(): Vector.<Number> {
- return overts.concat() ;
- }
- public function get debug(): Boolean {return _debug;}
- public function set debug(value: Boolean): void {
- _debug = value;
- if(!_debug) {
- stageShape.graphics.clear();
- } else {
- renderGraphics() ;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment