3D Game Programming

(C. Jardin) #1
renderer.render(scene, camera);
}

animate();

varstars =newTHREE.Geometry();
while(stars.vertices.length < 1e4) {
varlat = Math.PI * Math.random() - Math.PI/2;
varlon = 2*Math.PI * Math.random();

stars.vertices.push(newTHREE.Vector3(
1e5 * Math.cos(lon) * Math.cos(lat),
1e5 * Math.sin(lon) * Math.cos(lat),
1e5 * Math.sin(lat)
));
}
varstar_stuff =newTHREE.ParticleBasicMaterial({size: 500});
varstar_system =newTHREE.ParticleSystem(stars, star_stuff);
scene.add(star_system);

document.addEventListener("keydown",function(event) {
varcode = event.keyCode;

if(code == 65) {// A
camera = above_cam;
}
if(code == 69) {// E
camera = earth_cam;
}
});

</script>

A1.14Code: Phases of the Moon


This is the final version of the moon-phases code from Chapter 14, Project:
Phases of the Moon, on page 125.

<body></body>
<script src="http://gamingJS.com/Three.js"></script>
<script src="http://gamingJS.com/ChromeFixes.js"></script>
<script>
// This is where stuff in our game will happen:
varscene =newTHREE.Scene();

// This is what sees the stuff:
varaspect_ratio = window.innerWidth / window.innerHeight;
varabove_cam =newTHREE.PerspectiveCamera(75, aspect_ratio, 1, 1e6);
above_cam.position.z = 1000;
scene.add(above_cam);

report erratum • discuss

Code: Phases of the Moon • 243


Prepared exclusively for Michael Powell

Free download pdf