Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1

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


CHAPTER 12


3D Model Design and Primitives:


Using JavaFX 9 Shape3D Classes


Now that you have finished setting up a basic (empty) 3D Scene by adding a Camera object to the Scene
root and a PointLight object specifically designed to work with 3D assets, let’s start to get some foundational
knowledge about the 3D assets themselves. These assets come in the form of predefined basic 3D shapes,
called primitives, as well as more custom 3D geometry assets commonly referred to in the industry as mesh
or wireframe 3D assets. JavaFX 9 comes with seven classes in the javafx.scene.shape package in the javafx.
graphics module that specifically create 3D geometry (primitives or mesh) for you, and we are going to take
a look at them during this chapter. We will also get back into our JavaFXGame primary application class
coding here in Chapter 12 and start to add 3D primitives to the gameBoard Group Node of our SceneGraph
to get some practice adding 3D assets to our JavaFXGame application. Whereas we could do this in a 3D
software package such as Blender, a board game is simple enough (squares, spheres, cylinders) that we can
do this entirely in JavaFX code, which 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 9, as you can learn how to model complex objects (such as your board game’s
gameboard) using only the latest Java and JavaFX APIs.
During this chapter, you will be learning about the different types of JavaFX 3D classes contained in
the javafx.scene.shape package. We will cover Sphere, which can be used to create a Sphere primitive and
which you have used already to test your 3D Scene setup in Chapter 11. We will also look at the other two
primitive classes, Box and Cylinder, which can be used to create your Plane and Disk primitives. These
primitives are based on the Shape3D superclass, which we will be looking at first. We will also look at the
more advanced TriangleMesh class that allows you to build a polygon-based Mesh object and, finally, at
the Mesh and MeshView class hierarchy, which will allow you to render the 3D Mesh objects that you’ll
have created in external 3D modeling and rendering software packages such as Blender 2.8 (open source) or
Autodesk 3D Studio Max (a paid software package).


JavaFX Shape3D Superclass: Primitive or MeshView


The public abstract Shape3D superclass is used to create the four primary 3D classes: Box, Sphere,
Cylinder, and MeshView. You’ll use these classes to create and display the 3D assets for your pro Java 9
games development. Three of these subclasses create primitives, which are predefined 3D objects created
algorithmically, and the MeshView subclass allows more detailed complex 3D models based on polygonal
geometry to be rendered inside your 3D Scene. It is important to note that there is also a javafx.scene.shape.
Shape superclass that is not related (class hierarchy wise) to javafx.scene.shape.Shape3D; it is used for 2D
shapes like those commonly found in the SVG 2D digital illustration language, which is covered in Beginning
Java 8 Games Development (Apress, 2014) and Digital Illustration Fundamentals (Apress, 2016).

Free download pdf