lath3D
/**
* Copyright summerTree ( http://wonderfl.net/user/summerTree )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/80B5
*/
package
{
import flash.display.*;
import flash.events.*;
import flash.geom.*;
public class Main extends Sprite
{
private var canvas:BitmapData;
private var points:Vector.<Number > ;
private var vin:Vector.<Number > ;
private var vout:Vector.<Number > ;
private var uvts:Vector.<Number > ;
private var dx:Number = 0;
private var dy:Number = 0;
private var matrix:Matrix3D;
//舞台中心点
private var centerX:Number = stage.stageWidth / 2;
private var centerY:Number = stage.stageHeight / 2;
private var pen:Shape=new Shape();
public function Main()
{
init();
}
private function init():void
{
points = new Vector.<Number >();
uvts = new Vector.<Number>();
vin = new Vector.<Number >();
matrix=new Matrix3D();
canvas = new BitmapData(550,400,false,0xFEB6B6);//创建画布
addChild(new Bitmap(canvas));
addChild(pen);
creatPoint();
lath3D();
render();
addEventListener(Event.ENTER_FRAME,Run);
}
private function Run(event:Event):void
{
render();
}
//创建空间点集(x,y,z);
private function creatPoint():void
{
var angle:Number = Math.PI / 180;
var r:Number;
var radius:Number = 100;
for (var i:int=0; i<360; i++)
{
r=radius * (1 +Math.sin(angle*i));//心形公式
var point:Point = Point.polar(r,angle * i);//极坐标转换笛卡尔坐标
vin.push(point.x);
vin.push(point.y-100);
vin.push(150);
}
}
//进行放样
private function lath3D():void
{
var tVerts:Vector.<Number> = new Vector.<Number>();
var nVerts:Vector.<Number> = new Vector.<Number>();
for (var i:int=0; i<360; i+=3)
{
matrix.appendRotation(8,Vector3D.Y_AXIS);
matrix.transformVectors(vin,tVerts);
nVerts = nVerts.concat(tVerts);
}
vin = nVerts.concat();
}
//渲染
private function render():void
{
rotationXY();
canvas.lock();
canvas.fillRect(canvas.rect,0xFEB6B6);
var length:int = points.length;
pen.graphics.moveTo(points[0],points[1]);
for (var i:int=0; i<length; i+=2)
{
canvas.setPixel(points[i],points[i+1],0xffffff);
}
canvas.unlock();
}
//旋转
private function rotationXY():void
{
dx += (mouseX - dx)/6;
dy += (mouseY - dy)/6;
matrix.identity();
matrix.appendRotation(dx,Vector3D.Y_AXIS);
matrix.appendRotation(dy,Vector3D.X_AXIS);
matrix.appendTranslation(centerX, centerY,0);
Utils3D.projectVectors(matrix, vin, points, uvts);
}
}
}