Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquick3dlightmapper.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5
7
9
10/*!
11 \qmltype Lightmapper
12 \inherits QtObject
13 \inqmlmodule QtQuick3D
14 \brief Specifies lightmap baking settings for a scene.
15 \since 6.4
16
17 Used when baking direct and indirect lighting. Most of these settings are not
18 relevant at other times, such as when using already generated lightmaps to
19 render a scene. The exception is \l source, though this has a sensible default
20 for development.
21
22 On a successfull bake a single file will be generated at the value specified by
23 \l source. This binary file contains the results of the bake, including the
24 per-model lightmaps and the mesh files with lightmap-compatible UVs.
25 The individual model data is accessed via \l BakedLightmap::key.
26
27 The data contained in the resulting lightmap file is all tightly coupled
28 to each other and to the current scene state. This means that any modifications
29 to the original mesh files, Lightmapper settings or other scene changes will
30 require a new bake to be executed to see the updated result.
31
32 \note As of Qt 6.4, lightmap baking is in an early technical preview state.
33 Changes to features, quality, and API are likely to happen in future releases.
34
35 The Lightmapper object works in combination with:
36
37 \list
38 \li \l Model::bakedLightmap and the associated \l BakedLightmap,
39 \li \l Model::usedInBakedLighting and \l Model::texelsPerUnit,
40 \li \l Light::bakeMode,
41 \li the engine's built-in lightmap baker.
42 \endlist
43
44 \sa {Lightmaps and Global Illumination},
45 {Qt Quick 3D - Baked Lightmap Example},
46 {Qt Quick 3D - SSGI Lightmap Example}
47 */
48
49/*!
50 \qmlproperty real Lightmapper::opacityThreshold
51
52 The opacity (alpha) threshold below which an object is ignored in ray -
53 mesh intersections when calculating lighting via raytracing. When the
54 opacity falls below the threshold, the model (submesh) will not occlude
55 lights and thus will not generate shadows either.
56
57 The default value is 0.5.
58
59 \note The lightmapper takes the \l{PrincipledMaterial::opacity}{material's
60 opacity} and the \l{PrincipledMaterial::baseColor}{baseColor alpha}
61 combined with the \l{PrincipledMaterial::baseColorMap}{base color map's
62 alpha} into account. Other sources of semi-transparency, such as the
63 opacity map or alpha cut-off settings are ignored during the lightmap
64 baking process.
65 */
66
67/*!
68 \qmlproperty real Lightmapper::bias
69
70 Raycasting bias used during baking. Adapt the value in case artifacts
71 occur, for example in order to reduce undesired shadowing patterns. In many
72 cases the default value is sufficient.
73
74 The default value is 0.005.
75 */
76
77/*!
78 \qmlproperty bool Lightmapper::adaptiveBiasEnabled
79
80 Enables applying an additional, dynamic bias based on the surface normal.
81
82 The default value is true.
83 */
84
85/*!
86 \qmlproperty bool Lightmapper::indirectLightEnabled
87
88 Normally there is no need to change this value. The default value is true.
89 Setting this property to false disables indirect light computation during
90 lightmap baking. Thus the resulting texture maps will only contain direct
91 light information. At run time, the engine will continue to use the maps
92 normally, assuming they contain both direct and indirect lighting.
93 */
94
95/*!
96 \qmlproperty int Lightmapper::samples
97
98 The number of samples per lightmap texel.
99
100 The default value is 256.
101
102 The value heavily affects both the performance and quality of the resulting
103 lightmaps during lightmap baking.
104 */
105
106/*!
107 \qmlproperty int Lightmapper::indirectLightWorkgroupSize
108
109 The size of the sample workgroups. These workgroups are attempted to be
110 executed in parallel. (the exact behavior depends on the number of CPU
111 cores and the QThreadPool configuration)
112
113 The default value is 32. With the default sample count of 256 this means
114 attempting to run 8 groups in parallel per model.
115 */
116
117/*!
118 \qmlproperty int Lightmapper::bounces
119
120 The maximum number of indirect light bounces per sample. The value should
121 at least be 1, no point in indirect light calculation otherwise.
122
123 The default value is 3.
124
125 The value heavily affects both the performance and quality of the resulting
126 lightmaps during lightmap baking.
127*/
128
129/*!
130 \qmlproperty real Lightmapper::indirectLightFactor
131
132 Multiplier for the indirect light amount. While it is the value of 1 (i.e.,
133 not affecting the indirect light amount calculation) that provides the
134 strictly correct rendering results, a slightly higher value can often give
135 better looking results when using the lightmap, even with a lower number of
136 bounces.
137
138 The default value is 1.
139 */
140
141/*!
142 \qmlproperty url Lightmapper::source
143 \since 6.10
144 \default file:lightmaps.bin
145
146 The url for loading the lightmap file at runtime and the output file path
147 on a successful bake.
148
149 When baking, the output path will be deduced from the url and it needs to
150 resolve to a regular file location that is writable. By default the value
151 is \c{file:lightmaps.bin}, which means the file will be put in the current
152 working directory and the final result will instantly appear on a successful
153 bake.
154
155 In the same directory as the output file, a \c{.raw} file is created that
156 contains extra lightmap data used before creating the final lightmap file.
157 This makes it possible to do just denoising without having to bake the whole
158 scene between runs, assuming the \c{.raw} file is present.
159
160 If you want to read the lightmap as a QRC resource you need to embed
161 it in the usual way and add a \c{:/} or \c{qrc:/} prefix to the url.
162 The following example always tries to load the lightmap file embedded via
163 resources. First set the value to a writable location and bake. Then copy the
164 generated file into the source directoy. Then by listing the file in the
165 application's CMake project as a resource under the \c{/lightmaps} PREFIX,
166 lets the build process pick up the file and include it in the executable.
167
168 \qml
169 Lightmapper {
170 source: "qrc:/lightmaps/lightmaps.bin"
171 // will attempt to load from :/lightmaps/lightmaps.bin at runtime
172 // and write a file to lightmaps/lightmaps.bin when baking.
173 }
174 \endqml
175 */
176
177/*!
178 \qmlproperty real Lightmapper::denoiseSigma
179 \since 6.10
180 \default 8
181
182 This property defines the sigma value of the Non-local means based denoiser.
183 This means that the higher this value is the stronger the blurring will be.
184 Try to keep this value as low as possible to avoid losing visual features
185 while still removing the noise.
186*/
187
188/*!
189 \qmlproperty real Lightmapper::texelsPerUnit
190 \since 6.10
191 \default 1
192
193 This property defines the unit to texel scale, meaning a \c{1x1} quad with
194 texelsPerUnit of \c{32} will take up approximately \c{32x32} texels in the
195 lightmap.
196
197 \sa Model::texelsPerUnit
198*/
199
200float QQuick3DLightmapper::opacityThreshold() const
201{
202 return m_opacityThreshold;
203}
204
205float QQuick3DLightmapper::bias() const
206{
207 return m_bias;
208}
209
210bool QQuick3DLightmapper::isAdaptiveBiasEnabled() const
211{
212 return m_adaptiveBias;
213}
214
215bool QQuick3DLightmapper::isIndirectLightEnabled() const
216{
217 return m_indirectLight;
218}
219
220int QQuick3DLightmapper::samples() const
221{
222 return m_samples;
223}
224
225int QQuick3DLightmapper::indirectLightWorkgroupSize() const
226{
227 return m_workgroupSize;
228}
229
230int QQuick3DLightmapper::bounces() const
231{
232 return m_bounces;
233}
234
235float QQuick3DLightmapper::indirectLightFactor() const
236{
237 return m_indirectFactor;
238}
239
240QUrl QQuick3DLightmapper::source() const
241{
242 return m_source;
243}
244
245void QQuick3DLightmapper::setOpacityThreshold(float opacity)
246{
247 if (m_opacityThreshold == opacity)
248 return;
249
250 m_opacityThreshold = opacity;
251 emit opacityThresholdChanged();
252 emit changed();
253}
254
255void QQuick3DLightmapper::setBias(float bias)
256{
257 if (m_bias == bias)
258 return;
259
260 m_bias = bias;
261 emit biasChanged();
262 emit changed();
263}
264
265void QQuick3DLightmapper::setAdaptiveBiasEnabled(bool enabled)
266{
267 if (m_adaptiveBias == enabled)
268 return;
269
270 m_adaptiveBias = enabled;
271 emit adaptiveBiasEnabledChanged();
272 emit changed();
273}
274
275void QQuick3DLightmapper::setIndirectLightEnabled(bool enabled)
276{
277 if (m_indirectLight == enabled)
278 return;
279
280 m_indirectLight = enabled;
281 emit indirectLightEnabledChanged();
282 emit changed();
283}
284
285void QQuick3DLightmapper::setSamples(int count)
286{
287 if (m_samples == count)
288 return;
289
290 m_samples = count;
291 emit samplesChanged();
292 emit changed();
293}
294
295void QQuick3DLightmapper::setIndirectLightWorkgroupSize(int size)
296{
297 if (m_workgroupSize == size)
298 return;
299
300 m_workgroupSize = size;
301 emit indirectLightWorkgroupSizeChanged();
302 emit changed();
303}
304
305void QQuick3DLightmapper::setBounces(int count)
306{
307 if (m_bounces == count)
308 return;
309
310 m_bounces = count;
311 emit bouncesChanged();
312 emit changed();
313}
314
315void QQuick3DLightmapper::setIndirectLightFactor(float factor)
316{
317 if (m_indirectFactor == factor)
318 return;
319
320 m_indirectFactor = factor;
321 emit indirectLightFactorChanged();
322 emit changed();
323}
324
325void QQuick3DLightmapper::setSource(const QUrl &source)
326{
327 if (m_source == source)
328 return;
329
330 m_source = source;
331 emit sourceChanged();
332 emit changed();
333}
334
335float QQuick3DLightmapper::denoiseSigma() const
336{
337 return m_denoiseSigma;
338}
339
340void QQuick3DLightmapper::setDenoiseSigma(float newDenoiseSigma)
341{
342 if (qFuzzyCompare(m_denoiseSigma, newDenoiseSigma))
343 return;
344 m_denoiseSigma = newDenoiseSigma;
345 emit denoiseSigmaChanged();
346 emit changed();
347}
348
349float QQuick3DLightmapper::texelsPerUnit() const
350{
351 return m_texelsPerUnit;
352}
353
354void QQuick3DLightmapper::setTexelsPerUnit(float newTexelsPerUnit)
355{
356 if (qFuzzyCompare(m_texelsPerUnit, newTexelsPerUnit))
357 return;
358 m_texelsPerUnit = newTexelsPerUnit;
359 emit texelsPerUnitChanged();
360 emit changed();
361}
362
363QT_END_NAMESPACE
Combined button and popup list for selecting options.