Qt Quick 3D - 实例渲染示例

演示如何在Qt Quick 3D 中进行实例渲染。

本示例演示了如何使用基本的 QML API 进行实例渲染。

飞船和小行星模型是使用 Blender 3D 建模工具创建的,并用balsam 进行了导入。

随机实例化

我们使用RandomInstancing 制作一个定义小行星区域的随机表:

RandomInstancing {
    id: randomInstancing
    instanceCount: 1500

    position: InstanceRange {
        from: Qt.vector3d(-300, -200, -500)
        to: Qt.vector3d(300, 200, 200)
    }
    scale: InstanceRange {
        from: Qt.vector3d(1, 1, 1)
        to: Qt.vector3d(10, 10, 10)
        proportional: true
    }
    rotation: InstanceRange {
        from: Qt.vector3d(0, 0, 0)
        to: Qt.vector3d(360, 360, 360)
    }
    color: InstanceRange {
        from: "grey"
        to: "white"
        proportional: true
    }

    randomSeed: 2021
}

位置和旋转可以自由变化。缩放设置为所有方向一致,颜色为灰度。这可以通过设置proportional 属性来实现。

飞船是通过InstanceList 手动放置的:

InstanceListEntry {
    id: redShip
    position: Qt.vector3d(50, 10, 100)
    eulerRotation: Qt.vector3d(0, 180, 0)
    color: "red"
    NumberAnimation on position.x {
        from: 50
        to: -70
        duration: 8000
    }
}

InstanceListEntry {
    id: greenShip
    position: Qt.vector3d(0, 0, -60)
    eulerRotation: Qt.vector3d(-10, 0, 30)
    color: "green"
}

InstanceListEntry {
    id: blueShip
    position: Qt.vector3d(-100, -100, 0)
    color: "blue"
}

InstanceList {
    id: manualInstancing
    instances: [ redShip, greenShip, blueShip ]
}

InstanceListEntry 的属性是可绑定的:在这里,我们将红色飞船制作成动画,使其穿过摄像机的路径。

最后,我们将实例化表格应用到对象中:

Asteroid {
    instancing: randomInstancing
    NumberAnimation on eulerRotation.x {
        from: 0
        to: 360
        duration: 11000
        loops: Animation.Infinite
    }
}

SimpleSpaceship {
    instancing: manualInstancing
}

通过为小行星模型的旋转设置动画,所有实例都将旋转,而无需更改实例表的内容。由于小行星实例的旋转是随机的,因此所有小行星都将围绕不同的轴旋转。

示例项目 @ 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.