リビジョン 2f3bf69b
| learn/java/javafx/Hello3d/Hello3d.java | ||
|---|---|---|
|
import javafx.application.Application;
|
||
|
import javafx.animation.Timeline;
|
||
|
import javafx.animation.KeyFrame;
|
||
|
import javafx.animation.KeyValue;
|
||
|
import javafx.scene.AmbientLight;
|
||
|
import javafx.scene.Group;
|
||
|
import javafx.scene.PerspectiveCamera;
|
||
| ... | ... | |
|
import javafx.scene.transform.Rotate;
|
||
|
import javafx.scene.transform.Translate;
|
||
|
import javafx.stage.Stage;
|
||
|
import javafx.util.Duration;
|
||
|
|
||
|
public class Hello3d extends Application {
|
||
|
|
||
|
private final Image earthImage = new Image(getClass().getResourceAsStream("physical-free-world-map-b1.jpg"));
|
||
|
private final Image earthImage = new Image(
|
||
|
getClass().getResourceAsStream("physical-free-world-map-b1.jpg")
|
||
|
);
|
||
|
|
||
|
// TODO: あとでクラス化する
|
||
|
private final Rotate cameraRotateX = new Rotate(0, Rotate.X_AXIS);
|
||
|
private final Rotate cameraRotateY = new Rotate(0, Rotate.Y_AXIS);
|
||
|
private final Rotate cameraRotateZ = new Rotate(0, Rotate.Z_AXIS);
|
||
|
private final Translate cameraTranslate = new Translate(0, 0, -160);
|
||
|
|
||
|
@Override
|
||
|
public void start(final Stage stage) {
|
||
| ... | ... | |
|
root.getChildren().add(earth);
|
||
|
|
||
|
// 材質の定義
|
||
|
final PhongMaterial material = new PhongMaterial();
|
||
|
material.setDiffuseMap(earthImage);
|
||
|
earth.setMaterial(material);
|
||
|
final PhongMaterial material = new PhongMaterial();
|
||
|
material.setDiffuseMap(earthImage);
|
||
|
earth.setMaterial(material);
|
||
|
|
||
|
// カメラの定義
|
||
|
final PerspectiveCamera camera = new PerspectiveCamera(true);
|
||
|
camera.setFieldOfView(45.0);
|
||
|
camera.setFarClip(1000);
|
||
|
camera.getTransforms().addAll(
|
||
|
new Translate(0, 0, -260)
|
||
|
);
|
||
|
|
||
|
// 点光源の定義
|
||
|
final PointLight pointLight = new PointLight(Color.WHITE);
|
||
|
pointLight.setTranslateX(240);
|
||
|
pointLight.setTranslateY(0);
|
||
|
pointLight.setTranslateZ(-250);
|
||
|
root.getChildren().add(pointLight);
|
||
|
|
||
|
// 環境光の定義
|
||
|
final AmbientLight ambientLight = new AmbientLight(Color.rgb(80, 80, 80, 0.5));
|
||
|
root.getChildren().add(ambientLight);
|
||
|
|
||
|
final Scene scene = new Scene(root, 800, 600, Color.BLACK);
|
||
|
scene.setCamera(camera);
|
||
|
|
||
|
stage.setScene(scene);
|
||
|
stage.setTitle("Hello JavaFX 3D World");
|
||
|
stage.show();
|
||
|
final PerspectiveCamera camera = new PerspectiveCamera(true);
|
||
|
camera.setFieldOfView(60);
|
||
|
camera.setFarClip(1000);
|
||
|
camera.getTransforms().addAll(
|
||
|
cameraTranslate,
|
||
|
cameraRotateX,
|
||
|
cameraRotateY,
|
||
|
cameraRotateZ
|
||
|
);
|
||
|
|
||
|
// 点光源の定義
|
||
|
final PointLight pointLight = new PointLight(Color.WHITE);
|
||
|
pointLight.setTranslateX(500);
|
||
|
pointLight.setTranslateY(0);
|
||
|
pointLight.setTranslateZ(-500);
|
||
|
root.getChildren().add(pointLight);
|
||
|
|
||
|
// 環境光の定義
|
||
|
final AmbientLight ambientLight = new AmbientLight(Color.rgb(80, 80, 80, 0.5));
|
||
|
root.getChildren().add(ambientLight);
|
||
|
|
||
|
final Scene scene = new Scene(root, 800, 600, Color.BLACK);
|
||
|
scene.setCamera(camera);
|
||
|
|
||
|
// アニメーション定義
|
||
|
// カメラを(0, 0, -200)から(200, 0, 0)に移a動する
|
||
|
// その際、カメラの向きを(0, 0, 0)方向に維持するためY軸半時計回りに
|
||
|
// 90度回転させる
|
||
|
final Timeline animation = new Timeline();
|
||
|
animation.getKeyFrames().addAll(
|
||
|
new KeyFrame(Duration.ZERO,
|
||
|
new KeyValue(cameraRotateY.angleProperty(), 0),
|
||
|
new KeyValue(cameraTranslate.xProperty(), 0),
|
||
|
new KeyValue(cameraTranslate.yProperty(), 0),
|
||
|
new KeyValue(cameraTranslate.zProperty(), -200)
|
||
|
),
|
||
|
new KeyFrame(Duration.millis(3000),
|
||
|
new KeyValue(cameraRotateY.angleProperty(), -90),
|
||
|
new KeyValue(cameraTranslate.xProperty(), 200),
|
||
|
new KeyValue(cameraTranslate.yProperty(), 0),
|
||
|
new KeyValue(cameraTranslate.zProperty(), 0)
|
||
|
)
|
||
|
);
|
||
|
|
||
|
stage.setScene(scene);
|
||
|
stage.setTitle("Hello JavaFX 3D World");
|
||
|
stage.show();
|
||
|
|
||
|
root.setOnMouseClicked(event -> animation.play());
|
||
|
}
|
||
|
|
||
|
public static void main(final String... args) {
|
||
|
launch(args);
|
||
|
launch(args);
|
||
|
}
|
||
|
}
|
||
Timelineを使ってカメラを球体(地球)の赤道上空を円軌道で移動させるアニメーション追加。