functionmakeTreeAt(x, z) {
vartrunk =newTHREE.Mesh(
newTHREE.CylinderGeometry(50, 50, 200),
newTHREE.MeshBasicMaterial({color: 0xA0522D})
);vartop =newTHREE.Mesh(
newTHREE.SphereGeometry(150),
newTHREE.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, you’ll see the avatar standing in front
of a forest of four trees.That’s pretty cool, but how did we do that?
Breaking It Down
The first part of the forest-building is pretty easy to follow. We add trees at
different X and Z coordinates (remember that Y is up and down) around the
scene.makeTreeAt( 500, 0);
makeTreeAt(-500, 0);
makeTreeAt( 750, -1000);
makeTreeAt(-750, -1000);That’s easy enough, but how does that makeTreeAt() thing work?
As we’ll see in Chapter 5, Functions: Use and Use Again, on page 49, a Java-
Script function is a way to run the same code over and over. In this case, wereport erratum • discussBuilding a Forest with Functions • 41
Prepared exclusively for Michael Powell