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 ”属性、Light对象的bakeMode属性,以及与Model对象关联的BakedLightmap 的enabled属性。
设置模型
康奈尔盒子是一个包含 8 个子网格的模型。它被封装为 Box 类型,其实例在主场景中被实例化。
Box {
usedInBakedLighting: true
bakedLightmap: BakedLightmap {
enabled: root.lmEnabled
key: "box"
}
scale: Qt.vector3d(100, 100, 100)
}该模型既参与光照贴图场景的渲染,也拥有为其烘焙的光照贴图。 前者由usedInBakedLighting 属性指示。仅此并不能确保我们的盒子微场景会为其烘焙光照贴图,仅表示它参与光照贴图生成(即在计算间接照明时,光线可以从模型上反射)。
为了在烘焙过程中完全生成并存储光照贴图,还需设置一个 `BakedLightmap ` 对象,并将 `enabled ` 设为 `true `。`key ` 指定了一个唯一标识符。
为了简化部署,应用程序可以通过 Qt 资源系统将生成的光照贴图(lightmaps.bin 文件)打包到可执行文件中。当重新烘焙光照贴图时,需要重新构建应用程序,以便系统识别已更改的文件。光照贴图文件的源路径通过Lightmapper 中的source 属性进行设置。
© 2026 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.