Search in sources :

Example 1 with Point3D

use of javafx.geometry.Point3D in project FXyzLib by Birdasaur.

the class NormalMap method buildNormalMap.

private void buildNormalMap(double scale, double scaleFactor, boolean invert) {
    pReader = srcImage.getPixelReader();
    final int w = (int) srcImage.getWidth();
    final int h = (int) srcImage.getHeight();
    WritableImage gray = new WritableImage(w, h);
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            gray.getPixelWriter().setColor(x, y, pReader.getColor(x, y).grayscale());
        }
    }
    final byte[] heightPixels = new byte[w * h * 4];
    final byte[] normalPixels = new byte[w * h * 4];
    // get pixels
    pReader = gray.getPixelReader();
    pReader.getPixels(0, 0, w, h, PixelFormat.getByteBgraInstance(), heightPixels, 0, w * 4);
    if (invert) {
        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                final int pixelIndex = (y * w * 4) + (x * 4);
                heightPixels[pixelIndex + 0] = (byte) (255 - Byte.toUnsignedInt(heightPixels[pixelIndex]));
                heightPixels[pixelIndex + 1] = (byte) (255 - Byte.toUnsignedInt(heightPixels[pixelIndex + 1]));
                heightPixels[pixelIndex + 2] = (byte) (255 - Byte.toUnsignedInt(heightPixels[pixelIndex + 2]));
                heightPixels[pixelIndex + 3] = (byte) (heightPixels[pixelIndex + 3]);
            }
        }
    }
    // generate normal map
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            final int yAbove = Math.max(0, y - 1);
            final int yBelow = Math.min(h - 1, y + 1);
            final int xLeft = Math.max(0, x - 1);
            final int xRight = Math.min(w - 1, x + 1);
            final int pixelIndex = (y * w * 4) + (x * 4);
            final int pixelAboveIndex = (yAbove * w * 4) + (x * 4);
            final int pixelBelowIndex = (yBelow * w * 4) + (x * 4);
            final int pixelLeftIndex = (y * w * 4) + (xLeft * 4);
            final int pixelRightIndex = (y * w * 4) + (xRight * 4);
            final int pixelAboveHeight = Byte.toUnsignedInt(heightPixels[pixelAboveIndex]);
            final int pixelBelowHeight = Byte.toUnsignedInt(heightPixels[pixelBelowIndex]);
            final int pixelLeftHeight = Byte.toUnsignedInt(heightPixels[pixelLeftIndex]);
            final int pixelRightHeight = Byte.toUnsignedInt(heightPixels[pixelRightIndex]);
            Point3D pixelAbove = new Point3D(x, yAbove, pixelAboveHeight);
            Point3D pixelBelow = new Point3D(x, yBelow, pixelBelowHeight);
            Point3D pixelLeft = new Point3D(xLeft, y, pixelLeftHeight);
            Point3D pixelRight = new Point3D(xRight, y, pixelRightHeight);
            Point3D H = pixelLeft.subtract(pixelRight);
            Point3D V = pixelAbove.subtract(pixelBelow);
            Point3D normal = H.crossProduct(V);
            normal = new Point3D((normal.getX() / w), (normal.getY() / h), (1 / normal.getZ()) / (Math.max(1.0, scale) / Math.min(10, scaleFactor))).normalize();
            //Blue              
            normalPixels[pixelIndex + 0] = (byte) (255 - (normal.getZ()));
            //Green                 
            normalPixels[pixelIndex + 1] = (byte) (128 + (normal.getY() * 128.0));
            //Red                 
            normalPixels[pixelIndex + 2] = (byte) (128 + (normal.getX() * 128.0));
            //alpha
            normalPixels[pixelIndex + 3] = (byte) (255);
        }
    }
    // create output image
    pWriter.setPixels(0, 0, w, h, PixelFormat.getByteBgraPreInstance(), normalPixels, 0, w * 4);
}
Also used : WritableImage(javafx.scene.image.WritableImage) Point3D(javafx.geometry.Point3D)

