3D Game Programming

(C. Jardin) #1
varshape =newTHREE.SphereGeometry(100);
varcover =newTHREE.MeshBasicMaterial();
cover.color.setRGB(1, 0, 0);
varball =newTHREE.Mesh(shape, cover);
scene.add(ball);

MeshBasicMaterial


Notice that, instead of the MeshNormalMaterial that we have used to wrap things
so far, we’re now using a MeshBasicMaterial. Since it’s a basic cover, there’s not
much we can do with it. But we can change the color, which is new.

You should see a red ball on the screen:


Colors in computer programs are written as RGB numbers, which describe
how much red (R) green (G), and blue (B) is used. Believe it or not, you can
make just about every color there is by combining those three colors. Com-
bining RGB colors may not work quite like you would expect—for instance,
you make yellow by combining red and green: cover.color.setRGB(1,1,0).

Wikipedia Has a Very Nice List of Colors
The Wikipedia list at http://en.wikipedia.org/wiki/List_of_colors includes RGB
percentages, which you would need to write as decimals. For
instance, one of the first colors on that list is “Air Force blue (RAF)”
which has the following RGB percentages: 36%, 54%, 66%. To make
our ball that color, use this: cover.color.setRGB(0.36, 0.54, 0.66).

This basic material is a little more useful in real games than the MeshNormalMa-
terial that we’ve used so far. It’s particularly helpful for backgrounds and flat
surfaces. Even so, it’s not the most realistic-looking material that we can
choose. The color always looks the same no matter what light is shining on
it. It won’t reflect light or have shadows. That said, use it wherever possi-
ble—it’s easy for the computer to draw.

Now let’s look at a more interesting material. But first, move the basic red
ball out of the way:

ball.position.set(500, 0, 0);

A1.12Code: Working with Lights and Materials


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf