summaryrefslogtreecommitdiffstats
path: root/src/plugins/sceneparsers/gltfexport/gltfexporter.cpp
blob: 8b4fac1ce3162d7a2d9a488061b73256c9c93825 (plain)
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
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "gltfexporter.h"

#include <QtCore/qiodevice.h>
#include <QtCore/qfile.h>
#include <QtCore/qfileinfo.h>
#include <QtCore/qdir.h>
#include <QtCore/qhash.h>
#include <QtCore/qdebug.h>
#include <QtCore/qcoreapplication.h>
#include <QtCore/qjsondocument.h>
#include <QtCore/qjsonobject.h>
#include <QtCore/qjsonarray.h>
#include <QtCore/qmath.h>
#include <QtCore/qtemporarydir.h>
#include <QtCore/qregularexpression.h>
#include <QtCore/qmetaobject.h>
#include <QtGui/qvector2d.h>
#include <QtGui/qvector4d.h>
#include <QtGui/qmatrix4x4.h>

#include <Qt3DCore/qentity.h>
#include <Qt3DCore/qattribute.h>
#include <Qt3DCore/qbuffer.h>
#include <Qt3DCore/qgeometry.h>
#include <Qt3DCore/qtransform.h>
#include <Qt3DRender/qcameralens.h>
#include <Qt3DRender/qcamera.h>
#include <Qt3DRender/qblendequation.h>
#include <Qt3DRender/qblendequationarguments.h>
#include <Qt3DRender/qeffect.h>
#include <Qt3DRender/qmaterial.h>
#include <Qt3DRender/qgraphicsapifilter.h>
#include <Qt3DRender/qparameter.h>
#include <Qt3DRender/qtexture.h>
#include <Qt3DRender/qabstractlight.h>
#include <Qt3DRender/qpointlight.h>
#include <Qt3DRender/qspotlight.h>
#include <Qt3DRender/qdirectionallight.h>
#include <Qt3DRender/qgeometryrenderer.h>
#include <Qt3DRender/qtechnique.h>
#include <Qt3DRender/qalphacoverage.h>
#include <Qt3DRender/qalphatest.h>
#include <Qt3DRender/qclipplane.h>
#include <Qt3DRender/qcolormask.h>
#include <Qt3DRender/qcullface.h>
#include <Qt3DRender/qdepthrange.h>
#include <Qt3DRender/qdepthtest.h>
#include <Qt3DRender/qdithering.h>
#include <Qt3DRender/qfrontface.h>
#include <Qt3DRender/qmultisampleantialiasing.h>
#include <Qt3DRender/qnodepthmask.h>
#include <Qt3DRender/qpointsize.h>
#include <Qt3DRender/qpolygonoffset.h>
#include <Qt3DRender/qscissortest.h>
#include <Qt3DRender/qseamlesscubemap.h>
#include <Qt3DRender/qstencilmask.h>
#include <Qt3DRender/qstenciloperation.h>
#include <Qt3DRender/qstenciloperationarguments.h>
#include <Qt3DRender/qstenciltest.h>
#include <Qt3DRender/qstenciltestarguments.h>
#include <Qt3DExtras/qconemesh.h>
#include <Qt3DExtras/qcuboidmesh.h>
#include <Qt3DExtras/qcylindermesh.h>
#include <Qt3DExtras/qplanemesh.h>
#include <Qt3DExtras/qspheremesh.h>
#include <Qt3DExtras/qtorusmesh.h>
#include <Qt3DExtras/qphongmaterial.h>
#include <Qt3DExtras/qphongalphamaterial.h>
#include <Qt3DExtras/qdiffusemapmaterial.h>
#include <Qt3DExtras/qdiffusespecularmapmaterial.h>
#include <Qt3DExtras/qnormaldiffusemapmaterial.h>
#include <Qt3DExtras/qnormaldiffusemapalphamaterial.h>
#include <Qt3DExtras/qnormaldiffusespecularmapmaterial.h>
#include <Qt3DExtras/qgoochmaterial.h>
#include <Qt3DExtras/qpervertexcolormaterial.h>

#include <private/qurlhelper_p.h>

#ifndef qUtf16PrintableImpl
#  define qUtf16PrintableImpl(string) \
    static_cast<const wchar_t*>(static_cast<const void*>(string.utf16()))
#endif

namespace {

inline QJsonArray col2jsvec(const QColor &color, bool alpha = false)
{
    QJsonArray arr;
    arr << color.redF() << color.greenF() << color.blueF();
    if (alpha)
        arr << color.alphaF();
    return arr;
}

template <typename T>
inline QJsonArray vec2jsvec(const QVector<T> &v)
{
    QJsonArray arr;
    for (int i = 0; i < v.count(); ++i)
        arr << v.at(i);
    return arr;
}

inline QJsonArray size2jsvec(const QSize &size) {
    QJsonArray arr;
    arr << size.width() << size.height();
    return arr;
}

inline QJsonArray vec2jsvec(const QVector2D &v)
{
    QJsonArray arr;
    arr << v.x() << v.y();
    return arr;
}

inline QJsonArray vec2jsvec(const QVector3D &v)
{
    QJsonArray arr;
    arr << v.x() << v.y() << v.z();
    return arr;
}

inline QJsonArray vec2jsvec(const QVector4D &v)
{
    QJsonArray arr;
    arr << v.x() << v.y() << v.z() << v.w();
    return arr;
}

#if 0   // unused for now
inline QJsonArray matrix2jsvec(const QMatrix2x2 &matrix)
{
    QJsonArray jm;
    const float *mtxp = matrix.constData();
    for (int j = 0; j < 4; ++j)
        jm.append(*mtxp++);
    return jm;
}

inline QJsonArray matrix2jsvec(const QMatrix3x3 &matrix)
{
    QJsonArray jm;
    const float *mtxp = matrix.constData();
    for (int j = 0; j < 9; ++j)
        jm.append(*mtxp++);
    return jm;
}
#endif

inline QJsonArray matrix2jsvec(const QMatrix4x4 &matrix)
{
    QJsonArray jm;
    const float *mtxp = matrix.constData();
    for (int j = 0; j < 16; ++j)
        jm.append(*mtxp++);
    return jm;
}

inline void promoteColorsToRGBA(QJsonObject *obj)
{
    auto it = obj->begin();
    auto itEnd = obj->end();
    while (it != itEnd) {
        QJsonArray arr = it.value().toArray();
        if (arr.count() == 3) {
            const QString key = it.key();
            if (key == QStringLiteral("ambient")
                    || key == QStringLiteral("diffuse")
                    || key == QStringLiteral("specular")
                    || key == QStringLiteral("warm")
                    || key == QStringLiteral("cool")) {
                arr.append(1);
                *it = arr;
            }
        }
        ++it;
    }
}

} // namespace

QT_BEGIN_NAMESPACE

using namespace Qt3DCore;
using namespace Qt3DExtras;

namespace Qt3DRender {

Q_LOGGING_CATEGORY(GLTFExporterLog, "Qt3D.GLTFExport", QtWarningMsg)

const QString MATERIAL_DIFFUSE_COLOR = QStringLiteral("kd");
const QString MATERIAL_SPECULAR_COLOR = QStringLiteral("ks");
const QString MATERIAL_AMBIENT_COLOR = QStringLiteral("ka");

const QString MATERIAL_DIFFUSE_TEXTURE = QStringLiteral("diffuseTexture");
const QString MATERIAL_SPECULAR_TEXTURE = QStringLiteral("specularTexture");
const QString MATERIAL_NORMALS_TEXTURE = QStringLiteral("normalTexture");

const QString MATERIAL_SHININESS = QStringLiteral("shininess");
const QString MATERIAL_ALPHA = QStringLiteral("alpha");

// Custom extension for Qt3D
const QString MATERIAL_TEXTURE_SCALE = QStringLiteral("texCoordScale");

// Custom gooch material values
const QString MATERIAL_BETA = QStringLiteral("beta");
const QString MATERIAL_COOL_COLOR = QStringLiteral("kblue");
const QString MATERIAL_WARM_COLOR = QStringLiteral("kyellow");

const QString VERTICES_ATTRIBUTE_NAME = QAttribute::defaultPositionAttributeName();
const QString NORMAL_ATTRIBUTE_NAME =  QAttribute::defaultNormalAttributeName();
const QString TANGENT_ATTRIBUTE_NAME = QAttribute::defaultTangentAttributeName();
const QString TEXTCOORD_ATTRIBUTE_NAME = QAttribute::defaultTextureCoordinateAttributeName();
const QString COLOR_ATTRIBUTE_NAME = QAttribute::defaultColorAttributeName();

GLTFExporter::GLTFExporter() : QSceneExporter()
  , m_sceneRoot(nullptr)
  , m_rootNode(nullptr)
  , m_rootNodeEmpty(false)

{
}

GLTFExporter::~GLTFExporter()
{
}

/*!
    \class Qt3DRender::GLTFExporter
    \inmodule Qt3DRender
    \internal
    \brief Manages the export of a 3D scene to the GLTF format.

    Handles the export of a 3D scene to the GLTF format.
*/
// sceneRoot  : The root entity that contains the exported scene. If the sceneRoot doesn't have
//              any exportable components, it is not exported itself. This is because importing a
//              scene creates an empty top level entity to hold the scene.
// outDir     : The directory where the scene export directory is created in.
// exportName : Name of the directory created in outDir to hold the exported scene. Also used as
//              the file name base for generated files.
// options    : Export options.
//
// Supported options are:
// "compactJson" (bool): Removes unnecessary whitespace from the generated JSON file.

/*!
    Exports the scene to the GLTF format

    \a sceneRoot is the root entity that will be exported.
    If the sceneRoot does not have any exportable components, it is not exported itself.

    \a outDir is the directory in which the scene export is created.

    \a exportName is the name of the directory created in \c outDir that will hold
       the exported scene.

    \a options contain the export options.

    Returns true if the export was carried out successfully.
*/

bool GLTFExporter::exportScene(QEntity *sceneRoot, const QString &outDir,
                               const QString &exportName, const QVariantHash &options)
{
    m_bufferViewCount = 0;
    m_accessorCount = 0;
    m_meshCount = 0;
    m_materialCount = 0;
    m_techniqueCount = 0;
    m_textureCount = 0;
    m_imageCount = 0;
    m_shaderCount = 0;
    m_programCount = 0;
    m_nodeCount = 0;
    m_cameraCount = 0;
    m_lightCount = 0;
    m_renderPassCount = 0;
    m_effectCount = 0;

    m_gltfOpts.compactJson = options.value(QStringLiteral("compactJson"),
                                           QVariant(false)).toBool();

    QFileInfo outDirFileInfo(outDir);
    QString absoluteOutDir = outDirFileInfo.absoluteFilePath();
    if (!absoluteOutDir.endsWith(QLatin1Char('/')))
        absoluteOutDir.append(QLatin1Char('/'));
    m_exportName = exportName;
    m_sceneRoot = sceneRoot;
    QString finalExportDir = absoluteOutDir + m_exportName;
    if (!finalExportDir.endsWith(QLatin1Char('/')))
        finalExportDir.append(QLatin1Char('/'));

    QDir outDirDir(absoluteOutDir);

    // Make sure outDir exists
    if (outDirFileInfo.exists()) {
        if (!outDirFileInfo.isDir()) {
            qCWarning(GLTFExporterLog, "outDir is not a directory: '%ls'",
                      qUtf16PrintableImpl(absoluteOutDir));
            return false;
        }
    } else {
        if (!outDirDir.mkpath(outDirFileInfo.absoluteFilePath())) {
            qCWarning(GLTFExporterLog, "outDir could not be created: '%ls'",
                      qUtf16PrintableImpl(absoluteOutDir));
            return false;
        }
    }

    // Create temporary directory for exporting
    QTemporaryDir exportDir;

    if (!exportDir.isValid()) {
        qCWarning(GLTFExporterLog, "Temporary export directory could not be created");
        return false;
    }
    m_exportDir = exportDir.path();
    m_exportDir.append(QStringLiteral("/"));

    qCDebug(GLTFExporterLog, "Output directory: %ls", qUtf16PrintableImpl(absoluteOutDir));
    qCDebug(GLTFExporterLog, "Export name: %ls", qUtf16PrintableImpl(m_exportName));
    qCDebug(GLTFExporterLog, "Temp export dir: %ls", qUtf16PrintableImpl(m_exportDir));
    qCDebug(GLTFExporterLog, "Final export dir: %ls", qUtf16PrintableImpl(finalExportDir));

    parseScene();

    // Export scene to temporary directory
    if (!saveScene()) {
        qCWarning(GLTFExporterLog, "Exporting GLTF scene failed");
        return false;
    }

    // Create final export directory
    if (!outDirDir.mkpath(m_exportName)) {
        qCWarning(GLTFExporterLog, "Final export directory could not be created: '%ls'",
                  qUtf16PrintableImpl(finalExportDir));
        return false;
    }

    // As a safety feature, we don't indiscriminately delete existing directory or it's contents,
    // but instead look for an old export and delete only related files.
    clearOldExport(finalExportDir);

    // Files copied from resources will have read-only permissions, which isn't ideal in cases
    // where export is done on top of an existing export.
    // Since different file systems handle permissions differently, we grab the target permissions
    // from the qgltf file, which we created ourselves.
    QFile gltfFile(m_exportDir + m_exportName + QStringLiteral(".qgltf"));
    QFile::Permissions targetPermissions = gltfFile.permissions();

    // Copy exported scene to actual export directory
    for (const auto &sourceFileStr : qAsConst(m_exportedFiles)) {
        QFileInfo fiSource(m_exportDir + sourceFileStr);
        QFileInfo fiDestination(finalExportDir + sourceFileStr);
        if (fiDestination.exists()) {
            QFile(fiDestination.absoluteFilePath()).remove();
            qCDebug(GLTFExporterLog, "Removed old file: '%ls'",
                    qUtf16PrintableImpl(fiDestination.absoluteFilePath()));
        }
        QString srcPath = fiSource.absoluteFilePath();
        QString destPath = fiDestination.absoluteFilePath();
        if (!QFile(srcPath).copy(destPath)) {
            qCWarning(GLTFExporterLog, "  Failed to copy file: '%ls' -> '%ls'",
                      qUtf16PrintableImpl(srcPath), qUtf16PrintableImpl(destPath));
            // Don't fail entire export because file copy failed - if there is somehow a read-only
            // file with same name already in the export dir after cleanup we did, let's just assume
            // it's the same file we want rather than risk deleting unrelated protected file.
        } else {
            qCDebug(GLTFExporterLog, "  Copied file: '%ls' -> '%ls'",
                    qUtf16PrintableImpl(srcPath), qUtf16PrintableImpl(destPath));
            QFile(destPath).setPermissions(targetPermissions);
        }
    }

    // Clean up after export

    m_buffer.clear();
    m_meshMap.clear();
    m_materialMap.clear();
    m_cameraMap.clear();
    m_lightMap.clear();
    m_transformMap.clear();
    m_imageMap.clear();
    m_textureIdMap.clear();
    m_meshInfo.clear();
    m_materialInfo.clear();
    m_cameraInfo.clear();
    m_lightInfo.clear();
    m_exportedFiles.clear();
    m_renderPassIdMap.clear();
    m_shaderInfo.clear();
    m_programInfo.clear();
    m_techniqueIdMap.clear();
    m_effectIdMap.clear();
    qDeleteAll(m_defaultObjectCache);
    m_defaultObjectCache.clear();
    m_propertyCache.clear();

    delNode(m_rootNode);

    return true;
}

void GLTFExporter::cacheDefaultProperties(GLTFExporter::PropertyCacheType type)
{
    if (m_defaultObjectCache.contains(type))
        return;

    QObject *defaultObject = nullptr;

    switch (type) {
    case TypeConeMesh:
        defaultObject = new QConeMesh;
        break;
    case TypeCuboidMesh:
        defaultObject = new QCuboidMesh;
        break;
    case TypeCylinderMesh:
        defaultObject = new QCylinderMesh;
        break;
    case TypePlaneMesh:
        defaultObject = new QPlaneMesh;
        break;
    case TypeSphereMesh:
        defaultObject = new QSphereMesh;
        break;
    case TypeTorusMesh:
        defaultObject = new QTorusMesh;
        break;
    default:
        return; // Unsupported type
    }

    // Store the default object for property comparisons
    m_defaultObjectCache.insert(type, defaultObject);

    // Cache metaproperties of supported types (but not their parent class types)
    const QMetaObject *meta = defaultObject->metaObject();
    QVector<QMetaProperty> properties;
    properties.reserve(meta->propertyCount() - meta->propertyOffset());
    for (int i = meta->propertyOffset(); i < meta->propertyCount(); ++i) {
        if (meta->property(i).isWritable())
            properties.append(meta->property(i));
    }

    m_propertyCache.insert(type, properties);
}

// Copies textures from original locations to the temporary export directory.
// If texture names conflict, they are renamed.
void GLTFExporter::copyTextures()
{
    qCDebug(GLTFExporterLog, "Copying textures...");
    QHash<QString, QString> copiedMap;
    for (auto texIt = m_textureIdMap.constBegin(); texIt != m_textureIdMap.constEnd(); ++texIt) {
        QFileInfo fi(texIt.key());
        QString absoluteFilePath;
        if (texIt.key().startsWith(QStringLiteral(":")))
            absoluteFilePath = texIt.key();
        else
            absoluteFilePath = fi.absoluteFilePath();
        if (copiedMap.contains(absoluteFilePath)) {
            // Texture has already been copied
            qCDebug(GLTFExporterLog, "  Skipped copying duplicate texture: '%ls'",
                    qUtf16PrintableImpl(absoluteFilePath));
            if (!m_imageMap.contains(texIt.key()))
                m_imageMap.insert(texIt.key(), copiedMap.value(absoluteFilePath));
        } else {
            QString fileName = fi.fileName();
            QString outFile = m_exportDir;
            outFile.append(fileName);
            QFileInfo fiTry(outFile);
            if (fiTry.exists()) {
                static const QString outFileTemplate = QStringLiteral("%2_%3.%4");
                int counter = 0;
                QString tryFile = outFile;
                QString suffix = fiTry.suffix();
                QString base = fiTry.baseName();
                while (fiTry.exists()) {
                    fileName = outFileTemplate.arg(base).arg(counter++).arg(suffix);
                    tryFile = m_exportDir;
                    tryFile.append(fileName);
                    fiTry.setFile(tryFile);
                }
                outFile = tryFile;
            }
            if (!QFile(absoluteFilePath).copy(outFile)) {
                qCWarning(GLTFExporterLog, "  Failed to copy texture: '%ls' -> '%ls'",
                          qUtf16PrintableImpl(absoluteFilePath), qUtf16PrintableImpl(outFile));
            } else {
                qCDebug(GLTFExporterLog, "  Copied texture: '%ls' -> '%ls'",
                        qUtf16PrintableImpl(absoluteFilePath), qUtf16PrintableImpl(outFile));
            }
            // Generate actual target file (as current exportDir is temp dir)
            copiedMap.insert(absoluteFilePath, fileName);
            m_exportedFiles.insert(fileName);
            m_imageMap.insert(texIt.key(), fileName);
        }
    }
}

// Creates shaders to the temporary export directory.
void GLTFExporter::createShaders()
{
    qCDebug(GLTFExporterLog, "Creating shaders...");
    for (const auto &si : qAsConst(m_shaderInfo)) {
        const QString fileName = m_exportDir + si.uri;
        QFile f(fileName);
        if (f.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
            m_exportedFiles.insert(QFileInfo(f.fileName()).fileName());
            f.write(si.code);
            f.close();
        } else {
            qCWarning(GLTFExporterLog, "  Writing shaderfile '%ls' failed!",
                      qUtf16PrintableImpl(fileName));
        }
    }
}


void GLTFExporter::parseEntities(const QEntity *entity, Node *parentNode)
{
    if (entity) {
        Node *node =  new Node;
        node->name = entity->objectName();
        node->uniqueName = newNodeName();

        int irrelevantComponents = 0;
        const auto components = entity->components();
        for (auto component : components) {
            if (auto mesh = qobject_cast<QGeometryRenderer *>(component))
                m_meshMap.insert(node, mesh);
            else if (auto material = qobject_cast<QMaterial *>(component))
                m_materialMap.insert(node, material);
            else if (auto transform = qobject_cast<Qt3DCore::QTransform *>(component))
                m_transformMap.insert(node, transform);
            else if (auto camera = qobject_cast<QCameraLens *>(component))
                m_cameraMap.insert(node, camera);
            else if (auto light = qobject_cast<QAbstractLight *>(component))
                m_lightMap.insert(node, light);
            else
                irrelevantComponents++;
        }
        if (!parentNode) {
            m_rootNode = node;
            if (irrelevantComponents == entity->components().size())
                m_rootNodeEmpty = true;
        } else {
            parentNode->children.append(node);
        }
        qCDebug(GLTFExporterLog, "Parsed entity '%ls' -> '%ls'",
                qUtf16PrintableImpl(entity->objectName()), qUtf16PrintableImpl(node->uniqueName));

        for (auto child : entity->children())
            parseEntities(qobject_cast<QEntity *>(child), node);
    }
}

void GLTFExporter::parseScene()
{
    parseEntities(m_sceneRoot, nullptr);
    parseMaterials();
    parseMeshes();
    parseCameras();
    parseLights();
}

void GLTFExporter::parseMaterials()
{
    qCDebug(GLTFExporterLog, "Parsing materials...");

    int materialCount = 0;
    for (auto it = m_materialMap.constBegin(); it != m_materialMap.constEnd(); ++it) {
        QMaterial *material = it.value();

        MaterialInfo matInfo;
        matInfo.name = newMaterialName();
        matInfo.originalName = material->objectName();

        // Is material common or custom?
        if (qobject_cast<QPhongMaterial *>(material)) {
            matInfo.type = MaterialInfo::TypePhong;
        } else if (auto phongAlpha = qobject_cast<QPhongAlphaMaterial *>(material)) {
            matInfo.type = MaterialInfo::TypePhongAlpha;
            matInfo.blendArguments.resize(4);
            matInfo.blendEquations.resize(2);
            matInfo.blendArguments[0] = int(phongAlpha->sourceRgbArg());
            matInfo.blendArguments[1] = int(phongAlpha->sourceAlphaArg());
            matInfo.blendArguments[2] = int(phongAlpha->destinationRgbArg());
            matInfo.blendArguments[3] = int(phongAlpha->destinationAlphaArg());
            matInfo.blendEquations[0] = int(phongAlpha->blendFunctionArg());
            matInfo.blendEquations[1] = int(phongAlpha->blendFunctionArg());
        } else if (qobject_cast<QDiffuseMapMaterial *>(material)) {
            matInfo.type = MaterialInfo::TypeDiffuseMap;
        } else if (qobject_cast<QDiffuseSpecularMapMaterial *>(material)) {
            matInfo.type = MaterialInfo::TypeDiffuseSpecularMap;
        } else if (qobject_cast<QNormalDiffuseMapAlphaMaterial *>(material)) {
            matInfo.values.insert(QStringLiteral("transparent"), QVariant(true));
            matInfo.type = MaterialInfo::TypeNormalDiffuseMapAlpha;
        } else if (qobject_cast<QNormalDiffuseMapMaterial *>(material)) {
            matInfo.type = MaterialInfo::TypeNormalDiffuseMap;
        } else if (qobject_cast<QNormalDiffuseSpecularMapMaterial *>(material)) {
            matInfo.type = MaterialInfo::TypeNormalDiffuseSpecularMap;
        } else if (qobject_cast<QGoochMaterial *>(material)) {
            matInfo.type = MaterialInfo::TypeGooch;
        } else if (qobject_cast<QPerVertexColorMaterial *>(material)) {
            matInfo.type = MaterialInfo::TypePerVertex;
        } else {
            matInfo.type = MaterialInfo::TypeCustom;
        }

        if (matInfo.type == MaterialInfo::TypeCustom) {
            if (material->effect()) {
                if (!m_effectIdMap.contains(material->effect()))
                    m_effectIdMap.insert(material->effect(), newEffectName());
                parseTechniques(material);
            }
        } else {
            // Default materials do not have separate effect, all effect parameters are stored as
            // material values.
            if (material->effect()) {
                QVector<QParameter *> parameters = material->effect()->parameters();
                for (auto param : parameters) {
                    if (param->value().type() == QVariant::Color) {
                        QColor color = param->value().value<QColor>();
                        if (param->name() == MATERIAL_AMBIENT_COLOR) {
                            matInfo.colors.insert(QStringLiteral("ambient"), color);
                        } else if (param->name() == MATERIAL_DIFFUSE_COLOR) {
                            if (matInfo.type == MaterialInfo::TypePhongAlpha) {
                                matInfo.values.insert(QStringLiteral("transparency"), float(color.alphaF()));
                                color.setAlphaF(1.0f);
                            }
                            matInfo.colors.insert(QStringLiteral("diffuse"), color);
                        } else if (param->name() == MATERIAL_SPECULAR_COLOR) {
                            matInfo.colors.insert(QStringLiteral("specular"), color);
                        } else if (param->name() == MATERIAL_COOL_COLOR) { // Custom Qt3D gooch
                            matInfo.colors.insert(QStringLiteral("cool"), color);
                        } else if (param->name() == MATERIAL_WARM_COLOR) { // Custom Qt3D gooch
                            matInfo.colors.insert(QStringLiteral("warm"), color);
                        } else {
                            matInfo.colors.insert(param->name(), color);
                        }
                    } else if (param->value().canConvert<QAbstractTexture *>()) {
                        const QString urlString = textureVariantToUrl(param->value());
                        if (param->name() == MATERIAL_DIFFUSE_TEXTURE)
                            matInfo.textures.insert(QStringLiteral("diffuse"), urlString);
                        else if (param->name() == MATERIAL_SPECULAR_TEXTURE)
                            matInfo.textures.insert(QStringLiteral("specular"), urlString);
                        else if (param->name() == MATERIAL_NORMALS_TEXTURE)
                            matInfo.textures.insert(QStringLiteral("normal"), urlString);
                        else
                            matInfo.textures.insert(param->name(), urlString);
                    } else if (param->name() == MATERIAL_SHININESS) {
                        matInfo.values.insert(QStringLiteral("shininess"), param->value());
                    } else if (param->name() == MATERIAL_BETA) { // Custom Qt3D param for gooch
                        matInfo.values.insert(QStringLiteral("beta"), param->value());
                    } else if (param->name() == MATERIAL_ALPHA) {
                        if (matInfo.type == MaterialInfo::TypeGooch)
                            matInfo.values.insert(QStringLiteral("alpha"), param->value());
                        else
                            matInfo.values.insert(QStringLiteral("transparency"), param->value());
                    } else if (param->name() == MATERIAL_TEXTURE_SCALE) { // Custom Qt3D param
                        matInfo.values.insert(QStringLiteral("textureScale"), param->value());
                    } else {
                        qCDebug(GLTFExporterLog,
                                "Common material had unknown parameter: '%ls'",
                                qUtf16PrintableImpl(param->name()));
                    }
                }
            }
        }

        if (GLTFExporterLog().isDebugEnabled()) {
            qCDebug(GLTFExporterLog, "  Material #%i", materialCount);
            qCDebug(GLTFExporterLog, "    name: '%ls'", qUtf16PrintableImpl(matInfo.name));
            qCDebug(GLTFExporterLog, "    originalName: '%ls'",
                    qUtf16PrintableImpl(matInfo.originalName));
            qCDebug(GLTFExporterLog, "    type: %i", matInfo.type);
            qCDebug(GLTFExporterLog) << "    colors:" << matInfo.colors;
            qCDebug(GLTFExporterLog) << "    values:" << matInfo.values;
            qCDebug(GLTFExporterLog) << "    textures:" << matInfo.textures;
        }

        m_materialInfo.insert(material, matInfo);
        materialCount++;
    }
}

void GLTFExporter::parseMeshes()
{
    qCDebug(GLTFExporterLog, "Parsing meshes...");

    int meshCount = 0;
    for (auto it = m_meshMap.constBegin(); it != m_meshMap.constEnd(); ++it) {
        Node *node = it.key();
        QGeometryRenderer *mesh = it.value();

        MeshInfo meshInfo;
        meshInfo.originalName = mesh->objectName();
        meshInfo.name = newMeshName();
        meshInfo.materialName = m_materialInfo.value(m_materialMap.value(node)).name;

        if (qobject_cast<QConeMesh *>(mesh)) {
            meshInfo.meshType = TypeConeMesh;
            meshInfo.meshTypeStr = QStringLiteral("cone");
        } else if (qobject_cast<QCuboidMesh *>(mesh)) {
            meshInfo.meshType = TypeCuboidMesh;
            meshInfo.meshTypeStr = QStringLiteral("cuboid");
        } else if (qobject_cast<QCylinderMesh *>(mesh)) {
            meshInfo.meshType = TypeCylinderMesh;
            meshInfo.meshTypeStr = QStringLiteral("cylinder");
        } else if (qobject_cast<QPlaneMesh *>(mesh)) {
            meshInfo.meshType = TypePlaneMesh;
            meshInfo.meshTypeStr = QStringLiteral("plane");
        } else if (qobject_cast<QSphereMesh *>(mesh)) {
            meshInfo.meshType = TypeSphereMesh;
            meshInfo.meshTypeStr = QStringLiteral("sphere");
        } else if (qobject_cast<QTorusMesh *>(mesh)) {
            meshInfo.meshType = TypeTorusMesh;
            meshInfo.meshTypeStr = QStringLiteral("torus");
        } else {
            meshInfo.meshType = TypeNone;
        }

        if (meshInfo.meshType != TypeNone) {
            meshInfo.meshComponent = mesh;
            cacheDefaultProperties(meshInfo.meshType);

            if (GLTFExporterLog().isDebugEnabled()) {
                qCDebug(GLTFExporterLog, "  Mesh #%i: (%ls/%ls)", meshCount,
                        qUtf16PrintableImpl(meshInfo.name), qUtf16PrintableImpl(meshInfo.originalName));
                qCDebug(GLTFExporterLog, "    material: '%ls'",
                        qUtf16PrintableImpl(meshInfo.materialName));
                qCDebug(GLTFExporterLog, "    basic mesh type: '%s'",
                        mesh->metaObject()->className());
            }
        } else {
            meshInfo.meshComponent = nullptr;
            QGeometry *meshGeometry = mesh->geometry();

            if (!meshGeometry) {
                qCWarning(GLTFExporterLog, "Ignoring mesh without geometry!");
                continue;
            }

            QAttribute *indexAttrib = nullptr;
            const quint16 *indexPtr = nullptr;

            struct VertexAttrib {
                QAttribute *att;
                const float *ptr;
                QString usage;
                uint offset;
                uint stride;
                int index;
            };

            QVector<VertexAttrib> vAttribs;
            vAttribs.reserve(meshGeometry->attributes().size());

            uint stride(0);

            const auto attributes = meshGeometry->attributes();
            for (QAttribute *att : attributes) {
                if (att->attributeType() == QAttribute::IndexAttribute) {
                    indexAttrib = att;
                    indexPtr = reinterpret_cast<const quint16 *>(att->buffer()->data().constData());
                } else {
                    VertexAttrib vAtt;
                    vAtt.att = att;
                    vAtt.ptr = reinterpret_cast<const float *>(att->buffer()->data().constData());
                    if (att->name() == VERTICES_ATTRIBUTE_NAME)
                        vAtt.usage = QStringLiteral("POSITION");
                    else if (att->name() == NORMAL_ATTRIBUTE_NAME)
                        vAtt.usage = QStringLiteral("NORMAL");
                    else if (att->name() == TEXTCOORD_ATTRIBUTE_NAME)
                        vAtt.usage = QStringLiteral("TEXCOORD_0");
                    else if (att->name() == COLOR_ATTRIBUTE_NAME)
                        vAtt.usage = QStringLiteral("COLOR");
                    else if (att->name() == TANGENT_ATTRIBUTE_NAME)
                        vAtt.usage = QStringLiteral("TANGENT");
                    else
                        vAtt.usage = att->name();

                    vAtt.offset = att->byteOffset() / sizeof(float);
                    vAtt.index = vAtt.offset;
                    vAtt.stride = att->byteStride() > 0
                            ? att->byteStride() / sizeof(float) - att->vertexSize() : 0;
                    stride += att->vertexSize();

                    vAttribs << vAtt;
                }
            }

            int attribCount(vAttribs.size());
            if (!attribCount) {
                qCWarning(GLTFExporterLog, "Ignoring mesh without any attributes!");
                continue;
            }

            QByteArray vertexBuf;
            const int vertexCount = vAttribs.at(0).att->count();
            vertexBuf.resize(stride * vertexCount * sizeof(float));
            float *p = reinterpret_cast<float *>(vertexBuf.data());

            // Create interleaved buffer
            for (int i = 0; i < vertexCount; ++i) {
                for (int j = 0; j < attribCount; ++j) {
                    VertexAttrib &vAtt = vAttribs[j];
                    for (uint k = 0; k < vAtt.att->vertexSize(); ++k)
                        *p++ = vAtt.ptr[vAtt.index++];
                    vAtt.index += vAtt.stride;
                }
            }

            MeshInfo::BufferView vertexBufView;
            vertexBufView.name = newBufferViewName();
            vertexBufView.length = vertexBuf.size();
            vertexBufView.offset = m_buffer.size();
            vertexBufView.componentType = GL_FLOAT;
            vertexBufView.target = GL_ARRAY_BUFFER;
            meshInfo.views.append(vertexBufView);

            QByteArray indexBuf;
            MeshInfo::BufferView indexBufView;
            uint indexCount = 0;
            if (indexAttrib) {
                const uint indexSize = indexAttrib->vertexBaseType() == QAttribute::UnsignedShort
                        ? sizeof(quint16) : sizeof(quint32);
                indexCount = indexAttrib->count();
                uint srcIndex = indexAttrib->byteOffset() / indexSize;
                const uint indexStride = indexAttrib->byteStride()
                        ? indexAttrib->byteStride() / indexSize - 1: 0;
                indexBuf.resize(indexCount * indexSize);
                if (indexSize == sizeof(quint32)) {
                    quint32 *dst = reinterpret_cast<quint32 *>(indexBuf.data());
                    const quint32 *src = reinterpret_cast<const quint32 *>(indexPtr);
                    for (uint j = 0; j < indexCount; ++j) {
                        *dst++ = src[srcIndex++];
                        srcIndex += indexStride;
                    }
                } else {
                    quint16 *dst = reinterpret_cast<quint16 *>(indexBuf.data());
                    for (uint j = 0; j < indexCount; ++j) {
                        *dst++ = indexPtr[srcIndex++];
                        srcIndex += indexStride;
                    }
                }

                indexBufView.name = newBufferViewName();
                indexBufView.length = indexBuf.size();
                indexBufView.offset = vertexBufView.offset + vertexBufView.length;
                indexBufView.componentType = indexSize == sizeof(quint32)
                        ? GL_UNSIGNED_INT : GL_UNSIGNED_SHORT;
                indexBufView.target = GL_ELEMENT_ARRAY_BUFFER;
                meshInfo.views.append(indexBufView);
            }

            MeshInfo::Accessor acc;
            uint startOffset = 0;

            acc.bufferView = vertexBufView.name;
            acc.stride = stride * sizeof(float);
            acc.count = vertexCount;
            acc.componentType = vertexBufView.componentType;
            for (int i = 0; i < attribCount; ++i) {
                const VertexAttrib &vAtt = vAttribs.at(i);
                acc.name = newAccessorName();
                acc.usage = vAtt.usage;
                acc.offset = startOffset * sizeof(float);
                switch (vAtt.att->vertexSize()) {
                case 1:
                    acc.type = QStringLiteral("SCALAR");
                    break;
                case 2:
                    acc.type = QStringLiteral("VEC2");
                    break;
                case 3:
                    acc.type = QStringLiteral("VEC3");
                    break;
                case 4:
                    acc.type = QStringLiteral("VEC4");
                    break;
                case 9:
                    acc.type = QStringLiteral("MAT3");
                    break;
                case 16:
                    acc.type = QStringLiteral("MAT4");
                    break;
                default:
                    qCWarning(GLTFExporterLog, "Invalid vertex size: %d", vAtt.att->vertexSize());
                    break;
                }
                meshInfo.accessors.append(acc);
                startOffset += vAtt.att->vertexSize();
            }

            // Index
            if (indexAttrib) {
                acc.name = newAccessorName();
                acc.usage = QStringLiteral("INDEX");
                acc.bufferView = indexBufView.name;
                acc.offset = 0;
                acc.stride = 0;
                acc.count = indexCount;
                acc.componentType = indexBufView.componentType;
                acc.type = QStringLiteral("SCALAR");
                meshInfo.accessors.append(acc);
            }
            m_buffer.append(vertexBuf);
            m_buffer.append(indexBuf);

            if (GLTFExporterLog().isDebugEnabled()) {
                qCDebug(GLTFExporterLog, "  Mesh #%i: (%ls/%ls)", meshCount,
                        qUtf16PrintableImpl(meshInfo.name), qUtf16PrintableImpl(meshInfo.originalName));
                qCDebug(GLTFExporterLog, "    Vertex count: %i", vertexCount);
                qCDebug(GLTFExporterLog, "    Bytes per vertex: %i", stride);
                qCDebug(GLTFExporterLog, "    Vertex buffer size (bytes): %i", vertexBuf.size());
                qCDebug(GLTFExporterLog, "    Index buffer size (bytes): %i", indexBuf.size());
                QStringList sl;
                const auto views = meshInfo.views;
                for (const auto &bv : views)
                    sl << bv.name;
                qCDebug(GLTFExporterLog) << "    buffer views:" << sl;
                sl.clear();
                for (const auto &acc : qAsConst(meshInfo.accessors))
                    sl << acc.name;
                qCDebug(GLTFExporterLog) << "    accessors:" << sl;
                qCDebug(GLTFExporterLog, "    material: '%ls'",
                        qUtf16PrintableImpl(meshInfo.materialName));
            }
        }

        meshCount++;
        m_meshInfo.insert(mesh, meshInfo);
    }

    qCDebug(GLTFExporterLog, "Total buffer size: %i", m_buffer.size());
}

void GLTFExporter::parseCameras()
{
    qCDebug(GLTFExporterLog, "Parsing cameras...");
    int cameraCount = 0;

    for (auto it = m_cameraMap.constBegin(); it != m_cameraMap.constEnd(); ++it) {
        QCameraLens *camera = it.value();
        CameraInfo c;

        if (camera->projectionType() == QCameraLens::PerspectiveProjection) {
            c.perspective = true;
            c.aspectRatio = camera->aspectRatio();
            c.yfov = qDegreesToRadians(camera->fieldOfView());
        } else {
            c.perspective = false;
            // Note that accurate conversion from four properties of QCameraLens to just two
            // properties of gltf orthographic cameras is not feasible. Only centered cases
            // convert properly.
            c.xmag = qAbs(camera->left() - camera->right());
            c.ymag = qAbs(camera->top() - camera->bottom());
        }

        c.originalName = camera->objectName();
        c.name = newCameraName();
        c.znear = camera->nearPlane();
        c.zfar = camera->farPlane();

        // GLTF cameras point in -Z by default, the rest is in the
        // node matrix, so no separate look-at params given here, unless it's actually QCamera.
        QCamera *cameraEntity = nullptr;
        const QVector<QEntity *> entities = camera->entities();
        if (entities.size() == 1)
            cameraEntity = qobject_cast<QCamera *>(entities.at(0));
        c.cameraEntity = cameraEntity;

        m_cameraInfo.insert(camera, c);
        if (GLTFExporterLog().isDebugEnabled()) {
            qCDebug(GLTFExporterLog, "  Camera: #%i: (%ls/%ls)", cameraCount++,
                    qUtf16PrintableImpl(c.name), qUtf16PrintableImpl(c.originalName));
            qCDebug(GLTFExporterLog, "    Aspect ratio: %f", c.aspectRatio);
            qCDebug(GLTFExporterLog, "    Fov: %f", c.yfov);
            qCDebug(GLTFExporterLog, "    Near: %f", c.znear);
            qCDebug(GLTFExporterLog, "    Far: %f", c.zfar);
        }
    }
}

void GLTFExporter::parseLights()
{
    qCDebug(GLTFExporterLog, "Parsing lights...");
    int lightCount = 0;
    for (auto it = m_lightMap.constBegin(); it != m_lightMap.constEnd(); ++it) {
        QAbstractLight *light = it.value();
        LightInfo lightInfo;
        lightInfo.direction = QVector3D();
        lightInfo.attenuation = QVector3D();
        lightInfo.cutOffAngle = 0.0f;
        lightInfo.type = light->type();
        if (light->type() == QAbstractLight::SpotLight) {
            QSpotLight *spot = qobject_cast<QSpotLight *>(light);
            lightInfo.direction = spot->localDirection();
            lightInfo.attenuation = QVector3D(spot->constantAttenuation(),
                                              spot->linearAttenuation(),
                                              spot->quadraticAttenuation());
            lightInfo.cutOffAngle = spot->cutOffAngle();
        } else if (light->type() == QAbstractLight::PointLight) {
            QPointLight *point = qobject_cast<QPointLight *>(light);
            lightInfo.attenuation = QVector3D(point->constantAttenuation(),
                                              point->linearAttenuation(),
                                              point->quadraticAttenuation());
        } else if (light->type() == QAbstractLight::DirectionalLight) {
            QDirectionalLight *directional = qobject_cast<QDirectionalLight *>(light);
            lightInfo.direction = directional->worldDirection();
        }
        lightInfo.color = light->color();
        lightInfo.intensity = light->intensity();

        lightInfo.originalName = light->objectName();
        lightInfo.name = newLightName();

        m_lightInfo.insert(light, lightInfo);

        if (GLTFExporterLog().isDebugEnabled()) {
            qCDebug(GLTFExporterLog, "  Light #%i: (%ls/%ls)", lightCount++,
                    qUtf16PrintableImpl(lightInfo.name), qUtf16PrintableImpl(lightInfo.originalName));
            qCDebug(GLTFExporterLog, "    Type: %i", lightInfo.type);
            qCDebug(GLTFExporterLog, "    Color: (%i, %i, %i, %i)", lightInfo.color.red(),
                    lightInfo.color.green(), lightInfo.color.blue(), lightInfo.color.alpha());
            qCDebug(GLTFExporterLog, "    Intensity: %f", lightInfo.intensity);
            qCDebug(GLTFExporterLog, "    Direction: (%f, %f, %f)", lightInfo.direction.x(),
                    lightInfo.direction.y(), lightInfo.direction.z());
            qCDebug(GLTFExporterLog, "    Attenuation: (%f, %f, %f)", lightInfo.attenuation.x(),
                    lightInfo.attenuation.y(), lightInfo.attenuation.z());
            qCDebug(GLTFExporterLog, "    CutOffAngle: %f", lightInfo.cutOffAngle);
        }
    }
}

void GLTFExporter::parseTechniques(QMaterial *material)
{
    int techniqueCount = 0;
    qCDebug(GLTFExporterLog, "  Parsing material techniques...");

    const auto techniques = material->effect()->techniques();
    for (auto technique : techniques) {
        QString techName;
        if (m_techniqueIdMap.contains(technique)) {
            techName = m_techniqueIdMap.value(technique);
        } else {
            techName = newTechniqueName();
            parseRenderPasses(technique);

        }
        m_techniqueIdMap.insert(technique, techName);

        techniqueCount++;

        if (GLTFExporterLog().isDebugEnabled()) {
            qCDebug(GLTFExporterLog, "    Technique #%i", techniqueCount);
            qCDebug(GLTFExporterLog, "      name: '%ls'", qUtf16PrintableImpl(techName));
        }
    }
}

void GLTFExporter::parseRenderPasses(QTechnique *technique)
{
    int passCount = 0;
    qCDebug(GLTFExporterLog, "    Parsing render passes for technique...");

    const auto renderPasses = technique->renderPasses();
    for (auto pass : renderPasses) {
        QString name;
        if (m_renderPassIdMap.contains(pass)) {
            name = m_renderPassIdMap.value(pass);
        } else {
            name = newRenderPassName();
            m_renderPassIdMap.insert(pass, name);
            if (pass->shaderProgram() && !m_programInfo.contains(pass->shaderProgram())) {
                ProgramInfo pi;
                pi.name = newProgramName();
                pi.vertexShader = addShaderInfo(QShaderProgram::Vertex,
                                                pass->shaderProgram()->vertexShaderCode());
                pi.tessellationControlShader =
                        addShaderInfo(QShaderProgram::Fragment,
                                      pass->shaderProgram()->tessellationControlShaderCode());
                pi.tessellationEvaluationShader =
                        addShaderInfo(QShaderProgram::TessellationControl,
                                      pass->shaderProgram()->tessellationEvaluationShaderCode());
                pi.geometryShader = addShaderInfo(QShaderProgram::TessellationEvaluation,
                                                  pass->shaderProgram()->geometryShaderCode());
                pi.fragmentShader = addShaderInfo(QShaderProgram::Geometry,
                                                  pass->shaderProgram()->fragmentShaderCode());
                pi.computeShader = addShaderInfo(QShaderProgram::Compute,
                                                 pass->shaderProgram()->computeShaderCode());
                m_programInfo.insert(pass->shaderProgram(), pi);
                qCDebug(GLTFExporterLog, "      program: '%ls'", qUtf16PrintableImpl(pi.name));
            }
        }
        passCount++;

        if (GLTFExporterLog().isDebugEnabled()) {
            qCDebug(GLTFExporterLog, "      Render pass #%i", passCount);
            qCDebug(GLTFExporterLog, "        name: '%ls'", qUtf16PrintableImpl(name));
        }
    }
}

QString GLTFExporter::addShaderInfo(QShaderProgram::ShaderType type, QByteArray code)
{
    if (code.isEmpty())
        return QString();

    for (const auto &si : qAsConst(m_shaderInfo)) {
        if (si.type == QShaderProgram::Vertex && code == si.code)
            return si.name;
    }

    ShaderInfo newInfo;
    newInfo.type = type;
    newInfo.code = code;
    newInfo.name = newShaderName();
    newInfo.uri = newInfo.name + QStringLiteral(".glsl");

    m_shaderInfo.append(newInfo);

    qCDebug(GLTFExporterLog, "      shader: '%ls'", qUtf16PrintableImpl(newInfo.name));

    return newInfo.name;
}

bool GLTFExporter::saveScene()
{
    qCDebug(GLTFExporterLog, "Saving scene...");

    QVector<MeshInfo::BufferView> bvList;
    QVector<MeshInfo::Accessor> accList;
    for (auto it = m_meshInfo.begin(); it != m_meshInfo.end(); ++it) {
        auto &mi = it.value();
        for (auto &v : mi.views)
            bvList << v;
        for (auto &acc : mi.accessors)
            accList << acc;
    }

    m_obj = QJsonObject();

    QJsonObject asset;
    asset["generator"] = QString(QStringLiteral("GLTFExporter %1")).arg(qVersion());
    asset["version"] = QStringLiteral("1.0");
    asset["premultipliedAlpha"] = true;
    m_obj["asset"] = asset;

    QString bufName = m_exportName + QStringLiteral(".bin");
    QString binFileName = m_exportDir + bufName;
    QFile f(binFileName);
    QFileInfo fiBin(binFileName);

    if (f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
        qCDebug(GLTFExporterLog, "  Writing '%ls'", qUtf16PrintableImpl(binFileName));
        m_exportedFiles.insert(fiBin.fileName());
        f.write(m_buffer);
        f.close();
    } else {
        qCWarning(GLTFExporterLog, "  Creating buffers file '%ls' failed!",
                  qUtf16PrintableImpl(binFileName));
        return false;
    }

    QJsonObject buffers;
    QJsonObject buffer;
    buffer["byteLength"] = m_buffer.size();
    buffer["type"] = QStringLiteral("arraybuffer");
    buffer["uri"] = bufName;
    buffers["buf"] = buffer;
    m_obj["buffers"] = buffers;

    QJsonObject bufferViews;
    for (const auto &bv : qAsConst(bvList)) {
        QJsonObject bufferView;
        bufferView["buffer"] = QStringLiteral("buf");
        bufferView["byteLength"] = int(bv.length);
        bufferView["byteOffset"] = int(bv.offset);
        if (bv.target)
            bufferView["target"] = int(bv.target);
        bufferViews[bv.name] = bufferView;
    }
    if (bufferViews.size())
        m_obj["bufferViews"] = bufferViews;

    QJsonObject accessors;
    for (const auto &acc : qAsConst(accList)) {
        QJsonObject accessor;
        accessor["bufferView"] = acc.bufferView;
        accessor["byteOffset"] = int(acc.offset);
        accessor["byteStride"] = int(acc.stride);
        accessor["count"] = int(acc.count);
        accessor["componentType"] = int(acc.componentType);
        accessor["type"] = acc.type;
        accessors[acc.name] = accessor;
    }
    if (accessors.size())
        m_obj["accessors"] = accessors;

    QJsonObject meshes;
    for (auto it = m_meshInfo.begin(); it != m_meshInfo.end(); ++it) {
        auto &meshInfo = it.value();
        QJsonObject mesh;
        mesh["name"] = meshInfo.originalName;
        if (meshInfo.meshType != TypeNone) {
            QJsonObject properties;
            exportGenericProperties(properties, meshInfo.meshType, meshInfo.meshComponent);
            mesh["type"] = meshInfo.meshTypeStr;
            mesh["properties"] = properties;
            mesh["material"] = meshInfo.materialName;
        } else {
            QJsonArray prims;
            QJsonObject prim;
            prim["mode"] = 4; // triangles
            QJsonObject attrs;
            const auto meshAccessors = meshInfo.accessors;
            for (const auto &acc : meshAccessors) {
                if (acc.usage != QStringLiteral("INDEX"))
                    attrs[acc.usage] = acc.name;
                else
                    prim["indices"] = acc.name;
            }
            prim["attributes"] = attrs;
            prim["material"] = meshInfo.materialName;
            prims.append(prim);
            mesh["primitives"] = prims;
        }
        meshes[meshInfo.name] = mesh;
    }
    if (meshes.size())
        m_obj["meshes"] = meshes;

    QJsonObject cameras;
    for (const auto &camInfo : qAsConst(m_cameraInfo)) {
        QJsonObject camera;
        QJsonObject proj;
        proj["znear"] = camInfo.znear;
        proj["zfar"] = camInfo.zfar;
        if (camInfo.perspective) {
            proj["aspect_ratio"] = camInfo.aspectRatio;
            proj["yfov"] = camInfo.yfov;
            camera["type"] = QStringLiteral("perspective");
            camera["perspective"] = proj;
        } else {
            proj["xmag"] = camInfo.xmag;
            proj["ymag"] = camInfo.ymag;
            camera["type"] = QStringLiteral("orthographic");
            camera["orthographic"] = proj;
        }
        if (camInfo.cameraEntity) {
            camera["position"] = vec2jsvec(camInfo.cameraEntity->position());
            camera["upVector"] = vec2jsvec(camInfo.cameraEntity->upVector());
            camera["viewCenter"] = vec2jsvec(camInfo.cameraEntity->viewCenter());
        }
        camera["name"] = camInfo.originalName;
        cameras[camInfo.name] = camera;
    }
    if (cameras.size())
        m_obj["cameras"] = cameras;

    QJsonArray sceneNodes;
    QJsonObject nodes;
    if (m_rootNodeEmpty) {
        // Don't export the root node if it is there just to group the scene, so we don't get
        // an extra empty node when we import the scene back.
        for (auto c : qAsConst(m_rootNode->children))
            sceneNodes << exportNodes(c, nodes);
    } else {
        sceneNodes << exportNodes(m_rootNode, nodes);
    }
    m_obj["nodes"] = nodes;

    QJsonObject scenes;
    QJsonObject defaultScene;
    defaultScene["nodes"] = sceneNodes;
    scenes["defaultScene"] = defaultScene;
    m_obj["scenes"] = scenes;
    m_obj["scene"] = QStringLiteral("defaultScene");

    QJsonObject materials;

    exportMaterials(materials);
    if (materials.size())
        m_obj["materials"] = materials;

    // Lights must be declared as extensions to the top-level glTF object
    QJsonObject lights;
    for (auto it = m_lightInfo.begin(); it != m_lightInfo.end(); ++it) {
        const auto &lightInfo = it.value();
        QJsonObject light;
        QJsonObject lightDetails;
        QString type;
        if (lightInfo.type == QAbstractLight::SpotLight) {
            type = QStringLiteral("spot");
            lightDetails["falloffAngle"] = lightInfo.cutOffAngle;
        } else if (lightInfo.type == QAbstractLight::PointLight) {
            type = QStringLiteral("point");
        } else if (lightInfo.type == QAbstractLight::DirectionalLight) {
            type = QStringLiteral("directional");
        }
        light["type"] = type;
        if (lightInfo.type == QAbstractLight::SpotLight
                || lightInfo.type == QAbstractLight::DirectionalLight) {
            // The GLTF specs are bit unclear whether there is a direction parameter
            // for spot/directional lights, or are they supposed to just use the
            // parent transforms for direction, but we do need it in any case, so we add it.
            lightDetails["direction"] = vec2jsvec(lightInfo.direction);

        }
        if (lightInfo.type == QAbstractLight::SpotLight
                || lightInfo.type == QAbstractLight::PointLight) {
            lightDetails["constantAttenuation"] = lightInfo.attenuation.x();
            lightDetails["linearAttenuation"] = lightInfo.attenuation.y();
            lightDetails["quadraticAttenuation"] = lightInfo.attenuation.z();
        }
        lightDetails["color"] = col2jsvec(lightInfo.color, false);
        lightDetails["intensity"] = lightInfo.intensity; // Not in spec but needed
        light["name"] = lightInfo.originalName; // Not in spec but we want to pass the name anyway
        light[type] = lightDetails;
        lights[lightInfo.name] = light;
    }
    if (lights.size()) {
        QJsonObject extensions;
        QJsonObject common;
        common["lights"] = lights;
        extensions["KHR_materials_common"] = common;
        m_obj["extensions"] = extensions;
    }

    // Save effects for custom materials
    // Note that we are not saving effects, techniques, render passes, shader programs, or shaders
    // strictly according to GLTF format, but rather in our expanded QGLTF custom format,
    // since the GLTF format doesn't quite match our needs.
    // Having our own format also vastly simplifies export and import of custom materials,
    // since we are not trying to push a round peg into a square hole.
    // If use cases arise in future where our exported GLTF scenes need to be loaded by third party
    // GLTF loaders, we could add an export option to do so, but the exported scene would never
    // be quite the same as the original.
    QJsonObject effects;
    for (auto it = m_effectIdMap.constBegin(); it != m_effectIdMap.constEnd(); ++it) {
        QEffect *effect = it.key();
        const QString effectName = it.value();
        QJsonObject effectObj;
        QJsonObject paramObj;

        const auto effectParameters = effect->parameters();
        for (QParameter *param : effectParameters)
            exportParameter(paramObj, param->name(), param->value());
        if (!effect->objectName().isEmpty())
            effectObj["name"] = effect->objectName();
        if (!paramObj.isEmpty())
            effectObj["parameters"] = paramObj;
        QJsonArray techs;
        const auto effectTechniques = effect->techniques();
        for (auto tech : effectTechniques)
            techs << m_techniqueIdMap.value(tech);
        effectObj["techniques"] = techs;
        effects[effectName] = effectObj;
    }
    if (effects.size())
        m_obj["effects"] = effects;

    // Save techniques for custom materials.
    QJsonObject techniques;
    for (auto it = m_techniqueIdMap.constBegin(); it != m_techniqueIdMap.constEnd(); ++it) {
        QTechnique *technique = it.key();

        QJsonObject techObj;
        QJsonObject filterKeyObj;
        QJsonObject paramObj;
        QJsonArray renderPassArr;

        const auto techniqueFilterKeys = technique->filterKeys();
        for (QFilterKey *filterKey : techniqueFilterKeys)
            setVarToJSonObject(filterKeyObj, filterKey->name(), filterKey->value());

        const auto techniqueRenderPasses = technique->renderPasses();
        for (QRenderPass *pass : techniqueRenderPasses)
            renderPassArr << m_renderPassIdMap.value(pass);

        const auto techniqueParameters = technique->parameters();
        for (QParameter *param : techniqueParameters)
            exportParameter(paramObj, param->name(), param->value());

        const QGraphicsApiFilter *gFilter = technique->graphicsApiFilter();
        if (gFilter) {
            QJsonObject graphicsApiFilterObj;
            graphicsApiFilterObj["api"] = gFilter->api();
            graphicsApiFilterObj["profile"] = gFilter->profile();
            graphicsApiFilterObj["minorVersion"] = gFilter->minorVersion();
            graphicsApiFilterObj["majorVersion"] = gFilter->majorVersion();
            if (!gFilter->vendor().isEmpty())
                graphicsApiFilterObj["vendor"] = gFilter->vendor();
            QJsonArray extensions;
            for (const auto &extName : gFilter->extensions())
                extensions << extName;
            if (!extensions.isEmpty())
                graphicsApiFilterObj["extensions"] = extensions;
            techObj["gapifilter"] = graphicsApiFilterObj;
        }
        if (!technique->objectName().isEmpty())
            techObj["name"] = technique->objectName();
        if (!filterKeyObj.isEmpty())
            techObj["filterkeys"] = filterKeyObj;
        if (!paramObj.isEmpty())
            techObj["parameters"] = paramObj;
        if (!renderPassArr.isEmpty())
            techObj["renderpasses"] = renderPassArr;
        techniques[it.value()] = techObj;
    }
    if (techniques.size())
        m_obj["techniques"] = techniques;

    // Save render passes for custom materials.
    QJsonObject passes;
    for (auto it = m_renderPassIdMap.constBegin(); it != m_renderPassIdMap.constEnd(); ++it) {
        const QRenderPass *pass = it.key();
        const QString passId = it.value();

        QJsonObject passObj;
        QJsonObject filterKeyObj;
        QJsonObject paramObj;
        QJsonObject stateObj;

        for (QFilterKey *filterKey : pass->filterKeys())
            setVarToJSonObject(filterKeyObj, filterKey->name(), filterKey->value());
        for (QParameter *param : pass->parameters())
            exportParameter(paramObj, param->name(), param->value());
        exportRenderStates(stateObj, pass);

        if (!pass->objectName().isEmpty())
            passObj["name"] = pass->objectName();
        if (!filterKeyObj.isEmpty())
            passObj["filterkeys"] = filterKeyObj;
        if (!paramObj.isEmpty())
            passObj["parameters"] = paramObj;
        if (!stateObj.isEmpty())
            passObj["states"] = stateObj;
        passObj["program"] = m_programInfo.value(pass->shaderProgram()).name;
        passes[passId] = passObj;

    }
    if (passes.size())
        m_obj["renderpasses"] = passes;

    // Save programs for custom materials
    QJsonObject programs;
    for (auto it = m_programInfo.constBegin(); it != m_programInfo.constEnd(); ++it) {
        const QShaderProgram *program = it.key();
        const ProgramInfo pi = it.value();

        QJsonObject progObj;
        if (!program->objectName().isEmpty())
            progObj["name"] = program->objectName();
        progObj["vertexShader"] = pi.vertexShader;
        progObj["fragmentShader"] = pi.fragmentShader;
        // Qt3D additions
        if (!pi.tessellationControlShader.isEmpty())
            progObj["tessCtrlShader"] = pi.tessellationControlShader;
        if (!pi.tessellationEvaluationShader.isEmpty())
            progObj["tessEvalShader"] = pi.tessellationEvaluationShader;
        if (!pi.geometryShader.isEmpty())
            progObj["geometryShader"] = pi.geometryShader;
        if (!pi.computeShader.isEmpty())
            progObj["computeShader"] = pi.computeShader;
        programs[pi.name] = progObj;

    }
    if (programs.size())
        m_obj["programs"] = programs;

    // Save shaders for custom materials
    QJsonObject shaders;
    for (const auto &si : qAsConst(m_shaderInfo)) {
        QJsonObject shaderObj;
        shaderObj["uri"] = si.uri;
        shaders[si.name] = shaderObj;

    }
    if (shaders.size())
        m_obj["shaders"] = shaders;

    // Copy textures and shaders into temporary directory
    copyTextures();
    createShaders();

    QJsonObject textures;
    QHash<QString, QString> imageKeyMap; // uri -> key
    for (auto it = m_textureIdMap.constBegin(); it != m_textureIdMap.constEnd(); ++it) {
        QJsonObject texture;
        if (!imageKeyMap.contains(it.key()))
            imageKeyMap[it.key()] = newImageName();
        texture["source"] = imageKeyMap[it.key()];
        texture["format"] = GL_RGBA;
        texture["internalFormat"] = GL_RGBA;
        texture["sampler"] = QStringLiteral("sampler_mip_rep");
        texture["target"] = GL_TEXTURE_2D;
        texture["type"] = GL_UNSIGNED_BYTE;
        textures[it.value()] = texture;
    }
    if (textures.size()) {
        m_obj["textures"] = textures;
        QJsonObject samplers;
        QJsonObject sampler;
        sampler["magFilter"] = GL_LINEAR;
        sampler["minFilter"] = GL_LINEAR_MIPMAP_LINEAR;
        sampler["wrapS"] = GL_REPEAT;
        sampler["wrapT"] = GL_REPEAT;
        samplers["sampler_mip_rep"] = sampler;
        m_obj["samplers"] = samplers;
    }

    QJsonObject images;
    for (auto it = imageKeyMap.constBegin(); it != imageKeyMap.constEnd(); ++it) {
        QJsonObject image;
        image["uri"] = m_imageMap.value(it.key());
        images[it.value()] = image;
    }
    if (images.size())
        m_obj["images"] = images;

    m_doc.setObject(m_obj);

    QString gltfName = m_exportDir + m_exportName + QStringLiteral(".qgltf");
    f.setFileName(gltfName);
    qCDebug(GLTFExporterLog, "  Writing JSON file: '%ls'", qUtf16PrintableImpl(gltfName));

    if (f.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
        m_exportedFiles.insert(QFileInfo(f.fileName()).fileName());
        QByteArray json = m_doc.toJson(m_gltfOpts.compactJson ? QJsonDocument::Compact
                                                              : QJsonDocument::Indented);
        f.write(json);
        f.close();
    } else {
        qCWarning(GLTFExporterLog, "  Writing JSON file '%ls' failed!",
                  qUtf16PrintableImpl(gltfName));
        return false;
    }

    QString qrcName = m_exportDir + m_exportName + QStringLiteral(".qrc");
    f.setFileName(qrcName);
    qCDebug(GLTFExporterLog, "Writing '%ls'", qUtf16PrintableImpl(qrcName));
    if (f.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate)) {
        QByteArray pre = "<RCC><qresource prefix=\"/gltf_res\">\n";
        QByteArray post = "</qresource></RCC>\n";
        f.write(pre);
        for (const auto &file : qAsConst(m_exportedFiles)) {
            QString line = QString(QStringLiteral("  <file>%1</file>\n")).arg(file);
            f.write(line.toUtf8());
        }
        f.write(post);
        f.close();
        m_exportedFiles.insert(QFileInfo(f.fileName()).fileName());
    } else {
        qCWarning(GLTFExporterLog, "  Creating qrc file '%ls' failed!",
                  qUtf16PrintableImpl(qrcName));
        return false;
    }

