Search in sources :

Example 1 with ClothMesh

use of org.fxyz.shapes.complex.cloth.ClothMesh in project FXyzLib by Birdasaur.

the class ClothMeshTest method start.

@Override
public void start(Stage stage) throws Exception {
    camera = new PerspectiveCamera(true);
    cameraTransform.setTranslate(0, 0, 0);
    cameraTransform.getChildren().addAll(camera);
    camera.setNearClip(0.1);
    camera.setFarClip(1000000.0);
    camera.setFieldOfView(42);
    camera.setVerticalFieldOfView(true);
    camera.setTranslateZ(-1500);
    PointLight light = new PointLight(Color.LIGHTSKYBLUE);
    // cameraTransform.getChildren().add(light);
    light.translateXProperty().bind(camera.translateXProperty());
    light.translateYProperty().bind(camera.translateYProperty());
    light.translateZProperty().bind(camera.translateZProperty());
    ClothMesh cloth = new ClothMesh();
    cloth.setPerPointMass(10);
    cloth.setBendStrength(0.5);
    cloth.setStretchStrength(1.0);
    cloth.setShearStrength(0.55);
    cloth.setDrawMode(DrawMode.LINE);
    cloth.setCullFace(CullFace.NONE);
    // cloth.setDiffuseMap(new Image("https://kenai.com/attachments/wiki_images/duke/Duke3DprogressionSmall.jpg"));
    cloth.setSpecularPower(5);
    StackPane root = new StackPane();
    root.setPickOnBounds(false);
    PointLight light2 = new PointLight(Color.GAINSBORO);
    light2.setTranslateZ(-1500);
    PointLight light3 = new PointLight(Color.AZURE);
    light3.setTranslateZ(2500);
    Group g = new Group();
    g.getChildren().addAll(cameraTransform, cloth, light2, light3);
    root.getChildren().add(g);
    Scene scene = new Scene(root, 1200, 600, true, SceneAntialiasing.BALANCED);
    Stop[] stops = new Stop[] { new Stop(0, Color.BLACK), new Stop(1, Color.RED) };
    LinearGradient lg = new LinearGradient(0, 0, 0, 1, true, CycleMethod.NO_CYCLE, stops);
    scene.setFill(lg);
    scene.setCamera(camera);
    // First person shooter keyboard movement
    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);
        }
    });
    scene.setOnMousePressed((MouseEvent me) -> {
        mousePosX = me.getSceneX();
        mousePosY = me.getSceneY();
        mouseOldX = me.getSceneX();
        mouseOldY = me.getSceneY();
    });
    scene.setOnMouseDragged((MouseEvent me) -> {
        if (!cloth.isHover()) {
            mouseOldX = mousePosX;
            mouseOldY = mousePosY;
            mousePosX = me.getSceneX();
            mousePosY = me.getSceneY();
            mouseDeltaX = (mousePosX - mouseOldX);
            mouseDeltaY = (mousePosY - mouseOldY);
            double modifier = 10.0;
            double modifierFactor = 0.1;
            if (me.isControlDown()) {
                modifier = 0.1;
            }
            if (me.isShiftDown()) {
                modifier = 50.0;
            }
            if (me.isPrimaryButtonDown()) {
                // +
                cameraTransform.ry.setAngle(((cameraTransform.ry.getAngle() + mouseDeltaX * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180);
                // -
                cameraTransform.rx.setAngle(((cameraTransform.rx.getAngle() - mouseDeltaY * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180);
            } else if (me.isSecondaryButtonDown()) {
                double z = camera.getTranslateZ();
                double newZ = z + mouseDeltaX * modifierFactor * modifier;
                camera.setTranslateZ(newZ);
            } else if (me.isMiddleButtonDown()) {
                // -
                cameraTransform.t.setX(cameraTransform.t.getX() + mouseDeltaX * modifierFactor * modifier * 0.3);
                // -
                cameraTransform.t.setY(cameraTransform.t.getY() + mouseDeltaY * modifierFactor * modifier * 0.3);
            }
        }
    });
    stage.setScene(scene);
    // stage.setMaximized(true);
    stage.show();
    cloth.startSimulation();
}
Also used : Group(javafx.scene.Group) LinearGradient(javafx.scene.paint.LinearGradient) MouseEvent(javafx.scene.input.MouseEvent) Stop(javafx.scene.paint.Stop) KeyCode(javafx.scene.input.KeyCode) PerspectiveCamera(javafx.scene.PerspectiveCamera) PointLight(javafx.scene.PointLight) ClothMesh(org.fxyz.shapes.complex.cloth.ClothMesh) Scene(javafx.scene.Scene) StackPane(javafx.scene.layout.StackPane)

Aggregations

Group (javafx.scene.Group)1 PerspectiveCamera (javafx.scene.PerspectiveCamera)1 PointLight (javafx.scene.PointLight)1 Scene (javafx.scene.Scene)1 KeyCode (javafx.scene.input.KeyCode)1 MouseEvent (javafx.scene.input.MouseEvent)1 StackPane (javafx.scene.layout.StackPane)1 LinearGradient (javafx.scene.paint.LinearGradient)1 Stop (javafx.scene.paint.Stop)1 ClothMesh (org.fxyz.shapes.complex.cloth.ClothMesh)1