xxxxxxxxxx
110
let trees = [];
let maxdepth = 8;
let branchcount = 0;
let startingpoints = [];
let counter = 0;
const canvasHeight = 400;
const maxbranches = 4;
const minbranches = -1;
const noiseAmp = 10, noiseSpeed = 0.0002;
const maxMouseTurn = 90;
const amountOfTrees = 6;
function setup() {
createCanvas(canvasHeight * 2.5, canvasHeight);
angleMode(DEGREES);
for(let i = 0; i<amountOfTrees; i++){
trees.push(generateTree(0));
startingpoints.push([random(width*0.1,width*0.9), random(height*1.05,height*1.2)]);
}
print(branchcount);
}
function draw() {
background(220);
let noiseSize = 30;
strokeWeight(2*noiseSize);
for(let x=0;x<width/noiseSize;x++){
for(let y=0;y<height/noiseSize;y++){
stroke(noise(noiseSize*x/width, noiseSize*y/height, millis()/5000)*150);
point(noiseSize/2+x*noiseSize,noiseSize/2+y*noiseSize);
}
}
strokeWeight(1);
for(let i=0; i<trees.length; i++){
drawBranch(trees[i], startingpoints[i][0], startingpoints[i][1], trees[i].rot, height/4);
}
counter++;
if(counter>100){
print(frameRate());
counter=0;
}
}
function generateTree(depth) {
branchcount++;
let branch = {
depth: depth,
rot: random(-15, 15),
children: [],
};
if (depth < maxdepth && branchcount < 2000) {
// i trie making the randomness "weighted" so they always grow a little bit but i actually like the unweighted one as well so just comment either one.
// // normal distributed function, you have a lot of control over the results by changing the output parameters of the map
// let n = map(depth,0,maxdepth,10,3);
// let ran = 0;
// for(let j=0;j<n;j++){
// ran += random(-2,4)
// }
// ran = ran/floor(n);
// for (let i = 0; i < ran; i++) {
// branch.children.push(generateTree(depth + 1));
// }
// original function
for (let i = 0; i < random(-1,4); i++) {
branch.children.push(generateTree(depth + 1));
}
}
return branch;
}
function drawBranch(tree, x, y, rot, size) {
fill((1-tree.depth/maxdepth)*60);
noStroke();
translate(x, y);
rotate(rot);
beginShape();
vertex(-size / 7, 0);
vertex(-size / 8, -size);
vertex(size / 8, -size);
vertex(size / 7, 0);
endShape(CLOSE);
translate(-x, -y);
for (let branch of tree.children) {
drawBranch(branch, x, y - size / 1.1, branch.rot + (mouseX / width) * maxMouseTurn + noiseAmp*noise(millis()*0.0002) - noiseAmp/2, size / 1.2);
}
if (tree.children.length == 0) {
fill("white");
stroke("black");
ellipse(x, y - size / 1.1, size / 3, size / 1.5);
}
translate(x, y);
rotate(-rot);
translate(-x, -y);
}