3D Game Programming

(C. Jardin) #1

A1.3 Code: Making an Avatar


This is the final version of the avatar code from Chapter 3, Project: Making
an Avatar, on page 25.

<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.CanvasRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

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

varcover =newTHREE.MeshNormalMaterial();
varbody =newTHREE.SphereGeometry(100);
varavatar =newTHREE.Mesh(body, cover);
scene.add(avatar);

varhand =newTHREE.SphereGeometry(50);

varright_hand =newTHREE.Mesh(hand, cover);
right_hand.position.set(-150, 0, 0);
avatar.add(right_hand);

varleft_hand =newTHREE.Mesh(hand, cover);
left_hand.position.set(150, 0, 0);
avatar.add(left_hand);

varfoot =newTHREE.SphereGeometry(50);

varright_foot =newTHREE.Mesh(foot, cover);
right_foot.position.set(-75, -125, 0);
avatar.add(right_foot);

varleft_foot =newTHREE.Mesh(foot, cover);
left_foot.position.set(75, -125, 0);
avatar.add(left_foot);

report erratum • discuss

Code: Making an Avatar • 219


Prepared exclusively for Michael Powell

Free download pdf