forked from: forked from: whatever 8
/**
* Copyright tepe ( http://wonderfl.net/user/tepe )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/8gPe
*/
// forked from gazlag's forked from: whatever 8
// forked from wh0's whatever 8
package{
import flash.display.*;
import flash.events.*;
import flash.geom.*;
public class Draw3D extends Sprite {
private static const K:Number = 4 / 3 * (Math.SQRT2 - 1);//角の形状
private static const R1:Number = 32;//径
private static const R2:Number = 30;
private static const L:Number = 60;//長さ
public var color01:uint = 0x00ff00;
private var c:C;
public function Draw3D(color:uint=0xff0000) {
//color01 = color;
color01 = Math.random()*0xffffff;
c = new C();
c.m = new Matrix3D();
c.o = new Vector3D(320, 240, 0, 0);//初期位置
click(null);//回転セット
this.addEventListener(MouseEvent.CLICK, click);
this.addEventListener(Event.ENTER_FRAME, frame);
//stage.frameRate = 60;
}
private function click(e:MouseEvent):void {
c.v = uv();//回転量設定
c.m.identity();
c.m.appendRotation(2, uv());
}
//フレーム
private function frame(e:Event):void {
c.v = c.m.transformVector(c.v);//角度変更
graphics.clear();//画像クリア
r(graphics, c.o, c.v);//レンダリング
}
private static function uv():Vector3D {
var v:Vector3D = new Vector3D(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5);
v.normalize();
return v;
}
//レンダリングフェーズ
private function r(g:Graphics, o:Vector3D, v:Vector3D):void {
var i:Vector3D = v.clone();
var j:Vector3D = i.crossProduct(Vector3D.Z_AXIS);
if (j.normalize() < 0.001) {
g.drawCircle(o.x, o.y, R1);
return;
}
var k:Vector3D = i.crossProduct(j);
if (k.x * i.x + k.y * i.y > 0) {
k.scaleBy(-1);
rh(g, o, i, j, k, false,0xffffff);
rh(g, o, i, j, k, true,color01);
} else {
rh(g, o, i, j, k, true,color01);
rh(g, o, i, j, k, false,0xffffff);
}
}
private function rh(g:Graphics, o:Vector3D, i:Vector3D, j:Vector3D, k:Vector3D, h:Boolean, color:uint=0xffffff):void {
i = i.clone();
i.scaleBy(h ? L : -L);
j = j.clone();
j.scaleBy(h ? R1 : -R2);
k = k.clone();
k.scaleBy(h ? R1 : -R2);
g.beginFill(color,h ? 1:0.8);
g.lineStyle(2, 0x000000);
g.moveTo(o.x + j.x, o.y + j.y);//描画開始位置
g.lineTo(o.x + j.x + i.x, o.y + j.y + i.y);
//cubicCurveTo:三次ベジェ曲線
g.cubicCurveTo(
o.x + j.x + i.x - K * j.y,//x1
o.y + j.y + i.y + K * j.x,//y1
o.x + i.x - j.y + K * j.x,//x2
o.y + i.y + j.x + K * j.y,//y2
o.x + i.x - j.y,//x3
o.y + i.y + j.x//y3
);
g.cubicCurveTo(
o.x + i.x - j.y - K * j.x,
o.y + i.y + j.x - K * j.y,
o.x - j.x + i.x - K * j.y,
o.y - j.y + i.y + K * j.x,
o.x - j.x + i.x,
o.y - j.y + i.y
);
g.lineTo(o.x - j.x, o.y - j.y);
//g.moveTo(o.x-j.x,o.y-j.y);
g.cubicCurveTo(
o.x - j.x + K * k.x,
o.y - j.y + K * k.y,
o.x + k.x - K * j.x,
o.y + k.y - K * j.y,
o.x + k.x,
o.y + k.y
);
g.cubicCurveTo(
o.x + k.x + K * j.x,
o.y + k.y + K * j.y,
o.x + j.x + K * k.x,
o.y + j.y + K * k.y,
o.x + j.x,
o.y + j.y
);
}
}
}
internal class C {
public var o:flash.geom.Vector3D;
public var v:flash.geom.Vector3D;
public var m:flash.geom.Matrix3D;
}