3D Game Programming

(C. Jardin) #1
.Tween({jump: 0})
.to({jump: Math.PI}, 500)
.onUpdate(function() {
marker.position.y = 200* Math.sin(this.jump);
})
.start();
}
</script>

A1.12Code: Working with Lights and Materials


This is the final version of the code that we used to explore lights and materials
in Chapter 12, Working with Lights and Materials, on page 109.

<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;
varcamera =newTHREE.PerspectiveCamera(75, aspect_ratio, 1, 10000);
camera.position.z = 500;
scene.add(camera);

// This will draw what the camera sees onto the screen:
varrenderer =newTHREE.WebGLRenderer();
renderer.shadowMapEnabled = true;
//var renderer = new THREE.CanvasRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// ******** START CODING ON THE NEXT LINE ********

varshape =newTHREE.SphereGeometry(100);
varcover =newTHREE.MeshBasicMaterial();
cover.color.setRGB(1, 0, 0);
varball =newTHREE.Mesh(shape, cover);
scene.add(ball);
ball.position.set(500, 0, 0);

varshape =newTHREE.TorusGeometry(100, 50, 8, 20);
varcover =newTHREE.MeshPhongMaterial();
cover.emissive.setRGB(0.8, 0.1, 0.1);
cover.specular.setRGB(0.9, 0.9, 0.9);
vardonut =newTHREE.Mesh(shape, cover);
scene.add(donut);
donut.castShadow = true;

Appendix 1. Project Code • 240


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf