Pv3d: Random tree

[ 2009-08-21 17:19:13 | Author: liuhuan ]
Font Size: Large | Medium | Small
uploads/200908/21_165050_untitled2.jpg


Caution: bigger number may greatly slow down the computer! Slide to re-generate random tree.


package {
  import flash.display.*;
  import flash.events.*;
  import flash.filters.*;
  import fl.controls.Slider;

  import org.papervision3d.materials.*;
  import org.papervision3d.objects.*;
  import org.papervision3d.objects.primitives.*;
  import org.papervision3d.view.BasicView;
  import org.papervision3d.core.proto.*;
  import org.papervision3d.core.math.*;
  import gs.TweenMax;
  import gs.easing.*;

  public class Main extends BasicView {
    private var dpObj3D:DisplayObject3D;
    private var cylinderHeight:Number = 200;
    private var cylinderRadius:Number = 12;
    private var bmpMaterial:BitmapMaterial;
    private var bmpLeafMaterial:BitmapMaterial;
    private var lengthStep:Number = 50;
    public function Main():void {
      stage.quality = StageQuality.LOW;
      init();
    }
    private function init():void {
      dpObj3D = new DisplayObject3D();
      var bmp:Bitmap=new Bitmap(new Skin(150,150));
      bmpMaterial=new BitmapMaterial(bmp.bitmapData,true);
      bmpMaterial.tiled = true;
      var bmpLeaf:Bitmap=new Bitmap(new Leaf(52,52));
      bmpLeafMaterial=new BitmapMaterial(bmpLeaf.bitmapData,true);
      bmpLeafMaterial.tiled = true;
      dpObj3D.y = -300;
      scene.addChild(dpObj3D);
      camera.zoom = 80;
      addBranch(cylinderHeight,cylinderRadius,0,0,0,true,dpObj3D);
      startRendering();
      slider.addEventListener(Event.CHANGE, onChangeSlider);
    }
    private function addBranch(bh:Number,radius:Number=6, sX:Number=0, sY:Number=0, sZ:Number=0, mainBranch:Boolean = false , fatherObj:DisplayObject3D = null):void {
      var subDpObj3D:DisplayObject3D = new DisplayObject3D();
      var cylinder = new Cylinder(bmpMaterial,radius,bh,5,4);
      cylinder.scaleY = 0;
      subDpObj3D.addChild(cylinder);
      if (!mainBranch) {
        subDpObj3D.rotationX = 360+Math.random()*120-60;
        subDpObj3D.rotationZ = 360+Math.random()*120-60;
        subDpObj3D.rotationY = Math.random()*90-45;
      }
      subDpObj3D.x = sX;
      subDpObj3D.y = sY;
      subDpObj3D.z = sZ;
      var nX:Number =0;
      var nY:Number =bh;
      var nZ:Number =0;
      TweenMax.to(cylinder, 1, {scaleY:1,y:bh/2,delay:0, ease:Quad.easeInOut, onComplete:onFinishTween, onCompleteParams:[nX, nY,nZ,bh-lengthStep,radius-3,subDpObj3D]});
      fatherObj.addChild(subDpObj3D);
    }
    private function addLeaf(bh:Number,radius:Number=6, sX:Number=0, sY:Number=0, sZ:Number=0, mainBranch:Boolean = false , fatherObj:DisplayObject3D = null):void {
      var subDpObj3D:DisplayObject3D = new DisplayObject3D();
      var sphere = new Sphere(bmpLeafMaterial,30*Math.random()+25,3,3);
      sphere.scale = 0;
      subDpObj3D.addChild(sphere);
      TweenMax.to(sphere, 1, {scale:1,y:bh+lengthStep+25,delay:0, ease:Quad.easeInOut});
      fatherObj.addChild(subDpObj3D);
    }
    private function onFinishTween(sx:Number,sy:Number,sz:Number,nh:Number,nR:Number,father:*):void {
      if (nh>10) {
        for (var i:int=0; i<slider.value; i++) {
          addBranch(nh,nR,sx,sy,sz,false,father);
        }
      } else {
        addLeaf(nh,nR,sx,sy,sz,false,father);
      }
    }
    protected override function onRenderTick(event:Event = null):void {
      super.onRenderTick(event);
      dpObj3D.rotationY += 1;
    }
    private function onChangeSlider(e:Event) {
      scene.removeChild(dpObj3D);
      dpObj3D = null;
      init();
    }
  }
}
Comments Feed Comments Feed: http://www.liuhuan.com/blog/feed.asp?q=comment&id=1016

There is no comment on this article.

You can't post comment on this article.