    qCDebug(GLTFExporterLog, "Saving done!");

    return true;
}

void GLTFExporter::delNode(GLTFExporter::Node *n)
{
    if (!n)
        return;
    for (auto *c : qAsConst(n->children))
        delNode(c);
    delete n;
}

QString GLTFExporter::exportNodes(GLTFExporter::Node *n, QJsonObject &nodes)
{
    QJsonObject node;
    node["name"] = n->name;
    QJsonArray children;
    for (auto c : qAsConst(n->children))
        children << exportNodes(c, nodes);
    node["children"] = children;
    if (auto transform = m_transformMap.value(n))
        node["matrix"] = matrix2jsvec(transform->matrix());

    if (auto mesh = m_meshMap.value(n)) {
        QJsonArray meshList;
        meshList.append(m_meshInfo.value(mesh).name);
        node["meshes"] = meshList;
    }

    if (auto camera = m_cameraMap.value(n))
        node["camera"] = m_cameraInfo.value(camera).name;

    if (auto light = m_lightMap.value(n)) {
        QJsonObject extensions;
        QJsonObject lights;
        lights["light"] = m_lightInfo.value(light).name;
        extensions["KHR_materials_common"] = lights;
        node["extensions"] = extensions;
    }

    nodes[n->uniqueName] = node;
    return n->uniqueName;
}

