Building a Forest with Functions

We’ll need a lot of trees for our forest. We could build them one at a time, but we’re not going to do that. Instead, let’s add the following JavaScript after all of the avatar body parts:

 
makeTreeAt( 500, 0);
 
makeTreeAt(-500, 0);
 
makeTreeAt( 750, -1000);
 
makeTreeAt(-750, -1000);
 
 
function​ makeTreeAt(x, z) {
 
var​ trunk = ​new​ THREE.Mesh(
 
new​ THREE.CylinderGeometry(50, 50, 200),
 
new​ THREE.MeshBasicMaterial({color: 0xA0522D})
 
);
 
 
var​ top = ​new​ THREE.Mesh(
 
new​ THREE.SphereGeometry(150),
 
new​ THREE.MeshBasicMaterial({color: 0x228B22})
 
);
 
top.position.y = 175;
 
trunk.add(top);
 
 
trunk.position.set(x, -75, z);
 
scene.add(trunk);
 
}

If you entered all that code correctly, ...

Get 3D Game Programming for Kids now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.