swe_primus/learn/java/javafx/Hello3d/Hello3d.java @ 59e525ef
| 0ce05d74 | torutk | import javafx.animation.AnimationTimer;
|
||
| 29192059 | torutk | import javafx.application.Application;
|
||
| 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;
|
||||
public class Hello3d extends Application {
|
||||
| 59e525ef | torutk | private static final double FAR_ORBITAL_RADIUS = 240d;
|
||
private static final double NEAR_ORBITAL_RADIUS = 160d;
|
||||
| 29192059 | torutk | |||
| 59e525ef | torutk | private final Image earthImage = new Image(getClass().getResourceAsStream("earth_1000x500.png"));
|
||
| 5775a464 | torutk | |||
| 0ce05d74 | torutk | private long previousHandledTime = Long.MAX_VALUE;
|
||
private double azimuth;
|
||||
| 59e525ef | torutk | private double range = FAR_ORBITAL_RADIUS;
|
||
| 0ce05d74 | torutk | private AnimationTimer timer;
|
||
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);
|
||||
| 59e525ef | torutk | private final Translate cameraTranslate = new Translate(0d, -30d, -1 * FAR_ORBITAL_RADIUS);
|
||
| 0ce05d74 | torutk | |||
| 29192059 | torutk | @Override
|
||
public void start(final Stage stage) {
|
||||
final Group root = new Group();
|
||||
// 球体の定義
|
||||
| 0ce05d74 | torutk | final Sphere earth = new Sphere(100d);
|
||
| 29192059 | torutk | root.getChildren().add(earth);
|
||
| a87c4319 | torutk | // 材質の定義
|
||
final PhongMaterial material = new PhongMaterial();
|
||||
| 5775a464 | torutk | material.setDiffuseMap(earthImage);
|
||
| a87c4319 | torutk | earth.setMaterial(material);
|
||
| 29192059 | torutk | // カメラの定義
|
||
final PerspectiveCamera camera = new PerspectiveCamera(true);
|
||||
| 0ce05d74 | torutk | camera.setFieldOfView(45d);
|
||
camera.setFarClip(1000d);
|
||||
| 29192059 | torutk | camera.getTransforms().addAll(
|
||
| 0ce05d74 | torutk | cameraTranslate,
|
||
cameraRotateX,
|
||||
cameraRotateY,
|
||||
cameraRotateZ
|
||||
| 29192059 | torutk | );
|
||
| 38b62b0a | torutk | // 点光源の定義
|
||
final PointLight pointLight = new PointLight(Color.WHITE);
|
||||
| 59e525ef | torutk | pointLight.setTranslateX(-80d);
|
||
| 0ce05d74 | torutk | pointLight.setTranslateY(0d);
|
||
| 59e525ef | torutk | pointLight.setTranslateZ(220d);
|
||
| 38b62b0a | torutk | root.getChildren().add(pointLight);
|
||
| 685d257b | torutk | // 環境光の定義
|
||
| 59e525ef | torutk | final AmbientLight ambientLight = new AmbientLight(Color.rgb(100, 100, 100, 0.7));
|
||
| 685d257b | torutk | root.getChildren().add(ambientLight);
|
||
| 29192059 | torutk | final Scene scene = new Scene(root, 800, 600, Color.BLACK);
|
||
scene.setCamera(camera);
|
||||
stage.setScene(scene);
|
||||
stage.setTitle("Hello JavaFX 3D World");
|
||||
stage.show();
|
||||
| 0ce05d74 | torutk | |||
timer = new AnimationTimer() {
|
||||
@Override
|
||||
public void handle(long now) {
|
||||
update(now);
|
||||
}
|
||||
};
|
||||
| 59e525ef | torutk | |||
root.setOnMousePressed(ev -> timer.start());
|
||||
| 0ce05d74 | torutk | }
|
||
/**
|
||||
* カメラ位置を更新する。
|
||||
| 59e525ef | torutk | *
|
||
* カメラは、Y=0 の平面(X-Z平面)で原点を中心にした円軌道を移動する。
|
||||
*
|
||||
* - フェーズ1
|
||||
* 経度0度から140度EまでFAR_ORBITAL_RADIUS を半径とした円軌道を移動する。
|
||||
* - フェーズ2
|
||||
* 経度140度Eで、半径をFAR_ORBITAL_RADIUSからNEAR_ORBITAL_RADIUSに移動する。
|
||||
* - フェーズ3
|
||||
* 経度140度Eから122度WまでFAR_ORBITAL_RADIUS を半径とした円軌道を移動する。
|
||||
*
|
||||
| 0ce05d74 | torutk | * また、カメラの角度は常に球体を向くように円軌道の移動に合わせて設定する。
|
||
*/
|
||||
private void update(long now) {
|
||||
| 59e525ef | torutk | // フェーズ1
|
||
if (azimuth < 140d) {
|
||||
azimuth += 1;
|
||||
cameraTranslate.setX(Math.sin(Math.toRadians(azimuth)) * FAR_ORBITAL_RADIUS);
|
||||
cameraTranslate.setZ(-1 * Math.cos(Math.toRadians(azimuth)) * FAR_ORBITAL_RADIUS);
|
||||
cameraRotateY.setAngle(-1 * azimuth);
|
||||
return;
|
||||
}
|
||||
// フェーズ2
|
||||
if (range > NEAR_ORBITAL_RADIUS) {
|
||||
range -= 0.5d;
|
||||
cameraTranslate.setX(Math.sin(Math.toRadians(azimuth)) * range);
|
||||
cameraTranslate.setZ(-1 * Math.cos(Math.toRadians(azimuth)) * range);
|
||||
return;
|
||||
}
|
||||
// フェーズ3
|
||||
if (azimuth < 238d) {
|
||||
azimuth += 0.5;
|
||||
cameraTranslate.setX(Math.sin(Math.toRadians(azimuth)) * NEAR_ORBITAL_RADIUS);
|
||||
cameraTranslate.setZ(-1 * Math.cos(Math.toRadians(azimuth)) * NEAR_ORBITAL_RADIUS);
|
||||
cameraRotateY.setAngle(-1 * azimuth);
|
||||
return;
|
||||
}
|
||||
timer.stop();
|
||||
| 29192059 | torutk | }
|
||
public static void main(final String... args) {
|
||||
launch(args);
|
||||
}
|
||||
| 3cf7d266 | torutk | }
|