1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
|
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "qquick3dmodel_p.h"
#include "qquick3dobject_p.h"
#include "qquick3dscenemanager_p.h"
#include "qquick3dnode_p_p.h"
#include "qquick3dinstancing_p.h"
#include <QtQuick3DRuntimeRender/private/qssgrendergraphobject_p.h>
#include <QtQuick3DRuntimeRender/private/qssgrendercustommaterial_p.h>
#include <QtQuick3DRuntimeRender/private/qssgrenderdefaultmaterial_p.h>
#include <QtQuick3DRuntimeRender/private/qssgrendermodel_p.h>
#include <QtQuick3DUtils/private/qssgutils_p.h>
#include <QtQml/QQmlFile>
QT_BEGIN_NAMESPACE
/*!
\qmltype Model
\inherits Node
\inqmlmodule QtQuick3D
\brief Lets you load a 3D model data.
The Model item makes it possible to load a mesh and modify how its shaded, by adding materials
to it. For a model to be renderable, it needs at least a mesh and a material.
\section1 Mesh format and built-in primitives
The model can load static meshes from storage or one of the built-in primitive types.
The mesh format used is a run-time format that's native to the engine, but additional formats are
supported through the asset import tool \l {Balsam Asset Import Tool}{Balsam}.
The built-in primitives can be loaded by setting the \c source property to one of these values:
\c {#Rectangle, #Sphere, #Cube, #Cylinder or #Cone}.
\qml
Model {
source: "#Sphere"
}
\endqml
The table below describes the built-in meshes in more detail.
\table
\header
\li Name
\li Description
\li No. faces
\row
\li #Cone
\li A cone with a height and diameter of \c{100}. The base is at the
origin and the cone is pointing upwards in the y direction.
\li \c{78}
\row
\li #Cube
\li A cube centered at the origin with \c{100} long sides along x, y and z.
\li \c{12}
\row
\li #Cylinder
\li A cylinder with \c{100} height and diameter centered at the origin
expanding along the y direction.
\li \c{316}
\row
\li #Rectangle
\li A rectangle with \c{100} width and height centered at the origin
lying in the xy plane.
\li \c{2}
\row
\li #Sphere
\li A sphere with \c{100} diameter centered at the origin.
\li \c{4900}
\endtable
\section2 Custom geometry
In addition to using static meshes, you can implement a
\l {QQuick3DGeometry}{custom geometry} provider that provides the model with
custom vertex data at run-time. See the
\l {Qt Quick 3D - Custom Geometry Example}{Custom Geometry Example} for an
example on how to create and use a custom material with your model.
\section1 Materials
A model can consist of several sub-meshes, each of which can have its own
material. The sub-mesh uses a material from the \l{materials} list,
corresponding to its index. If the number of materials is less than the
sub-meshes, the last material in the list is used for subsequent sub-meshes.
This is demonstrated in the
\l {Qt Quick 3D - Sub-mesh Example}{Sub-mesh example}.
You can use the following materials with the model item:
\l {PrincipledMaterial}, \l {DefaultMaterial}, and \l {CustomMaterial}.
\section1 Picking
\e Picking is the process of sending a ray through the scene from some
starting position to find which models intersect with the ray. In
Qt Quick 3D, the ray is normally sent from the view using 2D coordinates
resulting from a touch or mouse event. If a model was hit by the ray,
\l {pickResult} will be returned with a handle to the model and information
about where the ray hit the model. For models that use
\l {QQuick3DGeometry}{custom geometry}, the picking is less accurate than
for static mesh data, as picking is only done against the model's
\l {bounds}{bounding volume}. If the ray goes through more than one model,
the closest \l {Model::pickable}{pickable} model is selected.
Note that for models to be \l {Model::pickable}{pickable}, their
\l {Model::pickable}{pickable} property must be set to \c true. For more
information, see \l {Qt Quick 3D - Picking example}.
*/
/*!
\qmlvaluetype bounds
\inqmlmodule QtQuick3D
\since 5.15
\brief Specifies the bounds of a model.
bounds specify a bounding box with minimum and maximum points.
bounds is a readonly property of the model.
*/
/*!
\qmlproperty vector3d bounds::minimum
Specifies the minimum point of the model bounds.
\sa maximum
*/
/*!
\qmlproperty vector3d bounds::maximum
Specifies the maximum point of the model bounds.
\sa minimum
*/
QQuick3DModel::QQuick3DModel(QQuick3DNode *parent)
: QQuick3DNode(*(new QQuick3DNodePrivate(QQuick3DNodePrivate::Type::Model)), parent) {}
QQuick3DModel::~QQuick3DModel()
{
disconnect(m_geometryConnection);
auto matList = materials();
qmlClearMaterials(&matList);
auto morphList = morphTargets();
qmlClearMorphTargets(&morphList);
}
/*!
\qmlproperty url Model::source
This property defines the location of the mesh file containing the geometry
of this Model or one of the built-in primitive meshes listed below
as described in \l {Mesh format and built-in primitives}.
\list
\li "#Rectangle"
\li "#Sphere"
\li "#Cube"
\li "#Cone"
\li "#Cylinder"
\endlist
*/
QUrl QQuick3DModel::source() const
{
return m_source;
}
/*!
\qmlproperty List<QtQuick3D::Material> Model::materials
This property contains a list of materials used to render the provided
geometry. To render anything, there must be at least one material. Normally
there should be one material for each sub-mesh included in the source
geometry.
\sa {Qt Quick 3D - Sub-mesh Example}
*/
QQmlListProperty<QQuick3DMaterial> QQuick3DModel::materials()
{
return QQmlListProperty<QQuick3DMaterial>(this,
nullptr,
QQuick3DModel::qmlAppendMaterial,
QQuick3DModel::qmlMaterialsCount,
QQuick3DModel::qmlMaterialAt,
QQuick3DModel::qmlClearMaterials);
}
/*!
\qmlproperty List<QtQuick3D::MorphTarget> Model::morphTargets
This property contains a list of \l [QtQuick3D QML] {MorphTarget}{MorphTarget}s used to
render the provided geometry. Meshes should have at least one attribute
among positions, normals, tangents, binormals, texture coordinates,
and vertex colors for the morph targets.
\sa {MorphTarget}
*/
QQmlListProperty<QQuick3DMorphTarget> QQuick3DModel::morphTargets()
{
return QQmlListProperty<QQuick3DMorphTarget>(this,
nullptr,
QQuick3DModel::qmlAppendMorphTarget,
QQuick3DModel::qmlMorphTargetsCount,
QQuick3DModel::qmlMorphTargetAt,
QQuick3DModel::qmlClearMorphTargets);
}
/*!
\qmlproperty QtQuick3D::Instancing Model::instancing
If this property is set, the model will not be rendered normally. Instead, a number of
instances of the model will be rendered, as defined by the instance table.
\sa Instancing
*/
QQuick3DInstancing *QQuick3DModel::instancing() const
{
return m_instancing;
}
/*!
\qmlproperty QtQuick3D::Node Model::instanceRoot
This property defines the origin of the instance’s coordinate system.
See the \l{Transforms and instancing}{overview documentation} for a detailed explanation.
\sa instancing, Instancing
*/
QQuick3DNode *QQuick3DModel::instanceRoot() const
{
return m_instanceRoot;
}
// Source URL's need a bit of translation for the engine because of the
// use of fragment syntax for specifiying primitives and sub-meshes
// So we need to check for the fragment before translating to a qmlfile
QString QQuick3DModel::translateMeshSource(const QUrl &source, QObject *contextObject)
{
QString fragment;
if (source.hasFragment()) {
// Check if this is an index, or primitive
bool isNumber = false;
source.fragment().toInt(&isNumber);
fragment = QStringLiteral("#") + source.fragment();
// If it wasn't an index, then it was a primitive
if (!isNumber)
return fragment;
}
const QQmlContext *context = qmlContext(contextObject);
const auto resolvedUrl = context ? context->resolvedUrl(source) : source;
const auto qmlSource = QQmlFile::urlToLocalFileOrQrc(resolvedUrl);
return (qmlSource.isEmpty() ? source.path() : qmlSource) + fragment;
}
void QQuick3DModel::markAllDirty()
{
m_dirtyAttributes = 0xffffffff;
QQuick3DNode::markAllDirty();
}
/*!
\qmlproperty bool Model::castsShadows
When this property is \c true, the geometry of this model is used when
rendering to the shadow maps, and is also generating shadows in baked
lighting.
The default value is \c true.
*/
bool QQuick3DModel::castsShadows() const
{
return m_castsShadows;
}
/*!
\qmlproperty bool Model::receivesShadows
When this property is set to \c true, the model's materials take shadow
contribution from shadow casting lights into account.
\note When lightmapping is enabled for this model, fully baked lights with
Light::bakeMode set to Light.BakeModeAll will always generate (baked)
shadows on the model, regardless of the value of this property.
The default value is \c true.
*/
bool QQuick3DModel::receivesShadows() const
{
return m_receivesShadows;
}
/*!
\qmlproperty bool Model::pickable
This property controls whether the model is pickable or not. Set this property
to \c true to make the model pickable. Models are not pickable by default.
\sa {View3D::pick}
*/
bool QQuick3DModel::pickable() const
{
return m_pickable;
}
/*!
\qmlproperty Geometry Model::geometry
Specify a custom geometry for the model. The Model::source must be empty when custom geometry
is used.
*/
QQuick3DGeometry *QQuick3DModel::geometry() const
{
return m_geometry;
}
/*!
\qmlproperty Skeleton Model::skeleton
Contains the skeleton for the model. The Skeleton is used together with \l {inverseBindPoses}
for \l {Vertex Skinning}{skinning}.
\note Meshes of the model must have both joints and weights attributes.
\note If this property is set, skinning animation is enabled. It means
that \l {Model} is transformed based on \l {Skeleton} ignoring Model's global
transformation.
\sa {Model::inverseBindPoses}, {Qt Quick 3D - Simple Skinning Example}
*/
QQuick3DSkeleton *QQuick3DModel::skeleton() const
{
return m_skeleton;
}
/*!
\qmlproperty Skin Model::skin
Contains the skeleton for the model. The Skeleton is used for
\l {Vertex Skinning}{skinning}.
\note Meshes of the model must have both joints and weights attributes.
\note If this property is set, skinning animation is enabled. This means
the \l {Model} will be transformed based on \l {Skin::joints} and the
Model's global transformation will be ignored.
\note If a model has both a skeleton and a skin, then the skin will be used.
\sa {Model::skeleton} {Qt Quick 3D - Simple Skinning Example}
*/
QQuick3DSkin *QQuick3DModel::skin() const
{
return m_skin;
}
/*!
\qmlproperty List<matrix4x4> Model::inverseBindPoses
This property contains a list of Inverse Bind Pose matrixes used for the
skeletal animation. Each inverseBindPose matrix means the inverse of the
global transform of the repective \l {Joint::index} in \l {skeleton}, which
will be used initially.
\note This property is only used if the Model::skeleton is valid.
\note If some of the matrices are not set, identity values will be used.
\sa {skeleton} {Joint::index}
*/
QList<QMatrix4x4> QQuick3DModel::inverseBindPoses() const
{
return m_inverseBindPoses;
}
/*!
\qmlproperty Bounds Model::bounds
The bounds of the model descibes the extents of the bounding volume around the model.
\note The bounds might not be immediately available if the model needs to be loaded first.
\readonly
*/
QQuick3DBounds3 QQuick3DModel::bounds() const
{
return m_bounds;
}
/*!
\qmlproperty real Model::depthBias
Holds the depth bias of the model. Depth bias is added to the object distance from camera when sorting
objects. This can be used to force rendering order between objects close to each other, that
might otherwise be rendered in different order in different frames. Negative values cause the
sorting value to move closer to the camera while positive values move it further from the camera.
*/
float QQuick3DModel::depthBias() const
{
return m_depthBias;
}
/*!
\qmlproperty bool Model::receivesReflections
When this property is set to \c true, the model's materials take reflections contribution from
a reflection probe. If the model is inside more than one reflection probe at the same time,
the nearest reflection probe is taken into account.
*/
bool QQuick3DModel::receivesReflections() const
{
return m_receivesReflections;
}
/*!
\qmlproperty bool Model::castsReflections
\since 6.4
When this property is set to \c true, the model is rendered by reflection probes and can be
seen in the reflections.
*/
bool QQuick3DModel::castsReflections() const
{
return m_castsReflections;
}
/*!
\qmlproperty bool Model::usedInBakedLighting
When this property is set to \c true, the model contributes to baked
lighting, such as lightmaps, for example in form of casting shadows or
indirect light. This setting is independent of controlling lightmap
generation for the model, use \l bakedLightmap for that.
The default value is \c false.
\note The default value is false, because designers and developers must
always evaluate on a per-model basis if the object is suitable to take part
in baked lighting.
\warning Models with dynamically changing properties, for example, animated
position, rotation, or other properties, are not suitable for participating
in baked lighting.
For more information on how to bake lightmaps, see the \l Lightmapper
documentation.
This property is relevant only when baking lightmaps. It has no effect
afterwards, when using the generated lightmaps during rendering.
\sa Light::bakeMode, bakedLightmap, Lightmapper, {Lightmaps and Global Illumination}
*/
bool QQuick3DModel::isUsedInBakedLighting() const
{
return m_usedInBakedLighting;
}
/*!
\qmlproperty int Model::lightmapBaseResolution
Defines the approximate size of the lightmap for this model. The default
value is 1024, indicating 1024x1024 as the base size. The actual size of
the lightmap texture is likely to be different, often bigger, depending on
the mesh.
For simpler, smaller meshes, or when it is known that using a bigger
lightmap is unnecessary, the value can be set to something smaller, for
example, 512 or 256.
The minimum value is 128.
This setting applies both to persistently stored and for intermediate,
partial lightmaps. When baking lightmaps, all models that have \l
usedInBakedLighting enabled are part of the path-traced scene. Thus all of
them need to have lightmap UV unwrapping performed and the rasterization
steps necessary to compute direct lighting which then can be taken into
account for indirect light bounces in the scene. However, for models that
just contribute to, but do not store a lightmap the default value is often
sufficient. Fine-tuning is more relevant for models that store and then use
the generated lightmaps.
This property is relevant only when baking lightmaps. It has no effect
afterwards, when using the generated lightmaps during rendering.
Models that have lightmap UV data pre-generated during asset import time
(e.g. via the balsam tool) will ignore this property because the lightmap
UV unwrapping and the lightmap size hint evaluation have already been done,
and will not be performed again during lightmap baking.
*/
int QQuick3DModel::lightmapBaseResolution() const
{
return m_lightmapBaseResolution;
}
/*!
\qmlproperty BakedLightmap Model::bakedLightmap
When this property is set to a valid, enabled BakedLightmap object, the
model will get a lightmap generated when baking lighting, the lightmap is
then stored persistently. When rendering, the model will load and use the
associated lightmap. The default value is null.
\note When the intention is to generate a persistently stored lightmap for
a Model, both bakedLightmap and \l usedInBakedLighting must be set on it,
in order to indicate that the Model not only participates in the
lightmapped scene, but also wants a full lightmap to be baked and stored.
For more information on how to bake lightmaps, see the \l Lightmapper
documentation.
This property is relevant both when baking and when using lightmaps. A
consistent state between the baking run and the subsequent runs that use
the generated data is essential. For example, changing the lightmap key
will make it impossible to load the previously generated data. An exception
is \l {BakedLightmap::}{enabled}, which can be used to dynamically toggle
the usage of lightmaps (outside of the baking run), but be aware that the
rendered results will depend on the Lights' \l{Light::bakeMode}{bakeMode}
settings in the scene.
\sa usedInBakedLighting, Lightmapper
*/
QQuick3DBakedLightmap *QQuick3DModel::bakedLightmap() const
{
return m_bakedLightmap;
}
/*!
\qmlproperty real Model::instancingLodMin
Defines the minimum distance from camera that an instance of this model is shown.
Used for a level of detail implementation.
*/
float QQuick3DModel::instancingLodMin() const
{
return m_instancingLodMin;
}
/*!
\qmlproperty real Model::instancingLodMax
Defines the maximum distance from camera that an instance of this model is shown.
Used for a level of detail implementation.
*/
float QQuick3DModel::instancingLodMax() const
{
return m_instancingLodMax;
}
void QQuick3DModel::setSource(const QUrl &source)
{
if (m_source == source)
return;
m_source = source;
emit sourceChanged();
markDirty(SourceDirty);
if (QQuick3DObjectPrivate::get(this)->sceneManager)
QQuick3DObjectPrivate::get(this)->sceneManager->dirtyBoundingBoxList.append(this);
}
void QQuick3DModel::setCastsShadows(bool castsShadows)
{
if (m_castsShadows == castsShadows)
return;
m_castsShadows = castsShadows;
emit castsShadowsChanged();
markDirty(ShadowsDirty);
}
void QQuick3DModel::setReceivesShadows(bool receivesShadows)
{
if (m_receivesShadows == receivesShadows)
return;
m_receivesShadows = receivesShadows;
emit receivesShadowsChanged();
markDirty(ShadowsDirty);
}
void QQuick3DModel::setPickable(bool isPickable)
{
if (m_pickable == isPickable)
return;
m_pickable = isPickable;
emit pickableChanged();
markDirty(PickingDirty);
}
void QQuick3DModel::setGeometry(QQuick3DGeometry *geometry)
{
if (geometry == m_geometry)
return;
// Make sure to disconnect if the geometry gets deleted out from under us
QQuick3DObjectPrivate::attachWatcher(this, &QQuick3DModel::setGeometry, geometry, m_geometry);
if (m_geometry)
QObject::disconnect(m_geometryConnection);
m_geometry = geometry;
if (m_geometry) {
m_geometryConnection
= QObject::connect(m_geometry, &QQuick3DGeometry::geometryNodeDirty, this, [this]() {
markDirty(GeometryDirty);
});
}
emit geometryChanged();
markDirty(GeometryDirty);
}
void QQuick3DModel::setSkeleton(QQuick3DSkeleton *skeleton)
{
if (skeleton == m_skeleton)
return;
// Make sure to disconnect if the skeleton gets deleted out from under us
QQuick3DObjectPrivate::attachWatcher(this, &QQuick3DModel::setSkeleton, skeleton, m_skeleton);
m_skeleton = skeleton;
emit skeletonChanged();
markDirty(SkeletonDirty);
}
void QQuick3DModel::setSkin(QQuick3DSkin *skin)
{
if (skin == m_skin)
return;
// Make sure to disconnect if the skin gets deleted out from under us
QQuick3DObjectPrivate::attachWatcher(this, &QQuick3DModel::setSkin, skin, m_skin);
m_skin = skin;
emit skinChanged();
markDirty(SkinDirty);
}
void QQuick3DModel::setInverseBindPoses(const QList<QMatrix4x4> &poses)
{
if (m_inverseBindPoses == poses)
return;
m_inverseBindPoses = poses;
emit inverseBindPosesChanged();
markDirty(PoseDirty);
}
void QQuick3DModel::setBounds(const QVector3D &min, const QVector3D &max)
{
if (!qFuzzyCompare(m_bounds.maximum(), max)
|| !qFuzzyCompare(m_bounds.minimum(), min)) {
m_bounds.bounds = QSSGBounds3 { min, max };
emit boundsChanged();
}
}
void QQuick3DModel::setInstancing(QQuick3DInstancing *instancing)
{
if (m_instancing == instancing)
return;
// Make sure to disconnect if the instance table gets deleted out from under us
QQuick3DObjectPrivate::attachWatcher(this, &QQuick3DModel::setInstancing, instancing, m_instancing);
if (m_instancing)
QObject::disconnect(m_instancingConnection);
m_instancing = instancing;
if (m_instancing) {
m_instancingConnection = QObject::connect
(m_instancing, &QQuick3DInstancing::instanceNodeDirty,
this, [this]{ markDirty(InstancesDirty);});
}
markDirty(InstancesDirty);
emit instancingChanged();
}
void QQuick3DModel::setInstanceRoot(QQuick3DNode *instanceRoot)
{
if (m_instanceRoot == instanceRoot)
return;
QQuick3DObjectPrivate::attachWatcher(this, &QQuick3DModel::setInstanceRoot, instanceRoot, m_instanceRoot);
m_instanceRoot = instanceRoot;
markDirty(InstanceRootDirty);
emit instanceRootChanged();
}
void QQuick3DModel::setDepthBias(float bias)
{
if (qFuzzyCompare(bias, m_depthBias))
return;
m_depthBias = bias;
markDirty(PropertyDirty);
emit depthBiasChanged();
}
void QQuick3DModel::setReceivesReflections(bool receivesReflections)
{
if (m_receivesReflections == receivesReflections)
return;
m_receivesReflections = receivesReflections;
emit receivesReflectionsChanged();
markDirty(ReflectionDirty);
}
void QQuick3DModel::setCastsReflections(bool castsReflections)
{
if (m_castsReflections == castsReflections)
return;
m_castsReflections = castsReflections;
emit castsReflectionsChanged();
markDirty(ReflectionDirty);
}
void QQuick3DModel::setUsedInBakedLighting(bool enable)
{
if (m_usedInBakedLighting == enable)
return;
m_usedInBakedLighting = enable;
emit usedInBakedLightingChanged();
markDirty(PropertyDirty);
}
void QQuick3DModel::setLightmapBaseResolution(int resolution)
{
resolution = qMax(128, resolution);
if (m_lightmapBaseResolution == resolution)
return;
m_lightmapBaseResolution = resolution;
emit lightmapBaseResolutionChanged();
markDirty(PropertyDirty);
}
void QQuick3DModel::setBakedLightmap(QQuick3DBakedLightmap *bakedLightmap)
{
if (m_bakedLightmap == bakedLightmap)
return;
if (m_bakedLightmap)
m_bakedLightmap->disconnect(m_bakedLightmapSignalConnection);
m_bakedLightmap = bakedLightmap;
m_bakedLightmapSignalConnection = QObject::connect(m_bakedLightmap, &QQuick3DBakedLightmap::changed, this,
[this] { markDirty(PropertyDirty); });
QObject::connect(m_bakedLightmap, &QObject::destroyed, this,
[this]
{
m_bakedLightmap = nullptr;
markDirty(PropertyDirty);
});
emit bakedLightmapChanged();
markDirty(PropertyDirty);
}
void QQuick3DModel::itemChange(ItemChange change, const ItemChangeData &value)
{
if (change == QQuick3DObject::ItemSceneChange)
updateSceneManager(value.sceneManager);
}
QSSGRenderGraphObject *QQuick3DModel::updateSpatialNode(QSSGRenderGraphObject *node)
{
if (!node) {
markAllDirty();
node = new QSSGRenderModel();
}
QQuick3DNode::updateSpatialNode(node);
int dirtyAttribute = 0;
auto modelNode = static_cast<QSSGRenderModel *>(node);
if (m_dirtyAttributes & SourceDirty)
modelNode->meshPath = QSSGRenderPath(translateMeshSource(m_source, this));
if (m_dirtyAttributes & PickingDirty)
modelNode->setState(QSSGRenderModel::LocalState::Pickable, m_pickable);
if (m_dirtyAttributes & ShadowsDirty) {
modelNode->castsShadows = m_castsShadows;
modelNode->receivesShadows = m_receivesShadows;
}
if (m_dirtyAttributes & MaterialsDirty) {
if (!m_materials.isEmpty()) {
if (modelNode->materials.isEmpty()) {
// Easy mode, just add each material
for (const Material &material : m_materials) {
QSSGRenderGraphObject *graphObject = QQuick3DObjectPrivate::get(material.material)->spatialNode;
if (graphObject)
modelNode->materials.append(graphObject);
else
dirtyAttribute |= MaterialsDirty; // We still got dirty materials
}
} else {
// Hard mode, go through each material and see if they match
if (modelNode->materials.size() != m_materials.size())
modelNode->materials.resize(m_materials.size());
for (int i = 0; i < m_materials.size(); ++i) {
QSSGRenderGraphObject *graphObject = QQuick3DObjectPrivate::get(m_materials[i].material)->spatialNode;
if (modelNode->materials[i] != graphObject)
modelNode->materials[i] = graphObject;
}
}
} else {
// No materials
modelNode->materials.clear();
}
}
if (m_dirtyAttributes & MorphTargetsDirty) {
if (!m_morphTargets.isEmpty()) {
const int numMorphTarget = m_morphTargets.size();
if (modelNode->morphTargets.isEmpty()) {
// Easy mode, just add each morphTarget
for (const auto morphTarget : std::as_const(m_morphTargets)) {
QSSGRenderGraphObject *graphObject = QQuick3DObjectPrivate::get(morphTarget)->spatialNode;
if (graphObject)
modelNode->morphTargets.append(graphObject);
else
dirtyAttribute |= MorphTargetsDirty; // We still got dirty morphTargets
}
modelNode->morphWeights.resize(numMorphTarget);
modelNode->morphAttributes.resize(numMorphTarget);
} else {
// Hard mode, go through each morphTarget and see if they match
if (modelNode->morphTargets.size() != numMorphTarget) {
modelNode->morphTargets.resize(numMorphTarget);
modelNode->morphWeights.resize(numMorphTarget);
modelNode->morphAttributes.resize(numMorphTarget);
}
for (int i = 0; i < numMorphTarget; ++i)
modelNode->morphTargets[i] = QQuick3DObjectPrivate::get(m_morphTargets.at(i))->spatialNode;
}
} else {
// No morphTargets
modelNode->morphTargets.clear();
}
}
if (m_dirtyAttributes & quint32(InstancesDirty | InstanceRootDirty)) {
// If we have an instance root set we have lower priority and the instance root node should already
// have been created.
QSSGRenderNode *instanceRootNode = nullptr;
if (m_instanceRoot) {
if (m_instanceRoot == this)
instanceRootNode = modelNode;
else
instanceRootNode = static_cast<QSSGRenderNode *>(QQuick3DObjectPrivate::get(m_instanceRoot)->spatialNode);
}
if (instanceRootNode != modelNode->instanceRoot) {
modelNode->instanceRoot = instanceRootNode;
modelNode->markDirty(QSSGRenderNode::DirtyFlag::TransformDirty);
}
if (m_instancing) {
modelNode->instanceTable = static_cast<QSSGRenderInstanceTable *>(QQuick3DObjectPrivate::get(m_instancing)->spatialNode);
} else {
modelNode->instanceTable = nullptr;
}
}
if (m_dirtyAttributes & GeometryDirty) {
if (m_geometry) {
modelNode->geometry = static_cast<QSSGRenderGeometry *>(QQuick3DObjectPrivate::get(m_geometry)->spatialNode);
setBounds(m_geometry->boundsMin(), m_geometry->boundsMax());
} else {
modelNode->geometry = nullptr;
setBounds(QVector3D(), QVector3D());
}
}
if (m_dirtyAttributes & SkeletonDirty) {
if (m_skeleton) {
modelNode->skeleton = static_cast<QSSGRenderSkeleton *>(QQuick3DObjectPrivate::get(m_skeleton)->spatialNode);
if (modelNode->skeleton)
modelNode->skeleton->skinningDirty = true;
} else {
modelNode->skeleton = nullptr;
}
}
if (m_dirtyAttributes & SkinDirty) {
if (m_skin)
modelNode->skin = static_cast<QSSGRenderSkin *>(QQuick3DObjectPrivate::get(m_skin)->spatialNode);
else
modelNode->skin = nullptr;
}
if (m_dirtyAttributes & LodDirty) {
modelNode->instancingLodMin = m_instancingLodMin;
modelNode->instancingLodMax = m_instancingLodMax;
}
if (m_dirtyAttributes & PoseDirty) {
modelNode->inverseBindPoses = m_inverseBindPoses.toVector();
if (modelNode->skeleton)
modelNode->skeleton->skinningDirty = true;
}
if (m_dirtyAttributes & PropertyDirty) {
modelNode->m_depthBiasSq = QSSGRenderModel::signedSquared(m_depthBias);
modelNode->usedInBakedLighting = m_usedInBakedLighting;
modelNode->lightmapBaseResolution = uint(m_lightmapBaseResolution);
if (m_bakedLightmap && m_bakedLightmap->isEnabled()) {
modelNode->lightmapKey = m_bakedLightmap->key();
const QString srcPrefix = m_bakedLightmap->loadPrefix();
const QString srcPath = srcPrefix.isEmpty() ? QStringLiteral(".") : srcPrefix;
const QQmlContext *context = qmlContext(m_bakedLightmap);
const QUrl resolvedUrl = context ? context->resolvedUrl(srcPath) : srcPath;
modelNode->lightmapLoadPath = QQmlFile::urlToLocalFileOrQrc(resolvedUrl);
} else {
modelNode->lightmapKey.clear();
modelNode->lightmapLoadPath.clear();
}
modelNode->levelOfDetailBias = m_levelOfDetailBias;
}
if (m_dirtyAttributes & ReflectionDirty) {
modelNode->receivesReflections = m_receivesReflections;
modelNode->castsReflections = m_castsReflections;
}
m_dirtyAttributes = dirtyAttribute;
return modelNode;
}
void QQuick3DModel::markDirty(QQuick3DModel::QSSGModelDirtyType type)
{
if (InstanceRootDirty & quint32(type))
QQuick3DObjectPrivate::get(this)->dirty(QQuick3DObjectPrivate::InstanceRootChanged);
if (!(m_dirtyAttributes & quint32(type))) {
m_dirtyAttributes |= quint32(type);
update();
}
}
void QQuick3DModel::updateSceneManager(QQuick3DSceneManager *sceneManager)
{
if (sceneManager) {
sceneManager->dirtyBoundingBoxList.append(this);
QQuick3DObjectPrivate::refSceneManager(m_skeleton, *sceneManager);
QQuick3DObjectPrivate::refSceneManager(m_skin, *sceneManager);
QQuick3DObjectPrivate::refSceneManager(m_geometry, *sceneManager);
QQuick3DObjectPrivate::refSceneManager(m_instancing, *sceneManager);
for (Material &mat : m_materials) {
if (!mat.material->parentItem() && !QQuick3DObjectPrivate::get(mat.material)->sceneManager) {
if (!mat.refed) {
QQuick3DObjectPrivate::refSceneManager(mat.material, *sceneManager);
mat.refed = true;
}
}
}
} else {
QQuick3DObjectPrivate::derefSceneManager(m_skeleton);
QQuick3DObjectPrivate::derefSceneManager(m_skin);
QQuick3DObjectPrivate::derefSceneManager(m_geometry);
QQuick3DObjectPrivate::derefSceneManager(m_instancing);
for (Material &mat : m_materials) {
if (mat.refed) {
QQuick3DObjectPrivate::derefSceneManager(mat.material);
mat.refed = false;
}
}
}
}
void QQuick3DModel::onMaterialDestroyed(QObject *object)
{
bool found = false;
for (int i = 0; i < m_materials.size(); ++i) {
if (m_materials[i].material == object) {
m_materials.removeAt(i--);
found = true;
}
}
if (found)
markDirty(QQuick3DModel::MaterialsDirty);
}
void QQuick3DModel::qmlAppendMaterial(QQmlListProperty<QQuick3DMaterial> *list, QQuick3DMaterial *material)
{
if (material == nullptr)
return;
QQuick3DModel *self = static_cast<QQuick3DModel *>(list->object);
self->m_materials.push_back({ material, false });
self->markDirty(QQuick3DModel::MaterialsDirty);
if (material->parentItem() == nullptr) {
// If the material has no parent, check if it has a hierarchical parent that's a QQuick3DObject
// and re-parent it to that, e.g., inline materials
QQuick3DObject *parentItem = qobject_cast<QQuick3DObject *>(material->parent());
if (parentItem) {
material->setParentItem(parentItem);
} else { // If no valid parent was found, make sure the material refs our scene manager
const auto &sceneManager = QQuick3DObjectPrivate::get(self)->sceneManager;
if (sceneManager) {
QQuick3DObjectPrivate::get(material)->refSceneManager(*sceneManager);
// Have to keep track if we called refSceneManager because we
// can end up in double deref attempts when a model is going
// away, due to updateSceneManager() being called on
// ItemSceneChanged (and also doing deref). We must ensure that
// is one deref for each ref.
self->m_materials.last().refed = true;
}
// else: If there's no scene manager, defer until one is set, see itemChange()
}
}
// Make sure materials are removed when destroyed
connect(material, &QQuick3DMaterial::destroyed, self, &QQuick3DModel::onMaterialDestroyed);
}
QQuick3DMaterial *QQuick3DModel::qmlMaterialAt(QQmlListProperty<QQuick3DMaterial> *list, qsizetype index)
{
QQuick3DModel *self = static_cast<QQuick3DModel *>(list->object);
return self->m_materials.at(index).material;
}
qsizetype QQuick3DModel::qmlMaterialsCount(QQmlListProperty<QQuick3DMaterial> *list)
{
QQuick3DModel *self = static_cast<QQuick3DModel *>(list->object);
return self->m_materials.size();
}
void QQuick3DModel::qmlClearMaterials(QQmlListProperty<QQuick3DMaterial> *list)
{
QQuick3DModel *self = static_cast<QQuick3DModel *>(list->object);
for (Material &mat : self->m_materials) {
if (mat.material->parentItem() == nullptr) {
if (mat.refed) {
QQuick3DObjectPrivate::get(mat.material)->derefSceneManager();
mat.refed = false;
}
}
disconnect(mat.material, &QQuick3DMaterial::destroyed, self, &QQuick3DModel::onMaterialDestroyed);
}
self->m_materials.clear();
self->markDirty(QQuick3DModel::MaterialsDirty);
}
void QQuick3DModel::onMorphTargetDestroyed(QObject *object)
{
bool found = false;
for (int i = 0; i < m_morphTargets.size(); ++i) {
if (m_morphTargets.at(i) == object) {
m_morphTargets.removeAt(i--);
found = true;
}
}
if (found) {
markDirty(QQuick3DModel::MorphTargetsDirty);
m_numMorphAttribs = 0;
}
}
void QQuick3DModel::qmlAppendMorphTarget(QQmlListProperty<QQuick3DMorphTarget> *list, QQuick3DMorphTarget *morphTarget)
{
if (morphTarget == nullptr)
return;
QQuick3DModel *self = static_cast<QQuick3DModel *>(list->object);
if (self->m_numMorphAttribs >= 8) {
qWarning("The number of morph attributes exceeds 8. This morph target will be ignored.");
return;
}
self->m_morphTargets.push_back(morphTarget);
self->m_numMorphAttribs += morphTarget->numAttribs();
if (self->m_numMorphAttribs > 8)
qWarning("The number of morph attributes exceeds 8. This morph target will be supported partially.");
self->markDirty(QQuick3DModel::MorphTargetsDirty);
if (morphTarget->parentItem() == nullptr) {
// If the morphTarget has no parent, check if it has a hierarchical parent that's a QQuick3DObject
// and re-parent it to that, e.g., inline morphTargets
QQuick3DObject *parentItem = qobject_cast<QQuick3DObject *>(morphTarget->parent());
if (parentItem) {
morphTarget->setParentItem(parentItem);
} else { // If no valid parent was found, make sure the morphTarget refs our scene manager
const auto &scenManager = QQuick3DObjectPrivate::get(self)->sceneManager;
if (scenManager)
QQuick3DObjectPrivate::get(morphTarget)->refSceneManager(*scenManager);
// else: If there's no scene manager, defer until one is set, see itemChange()
}
}
// Make sure morphTargets are removed when destroyed
connect(morphTarget, &QQuick3DMorphTarget::destroyed, self, &QQuick3DModel::onMorphTargetDestroyed);
}
QQuick3DMorphTarget *QQuick3DModel::qmlMorphTargetAt(QQmlListProperty<QQuick3DMorphTarget> *list, qsizetype index)
{
QQuick3DModel *self = static_cast<QQuick3DModel *>(list->object);
if (index >= self->m_morphTargets.size()) {
qWarning("The index exceeds the range of valid morph targets.");
return nullptr;
}
return self->m_morphTargets.at(index);
}
qsizetype QQuick3DModel::qmlMorphTargetsCount(QQmlListProperty<QQuick3DMorphTarget> *list)
{
QQuick3DModel *self = static_cast<QQuick3DModel *>(list->object);
return self->m_morphTargets.size();
}
void QQuick3DModel::qmlClearMorphTargets(QQmlListProperty<QQuick3DMorphTarget> *list)
{
QQuick3DModel *self = static_cast<QQuick3DModel *>(list->object);
for (const auto &morph : std::as_const(self->m_morphTargets)) {
if (morph->parentItem() == nullptr)
QQuick3DObjectPrivate::get(morph)->derefSceneManager();
disconnect(morph, &QQuick3DMorphTarget::destroyed, self, &QQuick3DModel::onMorphTargetDestroyed);
}
self->m_morphTargets.clear();
self->m_numMorphAttribs = 0;
self->markDirty(QQuick3DModel::MorphTargetsDirty);
}
void QQuick3DModel::setInstancingLodMin(float minDistance)
{
if (qFuzzyCompare(m_instancingLodMin, minDistance))
return;
m_instancingLodMin = minDistance;
emit instancingLodMinChanged();
markDirty(LodDirty);
}
void QQuick3DModel::setInstancingLodMax(float maxDistance)
{
if (qFuzzyCompare(m_instancingLodMax, maxDistance))
return;
m_instancingLodMax = maxDistance;
emit instancingLodMaxChanged();
markDirty(LodDirty);
}
/*!
\qmlproperty real Model::levelOfDetailBias
\since 6.5
This property changes the size the model needs to be when rendered before the
automatic level of detail meshes are used. Each generated level of detail
mesh contains an ideal size value that each level should be shown, which is
a ratio of how much of the rendered scene will be that mesh. A model that
represents only a few pixels on screen will not require the full geometry
to look correct, so a lower level of detail mesh will be used instead in
this case. This value is a bias to the ideal value such that a value smaller
than \c 1.0 will require an even smaller rendered size before switching to
a lesser level of detail. Values above \c 1.0 will lead to lower levels of detail
being used sooner. A value of \c 0.0 will disable the usage of levels of detail
completely.
The default value is \c 1.0
\note This property will only have an effect when the Model's geometry contains
levels of detail.
\sa Camera::levelOfDetailBias
*/
float QQuick3DModel::levelOfDetailBias() const
{
return m_levelOfDetailBias;
}
void QQuick3DModel::setLevelOfDetailBias(float newLevelOfDetailBias)
{
if (qFuzzyCompare(m_levelOfDetailBias, newLevelOfDetailBias))
return;
m_levelOfDetailBias = newLevelOfDetailBias;
emit levelOfDetailBiasChanged();
markDirty(QQuick3DModel::PropertyDirty);
}
QT_END_NAMESPACE
|