void GLTFExporter::exportMaterials(QJsonObject &materials)
{
    QHash<QString, bool> imageHasAlpha;

    for (auto matIt = m_materialInfo.constBegin(); matIt != m_materialInfo.constEnd(); ++matIt) {
        const QMaterial *material = matIt.key();
        const MaterialInfo &matInfo = matIt.value();

        QJsonObject materialObj;
        materialObj["name"] = matInfo.originalName;

        if (matInfo.type == MaterialInfo::TypeCustom) {
            QVector<QParameter *> parameters = material->parameters();
            QJsonObject paramObj;
            for (auto param : parameters)
                exportParameter(paramObj, param->name(), param->value());
            materialObj["effect"] = m_effectIdMap.value(material->effect());
            materialObj["parameters"] = paramObj;
        } else {
            bool opaque = true;
            QJsonObject vals;
            for (auto it = matInfo.textures.constBegin(); it != matInfo.textures.constEnd(); ++it) {
                QString key = it.key();
                if (key == QStringLiteral("normal")) // avoid clashing with the vertex normals
                    key = QStringLiteral("normalmap");
                // Alpha is supported for diffuse textures, but have to check the image data to
                // decide if blending is needed
                if (key == QStringLiteral("diffuse")) {
                    QString imgFn = it.value();
                    if (imageHasAlpha.contains(imgFn)) {
                        if (imageHasAlpha[imgFn])
                            opaque = false;
                    } else {
                        QImage img(imgFn);
                        if (!img.isNull()) {
                            if (img.hasAlphaChannel()) {
                                for (int y = 0; opaque && y < img.height(); ++y) {
                                    for (int x = 0; opaque && x < img.width(); ++x) {
                                        if (qAlpha(img.pixel(x, y)) < 255)
                                            opaque = false;
                                    }
                                }
                            }
                            imageHasAlpha[imgFn] = !opaque;
                        } else {
                            qCWarning(GLTFExporterLog,
                                      "Cannot determine presence of alpha for '%ls'",
                                      qUtf16PrintableImpl(imgFn));
                        }
                    }
                }
                vals[key] = m_textureIdMap.value(it.value());
            }
            for (auto it = matInfo.values.constBegin(); it != matInfo.values.constEnd(); ++it) {
                if (vals.contains(it.key()))
                    continue;
                setVarToJSonObject(vals, it.key(), it.value());
            }
            for (auto it = matInfo.colors.constBegin(); it != matInfo.colors.constEnd(); ++it) {
                if (vals.contains(it.key()))
                    continue;
                // Alpha is supported for the diffuse color. < 1 will enable blending.
                const bool alpha = (it.key() == QStringLiteral("diffuse"))
                        && (matInfo.type != MaterialInfo::TypeCustom);
                if (alpha && it.value().alphaF() < 1.0f)
                    opaque = false;
                vals[it.key()] = col2jsvec(it.value(), alpha);
            }
            // Material is a common material, so export it as such.
            QJsonObject commonMat;
            if (matInfo.type == MaterialInfo::TypeGooch)
                commonMat["technique"] = QStringLiteral("GOOCH"); // Qt3D specific extension
            else if (matInfo.type == MaterialInfo::TypePerVertex)
                commonMat["technique"] = QStringLiteral("PERVERTEX"); // Qt3D specific extension
            else
                commonMat["technique"] = QStringLiteral("PHONG");

            // Set the values as-is. "normalmap" is our own extension, not in the spec.
            // However, RGB colors have to be promoted to RGBA since the spec uses
            // vec4, and all types are pre-defined for common material values.
            promoteColorsToRGBA(&vals);
            if (!vals.isEmpty())
                commonMat["values"] = vals;

            // Blend function handling is our own extension used for Phong Alpha material.
            QJsonObject functions;
            if (!matInfo.blendEquations.isEmpty())
                functions["blendEquationSeparate"] = vec2jsvec(matInfo.blendEquations);
            if (!matInfo.blendArguments.isEmpty())
                functions["blendFuncSeparate"] = vec2jsvec(matInfo.blendArguments);
            if (!functions.isEmpty())
                commonMat["functions"] = functions;
            QJsonObject extensions;
            extensions["KHR_materials_common"] = commonMat;
            materialObj["extensions"] = extensions;
        }

        materials[matInfo.name] = materialObj;
    }
}

