阿修羅マン(drawTriangle勉強)
/**
* Copyright y_tti ( http://wonderfl.net/user/y_tti )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/AsO7
*/
package
{
import __AS3__.vec.Vector;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
import flash.system.SecurityDomain;
import mx.binding.utils.BindingUtils;
[SWF(width = "465", height = "465", frameRate = "30", backgroundColor = "0x000000")]
public class ytti_drawTriTest extends Sprite
{
private var _xCount:int = 10;
private var _yCount:int = 10;
private var _pts:Array;
private var _canvasShape:Sprite;
//[Embed(source="test.jpg")] private var TestImage:Class;
//private var _imageBmd:BitmapData = Bitmap(new TestImage()).bitmapData;
private var _imageBmd:BitmapData;
private var _vertices:Vector.<Number>;
private var _indices:Vector.<int>;
private var _uvtData:Vector.<Number>;
public function ytti_drawTriTest()
{
//画像ロード
var context:LoaderContext = new LoaderContext;
context.checkPolicyFile = true;
context.applicationDomain = ApplicationDomain.currentDomain;
context.securityDomain = SecurityDomain.currentDomain;
var url:String = "http://assets.wonderfl.net/images/related_images/c/c1/c1f2/c1f20299091a2bcfac2d52a254169bc849720cf4";
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE , _loadCompHandler );
loader.load( new URLRequest(url) , context);
}
private function _loadCompHandler(e:Event):void {
//bitmapdata取得
LoaderInfo(e.target).removeEventListener(Event.COMPLETE , _loadCompHandler );
_imageBmd = Bitmap(LoaderInfo(e.target).content).bitmapData;
_init();
}
private function _init():void {
//drawTrianglesの3種の神器用意
_vertices = new Vector.<Number>();
_indices = new Vector.<int>();
_uvtData = new Vector.<Number>();
//Point初期化
//_verticesと_uvtDataセット
_pts = [];
var w:Number = stage.stageWidth / _xCount;
var h:Number = stage.stageHeight / _yCount;
for(var iy:int = 0;iy <= _yCount;iy++ ){
for(var ix:int = 0;ix <= _xCount ;ix++ ){
var pt:Pt = new Pt( ix*w , iy*h );
_pts.push(pt);
_vertices.push(ix*w , iy*h);
//trace(ix*w + " --- " + iy*h);
_uvtData.push(ix / _xCount , iy / _yCount );
//trace( ix / _xCount + " --- " + iy / _yCount);
}
}
//_indicesセット
var indiceCountX:Number = _xCount + 1;
for(iy = 0;iy < _yCount;iy++ ){
for(ix = 0;ix < _xCount ;ix++ ){
//1 2
//
//3
_indices.push(iy*indiceCountX+ix, iy*indiceCountX+ix+1, (iy+1)*indiceCountX+ix);
// 1
//
//3 2
_indices.push(iy*indiceCountX+ix+1, (iy+1)*indiceCountX+ix+1, (iy+1)*indiceCountX+ix);
}
}
_canvasShape = new Sprite();
addChild(_canvasShape);
addEventListener(Event.ENTER_FRAME , _loop )
}
private function _loop(e:Event):void {
_update();
_draw();
}
private function _update():void {
_vertices = new Vector.<Number>();
for(var i:int = 0;i < _pts.length;i++ ){
var pt:Pt = _pts[i];
var dx:Number = mouseX - pt.initX; //xの差
var dy:Number = mouseY - pt.initY; //yの差
var dist:Number = Math.sqrt( dx*dx + dy*dy); //距離
var rad:Number = Math.atan2(dy,dx); //角度(ラジアン)
var dist2:Number = dist > 100 ? 100 : dist; //無理やり調整
var radius:Number = 100 * dist2 /100; //半径
var targetX:Number = pt.initX + Math.cos(rad) * radius; //x目的地
var targetY:Number = pt.initY + Math.sin(rad) * radius; //y目的地
pt.vx = (targetX - pt.x) /10; //王道イージング
pt.vy = (targetY - pt.y) /10; //王道イージング
pt.x += pt.vx;
pt.y += pt.vy;
_vertices.push(pt.x,pt.y);
}
}
private function _draw():void {
_canvasShape.graphics.clear();
_canvasShape.graphics.beginBitmapFill(_imageBmd);
_canvasShape.graphics.drawTriangles(_vertices , _indices , _uvtData );
_canvasShape.graphics.endFill();
}
}
}
class Pt {
public var initX:Number;
public var initY:Number;
public var x:Number;
public var y:Number;
public var vx:Number;
public var vy:Number;
public function Pt($x:Number,$y:Number){
this.x = this.initX = $x;
this.y = this.initY = $y;
this.vx = 0;
this.vy = 0;
}
}