forked from: flash on 2009-5-22
/**
* Copyright Fricks ( http://wonderfl.net/user/Fricks )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/jcpf
*/
// forked from little_dandy's flash on 2009-5-22
package {
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.Graphics;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.geom.Point;
import flash.geom.Matrix;
import flash.events.Event;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.system.Security;
public class FlashTest extends Sprite {
private var sw:Number = stage.stageWidth;
private var sh:Number = stage.stageHeight;
private var cx:Number = sw/2;
private var cy:Number = sh/2;
private var camz:Number = 300;
private var shape:Shape;
private var g:Graphics;
private var ary:Array;
private var cn:Number = 8;
private var degree:Number = 1.0;
private var radian:Number = 0.;
private var degree2:Number = 1.0;
private var radian2:Number = 0.;
private var iLoader:Loader;
private var iRequest:URLRequest;
public function FlashTest() {
// write as3 code here..
Security.loadPolicyFile("http://dandy-z.mods.jp/crossdomain.xml");
init();
}
private function init(){
iLoader = new Loader();
iRequest = new URLRequest("http://dandy-z.mods.jp/image.jpg");
iLoader.load(iRequest);
addChild(iLoader);
//iLoader.x = -cx;
//iLoader.y = -cy;
iLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,iLoadComplete);
}
private function iLoadComplete(e:Event) {
ary = new Array();
ary[0] = {px:100,py:100,pz:100};
ary[1] = {px:-100,py:100,pz:100};
ary[2] = {px:-100,py:-100,pz:100};
ary[3] = {px:100,py:-100,pz:100};
ary[4] = {px:100,py:100,pz:-100};
ary[5] = {px:-100,py:100,pz:-100};
ary[6] = {px:-100,py:-100,pz:-100};
ary[7] = {px:100,py:-100,pz:-100};
shape = new Shape();
addChild(shape);
g = shape.graphics;
addEventListener(Event.ENTER_FRAME,upDate);
}
private function upDate(e:Event){
g.clear();
degree = (mouseX-cx)/50;
degree2 = (mouseY-cy)/50;
radian = (degree/180)*Math.PI;
radian2 = (degree2/180)*Math.PI;
for(var i=0;i<cn;i++){
/*X軸に沿ったに回転
y' = ycosβ - zsinβ
z' = ysinβ + zcosβ
Y軸に沿った回転
z' = zcosβ - xsinβ
x' = zsinβ + xcosβ
Z軸に沿った回転 ---Z軸の回転は2次元(XY平面)の回転に対応
x' = xcosβ - ysinβ
y' = xsinβ + ycosβ*/
//x軸
var fpy = ((ary[i].py)*Math.cos(radian2)-(ary[i].pz)*Math.sin(radian2));
var fpz = ((ary[i].py)*Math.sin(radian2)+(ary[i].pz)*Math.cos(radian2));
//y軸
var pz = ((fpz)*Math.cos(radian2)-(ary[i].px)*Math.sin(radian2));
var fpx = ((fpz)*Math.sin(radian2)+(ary[i].px)*Math.cos(radian2));
//z軸
var px = ((fpx)*Math.cos(radian)-(fpy)*Math.sin(radian));
var py = ((fpx)*Math.sin(radian)+(fpy)*Math.cos(radian));
ary[i].px = px;
ary[i].py = py;
ary[i].pz = pz;
}
var ba:ByteArray = new ByteArray();
ba.writeObject(ary);
ba.position = 0;
var cAry:Array = ba.readObject();
for(var j=0;j<cAry.length;j++){
var scale = camz/(cAry[j].pz+camz);
var sx = (cAry[j].px)*scale;
var sy = (cAry[j].py)*scale;
cAry[j].px = sx;
cAry[j].py = sy;
//dPoint(cAry[j].px+cx-2,cAry[j].py+cy-2);
}
/*g.lineStyle(1,0xFF0000);
g.moveTo(cAry[0].px+cx,cAry[0].py+cy);
g.lineTo(cAry[1].px+cx,cAry[1].py+cy);
g.lineTo(cAry[2].px+cx,cAry[2].py+cy);
g.lineTo(cAry[3].px+cx,cAry[3].py+cy);
g.lineTo(cAry[0].px+cx,cAry[0].py+cy);
g.moveTo(cAry[4].px+cx,cAry[4].py+cy);
g.lineTo(cAry[5].px+cx,cAry[5].py+cy);
g.lineTo(cAry[6].px+cx,cAry[6].py+cy);
g.lineTo(cAry[7].px+cx,cAry[7].py+cy);
g.lineTo(cAry[4].px+cx,cAry[4].py+cy);
g.moveTo(cAry[0].px+cx,cAry[0].py+cy);
g.lineTo(cAry[4].px+cx,cAry[4].py+cy);
g.moveTo(cAry[1].px+cx,cAry[1].py+cy);
g.lineTo(cAry[5].px+cx,cAry[5].py+cy);
g.moveTo(cAry[2].px+cx,cAry[2].py+cy);
g.lineTo(cAry[6].px+cx,cAry[6].py+cy);
g.moveTo(cAry[3].px+cx,cAry[3].py+cy);
g.lineTo(cAry[7].px+cx,cAry[7].py+cy);*/
var bm:Bitmap = iLoader.getChildAt(0) as Bitmap;
var bmd:BitmapData = bm.bitmapData;
var mtx0:Matrix = new Matrix(0,iLoader.height,iLoader.width,0,0,0);
var mtx1:Matrix = new Matrix((cAry[1].px+cx)-(cAry[2].px+cx),(cAry[1].py+cy)-(cAry[2].py+cy),(cAry[3].px+cx)-(cAry[2].px+cx),(cAry[3].py+cy)-(cAry[2].py+cy),cAry[2].px+cx,cAry[2].py+cy);
mtx0.invert();
mtx0.concat(mtx1);
//g.lineStyle(1,0xFF0000);
g.beginBitmapFill(bmd,mtx0,false,false);
g.moveTo(cAry[2].px+cx,cAry[2].py+cy);
g.lineTo(cAry[1].px+cx,cAry[1].py+cy);
g.lineTo(cAry[3].px+cx,cAry[3].py+cy);
g.lineTo(cAry[2].px+cx,cAry[2].py+cy);
g.endFill();
var mtx2:Matrix = new Matrix(0-iLoader.width,iLoader.height-iLoader.height,iLoader.width-iLoader.width,0-iLoader.height,iLoader.width,iLoader.height);
var mtx3:Matrix = new Matrix((cAry[1].px+cx)-(cAry[0].px+cx),(cAry[1].py+cy)-(cAry[0].py+cy),(cAry[3].px+cx)-(cAry[0].px+cx),(cAry[3].py+cy)-(cAry[0].py+cy),cAry[0].px+cx,cAry[0].py+cy);
mtx2.invert();
mtx2.concat(mtx3);
g.beginBitmapFill(bmd,mtx2,false,false);
g.moveTo(cAry[0].px+cx,cAry[0].py+cy);
g.lineTo(cAry[1].px+cx,cAry[1].py+cy);
g.lineTo(cAry[3].px+cx,cAry[3].py+cy);
g.lineTo(cAry[0].px+cx,cAry[0].py+cy);
g.endFill();
}
private function dPoint(dx:Number,dy:Number){
g.beginFill(0x0000FF);
g.drawCircle(dx,dy,2);
}
}
}