void GLTFExporter::exportGenericProperties(QJsonObject &jsonObj, PropertyCacheType type,
                                           QObject *obj)
{
    QVector<QMetaProperty> properties = m_propertyCache.value(type);
    QObject *defaultObject = m_defaultObjectCache.value(type);
    for (const QMetaProperty &property : properties) {
        // Only output property if it is different from default
        QVariant defaultValue = defaultObject->property(property.name());
        QVariant objectValue = obj->property(property.name());
        if (defaultValue != objectValue)
            setVarToJSonObject(jsonObj, QString::fromLatin1(property.name()), objectValue);
    }
}

void GLTFExporter::clearOldExport(const QString &dir)
{
    // Look for .qrc file with same name
    QRegularExpression re(QStringLiteral("<file>(.*)</file>"));
    QFile qrcFile(dir + m_exportName + QStringLiteral(".qrc"));
    if (qrcFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
        while (!qrcFile.atEnd()) {
            QByteArray line = qrcFile.readLine();
            QRegularExpressionMatch match = re.match(line);
            if (match.hasMatch()) {
                QString fileName = match.captured(1);
                QString filePathName = dir + fileName;
                QFile::remove(filePathName);
                qCDebug(GLTFExporterLog, "Removed old file: '%ls'",
                        qUtf16PrintableImpl(filePathName));
            }
        }
        qrcFile.close();
        qrcFile.remove();
        qCDebug(GLTFExporterLog, "Removed old file: '%ls'",
                qUtf16PrintableImpl(qrcFile.fileName()));
    }
}