Example 2 with Point3D

use of javafx.geometry.Point3D in project FXyzLib by Birdasaur.

the class BillboardBehavior method updateMatrix.

/**
     * Updates the transformation matrix.
     * can change the Translate for fixed distance  
     */
default default void updateMatrix() {
    Transform cam = getOther().getLocalToSceneTransform(), self = getBillboardNode().getLocalToSceneTransform();
    Bounds b;
    double cX, cY, cZ;
    if (!(getBillboardNode() instanceof Shape3D)) {
        b = getBillboardNode().getBoundsInLocal();
        cX = b.getWidth() / 2;
        cY = b.getHeight() / 2;
        cZ = b.getDepth() / 2;
    } else {
        cX = self.getTx();
        cY = self.getTy();
        cZ = self.getTz();
    }
    Point3D camPos = new Point3D(cam.getTx(), cam.getTy(), cam.getTz());
    Point3D selfPos = new Point3D(cX, cY, cZ);
    Vector3D up = Vector3D.UP, forward = new Vector3D((selfPos.getX()) - camPos.getX(), (selfPos.getY()) - camPos.getY(), (selfPos.getZ()) - camPos.getZ()).toNormal(), right = up.crossProduct(forward).toNormal();
    up = forward.crossProduct(right).toNormal();
    switch(getBillboardMode()) {
        case SPHERICAL:
            affine.setMxx(right.x);
            affine.setMxy(up.x);
            affine.setMzx(forward.x);
            affine.setMyx(right.y);
            affine.setMyy(up.y);
            affine.setMzy(forward.y);
            affine.setMzx(right.z);
            affine.setMzy(up.z);
            affine.setMzz(forward.z);
            affine.setTx(cX * (1 - affine.getMxx()) - cY * affine.getMxy() - cZ * affine.getMxz());
            affine.setTy(cY * (1 - affine.getMyy()) - cX * affine.getMyx() - cZ * affine.getMyz());
            affine.setTz(cZ * (1 - affine.getMzz()) - cX * affine.getMzx() - cY * affine.getMzy());
            break;
        case CYLINDRICAL:
            affine.setMxx(right.x);
            affine.setMxy(0);
            affine.setMzx(forward.x);
            affine.setMyx(0);
            affine.setMyy(1);
            affine.setMzy(0);
            affine.setMzx(right.z);
            affine.setMzy(0);
            affine.setMzz(forward.z);
            affine.setTx(cX * (1 - affine.getMxx()) - cY * affine.getMxy() - cZ * affine.getMxz());
            affine.setTy(cY * (1 - affine.getMyy()) - cX * affine.getMyx() - cZ * affine.getMyz());
            affine.setTz(cZ * (1 - affine.getMzz()) - cX * affine.getMzx() - cY * affine.getMzy());
            break;
    }
}
Also used : Vector3D(org.fxyz.geometry.Vector3D) Point3D(javafx.geometry.Point3D) Bounds(javafx.geometry.Bounds) Shape3D(javafx.scene.shape.Shape3D) Transform(javafx.scene.transform.Transform)

Example 3 with Point3D

use of javafx.geometry.Point3D in project Gargoyle by callakrsos.

the class Drag3DObject method loadControls.

