Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 11 ■ 3D SCene Configuration: uSing the perSpeCtiveCamera anD pointLight


JavaFX PointLight Class: Lighting Your 3D Scene Dramatically


The public JavaFX PointLight class is a concrete class, used to create a local or “point source” instance of
illumination for a 3D scene. There is often more than one PointLight instance in a 3D Scene to allow the
artist to implement complex lighting models simulating real-world light sources. The PointLight class is
kept in the javafx.graphics module in the core javafx.scene package and is a subclass of LightBase, which is a
Node subclass, as it is ultimately a Node at the top of the SceneGraph. The PointLight class also implements
a Styleable interface so it can be styled and an EventTarget interface so that it can process events. The Java
class hierarchy for the JavaFX PointLight class therefore would look like the following:


java.lang.Object



javafx.scene.Node
javafx.scene.LightBase
javafx.scene.PointLight



The PointLight class defines the point light source (think lightbulb) objects for your 3D Scene, as
are needed. Try to use as few PointLight objects as possible as they are expensive to render (calculate, or
process, their algorithm). Point lights are defined as a local light emanation point and can be animated
to create a wide range of special effects. All PointLight object properties are inherited from the LightBase
superclass and should include an initial color for the light source and whether the light source is initially
turned on (enabled) or off (disabled). It’s again important to note that since this is a 3D feature, it is also a
conditional feature.
The PointLight has two overloaded constructor methods. The first one creates an unconfigured
PointLight object class with the default Color.WHITE light source, using the following Java instantiation
programming format:


PointLight light = new PointLight();


The second overloaded constructor method creates a new instance of PointLight using a specified color
that is not Color.WHITE, using the following Java instantiation programming format:


PointLight aqualight = new PointLight(Color.AQUA);


Next, let’s take a closer look at the work process for adding a PointLight object to use as a light source for
your JavaFXGame class infrastructure.


Adding Light to the Game’s 3D Scene: Using PointLight Objects


Next, let’s add a point light source into your JavaFXGame code so that we can learn about 3D primitives in
the next chapter. Declare a PointLight object named light at the top of your JavaFXGame class; then use the
Alt+Enter keystroke combination to bring up the helper pop-up and select the “Add import for javafx.scene.
PointLight” option, as highlighted in yellow and blue at the bottom of Figure 11-16.

Free download pdf