解釋 JavaFX 中 3D 形狀的材質面屬性
此材質屬性指定 3D 物件應覆蓋的材質型別。您可以使用 **setMaterial()** 方法將值設定為此屬性。
您需要傳遞一個 Material 型別的物件。**javafx.scene.paint** 包中的 **PhongMaterial** 類是此類的子類,並提供 7 個表示 Phong 著色材質的屬性。您可以使用這些屬性的 setter 方法將所有這些型別的材質應用於 3D 形狀的表面。以下是 JavaFX 中可用的材質型別:-
**bumpMap** - 這表示儲存為 RGB 影像的法線貼圖。
**diffuseMap** - 這表示漫反射貼圖。
**selfIlluminationMap** - 這表示此 PhongMaterial 的自發光貼圖。
**specularMap** - 這表示此 PhongMaterial 的鏡面反射貼圖。
**diffuseColor** - 這表示此 PhongMaterial 的漫反射顏色。
**specularColor** - 這表示此 PhongMaterial 的鏡面反射顏色。
**specularPower** - 這表示此 PhongMaterial 的鏡面反射強度。
示例
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.stage.Stage; import javafx.scene.shape.Cylinder; import javafx.scene.text.Font; import javafx.scene.text.FontPosture; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; public class MaterialProperty extends Application { public void start(Stage stage) { Cylinder cylinder1 = new Cylinder(50, 150); //Setting the properties of the cylinder cylinder1.setTranslateX(30.0); cylinder1.setTranslateY(180.0); cylinder1.setTranslateZ(150.0); //Setting the cull face "material" Image map = new Image("https://tutorialspoint.tw/images/tp-logo.gif"); PhongMaterial material1 = new PhongMaterial(); material1.setSelfIlluminationMap(map); cylinder1.setMaterial(material1); Cylinder cylinder2 = new Cylinder(50, 150); //Setting the properties of the cylinder cylinder2.setTranslateX(250.0); cylinder2.setTranslateY(180.0); cylinder2.setTranslateZ(150.0); //Setting the cull face "material" PhongMaterial material2 = new PhongMaterial(); material2.setDiffuseColor(Color.DARKRED); cylinder2.setMaterial(material2); Cylinder cylinder3 = new Cylinder(50, 150); //Setting the properties of the cylinder cylinder3.setTranslateX(480.0); cylinder3.setTranslateY(180.0); cylinder3.setTranslateZ(150.0); //Setting the cull face "material" PhongMaterial material3 = new PhongMaterial(); material3.setBumpMap(map); cylinder3.setMaterial(material3); //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("Self Illumination Map"); label1.setFont(font); label1.setX(0); label1.setY(270); Text label2 = new Text("Diffuse Color "); label2.setFont(font); label2.setX(200); label2.setY(270); Text label3 = new Text("Bump Map"); label3.setFont(font); label3.setX(390); label3.setY(270); //Setting the Scene Group root = new Group(cylinder1, cylinder2, cylinder3, label1, label2, label3); Scene scene = new Scene(root, 595, 300, Color.BEIGE); scene.setCamera(cam); stage.setTitle("3D Shape Property: Material"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
輸出
廣告