tree
/**
* Copyright yonatan ( http://wonderfl.net/user/yonatan )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/zhCK
*/
// forked from yonatan's Depth-aware Lines3D
package {
import org.papervision3d.view.BasicView;
import org.papervision3d.core.geom.*;
import org.papervision3d.core.geom.renderables.*;
import org.papervision3d.materials.special.*;
import frocessing.color.ColorHSV;
import flash.events.*;
import flash.utils.*;
import net.hires.debug.Stats;
public class FlashTest extends BasicView {
private var lines:BetterLines3D;
private var lineMat:LineMaterial;
private var particles:Particles;
public function FlashTest() {
stage.quality = "medium";
// addChild(new Stats);
lineMat = new LineMaterial(0);
lines = new BetterLines3D(lineMat);
particles = new Particles;
scene.addChild(lines);
scene.addChild(particles);
var t:Turtle = new Turtle(lines);
t.pitch(-90);
t.y = -500;
tree(t, 7, 300);
camera.zoom = 100;
startRendering();
}
override protected function onRenderTick(e:Event = null):void {
camera.x = 400 * Math.sin(getTimer()/900);
camera.z = 2550 * Math.cos(getTimer()/2300);
camera.y = 900 * Math.cos(getTimer()/2000);
super.onRenderTick(e);
}
private var mat:ParticleMaterial = new ParticleMaterial((new ColorHSV(60, 0.7, 0.5)).value, 0.7, ParticleMaterial.SHAPE_CIRCLE);
private function tree(t:Turtle, levels:int, size:Number):void {
if(!levels) {
particles.addParticle(new Particle(mat, size*5, t.x, t.y, t.z));
} else {
t.width = size/5;
t.fd(size);
t.roll(120);
t.pitch(-30);
tree(t.replicate(), levels-1, size*0.75);
t.pitch(70);
tree(t, levels-1, size*0.7);
}
}
}
}
import org.papervision3d.objects.DisplayObject3D;
class Turtle extends DisplayObject3D {
public var lines:BetterLines3D;
public var width:Number;
public function Turtle(lines_:BetterLines3D, width_:Number = 20) {
lines = lines_;
width = width_;
}
public function replicate():Turtle {
var ret:Turtle = new Turtle(lines, width);
ret.copyTransform(this);
return ret;
}
// forward/back
public function fd(n:Number):Turtle {
var px:Number = x;
var py:Number = y;
var pz:Number = z;
moveForward(n);
lines.addNewLine(width, px, py, pz, x, y, z);
return this;
}
public function bk(n:Number):Turtle { return fd(-n); }
}
import org.papervision3d.core.geom.renderables.Line3D;
import org.papervision3d.core.render.command.RenderLine;
import org.papervision3d.core.render.data.RenderSessionData;
import org.papervision3d.materials.special.LineMaterial;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.core.geom.*;
// This class scales the lines' width relative to their (center) screen Z.
// This can make interconnect points bulge, but I think it's better than the
// lines becoming skinny when they're close to the camera.
class BetterLines3D extends Lines3D {
public function BetterLines3D(material:LineMaterial = null, name:String=null) {
super(material, name);
}
/**
* Converts 3D vertices into 2D space, to prepare for rendering onto the stage.
*
* @param parent The parent DisplayObject3D
* @param renderSessionData The renderSessionData object for this render cycle.
*
*/
public override function project( parent :DisplayObject3D, renderSessionData:RenderSessionData ):Number
{
// This ugly hack is here because I can't do super.super.project(parent, renderSessionData)
// and I don't want Lines3D::project to add any lines to the render list.
var tmp:Array = lines;
lines = [];
super.project(parent, renderSessionData);
lines = tmp;
var line3D:Line3D;
var screenZ:Number;
var rc:RenderLine;
for each(line3D in lines)
{
if(renderSessionData.viewPort.lineCuller.testLine(line3D))
{
rc = line3D.renderCommand;
rc.renderer = line3D.material;
screenZ += rc.screenZ = (line3D.v0.vertex3DInstance.z + line3D.v1.vertex3DInstance.z)/2;
var fz:Number = (renderSessionData.camera.focus*renderSessionData.camera.zoom);
rc.size = line3D.size * fz / (renderSessionData.camera.focus + rc.screenZ);
rc.v0 = line3D.v0.vertex3DInstance;
rc.v1 = line3D.v1.vertex3DInstance;
renderSessionData.renderer.addToRenderList(rc);
}
}
return screenZ/(lines.length+1);
}
}