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-instancing.qdoc
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5
6\title Instanced Rendering
7\page quick3d-instancing
8
9\section1 Introduction
10
11Qt Quick 3D supports instancing of \l [QtQuick3D | QML] {Model} objects. Instancing refers to a
12technique where one object is rendered multiple times with a single draw call. (For example the
13OpenGL function \c glDrawElementsInstanced.)
14
15Instancing allows duplicating a model with variations. In contrast to using a \l{Repeater3D}, the
16model and its graphics resources are only allocated once. The rendering of the duplicated instances
17is done at a low level by the GPU. Depending on the complexity of the model, this can give a
18performance improvement of several orders of magnitude.
19
20In practice, instancing is done by defining a table that specifies how each instance is modified
21relative to the base model.
22
23\section1 Instancing API
24
25The main principle of the instancing API is that it is explicit: It doesn't try to autodetect
26opportunities for instancing within the existing API. Instead, each model is marked individually by
27setting its \l{Model::instancing}{instancing} property to reference an \l [QtQuick3D | QML]
28{QtQuick3D::Instancing}{Instancing} object. The same Instancing object can be used on multiple
29models at the same time.
30
31The Instancing object specifies a table that defines how each copy is rendered. The available modifications are:
32\list
33\li \e{transformation}: position, rotation, and scale
34\li \e{color}: a color that is blended with the model’s material
35\li \e{custom data}: data that can be used by custom materials
36\endlist
37
38Qt provides three QML types that inherit from Instancing:
39\list
40\li \l {InstanceList} enumerates all instances and allows binding to the properties of each instance.
41\li \l {RandomInstancing} provides a way to quickly test and prototype by generating random instances within defined bounds.
42\li \l {FileInstancing} reads an instance table from an external file.
43\endlist
44
45The \l{Qt Quick 3D - Instanced Rendering Example}{instancing example} shows how to create a scene using the QML API.
46
47Other kinds of instance tables can be defined in C++ by subclassing \l{QQuick3DInstancing}. For example, the
48\l {ParticleSystem3D}{particle system} uses its own instancing table internally. It is available as
49\l{ModelParticle3D::instanceTable}{ModelParticle3D.instanceTable}.
50
51By writing custom shader code, it is possible to use instancing to control additional properties,
52such as variables for physically based rendering, skeletal animation weights, distortion, or anything
53else that can be expressed with custom materials. The custom data in the instancing table consists
54of four floating point numbers.
55
56The \l{Qt Quick 3D - Custom Instanced Rendering}{custom instancing example} shows how to combine
57custom materials and an instance table implemented in C++.
58
59
60\section1 Alpha-blending and instancing
61
62Correct alpha blending requires that semi-transparent objects are rendered back-to-front. For this
63reason, QtQuick3D sorts opaque and semi-transparent objects separately, and renders them in the
64correct order. With instancing, however, the GPU will render the instances in the order specified by
65the instancing table, if \l {Instancing::depthSortingEnabled}{depth-sorting} is not turned on.
66When the \l {instanced-lod}{level of detail} of each instance is selected automatically, they are
67rendered one level at a time instead, the nearest level first, or the farthest for semi-transparent
68models, keeping the table order within each level.
69For performance reasons, QtQuick3D does not sort the table by default as it can take long time with
70large number of instances. This means that if semi-transparent instances overlap with each other,
71or with other semi-transparent objects, the results may look wrong. In general, the error is less
72visible when the opacity is low.
73
74A depth sorted instance table is drawn in one draw call, in the sorted order. So when the geometry
75of the model has \l {instanced-lod}{levels of detail}, all the instances in a sorted table are drawn
76at one level instead of at a level selected for each instance: the original geometry, or the level
77set by \l {Model::instancingLodFactor}{instancingLodFactor}. To blend semi-transparent instances
78correctly and keep the level of each instance, leave the table unsorted and use order independent
79transparency instead, set with \l {SceneEnvironment::oitMethod}{SceneEnvironment.oitMethod}, which
80does not depend on the order the instances are drawn in.
81
82Fully opaque objects together with non-overlapping semi-transparent objects will always be rendered
83correctly, since Qt uses depth buffer testing to avoid drawing behind opaque objects. However, the
84lack of sorting has potential performance implications for opaque objects: They may not be rendered
85in the optimal order, meaning that the same pixel can be written multiple times, making more work
86for the fragment shader.
87
88The renderer does not inspect the contents of the instancing table, so it must be specified
89explicitly when an instance table contains semi-transparent alpha values: Set the
90\l{Instancing::hasTransparency}{hasTransparency} property to to \c true to make sure that the renderer enables alpha
91blending. This applies to all the instances: Even fully opaque instances will be rendered
92without depth testing, potentially causing visible errors.
93
94The rendering order relative to the rest of the scene can be adjusted by setting the \l{Model::depthBias}{depth bias} of the model.
95
96\section1 Transforms and instancing
97
98Each instance has its own transform in the instance table. This is combined with the transforms on
99the instanced model. This is slightly complex, since there are several use cases:
100
101\list
102\li Doing a transform on the model that is applied to each individual instance. This allows cheap
103animations, for example by rotating all instances at once without having to change the instance table.
104\li Transforming the entire group of instances at once.
105\li Instancing a model hierarchy.
106\endlist
107
108To support all these cases, The model’s transform is split into two parts: the
109\e{local instance transform}, and the \e{global instance transform}.
110Conceptually, instancing is performed like this:
111
112\list
113\li First the model is transformed according to the local instance transform.
114\li Then each instance is calculated by applying the instance table transform
115\li Finally, the whole group of instanced objects is transformed according to the global instance transform.
116\endlist
117
118By default, the local instance transform of a model consists of the model’s scale and rotation,
119while the rest goes into the global instance transform.
120
121This can be controlled by setting the model’s \l{Model::instanceRoot}{instanceRoot} property. This
122defines the origin of the instance’s coordinate system. The most common use is when instancing a
123hierarchy of models. For example, a sphere orbiting around a cube:
124
125\qml
126Model {
127 id: cube
128 instancing: someInstanceTable
129 source: "#Cube"
130 materials: DefaultMaterial { diffuseColor: "lightgray" }
131 Node {
132 Model {
133 source: "#Sphere"
134 instanceRoot: cube
135 instancing: cube.instancing
136 x: 150
137 materials: DefaultMaterial { diffuseColor: "gray" }
138 }
139 NumberAnimation on eulerRotation.y {
140 from: 0
141 to: 360
142 duration: 4000
143 loops: Animation.Infinite
144 }
145 }
146}
147\endqml
148
149The \c instanceRoot is necessary to specify that the sphere instance should be positioned as if it were an element of the cube.
150Each model in a hierarchy still needs to specify the \c instancing property: in normal cases they should all be set to the same \c Instancing object.
151
152\c instanceRoot can also be used when instancing a single model. For example, a cylinder rotating around an off-center point:
153\qml
154 Node {
155 id: parentNode
156 Model {
157 source: "#Cylinder"
158 instanceRoot: parentNode
159 instancing: anotherInstanceTable
160 x: 25
161 materials: DefaultMaterial { diffuseColor: "white" }
162 }
163 NumberAnimation on eulerRotation.y {
164 from: 0
165 to: 360
166 duration: 1000
167 loops: Animation.Infinite
168 }
169}
170\endqml
171
172\section1 Picking and instancing
173
174\l{View3D::pick}{Picking} is a mechanism that enables selecting a model from a user interface
175interaction. With instanced rendering, there are several representations of the same model, so the
176pick result will include an \l {pickResult::instanceIndex}{instance index}. Instanced picking is
177enabled by setting the \l {Model::pickable}{pickable} property on the base model.
178
179*/