height = 10; number_of_iterations = 7; level_of_smoothness = 25; random_seed = 786; length_multiplier = 5; module trunk(size, depth, seed) { //create an array of random numbers operation = rands(1,100,1, seed+5); echo(operation); //choose a module based on that array if (operation[0] < 60) { straight_trunk(size*.9, depth, seed+23546); } else if (operation[0] < 70) { branch_crook(size*.9, depth, seed+28543); } else if (operation[0] < 98) { branch_left(size*.9, depth, seed+28543); } else { sphere(size); } } module straight_trunk(size, depth, seed) { rotations = rands(-10,10,10, seed+987654); color([0,1,0]) { cylinder(h = size*length_multiplier, r1 = size, r2 = size); //move to the tip of the branch translate([0,0,size*length_multiplier]) { union() { sphere(size); rotate([0+rotations[0],0+rotations[1],0+rotations[2]]) { //check for stop condition if (depth > 0) { //build another trunk trunk(size, depth-1, seed+8532); } } } } } } module branch_left(size, depth, seed) { rotations = rands(-10,10,10, seed+45678); color([0,1,0]) { //create the branch by tapering a cylinder from one radius to another cylinder(h = size*length_multiplier, r1 = size, r2 = size); //move to the tip of the branch translate([0,0,size*length_multiplier]) { union() { sphere(size); //trunk rotate([0+rotations[0],0+rotations[1],0+rotations[2]]) { //check for stop condition if (depth > 0) { //build another trunk trunk(size, depth-1, seed+24571); } } //branch rotate([0+rotations[0],0+rotations[1],0+rotations[2]]) { //check for stop condition if (depth > 0) { rotate([0,90,36*rotations[3]]) { cylinder(h = size*length_multiplier*0.75, r1 = size, r2 = size); translate([0,0,size*length_multiplier*0.75]) { sphere(size); rotate([0,-90,0]) { //build another trunk trunk(size*.75, depth-1, seed+1); } } } } } } } } } module branch_crook(size, depth, seed) { rotations = rands(-10,10,10, seed+75326); color([0,1,0]) { //create the branch by tapering a cylinder from one radius to another cylinder(h = size*length_multiplier, r1 = size, r2 = size); //move to the tip of the branch translate([0,0,size*length_multiplier]) { union() { sphere(size); //branch rotate([0+rotations[0],0+rotations[1],0+rotations[2]]) { //check for stop condition if (depth > 0) { rotate([0,-90,36*rotations[3]]) { cylinder(h = size*length_multiplier*0.75, r1 = size, r2 = size); translate([0,0,size*length_multiplier*0.75]) { sphere(size); rotate([0,90,0]) { //build another trunk trunk(size*.75, depth-1, seed+1); } } } } } } } } } union() { //build the tree trunk(height, number_of_iterations, $fn=level_of_smoothness, random_seed); }