# Recursive Tree Loop? How do i make the matrix-array?

I would like to make fractal tree in Unity3d, omitting branch angles and lengths, just to instantiate cubes as a tree, like 2,4,8,16… i look for solution all day and i figure it could take me days yet? Should i use an array to store and recall a list of coordinates that each new branch should be at? would it contain 2 or 16 values?

Here is an interactive JS code for trees:
http://www.processing.org/learning/topics/tree.html

It uses the following code, please bypass the canvas, rotations, stalk length, i’d be so happy to just have a tree like in the image.

``````  branch(120);

}

void branch(float h) {
// Each branch will be 2/3rds the size of the previous one
h *= 0.66;

// All recursive functions must have an exit condition!!!!
// Here, ours is when the length of the branch is 2 pixels or less
if (h > 2) {
pushMatrix();    // Save the current state of transformation (i.e. where are we now)
rotate(theta);   // Rotate by theta
line(0, 0, 0, -h);  // Draw the branch
translate(0, -h); // Move to the end of the branch
branch(h);       // Ok, now call myself to draw two new branches!!
popMatrix();     // Whenever we get back here, we "pop" in order to restore the previous matrix state

// Repeat the same thing, only branch off to the "left" this time!
pushMatrix();
rotate(-theta);
line(0, 0, 0, -h);
translate(0, -h);
branch(h);
popMatrix();
``````

}
}

here is my funny attempt thus far:

``````var bullet : Transform;
private var rot : Quaternion;

function Start()
{
for (var j : float = 0; j<10; j++)
{
for (var i : float = 0; i<10; i++)
{
rot.eulerAngles = Vector3(0,10,0);//Rotation to use with instatiation
var InstantiateObject = Instantiate(bullet, Vector3(Mathf.Sin(i*432)*i*j,j*5,Mathf.Cos(i*4375)*i*j), rot);//call clones
InstantiateObject.localScale += Vector3(25-j*2,22-j*2,22-j*2);//resize clones

}

}
}
``````

Try this as a staring point:

``````public class CubeSpawn : MonoBehaviour
{
public GameObject cubePrefab;
public GameObject root;

private Vector3[] CORNERS =
{
new Vector3(-1, 1, -1),
new Vector3(-1, 1, 1),
new Vector3(1, 1, 1),
new Vector3(1, 1, -1),
};

public void Start()
{
}

public void AddChildren(GameObject parent, float childToParentRatio, int depth)
{

foreach (Vector3 corner in CORNERS)
{
GameObject cube = (GameObject) Instantiate(cubePrefab);
Vector3 newPosition = parent.transform.TransformPoint(corner * 0.5f);

//align parent and child
cube.transform.rotation = parent.transform.rotation;

cube.transform.position = newPosition;
cube.transform.localScale *= childToParentRatio;

if(depth > 0)
{
AddChildren(cube, childToParentRatio * childToParentRatio, depth - 1);
}
}

}

public Vector3 prod(Vector3 v1, Vector3 v2)
{
return new Vector3(v1.x * v2.x, v1.y * v2.y, v1.z * v2.z);
}
}
``````

Put the script on an empty game object. Drag in the parent (you can modify it later to instantiate or whatever), and drag in your cube prefab.

The function above is called recursively on the new corners; the depth value makes sure it is not called indefinitely.

Here is a JS version of your C# script:

``````var cubePrefab: GameObject ;
var root : GameObject ;

private var corners = new Array (Vector3(-1, 1, -1), Vector3(-1, 1, 1),Vector3(1, 1, 1),Vector3(1, 1, -1));

function Start()
{
var root = Instantiate(cubePrefab,Vector3.zero,Quaternion.identity);
}

{

for (var x = 0; x < 4; x++)
{
var cube = Instantiate(cubePrefab,Vector3.zero,Quaternion.identity);

//align parent and child
//cube.transform.rotation = prt.transform.rotation;

cube.transform.position = ploppy.transform.TransformPoint(corners[x]*.5);
cube.transform.localScale *= ratio;

if(depth > 0)
{
AddChildren( cube, ratio - (ratio*.6) , depth - 1);
}
}

}
``````

There is some info and a source project to draw reference from here http://tonydincau.com/fractal-trees/