private void loadControls(Scene scene) {
    //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();
        PickResult pr = me.getPickResult();
        if (pr != null && pr.getIntersectedNode() != null && pr.getIntersectedNode() instanceof Sphere) {
            distance = pr.getIntersectedDistance();
            s = (Sphere) pr.getIntersectedNode();
            isPicking = true;
            vecIni = unProjectDirection(mousePosX, mousePosY, scene.getWidth(), scene.getHeight());
        }
    });
    scene.setOnMouseDragged((MouseEvent me) -> {
        mouseOldX = mousePosX;
        mouseOldY = mousePosY;
        mousePosX = me.getSceneX();
        mousePosY = me.getSceneY();
        mouseDeltaX = (mousePosX - mouseOldX);
        mouseDeltaY = (mousePosY - mouseOldY);
        if (RUN_JASON) {
            //objPos += mouseMovementx*scale*RightAxis 
            if (isPicking) {
                if (mousePosX < mouseOldX) {
                    System.out.println("moving left");
                    Point3D pos = CameraHelper.pickProjectPlane(camera, mousePosX, mousePosY);
                    s.setTranslateX(s.getTranslateX() + (pos.getX() - mouseOldX) * camera.getNearClip());
                    return;
                } else if (mousePosX > mouseOldX) {
                    System.err.println("moving right");
                    Point3D pos = CameraHelper.pickProjectPlane(camera, mousePosX, mousePosY);
                    s.setTranslateX(s.getTranslateX() - (pos.getX() - mouseOldX) * camera.getNearClip());
                    return;
                }
                if (mousePosY < mouseOldY) {
                    System.out.println("moving up");
                } else if (mousePosY > mouseOldY) {
                    System.err.println("moving down");
                }
                return;
            }
        } else {
            if (isPicking) {
                double modifier = (me.isControlDown() ? 0.2 : me.isAltDown() ? 2.0 : 1) * (30d / camera.getFieldOfView());
                modifier *= (30d / camera.getFieldOfView());
                vecPos = unProjectDirection(mousePosX, mousePosY, scene.getWidth(), scene.getHeight());
                Point3D p = new Point3D(distance * (vecPos.x - vecIni.x), distance * (vecPos.y - vecIni.y), distance * (vecPos.z - vecIni.z));
                s.getTransforms().add(new Translate(modifier * p.getX(), 0, modifier * p.getZ()));
                vecIni = vecPos;
            } else {
                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);
                }
            }
        }
    });
    scene.setOnMouseReleased((MouseEvent me) -> {
        if (isPicking) {
            isPicking = false;
        }
    });
}
Also used : Sphere(javafx.scene.shape.Sphere) MouseEvent(javafx.scene.input.MouseEvent) Point3D(javafx.geometry.Point3D) PickResult(javafx.scene.input.PickResult) KeyCode(javafx.scene.input.KeyCode) Translate(javafx.scene.transform.Translate)

Example 4 with Point3D

use of javafx.geometry.Point3D in project FXyzLib by Birdasaur.

