Pro Java 9 Games Development Leveraging the JavaFX APIs

(Michael S) #1
Chapter 12 ■ 3D MoDel Design anD priMitives: Using JavaFX 9 shape3D Classes

A better workflow is to use an external 3D software package and import your 3D object directly into a
Mesh object, which is then referenced by a MeshView object. This is a much faster way to get an advanced
i3D game up and running quickly and efficiently, as well as a way to bring specialized artisans into an i3D
game development workflow.


JavaFX Mesh Superclass: Your Raw 3D Model Data Container


The public abstract Mesh superclass is deceptively simple with only a Mesh() constructor method and
a TriangleMesh subclass (for loading it with mesh data using Java code), so we’ll cover it here first. It is
essentially an object used to contain 3D data and is contained in the javafx.scene.shape package with the
other 3D model-centric classes. The Java class hierarchy looks like the following, as the Mesh class was
scratch-coded to be a Java class holding a representation of a 3D Mesh:


java.lang.Object



javafx.scene.shape.Mesh



This is a base class for representing complex 3D geometric surfaces that are not JavaFX Shape3D
primitives. Note that this is obviously a conditional feature as complex 3D geometry will require a
3D rendering pipeline to be in place to be useful to your pro Java 9 games development. Polling the
ConditionalFeature.SCENE3D will be necessary.
As stated initially, the constructor method is very basic and will look like the following Java code:


protected Mesh() // Protected Code Cannot Be Used Directly (but can be used by a subclass)


Next, let’s take a look at the MeshView class, which will reference, hold in memory, and display this
Mesh object in the 3D Scene using the rendering engine. This class is the “bridge” between the Mesh engine
and Shape3D.


JavaFX MeshView Class: Format and Present Your 3D Mesh Data


The public MeshView class is almost as simple as the Mesh class, with only two overloaded MeshView()
constructor methods and no subclasses, so I’ll cover it here next. It is a subclass of Shape3D and stored in
the javafx.scene.shape package. It implements the Styleable and EventTarget interfaces just like the three
primitives classes do. It is used to define a 3D surface using the raw 3D model data held in a Mesh object.
The Java class hierarchy for the MeshView class looks like the following, as a MeshView class needs to inherit
all of those key Shape3D rendering characteristics:


java.lang.Object



javafx.scene.Node
javafx.scene.shape.Shape3D
javafx.scene.shape.MeshView



The MeshView object has one ObjectProperty mesh property that specifies the 3D mesh data
for the MeshView, which it gets from the second overloaded constructor method parameter or using a
.getMesh(mesh) method call. This class (object) also inherits the core Shape3D properties from the class
javafx.scene.shape.Shape3D, which you have already covered (except for material) and which are cullFace,
drawMode, and material.

Free download pdf