Search in sources :

Example 1 with CSGMesh

use of org.fxyz.shapes.primitives.CSGMesh in project FXyzLib by Birdasaur.

the class CuboidCSGTest 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.WHITESMOKE);
    camera = new PerspectiveCamera(true);
    //setup camera transform for rotational support
    cameraTransform.setTranslate(0, 0, 0);
    cameraTransform.getChildren().add(camera);
    camera.setNearClip(0.1);
    camera.setFarClip(10000.0);
    camera.setTranslateZ(-30);
    cameraTransform.ry.setAngle(-45.0);
    cameraTransform.rx.setAngle(-10.0);
    //add a Point Light for better viewing of the grid coordinate system
    PointLight light = new PointLight(Color.WHITE);
    cameraTransform.getChildren().add(light);
    light.setTranslateX(camera.getTranslateX());
    light.setTranslateY(camera.getTranslateY());
    light.setTranslateZ(10 * camera.getTranslateZ());
    scene.setCamera(camera);
    rotateY = new Rotate(0, 0, 0, 0, Rotate.Y_AXIS);
    Group group = new Group();
    group.getChildren().add(cameraTransform);
    cuboid = new CuboidMesh(10f, 12f, 4f, 2, new Point3D(1f, -1f, 2f));
    cuboid.setDrawMode(DrawMode.LINE);
    CSG cuboidCSG = MeshUtils.mesh2CSG(cuboid.getMesh());
    CSG cube = new Cube(5).toCSG().color(Color.RED);
    PrismMesh prism = new PrismMesh(4f, 8f);
    prism.setSectionType(TriangleMeshHelper.SectionType.TRIANGLE);
    CSG prismCSG = MeshUtils.mesh2CSG(prism.getMesh());
    CSG union = cuboidCSG.difference(cube.transformed(Transform.unity().translateX(5))).union(prismCSG.transformed(Transform.unity().translateX(-4).rotZ(90)));
    CSGMesh unionMesh = new CSGMesh(union);
    //        unionMesh.setDrawMode(DrawMode.LINE);
    //        
    //        unionMesh.setCullFace(CullFace.NONE);
    // NONE
    //        cuboid.setTextureModeNone(Color.ROYALBLUE);
    //        unionMesh.setTextureModeNone(Color.ROYALBLUE);
    // IMAGE
    //        cuboid.setTextureModeImage(getClass().getResource("res/netCuboid.png").toExternalForm());
    // DENSITY
    //        cuboid.setTextureModeVertices3D(1530,p->(double)p.x*p.y);
    unionMesh.setTextureModeVertices3D(1530, p -> (double) p.x * p.y);
    //        unionMesh.setTextureModeVertices1D(1530,p->(double)p);
    // FACES
    //        cuboid.setTextureModeFaces(1530);
    //        unionMesh.setTextureModeFaces(1530);
    //        cuboid.getTransforms().addAll(new Rotate(0,Rotate.X_AXIS),rotateY);
    //        group.getChildren().add(cuboid);
    unionMesh.getTransforms().addAll(new Rotate(0, Rotate.X_AXIS), rotateY);
    group.getChildren().add(unionMesh);
    sceneRoot.getChildren().addAll(group);
    //First person shooter keyboard movement 
    scene.setOnKeyPressed(event -> {
        double change = 10.0;
        if (event.isShiftDown()) {
            change = 50.0;
        }
        KeyCode keycode = event.getCode();
        if (keycode == KeyCode.W) {
            camera.setTranslateZ(camera.getTranslateZ() + change);
        }
        if (keycode == KeyCode.S) {
            camera.setTranslateZ(camera.getTranslateZ() - change);
        }
        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) -> {
        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);
        }
    });
    primaryStage.setTitle("F(X)yz & JCSG - Boolean Operations Test");
    primaryStage.setScene(scene);
    primaryStage.show();
    OBJWriter writer = new OBJWriter((TriangleMesh) unionMesh.getMesh(), "cuboid");
    //        writer.setMaterialColor(Color.AQUA);
    //        writer.setTextureImage(getClass().getResource("res/netCuboid.png").toExternalForm());
    writer.setTextureColors(1530);
    writer.exportMesh();
}
Also used : Group(javafx.scene.Group) MouseEvent(javafx.scene.input.MouseEvent) Rotate(javafx.scene.transform.Rotate) PerspectiveCamera(javafx.scene.PerspectiveCamera) Scene(javafx.scene.Scene) OBJWriter(org.fxyz.utils.OBJWriter) CSGMesh(org.fxyz.shapes.primitives.CSGMesh) Cube(eu.mihosoft.vrl.v3d.Cube) Point3D(org.fxyz.geometry.Point3D) CSG(eu.mihosoft.vrl.v3d.CSG) PrismMesh(org.fxyz.shapes.primitives.PrismMesh) KeyCode(javafx.scene.input.KeyCode) PointLight(javafx.scene.PointLight) CuboidMesh(org.fxyz.shapes.primitives.CuboidMesh)

Aggregations

CSG (eu.mihosoft.vrl.v3d.CSG)1 Cube (eu.mihosoft.vrl.v3d.Cube)1 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 Rotate (javafx.scene.transform.Rotate)1 Point3D (org.fxyz.geometry.Point3D)1 CSGMesh (org.fxyz.shapes.primitives.CSGMesh)1 CuboidMesh (org.fxyz.shapes.primitives.CuboidMesh)1 PrismMesh (org.fxyz.shapes.primitives.PrismMesh)1 OBJWriter (org.fxyz.utils.OBJWriter)1