the class RayTest 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.WHEAT);
    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(-40);
    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);
    cameraTransform.getChildren().add(new AmbientLight(Color.WHITE));
    light.setTranslateX(camera.getTranslateX());
    light.setTranslateY(camera.getTranslateY());
    light.setTranslateZ(camera.getTranslateZ());
    scene.setCamera(camera);
    rotateY = new Rotate(0, 0, 0, 0, Rotate.Y_AXIS);
    Group group = new Group();
    group.getChildren().add(cameraTransform);
    knot = new KnotMesh(2d, 1d, 0.4d, 2d, 3d, 100, 20, 0, 0);
    //        knot.setDrawMode(DrawMode.LINE);
    knot.setCullFace(CullFace.NONE);
    knot.setSectionType(SectionType.TRIANGLE);
    spring = new CurvedSpringMesh(6d, 2d, 0.4d, 25d, 6.25d * 2d * Math.PI, 1000, 60, 0, 0);
    spring.getTransforms().addAll(new Translate(6, -6, 0));
    spring.setDrawMode(DrawMode.LINE);
    spring.setCullFace(CullFace.NONE);
    //        spring.setTextureModeVertices3D(256*256,dens);
    // NONE
    knot.setTextureModeNone(Color.BROWN);
    spring.setTextureModeNone(Color.BROWN);
    knot.getTransforms().addAll(new Rotate(0, Rotate.X_AXIS), rotateY);
    group.getChildren().add(knot);
    group.getChildren().add(spring);
    /*
        Origin in knot
        Target in spring
        */
    org.fxyz.geometry.Point3D locOrigin = knot.getOrigin();
    Point3D gloOrigin = knot.localToScene(new Point3D(locOrigin.x, locOrigin.y, locOrigin.z));
    org.fxyz.geometry.Point3D locTarget1 = spring.getOrigin();
    Point3D locTarget2 = new Point3D(locTarget1.x, locTarget1.y, locTarget1.z);
    Point3D gloTarget = spring.localToScene(locTarget2);
    Point3D gloDirection = gloTarget.subtract(gloOrigin).normalize();
    Point3D gloOriginInLoc = spring.sceneToLocal(gloOrigin);
    Bounds locBounds = spring.getBoundsInLocal();
    Bounds gloBounds = spring.localToScene(locBounds);
    Sphere s = new Sphere(0.05d);
    s.getTransforms().add(new Translate(gloOrigin.getX(), gloOrigin.getY(), gloOrigin.getZ()));
    s.setMaterial(new PhongMaterial(Color.GREENYELLOW));
    group.getChildren().add(s);
    s = new Sphere(0.05d);
    s.getTransforms().add(new Translate(gloTarget.getX(), gloTarget.getY(), gloTarget.getZ()));
    s.setMaterial(new PhongMaterial(Color.GREENYELLOW));
    Point3D dir = gloTarget.subtract(gloOrigin).crossProduct(new Point3D(0, -1, 0));
    double angle = Math.acos(gloTarget.subtract(gloOrigin).normalize().dotProduct(new Point3D(0, -1, 0)));
    double h1 = gloTarget.subtract(gloOrigin).magnitude();
    Cylinder c = new Cylinder(0.01d, h1);
    c.getTransforms().addAll(new Translate(gloOrigin.getX(), gloOrigin.getY() - h1 / 2d, gloOrigin.getZ()), new Rotate(-Math.toDegrees(angle), 0d, h1 / 2d, 0d, new Point3D(dir.getX(), -dir.getY(), dir.getZ())));
    c.setMaterial(new PhongMaterial(Color.GREEN));
    group.getChildren().add(c);
    group.getChildren().add(new Axes(0.02));
    Box box = new Box(gloBounds.getWidth(), gloBounds.getHeight(), gloBounds.getDepth());
    // If transparency is enabled (8u60+), comment:
    box.setDrawMode(DrawMode.LINE);
    box.setCullFace(CullFace.NONE);
    // If transparency is enabled (8u60+), uncomment:
    //        box.setMaterial(new PhongMaterial(Color.web("8A2BE2",0.3)));
    box.getTransforms().add(new Translate(gloBounds.getMinX() + gloBounds.getWidth() / 2d, gloBounds.getMinY() + gloBounds.getHeight() / 2d, gloBounds.getMinZ() + gloBounds.getDepth() / 2d));
    group.getChildren().add(box);
    /*
        FIRST STEP; Check the ray crosses the bounding box of the shape at any of
        its 6 faces
        */
    List<Point3D> normals = Arrays.asList(new Point3D(-1, 0, 0), new Point3D(1, 0, 0), new Point3D(0, -1, 0), new Point3D(0, 1, 0), new Point3D(0, 0, -1), new Point3D(0, 0, 1));
    List<Point3D> positions = Arrays.asList(new Point3D(locBounds.getMinX(), 0, 0), new Point3D(locBounds.getMaxX(), 0, 0), new Point3D(0, locBounds.getMinY(), 0), new Point3D(0, locBounds.getMaxY(), 0), new Point3D(0, 0, locBounds.getMinZ()), new Point3D(0, 0, locBounds.getMaxZ()));
    AtomicInteger counter = new AtomicInteger();
    IntStream.range(0, 6).forEach(i -> {
        double d = -normals.get(i).dotProduct(positions.get(i));
        double t = -(gloOriginInLoc.dotProduct(normals.get(i)) + d) / (gloDirection.dotProduct(normals.get(i)));
        Point3D locInter = gloOriginInLoc.add(gloDirection.multiply(t));
        if (locBounds.contains(locInter)) {
            counter.getAndIncrement();
            Point3D gloInter = spring.localToScene(locInter);
            Sphere s2 = new Sphere(0.1d);
            s2.getTransforms().add(new Translate(gloInter.getX(), gloInter.getY(), gloInter.getZ()));
            s2.setMaterial(new PhongMaterial(Color.GOLD));
            group.getChildren().add(s2);
        }
    });
    if (counter.get() > 0) {
        /*
            SECOND STEP: Check if the ray crosses any of the triangles of the mesh
            */
        // triangle mesh
        org.fxyz.geometry.Point3D gloOriginInLoc1 = new org.fxyz.geometry.Point3D((float) gloOriginInLoc.getX(), (float) gloOriginInLoc.getY(), (float) gloOriginInLoc.getZ());
        org.fxyz.geometry.Point3D gloDirection1 = new org.fxyz.geometry.Point3D((float) gloDirection.getX(), (float) gloDirection.getY(), (float) gloDirection.getZ());
        System.out.println("inter: " + spring.getIntersections(gloOriginInLoc1, gloDirection1));
    }
    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 - Ray Picker Between Nodes");
    primaryStage.setScene(scene);
    primaryStage.show();
}
Also used : Group(javafx.scene.Group) Rotate(javafx.scene.transform.Rotate) PerspectiveCamera(javafx.scene.PerspectiveCamera) Sphere(javafx.scene.shape.Sphere) Axes(org.fxyz.utils.Axes) KeyCode(javafx.scene.input.KeyCode) PhongMaterial(javafx.scene.paint.PhongMaterial) Translate(javafx.scene.transform.Translate) KnotMesh(org.fxyz.shapes.primitives.KnotMesh) MouseEvent(javafx.scene.input.MouseEvent) Bounds(javafx.geometry.Bounds) Box(javafx.scene.shape.Box) Scene(javafx.scene.Scene) CurvedSpringMesh(org.fxyz.shapes.primitives.CurvedSpringMesh) Cylinder(javafx.scene.shape.Cylinder) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Point3D(javafx.geometry.Point3D) PointLight(javafx.scene.PointLight) AmbientLight(javafx.scene.AmbientLight)

