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 appearance of the Cylinder and Sphere class (object) primitives has changed drastically, however,
with the addition of a specular highlight using the specularColor property. I used YELLOW to give it a
metallic look, but if you use WHITE (the default), it will look more normal. Notice that the PointLight can be
set to WHITE, and you can intercede (add a color filter) to the PointLight before it hits the primitive surface.
Therefore, if you are seeking photorealism, make sure you match your PointLight and specularColor
values!
The specularPower property (attribute) of the PhongMaterial class (object) controls how shiny your
surface is, at least on curved objects. Having zero specular highlights, as shown in Figure 13-3, creates what
is called a matte surface. It is important to note that calling setSpecularPower(0) will not remove the specular
highlight. In fact, that would do just the opposite and give you a huge “blown-out” specular highlight,
which looks terrible. Let’s play around with this property next, and then we can move on to look at all the
other properties. The rest of the properties involve Maps and their Image objects, which will involve digital
imaging software, in our case GIMP 2.10 (or 3.0 if it has been released).
Let’s add a specularPower property setting to the phongMaterial shader object using a
setSpecularPower() method call with the double data value of 12. Technically, this is annotated in your
Java code as “12.0d.” However, since an Integer (just 12) data value fits into (conforms with) the Double
specification, you can just use 12 and the Java build and compile process will understand what you are doing
and make sure it’s configured as a Double value (at runtime).
Add a line of code after the PhongMaterial object instantiation and type in the phongMaterial object
name. Hit the period key, select the setSpecularPower(Double value) option from a pop-up helper selector,
and double-click it to insert it into your Java statement. Type 12 or 12.0d inside the parameter area.
Your resulting Java code will look like one of the following two Java statements, shown in the bottom
third of Figure 13-6:


sphere.setSpecularPower( 12 ); // If you use Integer (simpler) format Java will convert for
you
sphere.setSpecularPower(12.0d); // You can also use the 12.0d (double) required numeric format


Figure 13-5. Run your project to see the PhongShader object configured to use a Color.YELLOW specularColor
property

Free download pdf