Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 11 ■ 3D SCene Configuration: uSing the perSpeCtiveCamera anD pointLight

Since you’ll also need something for the light to illuminate, add a Sphere sphere declaration after
your PointLight light declaration so that we have something to test our code with, as shown highlighted
in yellow at the top of Figure 11-17. Next, instantiate your PointLight after the scene.setCamera(camera);
method call. I have used the second more explicit constructor method but given it the default Color.WHITE,
which we might change later, after we look at materials and how they interact with light color values. Move
the light down a bit so it’s not inside of the Sphere (at 0,0,0) using a light.setTranslateZ(-25); method
call. Next, use a light.getScope().add(sphere); method chain and add the sphere object to the scope of
what the PointLight object “sees.” Notice that this allows you to have different light objects affect different
3D objects in the 3D Scene, which is quite a powerful feature. The Java code for your PointLight and
Sphere object declarations, instantiation, and configuration Java statements is highlighted at the bottom of
Figure 11-17 and should look something like the following Java code:


PointLight light;
Sphere sphere;
...
private void createBoardGameNodes() {
...
light = new PointLight(Color.WHITE);
light.setTranslateY(-25);
light.getScope().add(sphere); // "Wire" the Sphere and Light together via .getScope().
add()
sphere = new Sphere( 100 );
...
}


Figure 11-16. Declare a PointLight named light at the top of your JavaFXGame class and hit Alt+Enter and
Add Import.

Free download pdf