解释 JavaFX 中 3D 形状的剔除面属性
一般来说,剔除是指移除形状中定向不当的部分(在视图区域中不可见)。
你可以使用 setCullFace() 方法(形状类)将值设置为 3d 对象的剔除面属性。
JavaFX 支持三种类型的剔除面类型,它们由名为 CullFace 的枚举的这三个常量表示,分别是NONE、FRONT、BACK。
示例
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.PerspectiveCamera; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.scene.shape.Box; import javafx.scene.shape.CullFace; import javafx.scene.text.Font; import javafx.scene.text.FontPosture; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; public class CullFaceProperty extends Application { public void start(Stage stage) { //Drawing a Box Box cube1 = new Box(100, 120, 100); cube1.setTranslateX(30); cube1.setTranslateY(180); cube1.setTranslateZ(150); //Setting the property "cull face" cube1.setCullFace(CullFace.FRONT); Box cube2 = new Box(100, 120, 100); cube2.setTranslateX(250); cube2.setTranslateY(180); cube2.setTranslateZ(150); //Setting the property "cull face" cube2.setCullFace(CullFace.BACK); Box cube3 = new Box(100, 120, 100); cube3.setTranslateX(480); cube3.setTranslateY(180); cube3.setTranslateZ(150); //Setting the property "cull face" cube3.setCullFace(CullFace.NONE); //Setting the perspective camera PerspectiveCamera cam = new PerspectiveCamera(); cam.setTranslateX(-50); cam.setTranslateY(50); cam.setTranslateZ(0); Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 15); Text label1 = new Text("CullFace: front"); label1.setFont(font); label1.setX(10); label1.setY(270); Text label2 = new Text("CullFace: back"); label2.setFont(font); label2.setX(180); label2.setY(270); Text label3 = new Text("CullFace: none"); label3.setFont(font); label3.setX(370); label3.setY(270); //Setting the Scene Group root = new Group(cube1, cube2, cube3, label1, label2, label3); Scene scene = new Scene(root, 595, 300, Color.BEIGE); scene.setCamera(cam); stage.setTitle("3D Shape Property: Cull Face"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
输出
广告