リビジョン 0ce05d74
| learn/java/javafx/Hello3d/Hello3d.java | ||
|---|---|---|
|
import javafx.animation.AnimationTimer;
|
||
|
import javafx.application.Application;
|
||
|
import javafx.scene.AmbientLight;
|
||
|
import javafx.scene.Group;
|
||
| ... | ... | |
|
|
||
|
private final Image earthImage = new Image(getClass().getResourceAsStream("physical-free-world-map-b1.jpg"));
|
||
|
|
||
|
private long previousHandledTime = Long.MAX_VALUE;
|
||
|
private double azimuth;
|
||
|
private AnimationTimer timer;
|
||
|
|
||
|
private static final double AZIMUTH_SPEED_PER_100MILLIS = 1.0;
|
||
|
private static final double ORBITAL_RADIUS = 240d;
|
||
|
|
||
|
private final Rotate cameraRotateX = new Rotate(0d, Rotate.X_AXIS);
|
||
|
private final Rotate cameraRotateY = new Rotate(0d, Rotate.Y_AXIS);
|
||
|
private final Rotate cameraRotateZ = new Rotate(0d, Rotate.Z_AXIS);
|
||
|
private final Translate cameraTranslate = new Translate(0d, 0d, -200d);
|
||
|
|
||
|
@Override
|
||
|
public void start(final Stage stage) {
|
||
|
final Group root = new Group();
|
||
|
|
||
|
// 球体の定義
|
||
|
final Sphere earth = new Sphere(100);
|
||
|
final Sphere earth = new Sphere(100d);
|
||
|
root.getChildren().add(earth);
|
||
|
|
||
|
// 材質の定義
|
||
| ... | ... | |
|
|
||
|
// カメラの定義
|
||
|
final PerspectiveCamera camera = new PerspectiveCamera(true);
|
||
|
camera.setFieldOfView(45.0);
|
||
|
camera.setFarClip(1000);
|
||
|
camera.setFieldOfView(45d);
|
||
|
camera.setFarClip(1000d);
|
||
|
camera.getTransforms().addAll(
|
||
|
new Translate(0, 0, -260)
|
||
|
cameraTranslate,
|
||
|
cameraRotateX,
|
||
|
cameraRotateY,
|
||
|
cameraRotateZ
|
||
|
);
|
||
|
|
||
|
// 点光源の定義
|
||
|
final PointLight pointLight = new PointLight(Color.WHITE);
|
||
|
pointLight.setTranslateX(240);
|
||
|
pointLight.setTranslateY(0);
|
||
|
pointLight.setTranslateZ(-250);
|
||
|
pointLight.setTranslateX(-60d);
|
||
|
pointLight.setTranslateY(0d);
|
||
|
pointLight.setTranslateZ(240d);
|
||
|
root.getChildren().add(pointLight);
|
||
|
|
||
|
// 環境光の定義
|
||
| ... | ... | |
|
stage.setScene(scene);
|
||
|
stage.setTitle("Hello JavaFX 3D World");
|
||
|
stage.show();
|
||
|
|
||
|
timer = new AnimationTimer() {
|
||
|
@Override
|
||
|
public void handle(long now) {
|
||
|
update(now);
|
||
|
}
|
||
|
};
|
||
|
timer.start();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* カメラ位置を更新する。
|
||
|
* カメラは Y=0 の平面(X-Z平面)で原点を中心に 定数 ORBITAL_RADIUS を半径とした
|
||
|
* 円軌道を移動する。
|
||
|
* また、カメラの角度は常に球体を向くように円軌道の移動に合わせて設定する。
|
||
|
*/
|
||
|
private void update(long now) {
|
||
|
if (previousHandledTime + 33_000_000 > now) return;
|
||
|
previousHandledTime = now;
|
||
|
azimuth += AZIMUTH_SPEED_PER_100MILLIS;
|
||
|
|
||
|
cameraTranslate.setX(Math.sin(Math.toRadians(azimuth)) * ORBITAL_RADIUS);
|
||
|
cameraTranslate.setZ(-1 * Math.cos(Math.toRadians(azimuth)) * ORBITAL_RADIUS);
|
||
|
cameraRotateY.setAngle(-1 * azimuth);
|
||
|
}
|
||
|
|
||
|
public static void main(final String... args) {
|
||
カメラ移動の実装を、AnimationTimerに替えて実装