JavaFX - 剔除面屬性



JavaFX 還為 3D 物件提供了各種屬性。這些屬性的範圍可以從決定形狀的材質:內部和外部,渲染 3D 物件幾何體以及剔除 3D 形狀的面。

提供所有這些屬性是為了改進 3D 物件的外觀和感覺;並檢查什麼適合應用程式並應用它們。

在本章中,讓我們進一步瞭解剔除面屬性。

剔除面屬性

通常,剔除是指移除形狀方向錯誤的部分(在檢視區域中不可見的部分)。

剔除面屬性的型別為CullFace,它表示 3D 形狀的剔除面。您可以使用setCullFace()方法設定形狀的剔除面,如下所示:

box.setCullFace(CullFace.NONE);

形狀的筆觸型別可以是:

  • None - 不執行剔除 (CullFace.NONE)。

  • Front - 剔除所有正面多邊形 (CullFace.FRONT)。

  • Back - 剔除所有背面多邊形 (StrokeType.BACK)。

預設情況下,三維形狀的剔除面為 Back。

示例

以下程式是一個示例,演示了球體的各種剔除面。將此程式碼儲存在名為SphereCullFace.java的檔案中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.shape.CullFace; 
import javafx.stage.Stage; 
import javafx.scene.shape.Sphere; 
         
public class SphereCullFace extends Application { 
   @Override 
   public void start(Stage stage) { 
      //Drawing Sphere1 
      Sphere sphere1 = new Sphere();
      
      //Setting the radius of the Sphere 
      sphere1.setRadius(50.0);   
      
      //Setting the position of the sphere 
      sphere1.setTranslateX(100); 
      sphere1.setTranslateY(150); 
      
      //setting the cull face of the sphere to front 
      sphere1.setCullFace(CullFace.FRONT); 
       
      //Drawing Sphere2 
      Sphere sphere2 = new Sphere(); 
      
      //Setting the radius of the Sphere 
      sphere2.setRadius(50.0);   
      
      //Setting the position of the sphere 
      sphere2.setTranslateX(300);  
      sphere2.setTranslateY(150); 
      
      //Setting the cull face of the sphere to back 
      sphere2.setCullFace(CullFace.BACK);          
       
      //Creating a Group object  
      Group root = new Group(sphere1, sphere2); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage
      stage.setTitle("Drawing a Sphere"); 
         
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of the stage 
      stage.show(); 
   } 
   public static void main(String args[]){ 
      launch(args); 
   } 
}

使用以下命令從命令提示符編譯並執行儲存的 Java 檔案。

javac --module-path %PATH_TO_FX% --add-modules javafx.controls SphereCullFace.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls SphereCullFace 

輸出

執行後,上述程式將生成一個 JavaFX 視窗,顯示兩個球體,分別具有FRONTBACK的剔除面值,如下所示:

Cull Faces

示例

以下程式是一個示例,演示了在長方體上使用 NONE 屬性時不應用剔除面屬性的情況。將此程式碼儲存在名為BoxCullFace.java的檔案中。

import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.shape.CullFace; 
import javafx.stage.Stage; 
import javafx.scene.shape.Box; 
         
public class BoxCullFace extends Application { 
   @Override 
   public void start(Stage stage) { 
      //Drawing Box1 
      Box box1 = new Box();
      
      //Setting the dimensions of the Box 
      box1.setWidth(100.0); 
      box1.setHeight(100.0);   
      box1.setDepth(100.0);  
      
      //Setting the position of the box 
      box1.setTranslateX(100); 
      box1.setTranslateY(150);
      box1.setTranslateZ(0);
      
      //setting the cull face of the box to NONE 
      box1.setCullFace(CullFace.NONE); 
          
       
      //Creating a Group object  
      Group root = new Group(box1); 
         
      //Creating a scene object 
      Scene scene = new Scene(root, 600, 300);  
      
      //Setting title to the Stage
      stage.setTitle("Drawing a Box"); 
         
      //Adding scene to the stage 
      stage.setScene(scene); 
         
      //Displaying the contents of the stage 
      stage.show(); 
   } 
   public static void main(String args[]){ 
      launch(args); 
   } 
}

使用以下命令從命令提示符編譯並執行儲存的 Java 檔案。

javac --module-path %PATH_TO_FX% --add-modules javafx.controls BoxCullFace.java 
java --module-path %PATH_TO_FX% --add-modules javafx.controls BoxCullFace 

輸出

執行後,上述程式將生成一個 JavaFX 視窗,顯示一個長方體,其剔除面值為NONE,如下所示:

Cull Faces
廣告