Qt Quick 3D - View3D 示例

演示如何使用View3D 从多个摄像头显示一个场景。

该示例演示了在一个应用程序中使用四个带有不同摄像头的独立View3D

定义摄像机

首先,我们要定义cameras 。虽然只添加 4 个视图,但我们要定义多个不同的摄像头。这样做是因为我们希望能够在其中一个视图中切换摄像头。

摄像机必须在根Node 中定义。摄像机就是这样定义的:

Node {
    id: standAloneScene
    ...
// The predefined cameras. They have to be part of the scene, i.e. inside the root node.
// Animated perspective camera
Node {
    PerspectiveCamera {
        id: cameraPerspectiveOne
        z: 600
    }

    PropertyAnimation on eulerRotation.x {
        loops: Animation.Infinite
        duration: 5000
        to: -360
        from: 0
    }
}

// Stationary perspective camera
PerspectiveCamera {
    id: cameraPerspectiveTwo
    z: 600
}
    ...
// Stationary orthographic camera viewing from left
OrthographicCamera {
    id: cameraOrthographicLeft
    x: -600
    eulerRotation.y: -90
}
}

添加视图

定义好摄像机后,我们就可以添加views 。我们将屏幕划分为四个部分,然后像这样逐个添加视图:

// The views
Rectangle {
    id: topLeft
    anchors.top: parent.top
    anchors.left: parent.left
    width: parent.width * 0.5
    height: parent.height * 0.5
    color: "#848895"
    border.color: "black"

    View3D {
        id: topLeftView
        anchors.fill: parent
        importScene: standAloneScene
        camera: cameraOrthographicFront
    }

    Label {
        text: "Front"
        anchors.top: parent.top
        anchors.left: parent.left
        anchors.margins: 10
        color: "#222840"
        font.pointSize: 14
    }
}
    ...

右上角视图中有三个按钮。这些按钮可用于即时切换该视图中使用的摄像机。切换只需设置摄像头属性即可:

RoundButton {
    text: "Camera 1"
    highlighted: topRightView.camera == cameraPerspectiveOne
    implicitWidth: controlsContainer.buttonWidth
    onClicked: {
        topRightView.camera = cameraPerspectiveOne
    }
}
    ...

示例项目 @ code.qt.io

© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.