本章给出一些Shape3D API的信息
有两种类型的3D图形
已定义的形状
自定义的形状
已定义的形状
如下图:
下面的代码展示了Shape3D类的层次结构,包含了MeshView类,定义了3D网格数据,也包含了BoxCylinder和Sphere类
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
我们可以用下面的代码闯将已定义的图形
Box myBox = new Box(width, height, depth);
Cylinder myCylinder = new Cylinder(radius, height);
Cylinder myCylinder2 = new Cylinder(radius, height, divisions);
Sphere mySphere = new Sphere(radius);
Sphere mySphere2 = new Sphere(radius, divisions);
下面是创建已定义图形的实例
final PhongMaterial redMaterial = new PhongMaterial();
redMaterial.setSpecularColor(Color.ORANGE);
redMaterial.setDiffuseColor(Color.RED);
final PhongMaterial blueMaterial = new PhongMaterial();
blueMaterial.setDiffuseColor(Color.BLUE);
blueMaterial.setSpecularColor(Color.LIGHTBLUE);
final PhongMaterial greyMaterial = new PhongMaterial();
greyMaterial.setDiffuseColor(Color.DARKGREY);
greyMaterial.setSpecularColor(Color.GREY);
final Box red = new Box(400, 400, 400);
red.setMaterial(redMaterial);
final Sphere blue = new Sphere(200);
blue.setMaterial(blueMaterial);
final Cylinder grey = new Cylinder(5, 100);
grey.setMaterial(greyMaterial);
自定义图形
下面的代码显示了JavaFX Mesh 类的层次结构,包含了TriangleMesh,这个类在3D布局中的应用是十分典型的。
java.lang.Object
javafx.scene.shape.Mesh (abstract)
javafx.scene.shape.TriangleMesh
TriangleMesh包含独立的点阵列,纹理坐标,面临描述一个三角几何网格。平滑组用于组三角形同一曲面的一部分。三角形的不同平滑组形成硬边。
1:创建一个TriangleMesh实例
mesh = new TriangleMesh();
2:定义网格的顶点
float points[] = { … };
mesh.getPoints().addAll(points);
3:描述每个顶点的纹理坐标
float texCoords[] = { … };
mesh.getTexCoords().addAll(texCoords);
4:使用这些顶点构建,构建描述拓扑的三角形面
int faces[] = { … };
mesh.getFaces().addAll(faces);
5:定义面所属的平滑组
int smoothingGroups[] = { … };
mesh.getFaceSmoothingGroups().addAll(smoothingGroups);
平滑组调整正常的顶点脸上是光滑或面。如果每一个面临不同的平滑组,那么网将在上雕琢平面的。如果每一个面对平滑组相同,那么网将看起来非常光滑。