void GLTFExporter::exportParameter(QJsonObject &jsonObj, const QString &name,
                                   const QVariant &variant)
{
    QLatin1String typeStr("type");
    QLatin1String valueStr("value");

    QJsonObject paramObj;

    if (variant.canConvert<QAbstractTexture *>()) {
        paramObj[typeStr] = GL_SAMPLER_2D;
        paramObj[valueStr] = m_textureIdMap.value(textureVariantToUrl(variant));
    } else {
        switch (QMetaType::Type(variant.type())) {
        case QMetaType::Bool:
            paramObj[typeStr] = GL_BOOL;
            paramObj[valueStr] = variant.toBool();
            break;
        case QMetaType::Int: // fall through
        case QMetaType::Long: // fall through
        case QMetaType::LongLong:
            paramObj[typeStr] = GL_INT;
            paramObj[valueStr] = variant.toInt();
            break;
        case QMetaType::UInt: // fall through
        case QMetaType::ULong: // fall through
        case QMetaType::ULongLong:
            paramObj[typeStr] = GL_UNSIGNED_INT;
            paramObj[valueStr] = variant.toInt();
            break;
        case QMetaType::Short:
            paramObj[typeStr] = GL_SHORT;
            paramObj[valueStr] = variant.toInt();
            break;
        case QMetaType::UShort:
            paramObj[typeStr] = GL_UNSIGNED_SHORT;
            paramObj[valueStr] = variant.toInt();
            break;
        case QMetaType::Char:
            paramObj[typeStr] = GL_BYTE;
            paramObj[valueStr] = variant.toInt();
            break;
        case QMetaType::UChar:
            paramObj[typeStr] = GL_UNSIGNED_BYTE;
            paramObj[valueStr] = variant.toInt();
            break;
        case QMetaType::QColor:
            paramObj[typeStr] = GL_FLOAT_VEC4;
            paramObj[valueStr] = col2jsvec(variant.value<QColor>(), true);
            break;
        case QMetaType::Float:
            paramObj[typeStr] = GL_FLOAT;
            paramObj[valueStr] = variant.value<float>();
            break;
        case QMetaType::QVector2D:
            paramObj[typeStr] = GL_FLOAT_VEC2;
            paramObj[valueStr] = vec2jsvec(variant.value<QVector2D>());
            break;
        case QMetaType::QVector3D:
            paramObj[typeStr] = GL_FLOAT_VEC3;
            paramObj[valueStr] = vec2jsvec(variant.value<QVector3D>());
            break;
        case QMetaType::QVector4D:
            paramObj[typeStr] = GL_FLOAT_VEC4;
            paramObj[valueStr] = vec2jsvec(variant.value<QVector4D>());
            break;
        case QMetaType::QMatrix4x4:
            paramObj[typeStr] = GL_FLOAT_MAT4;
            paramObj[valueStr] = matrix2jsvec(variant.value<QMatrix4x4>());
            break;
        default:
            qCWarning(GLTFExporterLog, "Unknown value type for '%ls'", qUtf16PrintableImpl(name));
            break;
        }
    }

    jsonObj[name] = paramObj;
}

