【发布时间】:2017-05-03 14:55:24
【问题描述】:
如何让相机在 javaFX 中围绕 3d 对象旋转?我知道我可以使用
camera.setRotate(angle);
但我希望一个物体保持静止,并且相机旋转并指向同一个点,就像旋转轴是那个物体一样。
【问题讨论】:
标签: javafx 3d perspectivecamera
如何让相机在 javaFX 中围绕 3d 对象旋转?我知道我可以使用
camera.setRotate(angle);
但我希望一个物体保持静止,并且相机旋转并指向同一个点,就像旋转轴是那个物体一样。
【问题讨论】:
标签: javafx 3d perspectivecamera
一般技术定义为:RotateTransition around a pivot? 您定义一个旋转变换,然后使用时间线(或动画计时器)根据需要为旋转变换的角度设置动画。如果您希望对象居中,则可以在旋转之前将相机平移到对象的原点。
此处的示例仅演示了如何为 3D 应用程序执行此操作:
在示例中,相机围绕立方体旋转,立方体的中心位于场景坐标 0,0,0 处。动画旋转围绕 y 轴。示例图像显示了不同旋转角度的快照。您可以单击场景中的某个对象,将相机对准该对象并围绕该对象旋转。
import javafx.animation.*;
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.scene.transform.*;
import javafx.stage.Stage;
import javafx.util.Duration;
public class CameraRotationApp extends Application {
private Parent createContent() throws Exception {
Sphere sphere = new Sphere(2.5);
sphere.setMaterial(new PhongMaterial(Color.FORESTGREEN));
sphere.setTranslateZ(7);
sphere.setTranslateX(2);
Box box = new Box(5, 5, 5);
box.setMaterial(new PhongMaterial(Color.RED));
Translate pivot = new Translate();
Rotate yRotate = new Rotate(0, Rotate.Y_AXIS);
// Create and position camera
PerspectiveCamera camera = new PerspectiveCamera(true);
camera.getTransforms().addAll (
pivot,
yRotate,
new Rotate(-20, Rotate.X_AXIS),
new Translate(0, 0, -50)
);
// animate the camera position.
Timeline timeline = new Timeline(
new KeyFrame(
Duration.seconds(0),
new KeyValue(yRotate.angleProperty(), 0)
),
new KeyFrame(
Duration.seconds(15),
new KeyValue(yRotate.angleProperty(), 360)
)
);
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
// Build the Scene Graph
Group root = new Group();
root.getChildren().add(camera);
root.getChildren().add(box);
root.getChildren().add(sphere);
// set the pivot for the camera position animation base upon mouse clicks on objects
root.getChildren().stream()
.filter(node -> !(node instanceof Camera))
.forEach(node ->
node.setOnMouseClicked(event -> {
pivot.setX(node.getTranslateX());
pivot.setY(node.getTranslateY());
pivot.setZ(node.getTranslateZ());
})
);
// Use a SubScene
SubScene subScene = new SubScene(
root,
300,300,
true,
SceneAntialiasing.BALANCED
);
subScene.setFill(Color.ALICEBLUE);
subScene.setCamera(camera);
Group group = new Group();
group.getChildren().add(subScene);
return group;
}
@Override
public void start(Stage stage) throws Exception {
stage.setResizable(false);
Scene scene = new Scene(createContent());
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
【讨论】: