forked from: flash on 2010-3-16
绘制一棵树
夏天的树人
/**
* Copyright JohnBrookes ( http://wonderfl.net/user/JohnBrookes )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/zfr0
*/
// forked from summerTree's flash on 2010-3-16
package
{
//绘制一棵树
//夏天的树人
import flash.display.Sprite;
import flash.display.Graphics;
import flash.events.*;
import org.papervision3d.materials.special.VectorShapeMaterial;
import org.papervision3d.objects.special.Graphics3D;
import org.papervision3d.objects.special.VectorShape3D;
import org.papervision3d.view.BasicView;
[SWF(backgroundColor="0xffffff", frameRate="30")];
public class Main extends BasicView
{
//private var shape:Sprite=new Sprite();
private var shape : VectorShape3D;
public function Main()
{
var material : VectorShapeMaterial = new VectorShapeMaterial();
shape = new VectorShape3D(material);
scene.addChild(shape);
//addChild(shape);
camera.z = -500;
CreatTree(shape.graphics,0, 60, 90, 180,6);
CreatTree(shape.graphics,0, 0, 45, 130,6);
CreatTree(shape.graphics,0, 0, 135, 130,6);
addEventListener(Event.ENTER_FRAME, tick);
}
private function tick(e:Event):void
{
camera.orbit(viewport.mouseY - viewport.viewportHeight * 0.5 +90, viewport.mouseX - viewport.viewportWidth * 0.5 - 90);
singleRender();
}
//绘图对象,基本坐标px,py,角度,偏移长度,数量
private function CreatTree(g:Graphics3D,px:Number,py:Number,angle:Number,len:Number,n:int):void
{
if (n>0)
{
var x1:Number=px+0.1*len*Math.cos(angle*Math.PI/180);
var y1:Number=py-0.1*len*Math.sin(angle*Math.PI/180);
var x2:Number=px+len*Math.cos(angle*Math.PI/180);
var y2:Number=py-len*Math.sin(angle*Math.PI/180);
drawLine(g,n-1,px,py,x2,y2);
var a_l:Number = angle+30;
var a_r:Number = angle-30;
len = len*2/3;
CreatTree(g,x2, y2, angle-Math.random()*10,len, n-1);
CreatTree(g,x1, y1, a_l,len*2/3, n-1);
CreatTree(g,x1, y1, a_r, len*2/3,n-1);
CreatTree(g,x2, y2, a_l,len*2/3, n-1);
CreatTree(g,x2, y2, a_r,len*2/3, n-1);
}
}
//绘制线
private function drawLine(g:Graphics3D,n:int,x1:Number,y1:Number,x2:Number,y2:Number):void
{
g.lineStyle(n,0x000000);
g.moveTo(x1,-y1);
g.lineTo(x2,-y2);
}
}
}