void GLTFExporter::exportRenderStates(QJsonObject &jsonObj, const QRenderPass *pass)
{
    QJsonArray enableStates;
    QJsonObject funcObj;
    const auto renderStates = pass->renderStates();
    for (QRenderState *state : renderStates) {
        QJsonArray arr;
        if (qobject_cast<QAlphaCoverage *>(state)) {
            enableStates << GL_SAMPLE_ALPHA_TO_COVERAGE;
        } else if (qobject_cast<QAlphaTest *>(state)) {
            auto s = qobject_cast<QAlphaTest *>(state);
            arr << s->alphaFunction();
            arr << s->referenceValue();
            funcObj["alphaTest"] = arr;
        } else if (qobject_cast<QBlendEquation *>(state)) {
            auto s = qobject_cast<QBlendEquation *>(state);
            arr << s->blendFunction();
            funcObj["blendEquationSeparate"] = arr;
        } else if (qobject_cast<QBlendEquationArguments *>(state)) {
            auto s = qobject_cast<QBlendEquationArguments *>(state);
            arr << s->sourceRgb();
            arr << s->sourceAlpha();
            arr << s->destinationRgb();
            arr << s->destinationAlpha();
            arr << s->bufferIndex();
            funcObj["blendFuncSeparate"] = arr;
        } else if (qobject_cast<QClipPlane *>(state)) {
            auto s = qobject_cast<QClipPlane *>(state);
            arr << s->planeIndex();
            arr << s->normal().x();
            arr << s->normal().y();
            arr << s->normal().z();
            arr << s->distance();
            funcObj["clipPlane"] = arr;
        } else if (qobject_cast<QColorMask *>(state)) {
            auto s = qobject_cast<QColorMask *>(state);
            arr << s->isRedMasked();
            arr << s->isGreenMasked();
            arr << s->isBlueMasked();
            arr << s->isAlphaMasked();
            funcObj["colorMask"] = arr;
        } else if (qobject_cast<QCullFace *>(state)) {
            auto s = qobject_cast<QCullFace *>(state);
            arr << s->mode();
            funcObj["cullFace"] = arr;
        } else if (qobject_cast<QDepthRange *>(state)) {
            auto s = qobject_cast<QDepthRange *>(state);
            arr << s->nearValue();
            arr << s->farValue();
            funcObj["depthRange"] = arr;
        } else if (qobject_cast<QDepthTest *>(state)) {
            auto s = qobject_cast<QDepthTest *>(state);
            arr << s->depthFunction();
            funcObj["depthFunc"] = arr;
        } else if (qobject_cast<QDithering *>(state)) {
            enableStates << GL_DITHER;
        } else if (qobject_cast<QFrontFace *>(state)) {
            auto s = qobject_cast<QFrontFace *>(state);
            arr << s->direction();
            funcObj["frontFace"] = arr;
        } else if (qobject_cast<QFrontFace *>(state)) {
            auto s = qobject_cast<QFrontFace *>(state);
            arr << s->direction();
            funcObj["frontFace"] = arr;
        } else if (qobject_cast<QMultiSampleAntiAliasing *>(state)) {
            enableStates << 0x809D; // GL_MULTISAMPLE
        } else if (qobject_cast<QNoDepthMask *>(state)) {
            arr << false;
            funcObj["depthMask"] = arr;
        } else if (qobject_cast<QPointSize *>(state)) {
            auto s = qobject_cast<QPointSize *>(state);
            arr << s->sizeMode();
            arr << s->value();
            funcObj["pointSize"] = arr;
        } else if (qobject_cast<QPolygonOffset *>(state)) {
            auto s = qobject_cast<QPolygonOffset *>(state);
            arr << s->scaleFactor();
            arr << s->depthSteps();
            funcObj["polygonOffset"] = arr;
        } else if (qobject_cast<QScissorTest *>(state)) {
            auto s = qobject_cast<QScissorTest *>(state);
            arr << s->left();
            arr << s->bottom();
            arr << s->width();
            arr << s->height();
            funcObj["scissor"] = arr;
        } else if (qobject_cast<QSeamlessCubemap *>(state)) {
            enableStates << 0x884F; // GL_TEXTURE_CUBE_MAP_SEAMLESS
        } else if (qobject_cast<QStencilMask *>(state)) {
            auto s = qobject_cast<QStencilMask *>(state);
            arr << int(s->frontOutputMask());
            arr << int(s->backOutputMask());
            funcObj["stencilMask"] = arr;
        } else if (qobject_cast<QStencilOperation *>(state)) {
            auto s = qobject_cast<QStencilOperation *>(state);
            arr << s->front()->stencilTestFailureOperation();
            arr << s->front()->depthTestFailureOperation();
            arr << s->front()->allTestsPassOperation();
            arr << s->back()->stencilTestFailureOperation();
            arr << s->back()->depthTestFailureOperation();
            arr << s->back()->allTestsPassOperation();
            funcObj["stencilOperation"] = arr;
        } else if (qobject_cast<QStencilTest *>(state)) {
            auto s = qobject_cast<QStencilTest *>(state);
            arr << int(s->front()->comparisonMask());
            arr << s->front()->referenceValue();
            arr << s->front()->stencilFunction();
            arr << int(s->back()->comparisonMask());
            arr << s->back()->referenceValue();
            arr << s->back()->stencilFunction();
            funcObj["stencilTest"] = arr;
        }
    }
    if (!enableStates.isEmpty())
        jsonObj["enable"] = enableStates;
    if (!funcObj.isEmpty())
        jsonObj["functions"] = funcObj;
}

