JavaFX 简单3D示例

 从Java8开始,在JavaFX中便增加了3D部分的内容,包括Camera,Material,Light,Shape3D等基础内容。

 当然,JavaFX 3D应该是OpenJFX里目前正在补充和完善的一个模块,很多地方还不尽如人意,所以该示例仅供参考。另外,OpenJFX目前已经有人通过RovoVM运行在Android和IOS的设备上了。不过,个人认为这个只是小打小闹,还远远不能进入实际运用当中。

  下面是JavaFX 3D示例,我会逐一解释:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import javafx.application.Application;  
  2. import javafx.application.Platform;  
  3. import javafx.scene.Group;  
  4. import javafx.scene.Parent;  
  5. import javafx.scene.PerspectiveCamera;  
  6. import javafx.scene.Scene;  
  7. import javafx.scene.SceneAntialiasing;  
  8. import javafx.scene.SubScene;  
  9. import javafx.scene.paint.Color;  
  10. import javafx.scene.paint.PhongMaterial;  
  11. import javafx.scene.shape.Box;  
  12. import javafx.scene.shape.DrawMode;  
  13. import javafx.scene.transform.Rotate;  
  14. import javafx.scene.transform.Translate;  
  15. import javafx.stage.Stage;  
  16.   
  17.   
  18. public class Main extends Application {  
  19.     private Thread thread;  
  20.     private boolean isRunning = true;  
  21.     private PerspectiveCamera camera;  
  22.     private int speed = -1;  
  23.     private int count = 1;  
  24.     private int maxCount = 50;  
  25.     public Parent createContent() throws Exception {  
  26.         // Box  
  27.         Box testBox = new Box(555);  
  28.         testBox.setMaterial(new PhongMaterial(Color.BLUE));  
  29.         testBox.setDrawMode(DrawMode.FILL);  
  30.           
  31.         // Create and position camera  
  32.         camera = new PerspectiveCamera(true);  
  33.         camera.getTransforms().addAll (  
  34.                 new Rotate(-20, Rotate.Y_AXIS),  
  35.                 new Rotate(-20, Rotate.X_AXIS),  
  36.                 new Translate(00, -20));  
  37.    
  38.         // Build the Scene Graph  
  39.         Group root = new Group();         
  40.         root.getChildren().add(camera);  
  41.         root.getChildren().add(testBox);  
  42.    
  43.         // Use a SubScene         
  44.         SubScene subScene = new SubScene(root, 310,310true, SceneAntialiasing.BALANCED);  
  45.         subScene.setFill(Color.ALICEBLUE);  
  46.           
  47.         subScene.setCamera(camera);  
  48.         Group group = new Group();  
  49.         group.getChildren().add(subScene);  
  50.         return group;  
  51.     }  
  52.   
  53.     @Override  
  54.     public void start(Stage primaryStage) throws Exception {  
  55.         primaryStage.setResizable(false);  
  56.         Scene scene = new Scene(createContent(), 300300);  
  57.         thread = new Thread(new Runnable() {  
  58.             @Override  
  59.             public void run() {  
  60.                 while(isRunning){  
  61.                     try {  
  62.                         Thread.sleep(10);  
  63.                     } catch (InterruptedException e) {  
  64.                         e.printStackTrace();  
  65.                     }  
  66.                     Platform.runLater(new Runnable() {  
  67.                         @Override  
  68.                         public void run() {  
  69.                             camera.getTransforms().addAll(  
  70.                                     new Translate(00,speed));  
  71.                             count++;  
  72.                             if(count >= maxCount){  
  73.                                 speed = -speed;  
  74.                                 count = 0;  
  75.                             }  
  76.                         }  
  77.                     });  
  78.                 }  
  79.             }  
  80.         });  
  81.         thread.start();  
  82.         primaryStage.setScene(scene);  
  83.         primaryStage.show();  
  84.     }  
  85.       
  86.     public static void main(String[] args) {  
  87.         launch(args);  
  88.     }  
  89. }  
  PerspectiveCamera是透视投影的摄像机,基本是3D开发中的标配了。Box是JavaFX 3D中内置的3D物体,通过setMaterial来设置材质,通过setDrawMode来设置绘制方式,有填充和线框两种模式。

  我们也可以通过Camera.getTransforms()来获取所有Object的Transform然后进行Rotate,Translate等变换。

  SubScene是一个子场景,是一个特殊的独立场景。我们可以通过SubScene来通过不同的Camera来渲染场景中的某一部分。例如2D UI,3D场景,整个背景的分离显示,也是很常见的用法。另外,SubScene中可以通过SceneAntialiasing来设置是否抗锯齿。

  在该示例中,我们另外通过线程对Camera中的transform进行translate变换,会循环移近移远。

  效果图:


  因为是动态变化的,大家可以自己运行看看效果。

  抗锯齿的效果也很明显,可以自行修改。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值