📅  最后修改于: 2020-10-14 05:30:49             🧑  作者: Mango
JavaFX允许我们设置3D对象的各种属性。 JavaFX中为每个属性定义了特殊的类。在本教程的这一部分中,我们将讨论可以应用于3D对象以获得更好外观的属性类型。
剔除可以简单地定义为删除3D对象在视图区域中不可见的部分的过程。在JavaFX中,3D形状包含CullFace类型的属性。可以使用下面的实例方法将此属性设置为适当的值,该实例方法将在3D形状类对象上调用。 。
setCullFace(CullFace值)
在CullFace类中定义了三种笔触类型:
在下面的示例中,我们创建了三个球体,并为每个球体设置了三个剔除面。
package application;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.SubScene;
import javafx.scene.shape.CullFace;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;
public class SphereTest extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
//Creating the first sphere
Sphere sphere = new Sphere();
sphere.setRadius(50);
sphere.setTranslateX(100);
sphere.setTranslateY(200);
//Setting cullface for the sphere 1
sphere.setCullFace(CullFace.BACK);
//creating second sphere
Sphere sphere1 = new Sphere();
sphere1.setRadius(50);
sphere1.setTranslateX(250);
sphere1.setTranslateY(200);
//setting CullFace for the sphere 2
sphere1.setCullFace(CullFace.FRONT);
//creating third sphere
Sphere sphere2 = new Sphere();
sphere2.setRadius(50);
sphere2.setTranslateX(400);
sphere2.setTranslateY(200);
//setting CullFace for the sphere 3
sphere2.setCullFace(CullFace.NONE);
//Configuring Group and Scene
Group root = new Group();
root.getChildren().addAll(sphere,sphere1,sphere2);
Scene scene = new Scene(root,500,400);
primaryStage.setScene(scene);
primaryStage.setTitle("CullFaces");
primaryStage.show();
}
publicstaticvoid main(String[] args) {
launch(args);
}
}
JavaFX提供了可以应用于3D形状表面的各种材料。包javafx.scene.paint的Material类提供了处理3D形状材料的所有类。 PhongMaterial类是Material类的子类。我们只需要实例化此类即可创建所需的材料。
此类提供了各种类型的材料作为此类的属性,可以使用setter方法将其设置为特定值。但是,可以使用以下方法将材料应用于形状。
shape.setMaterial()
下表描述了类javafx.scene.paint.PhongMaterial的属性及其设置方法。
Property | Description | Setter Methods |
---|---|---|
bumpMap | This property is of the type Image. This represents the Bump Map of the material. | setBumpMap(Image URL) |
diffuseColor | This is a color type property. This represents the diffuse color of the material. | setDiffuseColor(Color color) |
diffuseMap | This is an Image type property. This represents the DiffuseMap of the material. | setDiffuseMap(Image URL) |
selfIlluminationMap | This is an Image type property. This represents the selfillumination map of the material | setSelfIlluminationMap(Image URL) |
specularColor | This is a color type property. This represents the specular color of the material. | setSpecularColor(Color value) |
specularPower | This is a double type property. This represents the specular power of the Material. | setSpecularPower(Double value) |
package application;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Cylinder;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;
public class MaterialExample extends Application{
@Override
publicvoid start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
//Creating Cylinder 1
Cylinder cyn = new Cylinder();
cyn.setRadius(70);
cyn.setHeight(200);
cyn.setTranslateX(180);
cyn.setTranslateY(150);
//Creating cylinder 2
Cylinder cyn1=new Cylinder();
cyn1.setRadius(70);
cyn1.setHeight(200);
cyn1.setTranslateX(380);
cyn1.setTranslateY(150);
//Creating cylinder 3
Cylinder cyn2=new Cylinder();
cyn2.setRadius(70);
cyn2.setHeight(200);
cyn2.setTranslateX(580);
cyn2.setTranslateY(150);
//Creating Sphere
Sphere s=new Sphere();
s.setRadius(70);
s.setTranslateX(780);
s.setTranslateY(150);
//Setting Material 1
Image img1 = new Image("https://www.javatpoint.com/kotlin/images/kotlin-tutorial.png");
PhongMaterial mat = new PhongMaterial();
mat.setDiffuseMap(img1);
mat.setDiffuseColor(Color.WHITE);
//Setting Material 2
PhongMaterial mat2 = new PhongMaterial();
mat2.setSelfIlluminationMap(img1);
//Setting Material 3
PhongMaterial mat3 = new PhongMaterial();
mat3.setSpecularMap(img1);
//Setting Material for sphere
PhongMaterial mat4 = new PhongMaterial();
mat4.setDiffuseColor(Color.BLUE);
// Applying Material to mat4.setBumpMap(img1);the shape
cyn.setMaterial(mat);
cyn1.setMaterial(mat2);
cyn2.setMaterial(mat3);
s.setMaterial(mat4);
//Setting Camera mat4.setBumpMap(img1);
PerspectiveCamera camera = new PerspectiveCamera();
camera.setTranslateX(20);
camera.setTranslateY(10);
camera.setTranslateZ(50);
//setting group and stage
Group root = new Group();
root.getChildren().addAll(cyn,cyn1,cyn2,s);
Scene scene = new Scene(root,900,300,Color.WHITE);
scene.setCamera(camera);
primaryStage.setScene(scene);
primaryStage.setTitle("Material Example");
primaryStage.show();
}
publicstaticvoid main(String[] args) {
launch(args);
}
}
JavaFX提供了各种绘制模式来绘制3D形状。此属性的类型为DrawMode。在类中定义了以下DrawMode类型。
使用以下setter方法为3D形状设置绘制模式。
Shape3D.setDrawMode()
在下面的示例中,我们创建了两个框,一个是使用DrawMode Fill,另一个是使用DrawMode Line。
package application;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.shape.Box;
import javafx.scene.shape.DrawMode;
import javafx.stage.Stage;
public class DrawModeExample extends Application{
@Override
publicvoid start(Stage primaryStage) throws Exception {
// TODO Auto-generated method stub
//Creating Boxes
Box box1 = new Box();
Box box2 = new Box();
//Setting properties for second box
box2.setTranslateX(450);
box2.setTranslateY(300);
box2.setTranslateZ(100);
box2.setHeight(150);
box2.setWidth(50);
box2.setDepth(400);
//Setting properties for first box
box1.setHeight(100);
box1.setWidth(100);
box1.setDepth(400);
box1.setTranslateX(200);
box1.setTranslateY(200);
box1.setTranslateZ(200);
//Setting DrawModes for the boxes
box1.setDrawMode(DrawMode.FILL);
box2.setDrawMode(DrawMode.LINE);
//Setting the perspective camera
PerspectiveCamera camera = new PerspectiveCamera();
camera.setTranslateX(100);
camera.setTranslateY(100);
camera.setTranslateZ(50);
//Configuring Group, Scene and Stage
Group root = new Group();
root.getChildren().addAll(box1,box2);
Scene scene = new Scene(root,450,350);
scene.setCamera(camera);
primaryStage.setScene(scene);
primaryStage.setTitle("Setting DrawMode");
primaryStage.show();
}
publicstaticvoid main(String[] args) {
launch(args);
}
}
在JavaFX中,相机被视为可以在3D布局中移动并相应地更改“场景”视图的对象。类javafx.scene.Camera表示相机。但是,它与2D空间不同,在2D空间中,我们不需要在屏幕上移动相机。
在JavaFX中,相机坐标系的X坐标指向右侧,Y坐标指向下方,Z坐标指向远离查看者或进入屏幕的位置。
为了创建相机并将其添加到场景中,我们必须使用以下代码行。
//创建javafx.scene.Camera的实例
Camera camera = new Camera();
//将相机沿正确的X方向设置为50
camera.setTranslateX(50);
camera.setTranslateX(50);
camera.setTranslateY(100);
//将相机设置为20进入屏幕
camera.setTranslateZ(20);
//将相机添加到场景
scene.setCamera(camera);
子场景节点可以定义为一个容器,可以容纳场景中需要为其指定摄像机角度的某个节点。用于场景分离。如果需要为场景的一部分设置不同的摄像机,则可以使用子场景节点。可以在以下情况之一中使用Subscene。
使用以下语法实例化一个子场景。
SubScene = new SubScene(Parent root, double width, double height, boolean depthbuffer, boolean antiAliasing)