Skip to content

Instantly share code, notes, and snippets.

@Adrianl3d
Created July 6, 2014 10:40
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 Adrianl3d/18b3e3baffa7df41e09e to your computer and use it in GitHub Desktop.
Save Adrianl3d/18b3e3baffa7df41e09e to your computer and use it in GitHub Desktop.
Builds a 18 meter high tree with random green leaves
// tree18m.js
// Creates a single tree of 1 meter voxels to about 18 meters high with various shades of green leaves
// Created by Adrian on 4 July 2014
// Copyright 2014 Adrian McCarlie
// Released under Creative Commons By Attribution Share Alike, feel free to modify and share.
//
//
// Change these 3 values to suit the planting site of the tree.
//
var X_POS = 100;
var Y_POS = 100;
var Z_POS = 100;
//Change the height variable here to change the height of the trunk from the ground to the bottom of the leaves.
//Does not change the size of the leaves.
var height = 10;
//
///////////////////End of User Variables
//
var x = X_POS;
var y = Y_POS + height;
var z = Z_POS;
var l = 0;
var maxl = 2;
var halfh = 5;
var h = 0;
var w = 0;
var maxw = 2;
var leaves = 0;
var width = 3;
var start = 0;
var col = 0;
function rezTree()
{
while (h < halfh) //create leaf stacks to half the height
{
while (width < maxw) //create columns
{
while (leaves < width) //create rows of leaves
{
col = Math.floor((Math.random() * 130) + 100);//generates a random shade of green
Voxels.setVoxel(x, y+h, z+start, 1, 40, col, 40); z++; leaves++; //rows getting bigger
}
x++;
leaves = 0;
width = width + 2;
z = Z_POS;
start=start-1;
}
//////////////////////////////////////////////////////// stop getting bigger start getting smaller horizontally
width = width - 4;
start=start + 2 ;
while (width >= 0)
{
while (leaves < width)
{
col = Math.floor((Math.random() * 130) + 100);
Voxels.setVoxel(x, y+h, z+start, 1, 40, col, 40); z++; leaves++;//rows getting smaller
}
x++;
leaves = 0;
width = width - 2;
z = Z_POS;
start=start+1 ;
}
h++;
maxw = maxw + 2;
x = X_POS - h;
}
///////////////////////////////////////////////////////////////////// half way from the bottom up start getting smaller
var q = -4;
x = x +2;
width = 1;
start = start - 1;
maxw = maxw - 2;
while (h < (halfh *2)-1)//create stack for second half height
{
while (width < maxw)
{
while (leaves < width)
{
col = Math.floor((Math.random() * 130) + 100);
Voxels.setVoxel(x, y+h, z+start, 1, 40, col, 40); z++; leaves++; //rows getting bigger
}
x++;
leaves = 0;
width = width + 2;
z = Z_POS;
start=start-1;
}
width = width - 4;
start=start+2 ;
while (width >= 0)
{
while (leaves < width)
{
col = Math.floor((Math.random() * 130) + 100);
Voxels.setVoxel(x, y+h, z+start, 1, 40, col, 40); z++; leaves++; //rows getting smaller
}
x++;
leaves = 0;
width = width - 2;
z = Z_POS;
start=start+1 ;
}
h++;
maxw = maxw - 2;
q++;
x = X_POS + q;
}
x= X_POS+1;
y = Y_POS;
z= Z_POS+1;
while (y <= Y_POS + height)//build a post 10 high
{
Voxels.setVoxel(x, y, z, 1, 100, 80, 10); y++;
}
Voxels.setVoxel(x-1, y, z-1, 1, 100, 80, 10); //4 single brown voxels to branch the trunk nicely into the foliage
Voxels.setVoxel(x-1, y, z+1, 1, 100, 80, 10);
Voxels.setVoxel(x+1, y, z+1, 1, 100, 80, 10);
Voxels.setVoxel(x+1, y, z-1, 1, 100, 80, 10);
Script.stop();
}
rezTree();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment