use of javafx.scene.input.KeyCode in project FXyzLib by Birdasaur.
the class SegmentedTorusTest 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);
//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(-4000);
cameraTransform.ry.setAngle(-45.0);
cameraTransform.rx.setAngle(-30.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);
torus = new SegmentedTorusMesh(50, 42, 0, 500d, 300d);
// PhongMaterial matTorus = new PhongMaterial(Color.FIREBRICK);
// torus.setMaterial(matTorus);
banner = new SegmentedTorusMesh(50, 42, 14, 500d, 300d);
// PhongMaterial matBanner = new PhongMaterial();
// matBanner.setDiffuseMap(new Image(getClass().getResource("res/Duke3DprogressionSmall.jpg").toExternalForm()));
// banner.setMaterial(matBanner);
// torus.setDrawMode(DrawMode.LINE);
// NONE
// torus.setTextureModeNone(Color.FORESTGREEN);
// IMAGE
// torus.setTextureModeImage(getClass().getResource("res/grid1.png").toExternalForm());
banner.setTextureModeImage(getClass().getResource("res/office.jpg").toExternalForm());
// PATTERN
// torus.setTextureModePattern(1.0d);
// DENSITY
torus.setTextureModeVertices3D(1530, dens);
// FACES
// torus.setTextureModeFaces(256*256);
torus.getTransforms().addAll(new Rotate(0, Rotate.X_AXIS), rotateY);
banner.getTransforms().addAll(new Rotate(0, Rotate.X_AXIS), rotateY);
group.getChildren().addAll(torus, banner);
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);
}
});
final Timeline bannerEffect = new Timeline();
bannerEffect.setCycleCount(Timeline.INDEFINITE);
final KeyValue kv1 = new KeyValue(rotateY.angleProperty(), 360);
final KeyFrame kf1 = new KeyFrame(Duration.millis(10000), kv1);
bannerEffect.getKeyFrames().addAll(kf1);
bannerEffect.play();
lastEffect = System.nanoTime();
AtomicInteger count = new AtomicInteger();
AnimationTimer timerEffect = new AnimationTimer() {
@Override
public void handle(long now) {
if (now > lastEffect + 100_000_000l) {
dens = p -> p.x * Math.cos(count.get() % 100d * 2d * Math.PI / 50d) + p.y * Math.sin(count.get() % 100d * 2d * Math.PI / 50d);
torus.setDensity(dens);
// if(count.get()%100<50){
// torus.setDrawMode(DrawMode.LINE);
// } else {
// torus.setDrawMode(DrawMode.FILL);
// }
// spring.setLength(100+20*(count.get()%10));
// torus.setColors((int)Math.pow(2,count.get()%16));
// torus.setMajorRadius(500+100*(count.get()%10));
// torus.setMinorRadius(150+10*(count.get()%10));
// torus.setPatternScale(1d+(count.get()%10)*5d);
count.getAndIncrement();
lastEffect = now;
}
}
};
primaryStage.setTitle("F(X)yz - Segmented Torus");
primaryStage.setScene(scene);
primaryStage.show();
timerEffect.start();
}
use of javafx.scene.input.KeyCode 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();
}
use of javafx.scene.input.KeyCode in project FXyzLib by Birdasaur.
the class SkyBoxTest method start.
@Override
public void start(Stage primaryStage) throws NonInvertibleTransformException {
root = new Group();
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(-cameraDistance);
//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(camera.getTranslateZ());
root.getChildren().add(cameraTransform);
// Load Skybox AFTER camera is initialized
double size = 100000D;
skyBox = new Skybox(top, bottom, left, right, front, back, size, camera);
//Make a bunch of semi random Torusesessses(toroids?) and stuff : from torustest
Group torusGroup = new Group();
torusGroup.getChildren().add(cameraTransform);
for (int i = 0; i < 30; i++) {
Random r = new Random();
//A lot of magic numbers in here that just artificially constrain the math
float randomRadius = (float) ((r.nextFloat() * 300) + 50);
float randomTubeRadius = (float) ((r.nextFloat() * 100) + 1);
int randomTubeDivisions = (int) ((r.nextFloat() * 64) + 1);
int randomRadiusDivisions = (int) ((r.nextFloat() * 64) + 1);
Color randomColor = new Color(r.nextDouble(), r.nextDouble(), r.nextDouble(), r.nextDouble());
Torus torus = new Torus(randomTubeDivisions, randomRadiusDivisions, randomRadius, randomTubeRadius, randomColor);
double translationX = Math.random() * 1024 * 1.95;
if (Math.random() >= 0.5) {
translationX *= -1;
}
double translationY = Math.random() * 1024 * 1.95;
if (Math.random() >= 0.5) {
translationY *= -1;
}
double translationZ = Math.random() * 1024 * 1.95;
if (Math.random() >= 0.5) {
translationZ *= -1;
}
Translate translate = new Translate(translationX, translationY, translationZ);
Rotate rotateX = new Rotate(Math.random() * 360, Rotate.X_AXIS);
Rotate rotateY = new Rotate(Math.random() * 360, Rotate.Y_AXIS);
Rotate rotateZ = new Rotate(Math.random() * 360, Rotate.Z_AXIS);
torus.getTransforms().addAll(translate, rotateX, rotateY, rotateZ);
//torus.getTransforms().add(translate);
torusGroup.getChildren().add(torus);
}
Scene scene = new Scene(new Group(root), 1024, 668, true, SceneAntialiasing.BALANCED);
scene.setFill(Color.TRANSPARENT);
scene.setCamera(camera);
//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("SkyBoxTest!");
primaryStage.setScene(scene);
primaryStage.show();
root.getChildren().addAll(skyBox, torusGroup);
root.setAutoSizeChildren(true);
}
use of javafx.scene.input.KeyCode in project FXyzLib by Birdasaur.
the class SphereSegmentTest method start.
@Override
public void start(Stage primaryStage) throws Exception {
Group sceneRoot = new Group();
Scene scene = new Scene(sceneRoot, sceneWidth, sceneHeight);
scene.setFill(Color.BLACK);
camera = new PerspectiveCamera(true);
camera.setNearClip(0.1);
camera.setFarClip(10000.0);
camera.setTranslateZ(-1000);
scene.setCamera(camera);
//Make a bunch of semi random sphere segments and stuff
Group sphereGroup = new Group();
for (int i = 0; i < 30; i++) {
Random r = new Random();
//A lot of magic numbers in here that just artificially constrain the math
float randomRadius = (float) ((r.nextFloat() * 150) + 10);
float randomThetaMax = (float) ((r.nextFloat() * 360) + 1);
float randomThetaMin = (float) ((r.nextFloat()) + 1);
if (randomThetaMin > randomThetaMax) {
float swap = randomThetaMin;
randomThetaMin = randomThetaMax;
randomThetaMax = swap;
}
float randomPolarMax = (float) ((r.nextFloat() * 90) + 1);
float randomPolarMin = (float) ((r.nextFloat()) + 1);
if (randomPolarMin > randomPolarMax) {
float swap = randomPolarMin;
randomPolarMin = randomPolarMax;
randomPolarMax = swap;
}
int randomSegments = (int) ((r.nextFloat() * 15) + 5);
Color randomColor = new Color(r.nextDouble(), r.nextDouble(), r.nextDouble(), r.nextDouble());
boolean ambientRandom = r.nextBoolean();
boolean fillRandom = r.nextBoolean();
SphereSegment sphereSegment = new SphereSegment(randomRadius, randomColor, Math.toRadians(0), Math.toRadians(360), Math.toRadians(randomPolarMin), Math.toRadians(randomPolarMax), randomSegments, ambientRandom, fillRandom);
double translationX = Math.random() * sceneWidth / 2;
if (Math.random() >= 0.5)
translationX *= -1;
double translationY = Math.random() * sceneWidth / 2;
if (Math.random() >= 0.5)
translationY *= -1;
double translationZ = Math.random() * sceneWidth / 2;
if (Math.random() >= 0.5)
translationZ *= -1;
Translate translate = new Translate(translationX, translationY, translationZ);
Rotate rotateX = new Rotate(Math.random() * 360, Rotate.X_AXIS);
Rotate rotateY = new Rotate(Math.random() * 360, Rotate.Y_AXIS);
Rotate rotateZ = new Rotate(Math.random() * 360, Rotate.Z_AXIS);
sphereSegment.getTransforms().addAll(translate, rotateX, rotateY, rotateZ);
sphereSegment.getTransforms().add(translate);
sphereGroup.getChildren().add(sphereSegment);
}
sceneRoot.getChildren().addAll(sphereGroup);
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);
}
});
//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);
sphereGroup.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 SphereSegmentTest");
primaryStage.setScene(scene);
primaryStage.show();
}
use of javafx.scene.input.KeyCode in project FXyzLib by Birdasaur.
the class SpheroidTest method initFirstPersonControls.
private void initFirstPersonControls(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();
});
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(MathUtils.clamp(-90, (((cameraTransform.rx.getAngle() - mouseDeltaY * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180), // -
90));
} 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);
}
});
}
Aggregations