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:

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

Get 3D Game Programming for Kids, 2nd Edition 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.