javafx - 在javaFX中围绕对象旋转透视相机

标签 javafx 3d perspectivecamera

如何让相机围绕 javaFX 中的 3D 对象旋转?我知道我可以使用它围绕自身旋转

camera.setRotate(angle);

但我希望一个物体保持静止,并且相机旋转并指向同一点,就像旋转轴就是该物体一样。

最佳答案

一般技术的答案是:RotateTransition around a pivot?您定义旋转变换,然后使用时间轴(或动画计时器)根据需要对旋转变换的角度进行动画处理。如果您希望对象居中,则可以在旋转之前将相机平移到对象的原点。

此处的示例仅演示了如何为 3D 应用执行此操作:

image image

在示例中,相机围绕立方体旋转,立方体的中心位于场景坐标 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);
    }
}

关于javafx - 在javaFX中围绕对象旋转透视相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43763551/

相关文章:

javascript - 设置 THREE.PerspectiveCamera 的距离而不改变 View 方向

JAVA FX ScrollPane 不允许请求焦点在一个字段上

Java FX 文本区域对齐

javafx - 我如何知道 TableView 是否为空?

python - python中的3D多模态医学图像配准

javascript - "GL_INVALID_OPERATION: Insufficient buffer size."在可变数量的渲染调用之后

java libgdx 移动透视相机

opencv - 寻找两个平面之间的透视变换

java - 如何使 JavaFX MediaView 拉伸(stretch)媒体填充父容器?

vector - 从 3D 平面上的 3D 点检索 2D 坐标