Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

Chapter 13 ■ 3D MoDel ShaDer Creation: USing the JavaFX 9 phongMaterial ClaSS


The one empty constructor method is protected, which means that it is not instantiated directly.
However, this constructor method functionality is implemented in the PhongMaterial subclass, as
PhongMaterial(), which we will be covering in the next section of this chapter.


protected Material()


Next, let’s take a look at the PhongMaterial subclass, which represents the Phong shader rendering
algorithm. This is what we will be using (and learning about) directly during the chapter to color our 3D
primitives we created during Chapter 12.


JavaFX PhongMaterial: Phong Shading Algorithm and Attributes


The public PhongMaterial class extends the Material class to define Phong (algorithm) shader materials,
their color settings, and their texture maps for your JavaFX 3D Scene. This class is kept in the javafx.scene.
paint package in the javafx.graphics module and is a subclass of Material, as you know, so you will have the
following Java class hierarchy:


java.lang.Object



javafx.scene.paint.Material
javafx.scene.paint.PhongMaterial



The Phong shading (materials and texture rendering) algorithm in JavaFX 9 describes the interaction
between your PointLight object(s) and AmbientLight object (if present) and the surface of the 3D primitive
that the PhoneMaterial object is applied to. The PhongMaterial object reflects light while applying a diffuse
and specular color tinting, just like light in real life. When it bounces off a colored object, the light itself
becomes colored. The PhongMaterial algorithm supports the AmbientLight object settings, if present, and
supports self-illumination, or “glow” mapping, so that you can apply special effects to further enhance the
shader realism.
According to the JavaFX 9 PhongMaterial documentation, the coloration of any given point on a
geometric surface is a mathematical function of these four components: ambient, diffuse, specular,
and self-illumination map. Subcomponents (algorithm input) for these include AmbientLight (Object),
PointLight (Object), Diffuse Color (setting), Diffuse Color Map (Image Object), Specular Color (setting),
Specular Power (setting), Specular Map (Image Object), Self Illumination, or Glow Map (Image Object).
The final color for an AmbientLight source if there is more than one AmbientLight object, in which
case their values will simply be summed (which is why I suggested using one), will be computed using the
following equation:


For each AmbientLight (Object) Source [i]: { ambient += AmbientLightColor[i] } // Color Summed


The PointLight source algorithm calculation is far more advanced, which is why I suggested using
PointLight for your use in Pro Java 9 3D Games, as it allows fine-tuned control over how PhongMaterial
objects perform, as well as adding more dramatic lighting (fall-off, shadows, higher contrast, etc.) to your
3D Scene, making it more photoreal. It is important to note that the period used in these equations is
referencing the dot product mathematical operation.

Free download pdf