-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathScene3DLesson.java
More file actions
124 lines (100 loc) · 4.1 KB
/
Copy pathScene3DLesson.java
File metadata and controls
124 lines (100 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package fx;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Cylinder;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
public class Scene3DLesson extends Application {
private PerspectiveCamera camera;
private final double cameraModifier = 50.0;
private final double cameraQuantity = 10.0;
private final double sceneWidth = 600;
private final double sceneHeight = 600;
private double mouseXold = 0;
private double mouseYold = 0;
private final double cameraYlimit = 15;
private final double rotateModifier = 25;
@Override
public void start(Stage primaryStage) {
Group sceneRoot = new Group();
Scene scene = new Scene(sceneRoot, sceneWidth, sceneHeight);
scene.setFill(Color.BLACK);
scene.setOnMouseClicked(event-> {
Node picked = event.getPickResult().getIntersectedNode();
if(null != picked) {
double scalar = 2;
if(picked.getScaleX() > 1)
scalar = 1;
picked.setScaleX(scalar);
picked.setScaleY(scalar);
picked.setScaleZ(scalar);
}
});
PerspectiveCamera camera = new PerspectiveCamera(true);
// camera.setNearClip(0.1);
camera.setFarClip(50000.0);
camera.setTranslateZ(-1000);
//Box cylinder = new Box(50, 50, 50);
Cylinder cylinder = new Cylinder(50, 100);
PhongMaterial blueMaterial = new PhongMaterial();
blueMaterial.setDiffuseColor(Color.DARKBLUE);
blueMaterial.setSpecularColor(Color.BLUE);
cylinder.setMaterial(blueMaterial);
cylinder.setRotationAxis(Rotate.X_AXIS);
cylinder.setRotate(45);
// cylinder.setTranslateZ(-200);
sceneRoot.getChildren().add(cylinder);
scene.setCamera(camera);
scene.setOnKeyPressed(event -> {
double change = cameraQuantity;
if (event.isShiftDown()) {
change = cameraModifier;
}
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);
}
});
Rotate xRotate = new Rotate(0,0,0,0,Rotate.X_AXIS);
Rotate yRotate = new Rotate(0,0,0,0,Rotate.Y_AXIS);
camera.getTransforms().addAll(xRotate,yRotate);
scene.addEventHandler(MouseEvent.ANY, event -> {
if (event.getEventType() == MouseEvent.MOUSE_PRESSED ||
event.getEventType() == MouseEvent.MOUSE_DRAGGED) {
double mouseXnew = event.getSceneX();
double mouseYnew = event.getSceneY();
if (event.getEventType() == MouseEvent.MOUSE_DRAGGED) {
double pitchRotate = xRotate.getAngle() + (mouseYnew - mouseYold) / rotateModifier;
pitchRotate = pitchRotate > cameraYlimit ? cameraYlimit : pitchRotate;
pitchRotate = pitchRotate < -cameraYlimit ? -cameraYlimit : pitchRotate;
xRotate.setAngle(pitchRotate);
double yawRotate=yRotate.getAngle()-(mouseXnew - mouseXold) / rotateModifier;
yRotate.setAngle(yawRotate);
}
mouseXold = mouseXnew;
mouseYold = mouseYnew;
}
});
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}