Graphics Reference
In-Depth Information
1. The first thing to do is create a
THREE.PointLight
instance:
var pointLight = new THREE.PointLight();
pointLight.color = new
THREE.Color(0xff0000);
pointLight.intensity = 3;
pointLight.distance = 60;
pointLight.name = 'pointLight';
With the
color
property, we set the color
THREE.PointLight
object emits,
and the intensity allows us to set how much light is emitted. Finally, the
dis-
tance
property is used to calculate how much the intensity decreases the
farther away the lit object is from the light. In this case, the intensity will be
0
when the distance to the light is
60
.
2.
THREE.PointLight
emits lights in all directions, so we need to set the
po-
sition
property and then we can add the light to the scene:
pointLight.position = new
THREE.Vector3(-30,0,0);
scene.add(pointLight);
3. The last thing we need to do for this recipe is move
THREE.PointLight
through the scene. Like all animations, we do this in the render loop by
adding the following to the
render
function:
var light =
scene.getObjectByName('pointLight');
light.position.y = 15 *
Math.sin(count+=0.005);
In this small code snippet, we first get a reference to our
THREE.PointLight
object and then update its
position.y
property. For
this to work, we also need to define a global
count
property at the top of our
JavaScript like this:
var count = 0;