Qt Quick 3D - 烘焙光贴图示例

演示在 3D 场景中使用烘焙光贴图。

本示例演示了在场景中使用完全烘焙光照。结果,渲染后的场景看起来更加逼真,主要是因为增加了间接照明。在复选框的帮助下,还可以禁用光贴图,从而比较实时和完全烘焙两种方法的渲染效果。

设置灯光

场景中只有一个点光源。选中复选框后,灯光的bakeMode 设置为 Light.BakeModeAll。

PointLight {
    bakeMode: root.lightBakeMode
    y: 190
    brightness: brightnessSlider.value
    castsShadow: true
    shadowFactor: 75
    shadowBias: 20
}

将该属性设置为Light.BakeModeDisabled 以外的值有双重作用:在光贴图烘焙时,它表示灯光是光贴图场景的一部分,对其有贡献。在运行时,当正常渲染场景时,它表示灯光是完全或部分烘焙的灯光,这将导致禁用光贴图模型材质中的某些实时计算。这就是为什么复选框会切换both 灯光的烘焙模式(bakeMode)属性以及与模型对象相关的BakedLightmap 的启用属性。

设置模型

科内尔方框是一个包含 8 个子网格的模型。它被封装为盒子类型,其实例在主场景中实例化。

Box {
    usedInBakedLighting: true
    lightmapBaseResolution: 256
    bakedLightmap: BakedLightmap {
        enabled: root.lmEnabled
        key: "box"
        loadPrefix: "file:"
    }
    scale: Qt.vector3d(100, 100, 100)
}

该模型既可以在光贴图场景中participates ,也可以在光贴图baked for it 。前者由usedInBakedLighting 属性表示。仅凭这一点并不能确保我们的盒状微型场景获得光贴图,只能确保它参与了光贴图(即在计算间接光照时光线可以从模型上反弹)。

为了在烘焙过程中完全生成并存储光贴图,还需要设置一个BakedLightmap 对象,并将enabled 设置为truekey 指定了一个唯一标识符。

为了便于部署,应用程序会通过 Qt XML 资源系统将生成的光贴图(.exr 文件)打包到可执行文件中。CMakeLists.txt 列出了要作为资源添加的qlm_box.exr 。在重新制作光贴图时,应用程序需要再次构建,这样更改后的文件才能被接收。为确保引擎能在运行时找到该文件,需要设置loadPrefix

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