Explain the cull face property of 3D shapes in JavaFX


In general, culling is the removal of improperly oriented parts of a shape (which are not visible in the view area).

You can set the value to the cull face property of a 3d object using the setCullFace() method (Shape class).

JavaFX supports three kinds of cull face types represented by three constants of the Enum named CullFace namely, NONE, FRONT, BACK.

Example

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);
   }
}

Output

Updated on: 16-May-2020

230 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements