Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

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


Using 3D Lighting: Adding Illumination to 3D Games


There are two different sets of lighting APIs in JavaFX 9. One is for 3D Scene usage and is contained in
the javafx.scene package featuring an abstract LightBase superclass and the “concrete” (usable in your
code as objects you can construct) subclasses AmbientLight and PointLight. The other is the abstract
Light superclass and is contained in the javafx.scene.effect package; this package contains 2D digital
imaging effects, as we covered earlier during this book. For 3D use we are going to focus on the LightBase,
AmbientLight, and PointLight classes and use the PointLight class initially as we can obtain the most
dramatic and realistic results using that class.


JavaFX LightBase Class: An Abstract Superclass Defining Light


The public JavaFX LightBase superclass is an abstract class, used only to create different types of lights.
Currently there is a general or “ambient” level of illumination for a 3D scene provided by an AmbientLight
subclass (object) or a PointLight subclass (object) that emulates the properties of a light bulb. Your
application should not attempt to extend the abstract LightBase class directly; if you attempt this, Java will
throw an UnsupportedOperationException, and your pro Java 9 game will not compile or run. The LightBase
class is kept in the javafx.graphics module in the core javafx.scene package and is a subclass of Node, as it is
ultimately a Node at the top of the SceneGraph. The LightBase class implements a Styleable interface so it
can be styled and an EventTarget interface so it can process events. The Java 9 class hierarchy for the JavaFX
LightBase class therefore would look like the following:


java.lang.Object



javafx.scene.Node
javafx.scene.LightBase



The LightBase class provides definitions of common properties for subclasses that construct objects
that are used to represent (“cast”) some form of light in your 3D Scene. These LightBase object properties
should include your initial color for the light source and whether the light source is initially turned on
(enabled) or off (disabled). It’s important to note that since this is a 3D feature, it is a conditional feature.
Reference the example I laid out in the PerspectiveCamera section of the chapter as to how to set up code
that detects the ConditionalFeature.SCENE3D flag.
LightBase subclasses have two properties (or attributes or characteristics if you prefer those terms); one
is the color or ObjectProperty that specifies the color of light emanating from the light source, and
the second is a BooleanProperty called lightOn that allows the light to be turned on and off.
The LightBase abstract class has two overloaded protected constructor methods. One has no
parameters and creates a default Color.WHITE light source, using this constructor method call format:


protected LightBase()


The second overloaded protected constructor method allows the subclass to specify a color value for
the light, using the following constructor method call format:


protected LightBase(Color color)


The LightBase class has seven methods that will all be available to (inherited by) every LightBase
subclass, including the AmbientLight and PointLight subclasses, so pay attention to these here as I will cover
them only once.

Free download pdf