package {
import flash.geom.Point;
import flash.filters.ColorMatrixFilter;
import flash.events.Event;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
[SWF(width="465",height="465",frameRate="60",backgroundColor="0xffffff")]
public class FlashTest extends Sprite {
private var g3d:Graphics3D;
private var bmd:BitmapData;
private var bmp:Bitmap;
private var spr:Sprite;
private var r:Number;
private var cmf:ColorMatrixFilter;
public function FlashTest() {
spr=new Sprite();
g3d=new Graphics3D(spr,-200,465/2,465/2);
bmd=new BitmapData(465,465,true,0x00000000);
bmp=new Bitmap(bmd);
addChild(bmp);
r=0;
cmf=new ColorMatrixFilter([1,0,0,0,0, 0,1,0,0,0, 0,0,1,0,0, 0,0,0,0.99,0]);
addEventListener(Event.ENTER_FRAME,enterFrameHandler);
}
private function enterFrameHandler(event:Event):void{
g3d.lineStyle(10,(0x80-Math.cos(r)*0x7f>>0));
g3d.moveTo(0,0,512);
g3d.curveTo(0,-1024,512,Math.cos(r)*512,256,Math.sin(r)*512+512);
bmd.applyFilter(bmd,bmd.rect,new Point(0,0),cmf);
bmd.draw(spr);
g3d.clear();
r+=Math.PI/180;
}
}
}
import flash.display.Sprite;
import flash.display.Graphics;
class Graphics3D{
private var g:Graphics;
private var thickness:Number=0;
private var lineColor:uint=0;
private var lineAlpha:Number=1.0;
private var startX:Number=0;
private var startY:Number=0;
private var startZ:Number=0;
public var vs:Number=-200;
private var ox:Number;
private var oy:Number;
public function Graphics3D(target:Sprite,vs:Number=-200,ox:Number=0,oy:Number=0){
this.g=target.graphics;
this.vs=vs;
this.ox=ox;
this.oy=oy;
}
public function clear():void{
g.clear();
}
public function lineStyle(thickness:Number,color:uint=0,alpha:Number=1.0):void{
this.thickness=thickness;
this.lineColor=color;
this.lineAlpha=alpha;
}
public function moveTo(x:Number,y:Number,z:Number):void{
this.startX=x;
this.startY=y;
this.startZ=z;
}
public function lineTo(x:Number,y:Number,z:Number):void{
//g.lineStyle(undefined);
var p:Number=-vs/(-vs+this.startZ);
g.beginFill(lineColor,lineAlpha);
var r1:Number=this.thickness*p/2;
var startX:Number=this.startX*p;
var startY:Number=this.startY*p;
g.drawCircle(startX+ox,startY+oy,r1);
g.endFill();
g.beginFill(lineColor,lineAlpha);
p=-vs/(-vs+z);
var r2:Number=thickness*p/2;
var endX:Number=x*p;
var endY:Number=y*p;
g.drawCircle(endX+ox,endY+oy,r2);
g.endFill();
var s:Number=endX-startX;
var t:Number=endY-startY;
var d:Number=(r1-r2)/Math.sqrt(s*s+t*t);
if(Math.abs(d)<=1){
var u:Number=Math.asin(d);
var v:Number=Math.atan2(s,t);
var a1:Number=u-v;
var a2:Number=Math.PI-u-v;
g.beginFill(lineColor,lineAlpha);
//g.lineStyle(0,0xff0000);
g.moveTo(r1*Math.cos(a1)+startX+ox,r1*Math.sin(a1)+startY+oy);
g.lineTo(r1*Math.cos(a2)+startX+ox,r1*Math.sin(a2)+startY+oy);
g.lineTo(r2*Math.cos(a2)+endX+ox,r2*Math.sin(a2)+endY+oy);
g.lineTo(r2*Math.cos(a1)+endX+ox,r2*Math.sin(a1)+endY+oy);
g.endFill();
}
moveTo(x,y,z);
}
public function curveTo(controlX:Number,controlY:Number,controlZ:Number,anchorX:Number,anchorY:Number,anchorZ:Number):void{
var startX:Number=this.startX;
var startY:Number=this.startY;
var startZ:Number=this.startZ;
for(var i:Number=0;i<=1;i+=0.01){
var j:Number=1-i;
var tX:Number=j*(j*startX+i*controlX)+i*(j*controlX+i*anchorX);
var tY:Number=j*(j*startY+i*controlY)+i*(j*controlY+i*anchorY);
var tZ:Number=j*(j*startZ+i*controlZ)+i*(j*controlZ+i*anchorZ);
lineTo(tX,tY,tZ);
}
}
}