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
qtquick3d-lod.qdoc
Go to the documentation of this file.
1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qtquick3d-lod.html
6\title Qt Quick 3D Level of Detail
7\brief An overview of the usages of Level of Detail in Qt Quick 3D.
8
9\section1 Level of Detail (LoD)
10
11Level of Detail (LoD) refers to the technique of using alternative versions of a 3D
12model at different distances from the camera. The goal of doing this is to optimize
13resource usage by using simpler versions of the model when it is far away or a small
14share of the screen, while using more detailed versions when it is closer or larger
15portion of screen space. This approach allows for efficient use of GPU resources
16while maintaining visual fidelity.
17
18Qt Quick 3D provides both an automatic as well as an explicit level of detail system.
19The automatic system is based on using a single mesh with multiple levels of detail
20stored in the mesh file. The explicit system is based on using a LodManager component
21to specify the different levels of detail as child models.
22
23\section1 Automatic Level of Detail
24
25The primary way to use the the automatic level of detail support is by importing
26content with the \l {Balsam Asset Import Tool}. By using the flag
27\c {--generateMeshLevelsOfDetail} when importing assets, the tool will generate
28levels of detail for each mesh when possible. This is done by trying to simplify the
29geometry of the original mesh, while still maintaining the overall features. What
30results is additional geometry as well as new index values for each additional
31level of detail, as well as a distance factor used to determine which
32mesh to use for a given render size. This distance factor is related to the distance
33of the object being rendered to the camera, but the most important thing is having a
34good ratio of geometry per pixel. This data is written to the .mesh file and will
35be used automatically when used by a Model.
36
37\image lod_balsamui.png
38 {Balsam UI settings with LOD generation option highlighted}
39
40A tradeoff to the automatic system, and the reason why it is not enabled by default
41is that it is possible that when the geometry is reduced, visual artifacts can also
42be introduced. The geometry simplification algorithm used attempts to preserve
43the features of the model, but in some cases it may be necessary to recalculate
44the normals of the mesh to maintain the intended visual appearance. This can be
45done by using the \c {--recalculateLodNormals} flag when importing the asset.
46The normal recalculation process can be further tuned by passing euler angle
47values to the arguments \c {--recalculateLodNormalsMergeAngle} and
48\c {--recalculateLodNormalsSplitAngle} to control the angle at which normals
49are merged or split respectively.
50
51The main advantage of the automatic system is that it is easy to use once the
52asset has been imported with the appropriate settings. No additional code is
53required to use the automatic system and any Models that reference the generated
54mesh files containing levels of detail geometry will automatically choose the
55appropriate geometry based on how many pixels the model takes up on the screen.
56
57It is possible to tweak automatic level of detail selection behavior
58either at the \l {Camera::levelOfDetailBias}{global level} or at the
59\l {Model::levelOfDetailBias}{per-model level} by using the \c levelOfDetailBias
60property of either \l Camera or \l Model. The default level of 1.0 of each
61of these bias properties means to trust the value calculated by the automatic
62system. This value is a bias to the ideal value such that a value smaller
63than 1.0 will require an even smaller rendered size before switching to a
64lesser level of detail. Values above 1.0 will lead to lower levels of detail
65being used sooner. A value of 0.0 will disable the usage of levels of detail
66completely and always use the original mesh geometry.
67
68The automatic system is not as flexible as the explicit system. For example
69the automatic system always uses the same material for all levels of detail,
70which may not always be desirable. Another potential downside is that there
71is not transition between the different levels of detail which ideally
72should not be necessary with automatic level of detail, but may be desireable
73for certain use cases.
74
75\target instanced-lod
76\section2 Instanced models
77
78A \l Model drawn with \l {Instancing}{instancing} does not get one level of
79detail for the whole model: the instances can be spread over a large area, and
80one distance would not fit them all. Instead, each instance gets its own level,
81selected the same way as for a model without instancing: with the same ratios
82and the same \c levelOfDetailBias, by the depth along the view direction to the
83nearest point of the bounds, which for an instance is a sphere around it. The
84scale of an instance counts like the scale of a model: an instance that is drawn
85twice as large switches level at twice the distance. With an
86\l OrthographicCamera the distance does not change the size on screen, so the
87level of each instance follows its scale and the magnification of the camera.
88
89At most 12 levels are used for the instances: the original geometry and the
90first 11 levels of detail of the mesh. Instances that would get a coarser level
91get the last of these. \l {Model::instancingLodFactor}{instancingLodFactor} can
92still select any level of the mesh.
93
94Each level is drawn with a draw call of its own, with its instances in the
95order of the instance table. \c INSTANCE_INDEX in a \l CustomMaterial is still
96the row of each instance in the instance table.
97
98The levels are selected on the CPU, going through all the instances, so this
99increases the frame preparation time with large instance counts, as
100\l {Instancing::depthSortingEnabled}{depth sorting} does. This is done again
101when the model, the instance table or the camera changes, and not while they
102all stand still. A model with \l {Model::instancingLodMin}{instancingLodMin}
103or \l {Model::instancingLodMax}{instancingLodMax} that is drawn in several
104views with cameras in different places is the exception: its range, and so its
105levels, are selected again for each view in every frame.
106
107When the camera has \l {Camera::frustumCullingEnabled}{frustumCullingEnabled}
108set, this also leaves out the instances that are entirely outside the view of
109the camera, which otherwise would be processed by the GPU only to be clipped.
110This is not done when the view renders several views at once, as for XR, where
111the instances are drawn once for all of them.
112
113The level is selected for each instance when the mesh has levels of detail
114with an index buffer and level of detail is enabled on both the model and the
115camera (see \l {Model::levelOfDetailBias}{Model.levelOfDetailBias} and
116\l {Camera::levelOfDetailBias}{Camera.levelOfDetailBias}). In the following
117cases, all the instances get one level instead:
118
119\list
120\li The model sets \l {Model::instancingLodFactor}{instancingLodFactor}, which
121 picks one level for all the instances by hand, from \c 0.0 for the original
122 geometry to \c 1.0 for the coarsest level.
123\li The instance table is \l {Instancing::depthSortingEnabled}{depth sorted}.
124 It is drawn in one draw call, in the sorted order. Drawn level by level, the
125 instances would only be back to front within each level, as a larger
126 instance switches level farther away than a smaller one, and
127 semi-transparent instances could blend in the wrong order.
128\endlist
129
130The level is then the one given by \c instancingLodFactor, which is the
131original geometry when it is not set.
132
133To draw semi-transparent instances with a level for each instance, leave the
134instance table unsorted and set
135\l {SceneEnvironment::oitMethod}{SceneEnvironment.oitMethod} instead. Order
136independent transparency blends the instances in the right order whatever
137order they are drawn in.
138
139\l {Model::instancingLodMin}{instancingLodMin} and
140\l {Model::instancingLodMax}{instancingLodMax} limit the instances a model
141draws to a range of distances from the camera, in every pass. The level is
142then selected among the instances in the range, so a model can leave out the
143instances beyond some distance and still get the automatic level for the
144rest. Several models can also share one instance table and draw different
145ranges of it, each with its own material, and with the automatic level or
146with a level chosen by \c instancingLodFactor.
147
148When the level is selected for each instance, the shadow maps, reflection
149probes and motion vectors are drawn with the original geometry instead. They
150draw all the instances in the range of
151\c instancingLodMin and \c instancingLodMax, also those outside the view of the
152camera.
153
154To see which level each model or instance is drawn with, set
155\l {DebugSettings::drawLevelsOfDetail}{DebugSettings.drawLevelsOfDetail}.
156
157\section1 Explicit Level of Detail
158
159The Explicit level of detail system is more flexible than the automatic
160system, but requires more work to use. The explicit system is based on using
161a LodManager component to specify the different levels of detail as child
162\l Model components. Any children of the LodManager that are instances of Model
163will be considered a level of detail. This is quite powerful because each level of
164detail can be a completely custom model, with it's own geometries and materials.
165
166The LodManager component also has a \l {LodManager::distances}{distances} property which is used to
167determine which child \l Model to use. The LodManager node will transition to the
168next child \l Model as the camera distance approach each distance boundary.
169The \l {LodManager::fadeDistance}{fadeDistance} property can be used to set at which
170distance the cross fade transition begins and ends around the distances boundaries.
171
172This is an \l {Qt Quick 3D - Level of Detail Helper Example}{example} of how to use
173the explicit level of detail system:
174
175\image lodhelper-example.jpg
176 {Bust sculpture with instanced objects at varying detail levels}
177
178In the referenced example the \l LodManager is used to explicitly specify different mesh
179files for each level of detail of the marble bust.
180
181When a child of the LodManager is a model that uses
182\l {Instancing}{instancing}, the LodManager sets its
183\l {Model::instancingLodMin}{instancingLodMin} and
184\l {Model::instancingLodMax}{instancingLodMax} from the distances instead of
185its visibility, so that each instance is drawn by the child that matches its
186own distance, and such children are not cross-faded. When the geometry of such a child
187has levels of detail of its own, the level of each instance in its range is
188also selected \l {instanced-lod}{automatically}. Set
189\l {Model::instancingLodFactor}{instancingLodFactor} to \c 0.0 on the child to
190draw its original geometry instead.
191
192\snippet lodhelper/main.qml example
193
194For this code this is a diagram of what is being defined which points out how the distances
195list is used to define where the boundaries between the different levels of detail as well
196as the fadeDistance property which is used to define the cross fade transition.
197
198\image lodmanager_diagram.png {LOD system architecture diagram}
199
200*/