Getting Started with JavaFX 3D Graphics

Previous
Next

Beta Draft: 2013-09-17

2 3D Shapes

This chapter gives information about the Shapes3D API that is available with the JavaFX 3D Graphics library.

There are two types of 3D shapes:

Pre-defined Shapes

The pre-defined 3D shapes are provided to make it easier for you to quickly create 3D objects out-of-the-box. They include boxes, cylinders, and spheres, whose sample usages are shown in Figure 2-1.

Figure 2-1 Pre-defined Shapes

Description of Figure 2-1 follows
Description of "Figure 2-1 Pre-defined Shapes"

Example 2-1 shows the Shape3D class hierarchy. It includes the MeshView class, which defines a surface with the specified 3D mesh data. Also included are the Box, Cylinder, and Sphere subclasses.

Example 2-1 Shape3D Class Hierarchy

java.lang.Object
  javafx.scene.Node
     javafx.scene.shape.Shape3D
        javafx.scene.shape.MeshView
        javafx.scene.shape.Box
        javafx.scene.shape.Cylinder
        javafx.scene.shape.Sphere

Use the following information to create the pre-defined shapes:

  • To create a Box, specify the dimensions of width, height, and depth.

    Box myBox = new Box(width, height, depth);
    
  • To create a cylinder, specify the radius and height.

    Cylinder myCylinder = new Cylinder(radius, height);
    Cylinder myCylinder2 = new Cylinder(radius, height, divisions);
    
  • To create a sphere, specify the radius.

    Sphere mySphere = new Sphere(radius);
    Sphere mySphere2 = new Sphere(radius, divisions);
    

User-defined Shapes

Example 2-2 shows the JavaFX Mesh class hierarchy, which contains the TriangleMesh subclass. Triangle mesh is the most typical kind of mesh used in 3D layouts.

Example 2-2 Mesh Class Hierarchy

java.lang.Object
   javafx.scene.shape.Mesh (abstract)
   javafx.scene.shape.TriangleMesh

The TriangleMesh contains separate arrays of points, texture coordinates, and faces that describe a triangulated geometric mesh. Use the following steps to create a TriangleMesh instance:

  1. Create a new instance of a triangleMesh.

    mesh = new TriangleMesh();
    
  2. Define the set of points, which are the vertices of the mesh.

    float points[] = { … };
    mesh.getPoints().addAll(points);
    
  3. Describe the texture coordinates for each vertex.

    float texCoords[] = { … };
    mesh.getTexCoords().addAll(texCoords);
    
  4. Using the vertices, build the Faces, which are triangles that describe the topology.

    int faces[] = { … };
    mesh.getFaces().addAll(faces);
    
  5. Define the smoothing group to which each face belongs.

    int smoothingGroups[] = { … };
    mesh.getFaceSmoothingGroups().addAll(smoothingGroups);
    

    Smoothing group adjusts the normal on the vertices for the face to either be smooth or faceted. If every single face has a differing smoothing group, then the mesh will be very faceted. If every single face has the same smoothing group, then the mesh will look very smooth.

Previous
Next