Trying to create Random Trees


#1

I am trying to make random trees. Here is my code so far: `var shape = new THREE.SphereGeometry(100);
var cover = new THREE.MeshBasicMaterial({color: ‘forestgreen’});
var ball = new THREE.Mesh(shape, cover)
ball.position.y = 175;
scene.add(ball)

var shape = new THREE.CylinderGeometry(50, 50, 200);
var cover = new THREE.MeshBasicMaterial({color: ‘sienna’});
var trunk = new THREE.Mesh(shape, cover)
scene.add(trunk)

`
Thanks for any help!:grinning:


#2

Aside from some curly quotes around the color names instead of single quotes, that code looks OK. The following creates a tree for me:

var shape = new THREE.SphereGeometry(100);
var cover = new THREE.MeshBasicMaterial({color: 'forestgreen'});
var ball = new THREE.Mesh(shape, cover);
ball.position.y = 175;
scene.add(ball);

var shape = new THREE.CylinderGeometry(50, 50, 200);
var cover = new THREE.MeshBasicMaterial({color: 'sienna'});
var trunk = new THREE.Mesh(shape, cover);
scene.add(trunk);

The curly quotes may not be in your code – they could have just been from the forum software – so they may or may not be the cause of the problem you’re seeing. If that code is still not working for you, let me know what you’re seeing instead.

-Chris


#3

Sorry, I put the wrong code.
I’m trying to create that tree like the planets from chapter 5 (second edition), but I want it to be o the same y axis so there aren’t flying trees.

I tried a few times with different methods I thought would work, but the didn’t. :frowning:
If you could help me out, that would be great.
Thanks.


#4

Gotcha. The easiest way to do this is with the makeTreeAt() function from chapter 4. If you have that function, then you could create 10 trees at random x and z positions with:

for (var i=0; i<10; i++) {
  var x = 1000 - (2000 * Math.random());
  var z = 1000 - (2000 * Math.random());
  makeTreeAt(x, z);
}

Let me know if that doesn’t work for any reason or if you have any questions.

-Chris