QString GLTFExporter::newBufferViewName()
{
    return QString(QStringLiteral("bufferView_%1")).arg(++m_bufferViewCount);
}

QString GLTFExporter::newAccessorName()
{
    return QString(QStringLiteral("accessor_%1")).arg(++m_accessorCount);
}

QString GLTFExporter::newMeshName()
{
    return QString(QStringLiteral("mesh_%1")).arg(++m_meshCount);
}

QString GLTFExporter::newMaterialName()
{
    return QString(QStringLiteral("material_%1")).arg(++m_materialCount);
}

QString GLTFExporter::newTechniqueName()
{
    return QString(QStringLiteral("technique_%1")).arg(++m_techniqueCount);
}

QString GLTFExporter::newTextureName()
{
    return QString(QStringLiteral("texture_%1")).arg(++m_textureCount);
}

QString GLTFExporter::newImageName()
{
    return QString(QStringLiteral("image_%1")).arg(++m_imageCount);
}

QString GLTFExporter::newShaderName()
{
    return QString(QStringLiteral("shader_%1")).arg(++m_shaderCount);
}

QString GLTFExporter::newProgramName()
{
    return QString(QStringLiteral("program_%1")).arg(++m_programCount);
}

QString GLTFExporter::newNodeName()
{
    return QString(QStringLiteral("node_%1")).arg(++m_nodeCount);
}

QString GLTFExporter::newCameraName()
{
    return QString(QStringLiteral("camera_%1")).arg(++m_cameraCount);
}

QString GLTFExporter::newLightName()
{
    return QString(QStringLiteral("light_%1")).arg(++m_lightCount);
}

QString GLTFExporter::newRenderPassName()
{
    return QString(QStringLiteral("renderpass_%1")).arg(++m_renderPassCount);
}

QString GLTFExporter::newEffectName()
{
    return QString(QStringLiteral("effect_%1")).arg(++m_effectCount);
}

QString GLTFExporter::textureVariantToUrl(const QVariant &var)
{
    QString urlString;
    QAbstractTexture *texture = var.value<QAbstractTexture *>();
    if (texture->textureImages().size()) {
        QTextureImage *image = qobject_cast<QTextureImage *>(texture->textureImages().at(0));
        if (image) {
            urlString = QUrlHelper::urlToLocalFileOrQrc(image->source());
            if (!m_textureIdMap.contains(urlString))
                m_textureIdMap.insert(urlString, newTextureName());
        }
    }
    return urlString;
}

void GLTFExporter::setVarToJSonObject(QJsonObject &jsObj, const QString &key, const QVariant &var)
{
    switch (QMetaType::Type(var.type())) {
    case QMetaType::Bool:
        jsObj[key] = var.toBool();
        break;
    case QMetaType::Int:
        jsObj[key] = var.toInt();
        break;
    case QMetaType::Float:
        jsObj[key] = var.value<float>();
        break;
    case QMetaType::QSize:
        jsObj[key] = size2jsvec(var.toSize());
        break;
    case QMetaType::QVector2D:
        jsObj[key] = vec2jsvec(var.value<QVector2D>());
        break;
    case QMetaType::QVector3D:
        jsObj[key] = vec2jsvec(var.value<QVector3D>());
        break;
    case QMetaType::QVector4D:
        jsObj[key] = vec2jsvec(var.value<QVector4D>());
        break;
    case QMetaType::QMatrix4x4:
        jsObj[key] = matrix2jsvec(var.value<QMatrix4x4>());
        break;
    case QMetaType::QString:
        jsObj[key] = var.toString();
        break;
    case QMetaType::QColor:
        jsObj[key] = col2jsvec(var.value<QColor>(), true);
        break;
    default:
        qCWarning(GLTFExporterLog, "Unknown value type for '%ls'", qUtf16PrintableImpl(key));
        break;
    }
}

} // namespace Qt3DRender

QT_END_NAMESPACE

#include "moc_gltfexporter.cpp"