swe_primus/learn/java/javafx/Hello3d/Hello3d.java @ e485c9af
| 29192059 | torutk | import javafx.application.Application;
|
||
| e3ae6ff1 | torutk | import javafx.animation.Interpolator;
|
||
| 2f3bf69b | torutk | import javafx.animation.KeyFrame;
|
||
import javafx.animation.KeyValue;
|
||||
| e3ae6ff1 | torutk | import javafx.animation.Timeline;
|
||
| 685d257b | torutk | import javafx.scene.AmbientLight;
|
||
| 29192059 | torutk | import javafx.scene.Group;
|
||
import javafx.scene.PerspectiveCamera;
|
||||
| 38b62b0a | torutk | import javafx.scene.PointLight;
|
||
| 29192059 | torutk | import javafx.scene.Scene;
|
||
| 5775a464 | torutk | import javafx.scene.image.Image;
|
||
| 29192059 | torutk | import javafx.scene.paint.Color;
|
||
import javafx.scene.paint.PhongMaterial;
|
||||
import javafx.scene.shape.Sphere;
|
||||
import javafx.scene.transform.Rotate;
|
||||
import javafx.scene.transform.Translate;
|
||||
import javafx.stage.Stage;
|
||||
| 2f3bf69b | torutk | import javafx.util.Duration;
|
||
| 29192059 | torutk | |||
public class Hello3d extends Application {
|
||||
| 2f3bf69b | torutk | 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);
|
||||
| e3ae6ff1 | torutk | private final Translate cameraTranslate = new Translate(0, 0, -200);
|
||
| 5775a464 | torutk | |||
| 29192059 | torutk | @Override
|
||
public void start(final Stage stage) {
|
||||
final Group root = new Group();
|
||||
// 球体の定義
|
||||
final Sphere earth = new Sphere(100);
|
||||
root.getChildren().add(earth);
|
||||
| a87c4319 | torutk | // 材質の定義
|
||
| 2f3bf69b | torutk | final PhongMaterial material = new PhongMaterial();
|
||
material.setDiffuseMap(earthImage);
|
||||
earth.setMaterial(material);
|
||||
| a87c4319 | torutk | |||
| 29192059 | torutk | // カメラの定義
|
||
| 2f3bf69b | torutk | 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);
|
||||
// アニメーション定義
|
||||
| e3ae6ff1 | torutk | // カメラを(0, 0, -200)から(200, 0, 0)に移動する
|
||
| 2f3bf69b | torutk | // その際、カメラの向きを(0, 0, 0)方向に維持するためY軸半時計回りに
|
||
// 90度回転させる
|
||||
final Timeline animation = new Timeline();
|
||||
| e3ae6ff1 | torutk | final Interpolator fastSlowInterpolator = Interpolator.SPLINE(0.13, 0.5, 0.5, 0.87);
|
||
final Interpolator slowFastInterpolator = Interpolator.SPLINE(0.5, 0.13, 0.87, 0.5);
|
||||
| 2f3bf69b | torutk | animation.getKeyFrames().addAll(
|
||
new KeyFrame(Duration.ZERO,
|
||||
new KeyValue(cameraRotateY.angleProperty(), 0),
|
||||
| e3ae6ff1 | torutk | new KeyValue(cameraTranslate.xProperty(), 0, fastSlowInterpolator),
|
||
| 2f3bf69b | torutk | new KeyValue(cameraTranslate.yProperty(), 0),
|
||
| e3ae6ff1 | torutk | new KeyValue(cameraTranslate.zProperty(), -200, slowFastInterpolator)
|
||
| 2f3bf69b | torutk | ),
|
||
new KeyFrame(Duration.millis(3000),
|
||||
new KeyValue(cameraRotateY.angleProperty(), -90),
|
||||
| e3ae6ff1 | torutk | new KeyValue(cameraTranslate.xProperty(), 200, fastSlowInterpolator),
|
||
| 2f3bf69b | torutk | new KeyValue(cameraTranslate.yProperty(), 0),
|
||
| e3ae6ff1 | torutk | new KeyValue(cameraTranslate.zProperty(), 0, slowFastInterpolator)
|
||
| 2f3bf69b | torutk | )
|
||
);
|
||||
stage.setScene(scene);
|
||||
stage.setTitle("Hello JavaFX 3D World");
|
||||
stage.show();
|
||||
root.setOnMouseClicked(event -> animation.play());
|
||||
| e3ae6ff1 | torutk | |||
| 29192059 | torutk | }
|
||
public static void main(final String... args) {
|
||||
| 2f3bf69b | torutk | launch(args);
|
||
| 29192059 | torutk | }
|
||
| 3cf7d266 | torutk | }
|