Advertisement
MysteriousWolf

CubeComposer.java

Feb 23rd, 2018 (edited)
877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.96 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.embed.swing.SwingFXUtils;
  3. import javafx.scene.*;
  4. import javafx.scene.Cursor;
  5. import javafx.scene.image.Image;
  6. import javafx.scene.input.MouseButton;
  7. import javafx.scene.paint.Color;
  8. import javafx.scene.paint.PhongMaterial;
  9. import javafx.scene.shape.Box;
  10. import javafx.scene.shape.Cylinder;
  11. import javafx.scene.transform.Rotate;
  12. import javafx.scene.transform.Translate;
  13. import javafx.stage.Stage;
  14.  
  15. import javax.imageio.ImageIO;
  16. import java.awt.*;
  17. import java.awt.image.BufferedImage;
  18. import java.io.File;
  19. import java.io.IOException;
  20.  
  21. public class CubeComposer extends Application {
  22.  
  23.     //IMPORTANT: 16 units = 1 block
  24.  
  25.     private static final double BLOCK_UNIT = 16;
  26.  
  27.     private static final int PIXEL_PER_UNIT = 50; //used for increasing the texture quality
  28.  
  29.     private static final double MAJOR_LINE_THICCNESS = 0.1;
  30.     private static final double MINOR_LINE_THICCNESS = 0.03;
  31.  
  32.     private static final double GRID_SIZE = 6;
  33.  
  34.     private static final double SENSITIVITY = 1;
  35.     private static final double ZOOM_SENSITIVITY = 0.2;
  36.  
  37.     private static final int ZOOM_MIN = 80;
  38.     private static final int ZOOM_MAX = 2000;
  39.  
  40.     private final double sceneWidth = 1280;
  41.     private final double sceneHeight = 720;
  42.  
  43.     private static final String texture = "res/wood.png";
  44.  
  45.     private double cursorLocX, cursorLocY;
  46.  
  47.     private Translate zoom = new Translate(0, -16, -200);
  48.  
  49.     private double angleX = 0;
  50.     private double angleY = 0;
  51.  
  52.     private Rotate rotateX = new Rotate(0, 0, -16, 0, Rotate.X_AXIS);
  53.     private Rotate rotateY = new Rotate(0, 0, -16, 0, Rotate.Y_AXIS);
  54.     private Rotate rotateZ = new Rotate(0, 0, -16, 0, Rotate.Z_AXIS);
  55.  
  56.     private final Group root = new Group();
  57.     private PerspectiveCamera camera;
  58.  
  59.     private static Image increaseResolution(BufferedImage image, double sizex, double sizey) {
  60.         BufferedImage i = new BufferedImage((int) sizex * PIXEL_PER_UNIT, (int) sizey * PIXEL_PER_UNIT,
  61.                 BufferedImage.TYPE_INT_ARGB);
  62.         Graphics2D g = (Graphics2D) i.getGraphics();
  63.         g.drawImage(image, 0, 0, i.getWidth(), i.getHeight(), null);
  64.         return SwingFXUtils.toFXImage(i, null);
  65.     }
  66.  
  67.     @Override public void start(Stage stage) {
  68.         BufferedImage woodTexture = null;
  69.         try {
  70.             woodTexture = ImageIO.read(new File(texture));
  71.         } catch (IOException ignored) {
  72.         }
  73.  
  74.         //wooden box
  75.         Box wood = new Box(BLOCK_UNIT, BLOCK_UNIT, BLOCK_UNIT);
  76.         PhongMaterial woodenBox = new PhongMaterial();
  77.         woodenBox.setDiffuseMap(increaseResolution(woodTexture, BLOCK_UNIT, BLOCK_UNIT));
  78.         wood.setMaterial(woodenBox);
  79.         wood.getTransforms().addAll(new Translate(0, 8.1, 0));
  80.         root.getChildren().add(wood);
  81.  
  82.         //gridlines
  83.         PhongMaterial majorGridlineMaterial = new PhongMaterial(Color.GRAY);
  84.         PhongMaterial minorGridlineMaterial = new PhongMaterial(Color.GRAY);
  85.  
  86.         for (double x = -GRID_SIZE / 2 + 1 / BLOCK_UNIT; GRID_SIZE / 2 > x; x += 1 / BLOCK_UNIT) {
  87.             if ((x + 0.5) % 1 == 0) {
  88.                 Cylinder c = new Cylinder(MAJOR_LINE_THICCNESS, GRID_SIZE * BLOCK_UNIT);
  89.                 c.setMaterial(majorGridlineMaterial);
  90.                 c.getTransforms().addAll(new Translate(x * BLOCK_UNIT, 0, 0), new Rotate(90, Rotate.X_AXIS));
  91.                 root.getChildren().add(c);
  92.             } else {
  93.                 Cylinder c = new Cylinder(MINOR_LINE_THICCNESS, GRID_SIZE * BLOCK_UNIT);
  94.                 c.setMaterial(minorGridlineMaterial);
  95.                 c.getTransforms().addAll(new Translate(x * BLOCK_UNIT, 0, 0), new Rotate(90, Rotate.X_AXIS));
  96.                 root.getChildren().add(c);
  97.             }
  98.         }
  99.  
  100.         for (double y = -GRID_SIZE / 2 + 1 / BLOCK_UNIT; GRID_SIZE / 2 > y; y += 1 / BLOCK_UNIT) {
  101.             if ((y + 0.5) % 1 == 0) {
  102.                 Cylinder c = new Cylinder(MAJOR_LINE_THICCNESS, GRID_SIZE * BLOCK_UNIT);
  103.                 c.setMaterial(majorGridlineMaterial);
  104.                 c.getTransforms().addAll(new Translate(0, 0, y * BLOCK_UNIT), new Rotate(90, Rotate.Z_AXIS));
  105.                 root.getChildren().add(c);
  106.             } else {
  107.                 Cylinder c = new Cylinder(MINOR_LINE_THICCNESS, GRID_SIZE * BLOCK_UNIT);
  108.                 c.setMaterial(minorGridlineMaterial);
  109.                 c.getTransforms().addAll(new Translate(0, 0, y * BLOCK_UNIT), new Rotate(90, Rotate.Z_AXIS));
  110.                 root.getChildren().add(c);
  111.             }
  112.         }
  113.  
  114.         //scene creation
  115.         Scene scene = new Scene(root, sceneWidth, sceneHeight, true, SceneAntialiasing.BALANCED);
  116.         scene.setFill(new Color(0.4, 0.9, 0.9, 1));
  117.  
  118.         //creating the camera
  119.         camera = new PerspectiveCamera(true);
  120.         camera.setVerticalFieldOfView(false);
  121.         camera.setNearClip(0.1);
  122.         camera.setFarClip(100000.0);
  123.         camera.getTransforms().addAll(rotateX, rotateY, rotateZ, zoom);
  124.  
  125.         //lightning
  126.         root.getChildren().add(new AmbientLight(Color.WHITE));
  127.  
  128.         //add camera to the scene
  129.         scene.setCamera(camera);
  130.  
  131.         //controls
  132.         scene.setOnMousePressed(event -> {
  133.             if (event.getButton() == MouseButton.MIDDLE) {
  134.                 scene.setCursor(Cursor.MOVE);
  135.                 cursorLocX = event.getScreenX();
  136.                 cursorLocY = event.getScreenY();
  137.             }
  138.         });
  139.  
  140.         scene.setOnMouseReleased(event -> {
  141.             if (event.getButton() == MouseButton.MIDDLE)
  142.                 scene.setCursor(Cursor.DEFAULT);
  143.         });
  144.  
  145.         scene.setOnMouseDragged(event -> {
  146.             if (event.getButton() == MouseButton.MIDDLE) {
  147.                 rotateCameraBy(event.getScreenY() - cursorLocY, event.getScreenX() - cursorLocX);
  148.                 cursorLocX = event.getScreenX();
  149.                 cursorLocY = event.getScreenY();
  150.             }
  151.         });
  152.  
  153.         scene.setOnScroll(event -> zoomCameraBy(event.getDeltaY()));
  154.  
  155.         //showing the scene
  156.         stage.setTitle("3D Editor");
  157.         stage.setScene(scene);
  158.         stage.show();
  159.     }
  160.  
  161.     private void zoomCameraBy(double diff) {
  162.         if (diff == 0)
  163.             return;
  164.         if (-(zoom.getZ() + diff) >= ZOOM_MIN && -(zoom.getZ() + diff) <= ZOOM_MAX)
  165.             zoom.setZ(zoom.getZ() + diff * ZOOM_SENSITIVITY);
  166.     }
  167.  
  168.     private void rotateCameraBy(double diffX, double diffY) {
  169.         if (diffX == 0 && diffY == 0)
  170.             return;
  171.         angleX -= diffX / sceneWidth * 360 * SENSITIVITY;
  172.         angleY += diffY / sceneHeight * 360 * SENSITIVITY;
  173.  
  174.         rotateY.setAngle(angleY);
  175.         //rotateX.setAngle(angleX);
  176.         rotateX.setAngle(angleX*Math.cos(Math.toRadians(angleY)));
  177.         rotateZ.setAngle(angleX*Math.sin(Math.toRadians(angleY)));
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement