Skip to content

Instantly share code, notes, and snippets.

@geotheory
Created May 26, 2012 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geotheory/2793870 to your computer and use it in GitHub Desktop.
Save geotheory/2793870 to your computer and use it in GitHub Desktop.
3D trees
Branch[] trees;
int N = 5;
int Norig = N;
int ntrees = 10;
int area = 600;
boolean HD = false;
boolean HDswitch = false;
boolean levelswitch = false;
boolean levelup;
boolean spin = true;
int spinct;
PVector pvec;
import processing.dxf.*;
boolean record = false;
void setup()
{
size(800, 600, P3D);
textMode(SCREEN);
textSize(16);
trees = new Branch[ntrees];
for (int i=0; i<ntrees; i++) {
trees[i] = new Branch(new PVector(random(-area, area), random(-area, area), 0), N);
}
}
void draw()
{
if (record) {
String filename = "Outputs/output " + day() + "." + hour() + "." + minute() + "." + second() + ".dxf";
beginRaw(DXF, filename);
}
if(spin) spinct++;
background(255);
directionalLight(150, 150, 150, 0.3, -0.6, 0.3);
ambientLight(100, 100, 100);
fill(255);
rect(-area, -area, area*2, area*2);
float zoom = 0.6 + mouseX/800.0;
if(HDswitch) { // switch HD / non-HD
if(HD) smooth();
else noSmooth();
HDswitch = false;
}
camera(zoom * 1200 * sin(spinct/400.0), zoom * 1200 * cos(spinct/400.0), -mouseY*2, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
for (int i=0; i<ntrees; i++) trees[i].draw();
fill(0);
text("Tree levels: " + N, 10,15);
if(levelswitch) levelswitch = false;
if (record) {
endRaw();
record = false;
}
}
class Branch
{
Branch[] branches;
PVector start, end, v;
boolean parent = false;
boolean origparent = false;
int level;
color col;
float colweight, xy;
Branch(PVector pv, int n)
{
level = n;
start = new PVector(pv.x, pv.y, pv.z);
if (level==N) end = new PVector(random(-50.0, 50.0), random(-50.0, 50.0), random(-250.0, -150.0));
else end = new PVector(random(-100.0, 100.0), random(-100.0, 100.0), random(-100.0, -30.0));
v = new PVector(end.x, end.y, end.z);
xy = sqrt((v.x * v.x) + (v.y * v.y)); // x/y plane vector distance
end.add(start);
colweight = (1-level)*0.12 + 0.8;
col = color(200*colweight, 100*colweight, 100*colweight);
if (n>1) {
n--;
parent = true;
origparent = true;
branches = new Branch[2];
branches[0] = new Branch(end, n);
branches[1] = new Branch(end, n);
}
}
void draw()
{
fill(col);
pushMatrix();
translate(start.x, start.y, start.z);
pvec = PVector.sub(end, start);
translate(pvec.x/2.0, pvec.y/2.0, pvec.z/2.0);
rotateZ(atan2(v.y, v.x));
rotateY(atan2(xy, v.z));
box(level*1.5, level*1.5,pvec.mag());
popMatrix();
if (parent && origparent){
for (int i=0; i<branches.length; i++){
branches[i].draw();
}
}
else{
noStroke();
pushMatrix();
translate(end.x, end.y, end.z);
fill(50, 200, 100);
box(20);
fill(50, 200, 100, 30);
box(100);
popMatrix();
}
// Recalcuate parents
if(levelswitch){
if(levelup){
println(frameCount);
if(level==Norig +2 - N) parent = true;
}
else { // level-down
if(level==Norig + 1 - N) parent = false;
}
}
}
}
void keyPressed()
{
if (key==' ' | key ==mouseX) setup();
if(key=='l' | key=='L'){
levelswitch = true;
if(key=='L'){
N++;
levelup = true;
}
else {
N--;
levelup = false;
}
}
if(key=='s'|key=='S'){
HD = !HD;
HDswitch = true;
}
if (key == 'r') record = true;
if (key == 'z') spin = !spin;
}
void mousePressed()
{
setup();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment