3D Game Programming

(C. Jardin) #1

Below the donut code, add some sunlight:


varsunlight =newTHREE.DirectionalLight();
sunlight.intensity = 0.5;
sunlight.position.set(100, 100, 100);
scene.add(sunlight);

We’re positioning the sunlight to the right, above, and in front of the donut.
The result should be a pretty cool-looking donut:

Emissive


Unlike with the MeshBasicMaterial cover—where we adjusted the color attribute—
with MeshPhongMaterial we adjust the emissive attribute to describe the color:

cover.emissive.setRGB(0.8, 0.1, 0.1);

We can’t just use color because we need to adjust a number of color-related
attributes when working with a MeshPhongMaterial. The emissive attribute describes
the color that the cover “emits”—the color that it is.

Specular


Specular is another color attribute we can adjust. The specular attribute describes
the color of the shiny parts of the object. If we do not set this value, it’s not
very bright. Let’s make it bright.

Add the specular line below the line on which we set the emissive color:


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);

If all of the RGB colors are the same value, then we’ll see black, white, or
some shade of gray. All zeros would be black. All ones would be white. Any-
thing in between is gray. In this case we set the specular color—the color of

Chapter 12. Working with Lights and Materials • 112


Prepared exclusively for Michael Powell report erratum • discuss

Free download pdf