Search in sources :

Example 1 with ScatterPlotMesh

use of org.fxyz.shapes.composites.ScatterPlotMesh in project FXyzLib by Birdasaur.

the class ScatterPlotMeshTest method start.

@Override
public void start(Stage primaryStage) throws Exception {
    Group sceneRoot = new Group();
    Scene scene = new Scene(sceneRoot, sceneWidth, sceneHeight, true, SceneAntialiasing.BALANCED);
    scene.setFill(Color.BLACK);
    camera = new PerspectiveCamera(true);
    camera.setNearClip(0.1);
    camera.setFarClip(10000.0);
    camera.setTranslateZ(-1000);
    scene.setCamera(camera);
    scatterPlotMesh = new ScatterPlotMesh(1000, 1, true);
    sceneRoot.getChildren().addAll(scatterPlotMesh);
    ArrayList<Double> dataX = new ArrayList<>();
    ArrayList<Double> dataY = new ArrayList<>();
    ArrayList<Double> dataZ = new ArrayList<>();
    for (int i = -250; i < 250; i++) {
        dataX.add(new Double(i));
        dataY.add(new Double(Math.sin(i) * 50) + i);
        dataZ.add(new Double(Math.cos(i) * 50) + i);
    }
    scatterPlotMesh.setXYZData(dataX, dataY, dataZ);
    scene.setOnKeyPressed(event -> {
        double change = 10.0;
        // Add shift modifier to simulate "Running Speed"
        if (event.isShiftDown()) {
            change = 50.0;
        }
        // What key did the user press?
        KeyCode keycode = event.getCode();
        // Step 2c: Add Zoom controls
        if (keycode == KeyCode.W) {
            camera.setTranslateZ(camera.getTranslateZ() + change);
        }
        if (keycode == KeyCode.S) {
            camera.setTranslateZ(camera.getTranslateZ() - change);
        }
        // Step 2d:  Add Strafe controls
        if (keycode == KeyCode.A) {
            camera.setTranslateX(camera.getTranslateX() - change);
        }
        if (keycode == KeyCode.D) {
            camera.setTranslateX(camera.getTranslateX() + change);
        }
    });
    // Add a Mouse Handler for Rotations
    Rotate xRotate = new Rotate(0, Rotate.X_AXIS);
    Rotate yRotate = new Rotate(0, Rotate.Y_AXIS);
    Rotate zRotate = new Rotate(0, Rotate.Z_AXIS);
    scatterPlotMesh.getTransforms().addAll(xRotate, yRotate);
    // Use Binding so your rotation doesn't have to be recreated
    xRotate.angleProperty().bind(angleX);
    yRotate.angleProperty().bind(angleY);
    zRotate.angleProperty().bind(angleZ);
    // Start Tracking mouse movements only when a button is pressed
    scene.setOnMousePressed(event -> {
        scenex = event.getSceneX();
        sceney = event.getSceneY();
        fixedXAngle = angleX.get();
        fixedYAngle = angleY.get();
        if (event.isMiddleButtonDown()) {
            scenez = event.getSceneX();
            fixedZAngle = angleZ.get();
        }
    });
    // Angle calculation will only change when the button has been pressed
    scene.setOnMouseDragged(event -> {
        if (event.isMiddleButtonDown())
            angleZ.set(fixedZAngle - (scenez - event.getSceneY()));
        else
            angleX.set(fixedXAngle - (scenex - event.getSceneY()));
        angleY.set(fixedYAngle + sceney - event.getSceneX());
    });
    primaryStage.setTitle("F(X)yz ScatterPlotTest");
    primaryStage.setScene(scene);
    primaryStage.show();
}
Also used : Group(javafx.scene.Group) Rotate(javafx.scene.transform.Rotate) ArrayList(java.util.ArrayList) KeyCode(javafx.scene.input.KeyCode) PerspectiveCamera(javafx.scene.PerspectiveCamera) Scene(javafx.scene.Scene) ScatterPlotMesh(org.fxyz.shapes.composites.ScatterPlotMesh)

Aggregations

ArrayList (java.util.ArrayList)1 Group (javafx.scene.Group)1 PerspectiveCamera (javafx.scene.PerspectiveCamera)1 Scene (javafx.scene.Scene)1 KeyCode (javafx.scene.input.KeyCode)1 Rotate (javafx.scene.transform.Rotate)1 ScatterPlotMesh (org.fxyz.shapes.composites.ScatterPlotMesh)1