- Qt 6.12
- Qt Quick 3D
- Qt Quick 3D Examples and Tutorials
- Qt Quick 3D - Baked Lightmap Example
- Qt Quick 3D | Commercial or GPLv3

# Qt Quick 3D - Baked Lightmap Example

Demonstrates the use of baked lightmaps in a 3D scene.

This example demonstrates using fully baked lighting in a scene. As a result, the rendered scene looks a lot more realistic, mainly because of the added indirect illumination. With the help of a checkbox it also allows disabling lightmapping, thus allowing comparing the rendering results of the real-time and fully baked approaches.

#### Setting up the Light

The scene has a single point light. When the checkbox is checked, the bakeMode of the light is set to Light.BakeModeAll.

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

Setting the property to a value other than Light.BakeModeDisabled serves a double purpose: at lightmap baking time it indicates that the light is part of the lightmapped scene, contributing to it. At run time, when rendering the scene normally, it indicates the light is a fully or partially baked light, which leads to disabling certain real time calculations in materials for lightmapped models. This is why the checkbox toggles `both` the bakeMode property on the Light and also the enabled property of the BakedLightmap associated with the Model objects.

#### Setting up the Model

The Cornell box is a Model with 8 submeshes. This is encapsulated as the Box type, an instance of which is instantiated in the main scene.

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

This model both participates in the lightmapped scene, and has a lightmap baked for it. The former is indicated by the usedInBakedLighting property. This alone does not ensure our box miniscene gets a lightmap baked for it, only that it participates in lightmapping (i.e. the light rays can bounce off the model when calculating indirect illumination).

To get the lightmap fully generated and then stored during the baking process, a BakedLightmap object with enabled set to `true` is set as well. The key specifies a unique identifier.

For simple deployment, the application can pack the generated lightmaps (`lightmaps.bin` file) into the execuatble via the Qt resource system. When rebaking the lightmap, the application needs to be built again so the changed file is picked up. The source for the lightmaps file is set in Lightmapper through the source property.

Example project @ code.qt.io