How to fill a JavaFX Sphere with two Colors

后端 未结 2 1925
礼貌的吻别
礼貌的吻别 2021-01-23 05:46

How can I fill in JavaFX a 3D Sphere with a linear gradient like a 2d Circle? I work with the JavaFX Scene Builder.

2条回答
  •  情书的邮戳
    2021-01-23 06:02

    The way to achieve gradient-like effects on 3D shapes is by applying lighting material and lighting position. You can't simply apply two colours that gradually transform into each other. I cooked for you a small app that shows just how to achieve this.

    public class ShadedSphere extends Application {
      public void start(Stage stage) {
        StackPane layout = new StackPane();
        layout.setPrefSize(300, 300);
    
        Scene scene = new Scene(layout);
        createScene(scene);
    
        stage.setScene(scene);
        stage.show();
      }
    
      private void createScene(Scene scene) {
        PhongMaterial material = new PhongMaterial();
        material.setDiffuseColor(Color.ORANGE);
        material.setSpecularColor(Color.BLACK);
    
        Sphere sphere = new Sphere(100);
        sphere.setMaterial(material);
    
        Pane root = (Pane) scene.getRoot();
        root.getChildren().add(sphere);
      }
    
      public static void main(String[] args) {
        launch(args);
      }
    }
    

    Which will give you this: enter image description here

    If you change the location of the sphere (e.g., using setTranslateX() and same for Y and Z), you should notice different effects of lighting on it; so the next thing for you to grasp is how to control location of lighting fixtures. Also, lights can have colour! Which means you can achieve even Northern Lights effects if you want to see cool stuff.

    To learn a bit more about lighting, camera and effects, see this link.

提交回复
热议问题