Example 5 with Point3D

use of javafx.geometry.Point3D in project FXyzLib by Birdasaur.

the class SimpleRayTest method start.

@Override
public void start(Stage primaryStage) throws Exception {
    // add cameraTransform so it doesn't affect all nodes
    rayLight.getScope().add(cameraTransform);
    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.setTranslateZ(-5000);
    PointLight light = new PointLight(Color.GAINSBORO);
    PointLight light2 = new PointLight(Color.YELLOW);
    light2.setTranslateY(-2000);
    //create a target
    target1 = new Sphere(100);
    target1.setTranslateX(300);
    target1.setTranslateY(300);
    target1.setTranslateZ(1000);
    target1.setMaterial(red);
    // create another target
    target2 = new Sphere(100);
    target2.setTranslateX(800);
    target2.setTranslateY(-1200);
    target2.setTranslateZ(-500);
    target2.setMaterial(blue);
    root.getChildren().addAll(cameraTransform, target1, target2, light, light2, rayLight);
    root.setAutoSizeChildren(false);
    Scene scene = new Scene((root), 1200, 800, true, SceneAntialiasing.BALANCED);
    scene.setCamera(camera);
    Stop[] stops = new Stop[] { new Stop(0, Color.BLACK), new Stop(0.5, Color.DEEPSKYBLUE.darker()), new Stop(1.0, Color.BLACK) };
    LinearGradient lg = new LinearGradient(0, 0, 0, 1, true, CycleMethod.NO_CYCLE, stops);
    scene.setFill(lg);
    //First person shooter keyboard movement
    scene.setOnKeyPressed(ke -> {
        double change = 10.0;
        if (ke.isShiftDown()) {
            change = 50.0;
        }
        KeyCode keycode = ke.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);
        }
        if (keycode == KeyCode.CONTROL) {
            fireRay = false;
        }
    });
    scene.setOnKeyReleased(ke -> {
        if (ke.getCode().equals(KeyCode.CONTROL)) {
            fireRay = true;
        }
    });
    scene.setOnMousePressed(e -> {
        mousePosX = e.getSceneX();
        mousePosY = e.getSceneY();
        mouseOldX = e.getSceneX();
        mouseOldY = e.getSceneY();
        if (fireRay) {
            Point3D o = e.getPickResult().getIntersectedPoint();
            if (e.isPrimaryButtonDown()) {
                Point3D t = Point3D.ZERO.add(target2.getTranslateX(), target2.getTranslateY(), target2.getTranslateZ()), d = t.subtract(o);
                Ray r = new Ray(o, d);
                double dist = t.distance(o);
                if (target2.getBoundsInParent().contains(r.project(dist))) {
                    animateRayTo(r, target2, Duration.seconds(2));
                    System.out.println("Target Contains Ray!\n" + r + "\nTarget Bounds: " + target2.getBoundsInParent() + "\nDistance: " + dist + "\n");
                }
                e.consume();
            } else if (e.isSecondaryButtonDown()) {
                Point3D tgt = Point3D.ZERO.add(target1.getTranslateX(), target1.getTranslateY(), target1.getTranslateZ()), dir = tgt.subtract(o);
                Ray r = new Ray(o, dir);
                double dist = tgt.distance(o);
                if (target1.getBoundsInParent().contains(r.project(dist))) {
                    animateRayTo(r, target1, Duration.seconds(2));
                    System.out.println("Target Contains Ray: " + target1.getBoundsInParent().contains(r.project(dist)) + "\n" + r + "\nTarget Bounds: " + target1.getBoundsInParent() + "\nDistance: " + dist + "\n");
                }
                e.consume();
            }
        }
    });
    scene.setOnMouseDragged(e -> {
        if (!fireRay) {
            mouseOldX = mousePosX;
            mouseOldY = mousePosY;
            mousePosX = e.getSceneX();
            mousePosY = e.getSceneY();
            mouseDeltaX = (mousePosX - mouseOldX);
            mouseDeltaY = (mousePosY - mouseOldY);
            double modifier = 10.0;
            double modifierFactor = 0.1;
            if (e.isControlDown()) {
                modifier = 0.1;
            }
            if (e.isShiftDown()) {
                modifier = 50.0;
            }
            if (e.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 (e.isSecondaryButtonDown()) {
                double z = camera.getTranslateZ();
                double newZ = z + mouseDeltaX * modifierFactor * modifier;
                camera.setTranslateZ(newZ);
            } else if (e.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("Hello Ray! Animated Visual of a Ray casting");
    primaryStage.setScene(scene);
    primaryStage.show();
}
Also used : Sphere(javafx.scene.shape.Sphere) LinearGradient(javafx.scene.paint.LinearGradient) Stop(javafx.scene.paint.Stop) Point3D(javafx.geometry.Point3D) KeyCode(javafx.scene.input.KeyCode) PerspectiveCamera(javafx.scene.PerspectiveCamera) Ray(org.fxyz.geometry.Ray) PointLight(javafx.scene.PointLight) Scene(javafx.scene.Scene)

Aggregations

Point3D (javafx.geometry.Point3D)10 KeyCode (javafx.scene.input.KeyCode)5 MouseEvent (javafx.scene.input.MouseEvent)4 Sphere (javafx.scene.shape.Sphere)4 Scene (javafx.scene.Scene)3 Translate (javafx.scene.transform.Translate)3 Bounds (javafx.geometry.Bounds)2 Group (javafx.scene.Group)2 PerspectiveCamera (javafx.scene.PerspectiveCamera)2 PointLight (javafx.scene.PointLight)2 PickResult (javafx.scene.input.PickResult)2 Rotate (javafx.scene.transform.Rotate)2 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 java.util (java.util)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Interpolator (javafx.animation.Interpolator)1