1 Overview

This chapter provides an overview of the JavaFX 3D graphics features currently available through the Java 8 APIs for JavaFX.

The JavaFX 3D graphics APIs provide a general purpose three-dimensional graphics library for the JavaFX platform. You can use 3D geometry, cameras, and lights to create, display, and manipulate objects in 3D space.

Sample of 3D Graphics Use Cases

Figure 1-1 shows a snapshot of the JavaFX 3D application example that was demonstrated at the JavaOne 2012 keynote session. It was built as a proof of concept on an early prototype of JavaFX SDK with added 3D Mesh, Camera, and Lighting support. You can view it at http://www.youtube.com/embed/AS26gZrYNy8?rel=0 web site.

3D Feature in JavaFX 2.x Releases

In the JavaFX 2.x releases, it is possible to create two-dimensional objects and transform them in 3D space. You can subclass the Group class to create your own custom group and set the transform sub-matrices to anything you want. You are able to simulate the behavior of transform groups of other 3D content concentration packages, such as Maya and 3D Studio Max, because you can customize which sub-matrices are part of that transform group. See Applying Transformations in JavaFX to learn more about this transformation feature.

Example 1-1 shows a sample code that creates a Group subclass, Xform, that has a translation, a pivot, three rotations, a scale, and the inverse pivot.

The Xform subclass is created from Group because groups are originally designed for two-dimensional (2D) UI layout. The pivot of a node is recalculated under certain conditions for 2D UI layout, but if you subclass group and create Xform, as shown in Example 1-1 and use those new transforms, it bypasses the 2D UI layout.

Although, 2D UI pivot recalculation is very desirable for UI controls in a 2D layout, it is not something you would want in a 3D layout. The pivot point is recomputed as the center of the Node's layout bounds and so any change to the layout bounds will cause the pivot point to change, which ends up automatically moving your object. So, for a Group node, any change to its children, including position, geometry, effect, orientation, or scale, will cause the group's layout bounds to change. This will automatically move the object in unintended ways, when it comes to 3D layout, but in desirable ways when it comes to 2D. So, in a 3D layout, you definitely want to bypass the automatic pivot recomputation.

Some of the useful 3D Transform methods on Node are listed in Example 1-2.