Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

© Wallace Jackson 2017 315
W. Jackson, Pro Java 9 Games Development, https://doi.org/10.1007/978-1-4842-0973-8_13


CHAPTER 13


3D Model Shader Creation: Using


the JavaFX 9 PhongMaterial Class


Now that you have learned about the 3D assets, called primitives, that are included in the JavaFX API, let’s start
to get some foundational knowledge about how to “dress up” those 3D assets using 2D image assets, which we
will turn into materials that we can apply to the 3D surface using a shader. JavaFX supports the Phong shader,
which contains several channels that accept special images called texture maps that apply different effects such
as coloration, illumination, surface bumps, surface shininess, and so forth. JavaFX comes with two core shader
classes in the javafx.scene.paint package in the javafx.graphics module that specifically “shade” or surface 3D
geometry (primitives or mesh) for you, and we are going to take a look at them during this chapter. We will also
look at how to use GIMP 2.8.22 to create texture maps rapidly and accurately based on pixels and mathematics so
they provide an accurate texture mapping result. We will also get back into our JavaFXGame primary application
class coding and start to add Phong shader materials to 3D primitives to get some practice. You could do this in
3D software packages like Blender, but a board game is simple enough (squares, spheres, cylinders) that we can
do this using only JavaFX code. This means we do not need to import (and distribute) 3D models but can rather
write code to model your i3D game “out of thin air.” This will also teach you a lot more about the 3D APIs in Java 9
and JavaFX, as you will learn how to model complex 3D objects using only Java 9 and its JavaFX APIs.
During this chapter, you will be learning about the JavaFX 3D shader class hierarchy, which is contained
in the javafx.scene.paint package. In Java 9 and Android 8, the Paint class applies pixel colors and attributes
to the Canvas and in this case the surface of 3D primitives. The paint package contains classes that are
related to this “skinning,” or texture mapping objective. You will cover Material, a superclass that holds the
top-level shader definition, and the PhongMaterial class, which can be used to create a texture map, or
“skin,” for 3D primitives (covered in Chapter 12 ).


JavaFX Material Superclass: i3D Shader Properties


The public abstract Material superclass is used to create the PhongMaterial class that you will use to create
a material attribute used by the Shape3D subclasses for the i3D primitives that you use in your pro Java 9
games design and development. The advanced models that you import from external 3D software packages
will already have materials (sometimes called shaders) and texture maps applied to them in the 3D software
production environment, and after import, they will be in Mesh objects displayed using MeshView objects,
so you will not always directly use the PhongMaterial class at this low level to shade advanced 3D objects in
most practical applications. The Material superclass is even more of an empty shell than Mesh, as it has only
one empty constructor and no properties or methods! The Material class is part of the javafx.scene.paint
package and has the following Java class hierarchy:


java.lang.Object



javafx.scene.paint.Material


Free download pdf