Starting with Java8, the content of 3D is added to JavaFX, including basic content such as Camera,material,light,shape3d.
Of course, JavaFX 3D should be a module that is currently being supplemented and perfected in openjfx, and many places are not satisfactory, so this example is for reference only. In addition, OPENJFX is currently running on Android and iOS devices through ROVOVM. However, the individual thinks that this is only a short-chipping, still far from the actual application.
Here is the JavaFX 3D example, which I'll explain in one of the following:
Import Javafx.application.application;import Javafx.application.platform;import Javafx.scene.group;import Javafx.scene.parent;import Javafx.scene.perspectivecamera;import Javafx.scene.scene;import Javafx.scene.sceneantialiasing;import Javafx.scene.subscene;import Javafx.scene.paint.color;import Javafx.scene.paint.phongmaterial;import Javafx.scene.shape.box;import Javafx.scene.shape.drawmode;import Javafx.scene.transform.rotate;import Javafx.scene.transform.translate;import Javafx.stage.Stage;public class Main Extends application {Private Thread thread;private boolean isrunning = true;private perspectivecamera camera;private int S peed = -1;private int count = 1;private int maxCount = 50;public Parent createcontent () throws Exception {//Box Box Testbox = new Box (5, 5, 5); Testbox.setmaterial (New Phongmaterial (Color.Blue)); Testbox.setdrawmode (Drawmode.fill); Create and Position camera camera = new Perspectivecamera (true); CameRa.gettransforms (). AddAll (New Rotate ( -20, Rotate.y_axis), New Rotate ( -20, Rotate.x_axis), New Translate (0, 0,-20)); Build the Scene Graph Group root = new Group (); Root.getchildren (). Add (camera); Root.getchildren (). Add (Testbox); Use a subscene subscene subscene = new Subscene (root, 310,310, true, sceneantialiasing.balanced); Subscene.setfill (Color.aliceblue); Subscene.setcamera (camera); Group Group = new Group (); Group.getchildren (). Add (Subscene); return group; } @Override public void start (Stage primarystage) throws Exception {primarystage.setresizable (false); Scene scene = new Scene (Createcontent (), 300, 300); thread = new Thread (new Runnable () {@Overridepublic void run () {while (isrunning) {try {Thread.Sleep (Ten)} catch (Interr Uptedexception e) {e.printstacktrace ();} Platform.runlater (New Runnable() {@Overridepublic void run () {camera.gettransforms (). AddAll (New Translate (0, 0,speed)); count++; if (count >= maxCount) {speed =-speed; Count = 0; }}}); }}}); Thread.Start (); Primarystage.setscene (Scene); Primarystage.show (); }public static void Main (string[] args) {launch (args);}}
Perspectivecamera is a camera for perspective projection and is basically standard in 3D development. Box is a 3D object built into JavaFX 3D, with setmaterial to set the material, and Setdrawmode to set the drawing style, with fill and wireframe modes.
We can also use camera.gettransforms () to get all the transform of an object and then perform transformations such as rotate,translate.
Subscene is a sub-scene and is a special stand-alone scene. We can use Subscene to render a part of the scene through different camera. For example, the 2D ui,3d scene, the separation of the entire background display, is also a very common use. In addition,Subscene can be sceneantialiasing to set whether antialiasing is possible.
In this example, we also translate transform the transform in the camera by thread, which moves closer to the distance.
:
Because it is dynamic change, you can run your own to see the effect.
The anti-aliasing effect is also obvious and can be modified by itself.
This article is personal original, all rights reserved, reprint please indicate source:http://blog.csdn.net/ml3947. In addition to my personal blog:http://www.wjfxgame.com.
JavaFX Simple 3D Example