summaryrefslogtreecommitdiff
path: root/editor/import/editor_import_collada.cpp
blob: 1ffede650231191dbc213c4f2d6a2732e1dbc39b (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
/**************************************************************************/
/*  editor_import_collada.cpp                                             */
/**************************************************************************/
/*                         This file is part of:                          */
/*                             GODOT ENGINE                               */
/*                        https://godotengine.org                         */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
/*                                                                        */
/* Permission is hereby granted, free of charge, to any person obtaining  */
/* a copy of this software and associated documentation files (the        */
/* "Software"), to deal in the Software without restriction, including    */
/* without limitation the rights to use, copy, modify, merge, publish,    */
/* distribute, sublicense, and/or sell copies of the Software, and to     */
/* permit persons to whom the Software is furnished to do so, subject to  */
/* the following conditions:                                              */
/*                                                                        */
/* The above copyright notice and this permission notice shall be         */
/* included in all copies or substantial portions of the Software.        */
/*                                                                        */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
/**************************************************************************/

#include "editor_import_collada.h"

#include "core/os/os.h"
#include "editor/editor_node.h"
#include "editor/import/collada.h"
#include "scene/3d/camera_3d.h"
#include "scene/3d/importer_mesh_instance_3d.h"
#include "scene/3d/light_3d.h"
#include "scene/3d/mesh_instance_3d.h"
#include "scene/3d/node_3d.h"
#include "scene/3d/path_3d.h"
#include "scene/3d/skeleton_3d.h"
#include "scene/animation/animation_player.h"
#include "scene/resources/animation.h"
#include "scene/resources/importer_mesh.h"
#include "scene/resources/packed_scene.h"
#include "scene/resources/surface_tool.h"

struct ColladaImport {
	Collada collada;
	Node3D *scene = nullptr;

	Vector<Ref<Animation>> animations;

	struct NodeMap {
		//String path;
		Node3D *node = nullptr;
		int bone = -1;
		List<int> anim_tracks;
	};

	bool found_ambient = false;
	Color ambient;
	bool found_directional = false;
	bool force_make_tangents = false;
	bool apply_mesh_xform_to_vertices = true;
	bool use_mesh_builtin_materials = false;
	float bake_fps = 30;

	HashMap<String, NodeMap> node_map; //map from collada node to engine node
	HashMap<String, String> node_name_map; //map from collada node to engine node
	HashMap<String, Ref<ImporterMesh>> mesh_cache;
	HashMap<String, Ref<Curve3D>> curve_cache;
	HashMap<String, Ref<Material>> material_cache;
	HashMap<Collada::Node *, Skeleton3D *> skeleton_map;

	HashMap<Skeleton3D *, HashMap<String, int>> skeleton_bone_map;

	HashSet<String> valid_animated_nodes;
	Vector<int> valid_animated_properties;
	HashMap<String, bool> bones_with_animation;

	HashSet<String> mesh_unique_names;
	HashSet<String> material_unique_names;

	Error _populate_skeleton(Skeleton3D *p_skeleton, Collada::Node *p_node, int &r_bone, int p_parent);
	Error _create_scene_skeletons(Collada::Node *p_node);
	Error _create_scene(Collada::Node *p_node, Node3D *p_parent);
	Error _create_resources(Collada::Node *p_node, bool p_use_compression);
	Error _create_material(const String &p_target);
	Error _create_mesh_surfaces(bool p_optimize, Ref<ImporterMesh> &p_mesh, const HashMap<String, Collada::NodeGeometry::Material> &p_material_map, const Collada::MeshData &meshdata, const Transform3D &p_local_xform, const Vector<int> &bone_remap, const Collada::SkinControllerData *p_skin_controller, const Collada::MorphControllerData *p_morph_data, Vector<Ref<ImporterMesh>> p_morph_meshes = Vector<Ref<ImporterMesh>>(), bool p_use_compression = false, bool p_use_mesh_material = false);
	Error load(const String &p_path, int p_flags, bool p_force_make_tangents = false, bool p_use_compression = false);
	void _fix_param_animation_tracks();
	void create_animation(int p_clip, bool p_import_value_tracks);
	void create_animations(bool p_import_value_tracks);

	HashSet<String> tracks_in_clips;
	Vector<String> missing_textures;

	void _pre_process_lights(Collada::Node *p_node);
};

Error ColladaImport::_populate_skeleton(Skeleton3D *p_skeleton, Collada::Node *p_node, int &r_bone, int p_parent) {
	if (p_node->type != Collada::Node::TYPE_JOINT) {
		return OK;
	}

	Collada::NodeJoint *joint = static_cast<Collada::NodeJoint *>(p_node);

	p_skeleton->add_bone(p_node->name);
	if (p_parent >= 0) {
		p_skeleton->set_bone_parent(r_bone, p_parent);
	}

	NodeMap nm;
	nm.node = p_skeleton;
	nm.bone = r_bone;
	node_map[p_node->id] = nm;
	node_name_map[p_node->name] = p_node->id;

	skeleton_bone_map[p_skeleton][joint->sid] = r_bone;

	{
		Transform3D xform = joint->compute_transform(collada);
		xform = collada.fix_transform(xform) * joint->post_transform;

		p_skeleton->set_bone_pose_position(r_bone, xform.origin);
		p_skeleton->set_bone_pose_rotation(r_bone, xform.basis.get_rotation_quaternion());
		p_skeleton->set_bone_pose_scale(r_bone, xform.basis.get_scale());
	}

	if (collada.state.bone_rest_map.has(joint->sid)) {
		p_skeleton->set_bone_rest(r_bone, collada.fix_transform(collada.state.bone_rest_map[joint->sid]));
		//should map this bone to something for animation?
	} else {
		WARN_PRINT("Collada: Joint has no rest.");
	}

	int id = r_bone++;
	for (int i = 0; i < p_node->children.size(); i++) {
		Error err = _populate_skeleton(p_skeleton, p_node->children[i], r_bone, id);
		if (err) {
			return err;
		}
	}

	return OK;
}

void ColladaImport::_pre_process_lights(Collada::Node *p_node) {
	if (p_node->type == Collada::Node::TYPE_LIGHT) {
		Collada::NodeLight *light = static_cast<Collada::NodeLight *>(p_node);
		if (collada.state.light_data_map.has(light->light)) {
			Collada::LightData &ld = collada.state.light_data_map[light->light];
			if (ld.mode == Collada::LightData::MODE_AMBIENT) {
				found_ambient = true;
				ambient = ld.color;
			}
			if (ld.mode == Collada::LightData::MODE_DIRECTIONAL) {
				found_directional = true;
			}
		}
	}

	for (int i = 0; i < p_node->children.size(); i++) {
		_pre_process_lights(p_node->children[i]);
	}
}

Error ColladaImport::_create_scene_skeletons(Collada::Node *p_node) {
	if (p_node->type == Collada::Node::TYPE_SKELETON) {
		Skeleton3D *sk = memnew(Skeleton3D);
		int bone = 0;
		for (int i = 0; i < p_node->children.size(); i++) {
			_populate_skeleton(sk, p_node->children[i], bone, -1);
		}
		sk->localize_rests(); //after creating skeleton, rests must be localized...!
		skeleton_map[p_node] = sk;
	}

	for (int i = 0; i < p_node->children.size(); i++) {
		Error err = _create_scene_skeletons(p_node->children[i]);
		if (err) {
			return err;
		}
	}
	return OK;
}

Error ColladaImport::_create_scene(Collada::Node *p_node, Node3D *p_parent) {
	Node3D *node = nullptr;

	switch (p_node->type) {
		case Collada::Node::TYPE_NODE: {
			node = memnew(Node3D);
		} break;
		case Collada::Node::TYPE_JOINT: {
			return OK; // do nothing
		} break;
		case Collada::Node::TYPE_LIGHT: {
			//node = memnew( Light)
			Collada::NodeLight *light = static_cast<Collada::NodeLight *>(p_node);
			if (collada.state.light_data_map.has(light->light)) {
				Collada::LightData &ld = collada.state.light_data_map[light->light];

				if (ld.mode == Collada::LightData::MODE_AMBIENT) {
					if (found_directional) {
						return OK; //do nothing not needed
					}

					if (!bool(GLOBAL_DEF("collada/use_ambient", false))) {
						return OK;
					}
					//well, it's an ambient light..
					Light3D *l = memnew(DirectionalLight3D);
					//l->set_color(Light::COLOR_AMBIENT,ld.color);
					//l->set_color(Light::COLOR_DIFFUSE,Color(0,0,0));
					//l->set_color(Light::COLOR_SPECULAR,Color(0,0,0));
					node = l;

				} else if (ld.mode == Collada::LightData::MODE_DIRECTIONAL) {
					//well, it's an ambient light..
					Light3D *l = memnew(DirectionalLight3D);
					/*
					if (found_ambient) //use it here
						l->set_color(Light::COLOR_AMBIENT,ambient);

					l->set_color(Light::COLOR_DIFFUSE,ld.color);
					l->set_color(Light::COLOR_SPECULAR,Color(1,1,1));
					*/
					node = l;
				} else {
					Light3D *l;

					if (ld.mode == Collada::LightData::MODE_OMNI) {
						l = memnew(OmniLight3D);
					} else {
						l = memnew(SpotLight3D);
						//l->set_parameter(Light::PARAM_SPOT_ANGLE,ld.spot_angle);
						//l->set_parameter(Light::PARAM_SPOT_ATTENUATION,ld.spot_exp);
					}

					//
					//l->set_color(Light::COLOR_DIFFUSE,ld.color);
					//l->set_color(Light::COLOR_SPECULAR,Color(1,1,1));
					//l->approximate_opengl_attenuation(ld.constant_att,ld.linear_att,ld.quad_att);
					node = l;
				}

			} else {
				node = memnew(Node3D);
			}
		} break;
		case Collada::Node::TYPE_CAMERA: {
			Collada::NodeCamera *cam = static_cast<Collada::NodeCamera *>(p_node);
			Camera3D *camera = memnew(Camera3D);

			if (collada.state.camera_data_map.has(cam->camera)) {
				const Collada::CameraData &cd = collada.state.camera_data_map[cam->camera];

				switch (cd.mode) {
					case Collada::CameraData::MODE_ORTHOGONAL: {
						if (cd.orthogonal.y_mag) {
							camera->set_keep_aspect_mode(Camera3D::KEEP_HEIGHT);
							camera->set_orthogonal(cd.orthogonal.y_mag * 2.0, cd.z_near, cd.z_far);

						} else if (!cd.orthogonal.y_mag && cd.orthogonal.x_mag) {
							camera->set_keep_aspect_mode(Camera3D::KEEP_WIDTH);
							camera->set_orthogonal(cd.orthogonal.x_mag * 2.0, cd.z_near, cd.z_far);
						}

					} break;
					case Collada::CameraData::MODE_PERSPECTIVE: {
						if (cd.perspective.y_fov) {
							camera->set_perspective(cd.perspective.y_fov, cd.z_near, cd.z_far);

						} else if (!cd.perspective.y_fov && cd.perspective.x_fov) {
							camera->set_perspective(cd.perspective.x_fov / cd.aspect, cd.z_near, cd.z_far);
						}

					} break;
				}
			}

			node = camera;

		} break;
		case Collada::Node::TYPE_GEOMETRY: {
			Collada::NodeGeometry *ng = static_cast<Collada::NodeGeometry *>(p_node);

			if (collada.state.curve_data_map.has(ng->source)) {
				node = memnew(Path3D);
			} else {
				//mesh since nothing else
				node = memnew(ImporterMeshInstance3D);
				//Object::cast_to<ImporterMeshInstance3D>(node)->set_flag(GeometryInstance3D::FLAG_USE_BAKED_LIGHT, true);
			}
		} break;
		case Collada::Node::TYPE_SKELETON: {
			ERR_FAIL_COND_V(!skeleton_map.has(p_node), ERR_CANT_CREATE);
			Skeleton3D *sk = skeleton_map[p_node];
			node = sk;
		} break;
	}

	if (!p_node->name.is_empty()) {
		node->set_name(p_node->name);
	}
	NodeMap nm;
	nm.node = node;
	node_map[p_node->id] = nm;
	node_name_map[node->get_name()] = p_node->id;
	Transform3D xf = p_node->default_transform;

	xf = collada.fix_transform(xf) * p_node->post_transform;
	node->set_transform(xf);
	p_parent->add_child(node, true);
	node->set_owner(scene);

	if (!p_node->empty_draw_type.is_empty()) {
		node->set_meta("empty_draw_type", Variant(p_node->empty_draw_type));
	}

	for (int i = 0; i < p_node->children.size(); i++) {
		Error err = _create_scene(p_node->children[i], node);
		if (err) {
			return err;
		}
	}
	return OK;
}

Error ColladaImport::_create_material(const String &p_target) {
	ERR_FAIL_COND_V(material_cache.has(p_target), ERR_ALREADY_EXISTS);
	ERR_FAIL_COND_V(!collada.state.material_map.has(p_target), ERR_INVALID_PARAMETER);
	Collada::Material &src_mat = collada.state.material_map[p_target];
	ERR_FAIL_COND_V(!collada.state.effect_map.has(src_mat.instance_effect), ERR_INVALID_PARAMETER);
	Collada::Effect &effect = collada.state.effect_map[src_mat.instance_effect];

	Ref<StandardMaterial3D> material = memnew(StandardMaterial3D);

	String base_name;
	if (!src_mat.name.is_empty()) {
		base_name = src_mat.name;
	} else if (!effect.name.is_empty()) {
		base_name = effect.name;
	} else {
		base_name = "Material";
	}

	String name = base_name;
	int counter = 2;
	while (material_unique_names.has(name)) {
		name = base_name + itos(counter++);
	}

	material_unique_names.insert(name);

	material->set_name(name);

	// DIFFUSE

	if (!effect.diffuse.texture.is_empty()) {
		String texfile = effect.get_texture_path(effect.diffuse.texture, collada);
		if (!texfile.is_empty()) {
			if (texfile.begins_with("/")) {
				texfile = texfile.replace_first("/", "res://");
			}
			Ref<Texture2D> texture = ResourceLoader::load(texfile, "Texture2D");
			if (texture.is_valid()) {
				material->set_texture(StandardMaterial3D::TEXTURE_ALBEDO, texture);
				material->set_albedo(Color(1, 1, 1, 1));
				//material->set_parameter(StandardMaterial3D::PARAM_DIFFUSE,Color(1,1,1,1));
			} else {
				missing_textures.push_back(texfile.get_file());
			}
		}
	} else {
		material->set_albedo(effect.diffuse.color);
	}

	// SPECULAR

	if (!effect.specular.texture.is_empty()) {
		String texfile = effect.get_texture_path(effect.specular.texture, collada);
		if (!texfile.is_empty()) {
			if (texfile.begins_with("/")) {
				texfile = texfile.replace_first("/", "res://");
			}

			Ref<Texture2D> texture = ResourceLoader::load(texfile, "Texture2D");
			if (texture.is_valid()) {
				material->set_texture(StandardMaterial3D::TEXTURE_METALLIC, texture);
				material->set_specular(1.0);

				//material->set_texture(StandardMaterial3D::PARAM_SPECULAR,texture);
				//material->set_parameter(StandardMaterial3D::PARAM_SPECULAR,Color(1,1,1,1));
			} else {
				missing_textures.push_back(texfile.get_file());
			}
		}

	} else {
		material->set_metallic(effect.specular.color.get_v());
	}

	// EMISSION

	if (!effect.emission.texture.is_empty()) {
		String texfile = effect.get_texture_path(effect.emission.texture, collada);
		if (!texfile.is_empty()) {
			if (texfile.begins_with("/")) {
				texfile = texfile.replace_first("/", "res://");
			}

			Ref<Texture2D> texture = ResourceLoader::load(texfile, "Texture2D");
			if (texture.is_valid()) {
				material->set_feature(StandardMaterial3D::FEATURE_EMISSION, true);
				material->set_texture(StandardMaterial3D::TEXTURE_EMISSION, texture);
				material->set_emission(Color(1, 1, 1, 1));

				//material->set_parameter(StandardMaterial3D::PARAM_EMISSION,Color(1,1,1,1));
			} else {
				missing_textures.push_back(texfile.get_file());
			}
		}
	} else {
		if (effect.emission.color != Color()) {
			material->set_feature(StandardMaterial3D::FEATURE_EMISSION, true);
			material->set_emission(effect.emission.color);
		}
	}

	// NORMAL

	if (!effect.bump.texture.is_empty()) {
		String texfile = effect.get_texture_path(effect.bump.texture, collada);
		if (!texfile.is_empty()) {
			if (texfile.begins_with("/")) {
				texfile = texfile.replace_first("/", "res://");
			}

			Ref<Texture2D> texture = ResourceLoader::load(texfile, "Texture2D");
			if (texture.is_valid()) {
				material->set_feature(StandardMaterial3D::FEATURE_NORMAL_MAPPING, true);
				material->set_texture(StandardMaterial3D::TEXTURE_NORMAL, texture);
				//material->set_emission(Color(1,1,1,1));

				//material->set_texture(StandardMaterial3D::PARAM_NORMAL,texture);
			} else {
				//missing_textures.push_back(texfile.get_file());
			}
		}
	}

	float roughness = (effect.shininess - 1.0) / 510;
	material->set_roughness(roughness);

	if (effect.double_sided) {
		material->set_cull_mode(StandardMaterial3D::CULL_DISABLED);
	}
	if (effect.unshaded) {
		material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
	}

	material_cache[p_target] = material;
	return OK;
}

Error ColladaImport::_create_mesh_surfaces(bool p_optimize, Ref<ImporterMesh> &p_mesh, const HashMap<String, Collada::NodeGeometry::Material> &p_material_map, const Collada::MeshData &meshdata, const Transform3D &p_local_xform, const Vector<int> &bone_remap, const Collada::SkinControllerData *p_skin_controller, const Collada::MorphControllerData *p_morph_data, Vector<Ref<ImporterMesh>> p_morph_meshes, bool p_use_compression, bool p_use_mesh_material) {
	bool local_xform_mirror = p_local_xform.basis.determinant() < 0;

	if (p_morph_data) {
		//add morph target
		ERR_FAIL_COND_V(!p_morph_data->targets.has("MORPH_TARGET"), ERR_INVALID_DATA);
		String mt = p_morph_data->targets["MORPH_TARGET"];
		ERR_FAIL_COND_V(!p_morph_data->sources.has(mt), ERR_INVALID_DATA);
		int morph_targets = p_morph_data->sources[mt].sarray.size();
		for (int i = 0; i < morph_targets; i++) {
			String target = p_morph_data->sources[mt].sarray[i];
			ERR_FAIL_COND_V(!collada.state.mesh_data_map.has(target), ERR_INVALID_DATA);
			String name = collada.state.mesh_data_map[target].name;

			p_mesh->add_blend_shape(name);
		}
		if (p_morph_data->mode == "RELATIVE") {
			p_mesh->set_blend_shape_mode(Mesh::BLEND_SHAPE_MODE_RELATIVE);
		} else if (p_morph_data->mode == "NORMALIZED") {
			p_mesh->set_blend_shape_mode(Mesh::BLEND_SHAPE_MODE_NORMALIZED);
		}
	}

	int surface = 0;
	for (int p_i = 0; p_i < meshdata.primitives.size(); p_i++) {
		const Collada::MeshData::Primitives &p = meshdata.primitives[p_i];

		/* VERTEX SOURCE */
		ERR_FAIL_COND_V(!p.sources.has("VERTEX"), ERR_INVALID_DATA);

		String vertex_src_id = p.sources["VERTEX"].source;
		int vertex_ofs = p.sources["VERTEX"].offset;

		ERR_FAIL_COND_V(!meshdata.vertices.has(vertex_src_id), ERR_INVALID_DATA);

		ERR_FAIL_COND_V(!meshdata.vertices[vertex_src_id].sources.has("POSITION"), ERR_INVALID_DATA);
		String position_src_id = meshdata.vertices[vertex_src_id].sources["POSITION"];

		ERR_FAIL_COND_V(!meshdata.sources.has(position_src_id), ERR_INVALID_DATA);

		const Collada::MeshData::Source *vertex_src = &meshdata.sources[position_src_id];

		/* NORMAL SOURCE */

		const Collada::MeshData::Source *normal_src = nullptr;
		int normal_ofs = 0;

		{
			String normal_source_id = "";

			if (p.sources.has("NORMAL")) {
				normal_source_id = p.sources["NORMAL"].source;
				normal_ofs = p.sources["NORMAL"].offset;
			} else if (meshdata.vertices[vertex_src_id].sources.has("NORMAL")) {
				normal_source_id = meshdata.vertices[vertex_src_id].sources["NORMAL"];
				normal_ofs = vertex_ofs;
			}

			if (!normal_source_id.is_empty()) {
				ERR_FAIL_COND_V(!meshdata.sources.has(normal_source_id), ERR_INVALID_DATA);
				normal_src = &meshdata.sources[normal_source_id];
			}
		}

		const Collada::MeshData::Source *binormal_src = nullptr;
		int binormal_ofs = 0;

		{
			String binormal_source_id = "";

			if (p.sources.has("TEXBINORMAL")) {
				binormal_source_id = p.sources["TEXBINORMAL"].source;
				binormal_ofs = p.sources["TEXBINORMAL"].offset;
			} else if (meshdata.vertices[vertex_src_id].sources.has("TEXBINORMAL")) {
				binormal_source_id = meshdata.vertices[vertex_src_id].sources["TEXBINORMAL"];
				binormal_ofs = vertex_ofs;
			}

			if (!binormal_source_id.is_empty()) {
				ERR_FAIL_COND_V(!meshdata.sources.has(binormal_source_id), ERR_INVALID_DATA);
				binormal_src = &meshdata.sources[binormal_source_id];
			}
		}

		const Collada::MeshData::Source *tangent_src = nullptr;
		int tangent_ofs = 0;

		{
			String tangent_source_id = "";

			if (p.sources.has("TEXTANGENT")) {
				tangent_source_id = p.sources["TEXTANGENT"].source;
				tangent_ofs = p.sources["TEXTANGENT"].offset;
			} else if (meshdata.vertices[vertex_src_id].sources.has("TEXTANGENT")) {
				tangent_source_id = meshdata.vertices[vertex_src_id].sources["TEXTANGENT"];
				tangent_ofs = vertex_ofs;
			}

			if (!tangent_source_id.is_empty()) {
				ERR_FAIL_COND_V(!meshdata.sources.has(tangent_source_id), ERR_INVALID_DATA);
				tangent_src = &meshdata.sources[tangent_source_id];
			}
		}

		const Collada::MeshData::Source *uv_src = nullptr;
		int uv_ofs = 0;

		{
			String uv_source_id = "";

			if (p.sources.has("TEXCOORD0")) {
				uv_source_id = p.sources["TEXCOORD0"].source;
				uv_ofs = p.sources["TEXCOORD0"].offset;
			} else if (meshdata.vertices[vertex_src_id].sources.has("TEXCOORD0")) {
				uv_source_id = meshdata.vertices[vertex_src_id].sources["TEXCOORD0"];
				uv_ofs = vertex_ofs;
			}

			if (!uv_source_id.is_empty()) {
				ERR_FAIL_COND_V(!meshdata.sources.has(uv_source_id), ERR_INVALID_DATA);
				uv_src = &meshdata.sources[uv_source_id];
			}
		}

		const Collada::MeshData::Source *uv2_src = nullptr;
		int uv2_ofs = 0;

		{
			String uv2_source_id = "";

			if (p.sources.has("TEXCOORD1")) {
				uv2_source_id = p.sources["TEXCOORD1"].source;
				uv2_ofs = p.sources["TEXCOORD1"].offset;
			} else if (meshdata.vertices[vertex_src_id].sources.has("TEXCOORD1")) {
				uv2_source_id = meshdata.vertices[vertex_src_id].sources["TEXCOORD1"];
				uv2_ofs = vertex_ofs;
			}

			if (!uv2_source_id.is_empty()) {
				ERR_FAIL_COND_V(!meshdata.sources.has(uv2_source_id), ERR_INVALID_DATA);
				uv2_src = &meshdata.sources[uv2_source_id];
			}
		}

		const Collada::MeshData::Source *color_src = nullptr;
		int color_ofs = 0;

		{
			String color_source_id = "";

			if (p.sources.has("COLOR")) {
				color_source_id = p.sources["COLOR"].source;
				color_ofs = p.sources["COLOR"].offset;
			} else if (meshdata.vertices[vertex_src_id].sources.has("COLOR")) {
				color_source_id = meshdata.vertices[vertex_src_id].sources["COLOR"];
				color_ofs = vertex_ofs;
			}

			if (!color_source_id.is_empty()) {
				ERR_FAIL_COND_V(!meshdata.sources.has(color_source_id), ERR_INVALID_DATA);
				color_src = &meshdata.sources[color_source_id];
			}
		}

		//find largest source..

		/************************/
		/* ADD WEIGHTS IF EXIST */
		/************************/

		HashMap<int, Vector<Collada::Vertex::Weight>> pre_weights;

		bool has_weights = false;

		if (p_skin_controller) {
			const Collada::SkinControllerData::Source *weight_src = nullptr;
			int weight_ofs = 0;

			if (p_skin_controller->weights.sources.has("WEIGHT")) {
				String weight_id = p_skin_controller->weights.sources["WEIGHT"].source;
				weight_ofs = p_skin_controller->weights.sources["WEIGHT"].offset;
				if (p_skin_controller->sources.has(weight_id)) {
					weight_src = &p_skin_controller->sources[weight_id];
				}
			}

			int joint_ofs = 0;

			if (p_skin_controller->weights.sources.has("JOINT")) {
				joint_ofs = p_skin_controller->weights.sources["JOINT"].offset;
			}

			//should be OK, given this was pre-checked.

			int index_ofs = 0;
			int wstride = p_skin_controller->weights.sources.size();
			for (int w_i = 0; w_i < p_skin_controller->weights.sets.size(); w_i++) {
				int amount = p_skin_controller->weights.sets[w_i];

				Vector<Collada::Vertex::Weight> weights;

				for (int a_i = 0; a_i < amount; a_i++) {
					Collada::Vertex::Weight w;

					int read_from = index_ofs + a_i * wstride;
					ERR_FAIL_INDEX_V(read_from + wstride - 1, p_skin_controller->weights.indices.size(), ERR_INVALID_DATA);
					int weight_index = p_skin_controller->weights.indices[read_from + weight_ofs];
					ERR_FAIL_INDEX_V(weight_index, weight_src->array.size(), ERR_INVALID_DATA);

					w.weight = weight_src->array[weight_index];

					int bone_index = p_skin_controller->weights.indices[read_from + joint_ofs];
					if (bone_index == -1) {
						continue; //ignore this weight (refers to bind shape)
					}
					ERR_FAIL_INDEX_V(bone_index, bone_remap.size(), ERR_INVALID_DATA);

					w.bone_idx = bone_remap[bone_index];

					weights.push_back(w);
				}

				/* FIX WEIGHTS */

				weights.sort();

				if (weights.size() > 4) {
					//cap to 4 and make weights add up 1
					weights.resize(4);
				}

				//make sure weights always add up to 1
				float total = 0;
				for (int i = 0; i < weights.size(); i++) {
					total += weights[i].weight;
				}
				if (total) {
					for (int i = 0; i < weights.size(); i++) {
						weights.write[i].weight /= total;
					}
				}

				if (weights.size() == 0 || total == 0) { //if nothing, add a weight to bone 0
					//no weights assigned
					Collada::Vertex::Weight w;
					w.bone_idx = 0;
					w.weight = 1.0;
					weights.clear();
					weights.push_back(w);
				}

				pre_weights[w_i] = weights;

				index_ofs += wstride * amount;
			}

			//vertices need to be localized
			has_weights = true;
		}

		RBSet<Collada::Vertex> vertex_set; //vertex set will be the vertices
		List<int> indices_list; //indices will be the indices

		/**************************/
		/* CREATE PRIMITIVE ARRAY */
		/**************************/

		// The way collada uses indices is more optimal, and friendlier with 3D modeling software,
		// because it can index everything, not only vertices (similar to how the WII works).
		// This is, however, more incompatible with standard video cards, so arrays must be converted.
		// Must convert to GL/DX format.

		int _prim_ofs = 0;
		int vertidx = 0;
		for (int p_j = 0; p_j < p.count; p_j++) {
			int amount;
			if (p.polygons.size()) {
				ERR_FAIL_INDEX_V(p_j, p.polygons.size(), ERR_INVALID_DATA);
				amount = p.polygons[p_j];
			} else {
				amount = 3; //triangles;
			}

			//COLLADA_PRINT("amount: "+itos(amount));

			int prev2[2] = { 0, 0 };

			for (int j = 0; j < amount; j++) {
				int src = _prim_ofs;
				//_prim_ofs+=p.sources.size()

				ERR_FAIL_INDEX_V(src, p.indices.size(), ERR_INVALID_DATA);

				Collada::Vertex vertex;
				if (!p_optimize) {
					vertex.uid = vertidx++;
				}

				int vertex_index = p.indices[src + vertex_ofs]; //used for index field (later used by controllers)
				int vertex_pos = (vertex_src->stride ? vertex_src->stride : 3) * vertex_index;
				ERR_FAIL_INDEX_V(vertex_pos + 0, vertex_src->array.size(), ERR_INVALID_DATA);
				ERR_FAIL_INDEX_V(vertex_pos + 2, vertex_src->array.size(), ERR_INVALID_DATA);
				vertex.vertex = Vector3(vertex_src->array[vertex_pos + 0], vertex_src->array[vertex_pos + 1], vertex_src->array[vertex_pos + 2]);

				if (pre_weights.has(vertex_index)) {
					vertex.weights = pre_weights[vertex_index];
				}

				if (normal_src) {
					int normal_pos = (normal_src->stride ? normal_src->stride : 3) * p.indices[src + normal_ofs];
					ERR_FAIL_INDEX_V(normal_pos + 0, normal_src->array.size(), ERR_INVALID_DATA);
					ERR_FAIL_INDEX_V(normal_pos + 2, normal_src->array.size(), ERR_INVALID_DATA);
					vertex.normal = Vector3(normal_src->array[normal_pos + 0], normal_src->array[normal_pos + 1], normal_src->array[normal_pos + 2]);

					if (tangent_src && binormal_src) {
						int binormal_pos = (binormal_src->stride ? binormal_src->stride : 3) * p.indices[src + binormal_ofs];
						ERR_FAIL_INDEX_V(binormal_pos + 0, binormal_src->array.size(), ERR_INVALID_DATA);
						ERR_FAIL_INDEX_V(binormal_pos + 2, binormal_src->array.size(), ERR_INVALID_DATA);
						Vector3 binormal = Vector3(binormal_src->array[binormal_pos + 0], binormal_src->array[binormal_pos + 1], binormal_src->array[binormal_pos + 2]);

						int tangent_pos = (tangent_src->stride ? tangent_src->stride : 3) * p.indices[src + tangent_ofs];
						ERR_FAIL_INDEX_V(tangent_pos + 0, tangent_src->array.size(), ERR_INVALID_DATA);
						ERR_FAIL_INDEX_V(tangent_pos + 2, tangent_src->array.size(), ERR_INVALID_DATA);
						Vector3 tangent = Vector3(tangent_src->array[tangent_pos + 0], tangent_src->array[tangent_pos + 1], tangent_src->array[tangent_pos + 2]);

						vertex.tangent.normal = tangent;
						vertex.tangent.d = vertex.normal.cross(tangent).dot(binormal) > 0 ? 1 : -1;
					}
				}

				if (uv_src) {
					int uv_pos = (uv_src->stride ? uv_src->stride : 2) * p.indices[src + uv_ofs];
					ERR_FAIL_INDEX_V(uv_pos + 0, uv_src->array.size(), ERR_INVALID_DATA);
					ERR_FAIL_INDEX_V(uv_pos + 1, uv_src->array.size(), ERR_INVALID_DATA);
					vertex.uv = Vector3(uv_src->array[uv_pos + 0], 1.0 - uv_src->array[uv_pos + 1], 0);
				}

				if (uv2_src) {
					int uv2_pos = (uv2_src->stride ? uv2_src->stride : 2) * p.indices[src + uv2_ofs];
					ERR_FAIL_INDEX_V(uv2_pos + 0, uv2_src->array.size(), ERR_INVALID_DATA);
					ERR_FAIL_INDEX_V(uv2_pos + 1, uv2_src->array.size(), ERR_INVALID_DATA);
					vertex.uv2 = Vector3(uv2_src->array[uv2_pos + 0], 1.0 - uv2_src->array[uv2_pos + 1], 0);
				}

				if (color_src) {
					int color_pos = (color_src->stride ? color_src->stride : 3) * p.indices[src + color_ofs]; // colors are RGB in collada..
					ERR_FAIL_INDEX_V(color_pos + 0, color_src->array.size(), ERR_INVALID_DATA);
					ERR_FAIL_INDEX_V(color_pos + ((color_src->stride > 3) ? 3 : 2), color_src->array.size(), ERR_INVALID_DATA);
					vertex.color = Color(color_src->array[color_pos + 0], color_src->array[color_pos + 1], color_src->array[color_pos + 2], (color_src->stride > 3) ? color_src->array[color_pos + 3] : 1.0);
				}

#ifndef NO_UP_AXIS_SWAP
				if (collada.state.up_axis == Vector3::AXIS_Z) {
					Vector3 bn = vertex.normal.cross(vertex.tangent.normal) * vertex.tangent.d;

					SWAP(vertex.vertex.z, vertex.vertex.y);
					vertex.vertex.z = -vertex.vertex.z;
					SWAP(vertex.normal.z, vertex.normal.y);
					vertex.normal.z = -vertex.normal.z;
					SWAP(vertex.tangent.normal.z, vertex.tangent.normal.y);
					vertex.tangent.normal.z = -vertex.tangent.normal.z;
					SWAP(bn.z, bn.y);
					bn.z = -bn.z;

					vertex.tangent.d = vertex.normal.cross(vertex.tangent.normal).dot(bn) > 0 ? 1 : -1;
				}

#endif

				vertex.fix_unit_scale(collada);
				int index = 0;
				//COLLADA_PRINT("vertex: "+vertex.vertex);

				if (vertex_set.has(vertex)) {
					index = vertex_set.find(vertex)->get().idx;
				} else {
					index = vertex_set.size();
					vertex.idx = index;
					vertex_set.insert(vertex);
				}

				//build triangles if needed
				if (j == 0) {
					prev2[0] = index;
				}

				if (j >= 2) {
					//insert indices in reverse order (collada uses CCW as frontface)
					if (local_xform_mirror) {
						indices_list.push_back(prev2[0]);
						indices_list.push_back(prev2[1]);
						indices_list.push_back(index);

					} else {
						indices_list.push_back(prev2[0]);
						indices_list.push_back(index);
						indices_list.push_back(prev2[1]);
					}
				}

				prev2[1] = index;
				_prim_ofs += p.vertex_size;
			}
		}

		Vector<Collada::Vertex> vertex_array; //there we go, vertex array

		vertex_array.resize(vertex_set.size());
		for (const Collada::Vertex &F : vertex_set) {
			vertex_array.write[F.idx] = F;
		}

		if (has_weights) {
			//if skeleton, localize
			Transform3D local_xform = p_local_xform;
			for (int i = 0; i < vertex_array.size(); i++) {
				vertex_array.write[i].vertex = local_xform.xform(vertex_array[i].vertex);
				vertex_array.write[i].normal = local_xform.basis.xform(vertex_array[i].normal).normalized();
				vertex_array.write[i].tangent.normal = local_xform.basis.xform(vertex_array[i].tangent.normal).normalized();
				if (local_xform_mirror) {
					//i shouldn't do this? wtf?
					//vertex_array[i].normal*=-1.0;
					//vertex_array[i].tangent.normal*=-1.0;
				}
			}
		}

		/*****************/
		/* MAKE SURFACES  */
		/*****************/

		{
			Ref<StandardMaterial3D> material;

			{
				if (p_material_map.has(p.material)) {
					String target = p_material_map[p.material].target;

					if (!material_cache.has(target)) {
						Error err = _create_material(target);
						if (!err) {
							material = material_cache[target];
						}
					} else {
						material = material_cache[target];
					}

				} else if (!p.material.is_empty()) {
					WARN_PRINT("Collada: Unreferenced material in geometry instance: " + p.material);
				}
			}

			Ref<SurfaceTool> surftool;
			surftool.instantiate();
			surftool->begin(Mesh::PRIMITIVE_TRIANGLES);

			for (int k = 0; k < vertex_array.size(); k++) {
				if (normal_src) {
					surftool->set_normal(vertex_array[k].normal);
					if (binormal_src && tangent_src) {
						surftool->set_tangent(vertex_array[k].tangent);
					}
				}
				if (uv_src) {
					surftool->set_uv(Vector2(vertex_array[k].uv.x, vertex_array[k].uv.y));
				}
				if (uv2_src) {
					surftool->set_uv2(Vector2(vertex_array[k].uv2.x, vertex_array[k].uv2.y));
				}
				if (color_src) {
					surftool->set_color(vertex_array[k].color);
				}

				if (has_weights) {
					Vector<float> weights;
					Vector<int> bones;
					weights.resize(RS::ARRAY_WEIGHTS_SIZE);
					bones.resize(RS::ARRAY_WEIGHTS_SIZE);
					//float sum=0.0;
					for (int l = 0; l < RS::ARRAY_WEIGHTS_SIZE; l++) {
						if (l < vertex_array[k].weights.size()) {
							weights.write[l] = vertex_array[k].weights[l].weight;
							bones.write[l] = vertex_array[k].weights[l].bone_idx;
							//sum += vertex_array[k].weights[l].weight;
						} else {
							weights.write[l] = 0;
							bones.write[l] = 0;
						}
					}

					surftool->set_bones(bones);
					surftool->set_weights(weights);
				}

				surftool->add_vertex(vertex_array[k].vertex);
			}

			for (int &E : indices_list) {
				surftool->add_index(E);
			}

			if (!normal_src) {
				//should always be normals
				surftool->generate_normals();
			}

			if ((!binormal_src || !tangent_src) && normal_src && uv_src && force_make_tangents) {
				surftool->generate_tangents();
			}

			////////////////////////////
			// FINALLY CREATE SUFRACE //
			////////////////////////////

			Array d = surftool->commit_to_arrays();
			d.resize(RS::ARRAY_MAX);

			Array mr;

			////////////////////////////
			// THEN THE MORPH TARGETS //
			////////////////////////////

			for (int mi = 0; mi < p_morph_meshes.size(); mi++) {
				Array a = p_morph_meshes[mi]->get_surface_arrays(surface);
				//add valid weight and bone arrays if they exist, TODO check if they are unique to shape (generally not)

				// Enforce blend shape mask array format
				for (int mj = 0; mj < Mesh::ARRAY_MAX; mj++) {
					if (!(Mesh::ARRAY_FORMAT_BLEND_SHAPE_MASK & (1 << mj))) {
						a[mj] = Variant();
					}
				}
				mr.push_back(a);
			}

			String surface_name;
			Ref<Material> mat;
			if (material.is_valid()) {
				if (p_use_mesh_material) {
					mat = material;
				}
				surface_name = material->get_name();
			}
			p_mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES, d, mr, Dictionary(), mat, surface_name);
		}

		/*****************/
		/* FIND MATERIAL */
		/*****************/

		surface++;
	}

	return OK;
}

Error ColladaImport::_create_resources(Collada::Node *p_node, bool p_use_compression) {
	if (p_node->type == Collada::Node::TYPE_GEOMETRY && node_map.has(p_node->id)) {
		Node3D *node = node_map[p_node->id].node;
		Collada::NodeGeometry *ng = static_cast<Collada::NodeGeometry *>(p_node);

		if (Object::cast_to<Path3D>(node)) {
			Path3D *path = Object::cast_to<Path3D>(node);

			if (curve_cache.has(ng->source)) {
				path->set_curve(curve_cache[ng->source]);
			} else {
				Ref<Curve3D> c = memnew(Curve3D);

				const Collada::CurveData &cd = collada.state.curve_data_map[ng->source];

				ERR_FAIL_COND_V(!cd.control_vertices.has("POSITION"), ERR_INVALID_DATA);
				ERR_FAIL_COND_V(!cd.control_vertices.has("IN_TANGENT"), ERR_INVALID_DATA);
				ERR_FAIL_COND_V(!cd.control_vertices.has("OUT_TANGENT"), ERR_INVALID_DATA);
				ERR_FAIL_COND_V(!cd.control_vertices.has("INTERPOLATION"), ERR_INVALID_DATA);

				ERR_FAIL_COND_V(!cd.sources.has(cd.control_vertices["POSITION"]), ERR_INVALID_DATA);
				const Collada::CurveData::Source &vertices = cd.sources[cd.control_vertices["POSITION"]];
				ERR_FAIL_COND_V(vertices.stride != 3, ERR_INVALID_DATA);

				ERR_FAIL_COND_V(!cd.sources.has(cd.control_vertices["IN_TANGENT"]), ERR_INVALID_DATA);
				const Collada::CurveData::Source &in_tangents = cd.sources[cd.control_vertices["IN_TANGENT"]];
				ERR_FAIL_COND_V(in_tangents.stride != 3, ERR_INVALID_DATA);

				ERR_FAIL_COND_V(!cd.sources.has(cd.control_vertices["OUT_TANGENT"]), ERR_INVALID_DATA);
				const Collada::CurveData::Source &out_tangents = cd.sources[cd.control_vertices["OUT_TANGENT"]];
				ERR_FAIL_COND_V(out_tangents.stride != 3, ERR_INVALID_DATA);

				ERR_FAIL_COND_V(!cd.sources.has(cd.control_vertices["INTERPOLATION"]), ERR_INVALID_DATA);
				const Collada::CurveData::Source &interps = cd.sources[cd.control_vertices["INTERPOLATION"]];
				ERR_FAIL_COND_V(interps.stride != 1, ERR_INVALID_DATA);

				const Collada::CurveData::Source *tilts = nullptr;
				if (cd.control_vertices.has("TILT") && cd.sources.has(cd.control_vertices["TILT"])) {
					tilts = &cd.sources[cd.control_vertices["TILT"]];
				}

				int pc = vertices.array.size() / 3;
				for (int i = 0; i < pc; i++) {
					Vector3 pos(vertices.array[i * 3 + 0], vertices.array[i * 3 + 1], vertices.array[i * 3 + 2]);
					Vector3 in(in_tangents.array[i * 3 + 0], in_tangents.array[i * 3 + 1], in_tangents.array[i * 3 + 2]);
					Vector3 out(out_tangents.array[i * 3 + 0], out_tangents.array[i * 3 + 1], out_tangents.array[i * 3 + 2]);

#ifndef NO_UP_AXIS_SWAP
					if (collada.state.up_axis == Vector3::AXIS_Z) {
						SWAP(pos.y, pos.z);
						pos.z = -pos.z;
						SWAP(in.y, in.z);
						in.z = -in.z;
						SWAP(out.y, out.z);
						out.z = -out.z;
					}
#endif
					pos *= collada.state.unit_scale;
					in *= collada.state.unit_scale;
					out *= collada.state.unit_scale;

					c->add_point(pos, in - pos, out - pos);
					if (tilts) {
						c->set_point_tilt(i, tilts->array[i]);
					}
				}
				if (cd.closed && pc > 1) {
					Vector3 pos = c->get_point_position(0);
					Vector3 in = c->get_point_in(0);
					Vector3 out = c->get_point_out(0);
					c->add_point(pos, in, out, -1);
				}

				curve_cache[ng->source] = c;
				path->set_curve(c);
			}
		}

		if (Object::cast_to<ImporterMeshInstance3D>(node)) {
			Collada::NodeGeometry *ng2 = static_cast<Collada::NodeGeometry *>(p_node);

			ImporterMeshInstance3D *mi = Object::cast_to<ImporterMeshInstance3D>(node);

			ERR_FAIL_COND_V(!mi, ERR_BUG);

			Collada::SkinControllerData *skin = nullptr;
			Collada::MorphControllerData *morph = nullptr;
			String meshid;
			Transform3D apply_xform;
			Vector<int> bone_remap;
			Vector<Ref<ImporterMesh>> morphs;

			if (ng2->controller) {
				String ngsource = ng2->source;

				if (collada.state.skin_controller_data_map.has(ngsource)) {
					ERR_FAIL_COND_V(!collada.state.skin_controller_data_map.has(ngsource), ERR_INVALID_DATA);
					skin = &collada.state.skin_controller_data_map[ngsource];

					Vector<String> skeletons = ng2->skeletons;

					ERR_FAIL_COND_V(skeletons.is_empty(), ERR_INVALID_DATA);

					String skname = skeletons[0];
					ERR_FAIL_COND_V(!node_map.has(skname), ERR_INVALID_DATA);
					NodeMap nmsk = node_map[skname];
					Skeleton3D *sk = Object::cast_to<Skeleton3D>(nmsk.node);
					ERR_FAIL_COND_V(!sk, ERR_INVALID_DATA);
					ERR_FAIL_COND_V(!skeleton_bone_map.has(sk), ERR_INVALID_DATA);
					HashMap<String, int> &bone_remap_map = skeleton_bone_map[sk];

					meshid = skin->base;

					if (collada.state.morph_controller_data_map.has(meshid)) {
						//it's a morph!!
						morph = &collada.state.morph_controller_data_map[meshid];
						ngsource = meshid;
						meshid = morph->mesh;
					} else {
						ngsource = "";
					}

					if (apply_mesh_xform_to_vertices) {
						apply_xform = collada.fix_transform(p_node->default_transform);
						node->set_transform(Transform3D());
					} else {
						apply_xform = Transform3D();
					}

					ERR_FAIL_COND_V(!skin->weights.sources.has("JOINT"), ERR_INVALID_DATA);

					String joint_id = skin->weights.sources["JOINT"].source;
					ERR_FAIL_COND_V(!skin->sources.has(joint_id), ERR_INVALID_DATA);

					Collada::SkinControllerData::Source *joint_src = &skin->sources[joint_id];

					bone_remap.resize(joint_src->sarray.size());

					for (int i = 0; i < bone_remap.size(); i++) {
						String str = joint_src->sarray[i];
						ERR_FAIL_COND_V(!bone_remap_map.has(str), ERR_INVALID_DATA);
						bone_remap.write[i] = bone_remap_map[str];
					}
				}

				if (collada.state.morph_controller_data_map.has(ngsource)) {
					//it's a morph!!
					morph = &collada.state.morph_controller_data_map[ngsource];
					meshid = morph->mesh;

					if (morph->targets.has("MORPH_TARGET")) {
						String target = morph->targets["MORPH_TARGET"];
						bool valid = false;
						if (morph->sources.has(target)) {
							valid = true;
							Vector<String> names = morph->sources[target].sarray;
							for (int i = 0; i < names.size(); i++) {
								String meshid2 = names[i];
								if (collada.state.mesh_data_map.has(meshid2)) {
									Ref<ImporterMesh> mesh = Ref<ImporterMesh>(memnew(ImporterMesh));
									const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid2];
									mesh->set_name(meshdata.name);
									Error err = _create_mesh_surfaces(false, mesh, ng2->material_map, meshdata, apply_xform, bone_remap, skin, nullptr, Vector<Ref<ImporterMesh>>(), false);
									ERR_FAIL_COND_V(err, err);

									morphs.push_back(mesh);
								} else {
									valid = false;
								}
							}
						}

						if (!valid) {
							morphs.clear();
						}
						ngsource = "";
					}
				}

				ERR_FAIL_COND_V_MSG(!ngsource.is_empty(), ERR_INVALID_DATA, "Controller instance source '" + ngsource + "' is neither skin or morph!");

			} else {
				meshid = ng2->source;
			}

			Ref<ImporterMesh> mesh;
			if (mesh_cache.has(meshid)) {
				mesh = mesh_cache[meshid];
			} else {
				if (collada.state.mesh_data_map.has(meshid)) {
					//bleh, must ignore invalid

					ERR_FAIL_COND_V(!collada.state.mesh_data_map.has(meshid), ERR_INVALID_DATA);
					mesh = Ref<ImporterMesh>(memnew(ImporterMesh));
					const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid];
					String name = meshdata.name;
					if (name.is_empty()) {
						name = "Mesh";
					}
					int counter = 2;
					while (mesh_unique_names.has(name)) {
						name = meshdata.name;
						if (name.is_empty()) {
							name = "Mesh";
						}
						name += itos(counter++);
					}

					mesh_unique_names.insert(name);

					mesh->set_name(name);
					Error err = _create_mesh_surfaces(morphs.size() == 0, mesh, ng2->material_map, meshdata, apply_xform, bone_remap, skin, morph, morphs, p_use_compression, use_mesh_builtin_materials);
					ERR_FAIL_COND_V_MSG(err, err, "Cannot create mesh surface.");

					mesh_cache[meshid] = mesh;
				} else {
					WARN_PRINT("Collada: Will not import geometry: " + meshid);
				}
			}

			if (!mesh.is_null()) {
				mi->set_mesh(mesh);
				if (!use_mesh_builtin_materials) {
					const Collada::MeshData &meshdata = collada.state.mesh_data_map[meshid];

					for (int i = 0; i < meshdata.primitives.size(); i++) {
						String matname = meshdata.primitives[i].material;

						if (ng2->material_map.has(matname)) {
							String target = ng2->material_map[matname].target;

							Ref<Material> material;
							if (!material_cache.has(target)) {
								Error err = _create_material(target);
								if (!err) {
									material = material_cache[target];
								}
							} else {
								material = material_cache[target];
							}

							mi->set_surface_material(i, material);
						} else if (!matname.is_empty()) {
							WARN_PRINT("Collada: Unreferenced material in geometry instance: " + matname);
						}
					}
				}
			}
		}
	}

	for (int i = 0; i < p_node->children.size(); i++) {
		Error err = _create_resources(p_node->children[i], p_use_compression);
		if (err) {
			return err;
		}
	}
	return OK;
}

Error ColladaImport::load(const String &p_path, int p_flags, bool p_force_make_tangents, bool p_use_compression) {
	Error err = collada.load(p_path, p_flags);
	ERR_FAIL_COND_V_MSG(err, err, "Cannot load file '" + p_path + "'.");

	force_make_tangents = p_force_make_tangents;
	ERR_FAIL_COND_V(!collada.state.visual_scene_map.has(collada.state.root_visual_scene), ERR_INVALID_DATA);
	Collada::VisualScene &vs = collada.state.visual_scene_map[collada.state.root_visual_scene];

	scene = memnew(Node3D); // root

	//determine what's going on with the lights
	for (int i = 0; i < vs.root_nodes.size(); i++) {
		_pre_process_lights(vs.root_nodes[i]);
	}
	//import scene

	for (int i = 0; i < vs.root_nodes.size(); i++) {
		Error err2 = _create_scene_skeletons(vs.root_nodes[i]);
		if (err2 != OK) {
			memdelete(scene);
			ERR_FAIL_COND_V(err2, err2);
		}
	}

	for (int i = 0; i < vs.root_nodes.size(); i++) {
		Error err2 = _create_scene(vs.root_nodes[i], scene);
		if (err2 != OK) {
			memdelete(scene);
			ERR_FAIL_COND_V(err2, err2);
		}

		Error err3 = _create_resources(vs.root_nodes[i], p_use_compression);
		if (err3 != OK) {
			memdelete(scene);
			ERR_FAIL_COND_V(err3, err3);
		}
	}

	//optatively, set unit scale in the root
	scene->set_transform(collada.get_root_transform());

	return OK;
}

void ColladaImport::_fix_param_animation_tracks() {
	for (KeyValue<String, Collada::Node *> &E : collada.state.scene_map) {
		Collada::Node *n = E.value;
		switch (n->type) {
			case Collada::Node::TYPE_NODE: {
				// ? do nothing
			} break;
			case Collada::Node::TYPE_JOINT: {
			} break;
			case Collada::Node::TYPE_SKELETON: {
			} break;
			case Collada::Node::TYPE_LIGHT: {
			} break;
			case Collada::Node::TYPE_CAMERA: {
			} break;
			case Collada::Node::TYPE_GEOMETRY: {
				Collada::NodeGeometry *ng = static_cast<Collada::NodeGeometry *>(n);
				// test source(s)
				String source = ng->source;

				while (!source.is_empty()) {
					if (collada.state.skin_controller_data_map.has(source)) {
						const Collada::SkinControllerData &skin = collada.state.skin_controller_data_map[source];

						//nothing to animate here i think

						source = skin.base;
					} else if (collada.state.morph_controller_data_map.has(source)) {
						const Collada::MorphControllerData &morph = collada.state.morph_controller_data_map[source];

						if (morph.targets.has("MORPH_WEIGHT") && morph.targets.has("MORPH_TARGET")) {
							String weights = morph.targets["MORPH_WEIGHT"];
							String targets = morph.targets["MORPH_TARGET"];
							//fails here

							if (morph.sources.has(targets) && morph.sources.has(weights)) {
								const Collada::MorphControllerData::Source &weight_src = morph.sources[weights];
								const Collada::MorphControllerData::Source &target_src = morph.sources[targets];

								ERR_FAIL_COND(weight_src.array.size() != target_src.sarray.size());

								for (int i = 0; i < weight_src.array.size(); i++) {
									String track_name = weights + "(" + itos(i) + ")";
									String mesh_name = target_src.sarray[i];
									if (collada.state.mesh_name_map.has(mesh_name) && collada.state.referenced_tracks.has(track_name)) {
										const Vector<int> &rt = collada.state.referenced_tracks[track_name];

										for (int rti = 0; rti < rt.size(); rti++) {
											Collada::AnimationTrack *at = &collada.state.animation_tracks.write[rt[rti]];

											at->target = E.key;
											at->param = "morph/" + collada.state.mesh_name_map[mesh_name];
											at->property = true;
											//at->param
										}
									}
								}
							}
						}
						source = morph.mesh;
					} else {
						source = ""; // for now nothing else supported
					}
				}

			} break;
		}
	}
}

void ColladaImport::create_animations(bool p_import_value_tracks) {
	_fix_param_animation_tracks();
	for (int i = 0; i < collada.state.animation_clips.size(); i++) {
		for (int j = 0; j < collada.state.animation_clips[i].tracks.size(); j++) {
			tracks_in_clips.insert(collada.state.animation_clips[i].tracks[j]);
		}
	}

	for (int i = 0; i < collada.state.animation_tracks.size(); i++) {
		const Collada::AnimationTrack &at = collada.state.animation_tracks[i];

		String node;

		if (!node_map.has(at.target)) {
			if (node_name_map.has(at.target)) {
				node = node_name_map[at.target];
			} else {
				WARN_PRINT("Collada: Couldn't find node: " + at.target);
				continue;
			}
		} else {
			node = at.target;
		}

		if (at.property) {
			valid_animated_properties.push_back(i);

		} else {
			node_map[node].anim_tracks.push_back(i);
			valid_animated_nodes.insert(node);
		}
	}

	create_animation(-1, p_import_value_tracks);
	for (int i = 0; i < collada.state.animation_clips.size(); i++) {
		create_animation(i, p_import_value_tracks);
	}
}

void ColladaImport::create_animation(int p_clip, bool p_import_value_tracks) {
	Ref<Animation> animation = Ref<Animation>(memnew(Animation));

	if (p_clip == -1) {
		animation->set_name("default");
	} else {
		animation->set_name(collada.state.animation_clips[p_clip].name);
	}

	for (const KeyValue<String, NodeMap> &E : node_map) {
		if (E.value.bone < 0) {
			continue;
		}
		bones_with_animation[E.key] = false;
	}
	//store and validate tracks

	if (p_clip == -1) {
		//main anim
	}

	HashSet<int> track_filter;

	if (p_clip == -1) {
		for (int i = 0; i < collada.state.animation_clips.size(); i++) {
			int tc = collada.state.animation_clips[i].tracks.size();
			for (int j = 0; j < tc; j++) {
				String n = collada.state.animation_clips[i].tracks[j];
				if (collada.state.by_id_tracks.has(n)) {
					const Vector<int> &ti = collada.state.by_id_tracks[n];
					for (int k = 0; k < ti.size(); k++) {
						track_filter.insert(ti[k]);
					}
				}
			}
		}
	} else {
		int tc = collada.state.animation_clips[p_clip].tracks.size();
		for (int j = 0; j < tc; j++) {
			String n = collada.state.animation_clips[p_clip].tracks[j];
			if (collada.state.by_id_tracks.has(n)) {
				const Vector<int> &ti = collada.state.by_id_tracks[n];
				for (int k = 0; k < ti.size(); k++) {
					track_filter.insert(ti[k]);
				}
			}
		}
	}

	//animation->set_loop(true);
	//create animation tracks

	Vector<real_t> base_snapshots;

	float f = 0;
	float snapshot_interval = 1.0 / bake_fps; //should be customizable somewhere...

	float anim_length = collada.state.animation_length;
	if (p_clip >= 0 && collada.state.animation_clips[p_clip].end) {
		anim_length = collada.state.animation_clips[p_clip].end;
	}

	while (f < anim_length) {
		base_snapshots.push_back(f);

		f += snapshot_interval;

		if (f >= anim_length) {
			base_snapshots.push_back(anim_length);
		}
	}

	animation->set_length(anim_length);

	bool tracks_found = false;

	for (const String &E : valid_animated_nodes) {
		// take snapshots

		if (!collada.state.scene_map.has(E)) {
			continue;
		}

		NodeMap &nm = node_map[E];
		String path = scene->get_path_to(nm.node);

		if (nm.bone >= 0) {
			Skeleton3D *sk = static_cast<Skeleton3D *>(nm.node);
			String name = sk->get_bone_name(nm.bone);
			path = path + ":" + name;
		}

		bool found_anim = false;

		Collada::Node *cn = collada.state.scene_map[E];
		if (cn->ignore_anim) {
			continue;
		}

		bool has_position = false;
		bool has_rotation = false;
		bool has_scale = false;

		for (int i = 0; i < cn->xform_list.size(); i++) {
			switch (cn->xform_list[i].op) {
				case Collada::Node::XForm::OP_ROTATE: {
					has_rotation = true;
				} break;
				case Collada::Node::XForm::OP_SCALE: {
					has_scale = true;
				} break;
				case Collada::Node::XForm::OP_TRANSLATE: {
					has_position = true;
				} break;
				case Collada::Node::XForm::OP_MATRIX: {
					has_position = true;
					has_rotation = true;
					has_scale = true;
				} break;
				case Collada::Node::XForm::OP_VISIBILITY: {
				} break;
			}
		}

		int base_track = animation->get_track_count();
		int position_idx = -1;
		if (has_position) {
			position_idx = animation->get_track_count();
			animation->add_track(Animation::TYPE_POSITION_3D);
			animation->track_set_path(position_idx, path);
			animation->track_set_imported(position_idx, true); //helps merging later
		}

		int rotation_idx = -1;
		if (has_rotation) {
			rotation_idx = animation->get_track_count();
			animation->add_track(Animation::TYPE_ROTATION_3D);
			animation->track_set_path(rotation_idx, path);
			animation->track_set_imported(rotation_idx, true); //helps merging later
		}

		int scale_idx = -1;
		if (has_scale) {
			scale_idx = animation->get_track_count();
			animation->add_track(Animation::TYPE_SCALE_3D);
			animation->track_set_path(scale_idx, path);
			animation->track_set_imported(scale_idx, true); //helps merging later
		}

		Vector<real_t> snapshots = base_snapshots;

		if (nm.anim_tracks.size() == 1) {
			//use snapshot keys from anim track instead, because this was most likely exported baked
			const Collada::AnimationTrack &at = collada.state.animation_tracks[nm.anim_tracks.front()->get()];
			snapshots.clear();
			for (int i = 0; i < at.keys.size(); i++) {
				snapshots.push_back(at.keys[i].time);
			}
		}

		for (int i = 0; i < snapshots.size(); i++) {
			for (List<int>::Element *ET = nm.anim_tracks.front(); ET; ET = ET->next()) {
				//apply tracks

				if (p_clip == -1) {
					if (track_filter.has(ET->get())) {
						continue;
					}
				} else {
					if (!track_filter.has(ET->get())) {
						continue;
					}
				}

				found_anim = true;

				const Collada::AnimationTrack &at = collada.state.animation_tracks[ET->get()];

				int xform_idx = -1;
				for (int j = 0; j < cn->xform_list.size(); j++) {
					if (cn->xform_list[j].id == at.param) {
						xform_idx = j;
						break;
					}
				}

				if (xform_idx == -1) {
					WARN_PRINT("Collada: Couldn't find matching node " + at.target + " xform for track " + at.param + ".");
					continue;
				}

				Vector<float> data = at.get_value_at_time(snapshots[i]);
				ERR_CONTINUE(data.is_empty());

				Collada::Node::XForm &xf = cn->xform_list.write[xform_idx];

				if (at.component == "ANGLE") {
					ERR_CONTINUE(data.size() != 1);
					ERR_CONTINUE(xf.op != Collada::Node::XForm::OP_ROTATE);
					ERR_CONTINUE(xf.data.size() < 4);
					xf.data.write[3] = data[0];
				} else if (at.component == "X" || at.component == "Y" || at.component == "Z") {
					int cn2 = at.component[0] - 'X';
					ERR_CONTINUE(cn2 >= xf.data.size());
					ERR_CONTINUE(data.size() > 1);
					xf.data.write[cn2] = data[0];
				} else if (data.size() == xf.data.size()) {
					xf.data = data;
				} else {
					ERR_CONTINUE_MSG(data.size() != xf.data.size(), "Component " + at.component + " has datasize " + itos(data.size()) + ", xfdatasize " + itos(xf.data.size()) + ".");
				}
			}

			Transform3D xform = cn->compute_transform(collada);
			xform = collada.fix_transform(xform) * cn->post_transform;

			Vector3 s = xform.basis.get_scale();
			bool singular_matrix = Math::is_zero_approx(s.x) || Math::is_zero_approx(s.y) || Math::is_zero_approx(s.z);
			Quaternion q = singular_matrix ? Quaternion() : xform.basis.get_rotation_quaternion();
			Vector3 l = xform.origin;

			if (position_idx >= 0) {
				animation->position_track_insert_key(position_idx, snapshots[i], l);
			}
			if (rotation_idx >= 0) {
				animation->rotation_track_insert_key(rotation_idx, snapshots[i], q);
			}
			if (scale_idx >= 0) {
				animation->scale_track_insert_key(scale_idx, snapshots[i], s);
			}
		}

		if (nm.bone >= 0) {
			if (found_anim) {
				bones_with_animation[E] = true;
			}
		}

		if (found_anim) {
			tracks_found = true;
		} else {
			if (position_idx >= 0) {
				animation->remove_track(base_track);
			}
			if (rotation_idx >= 0) {
				animation->remove_track(base_track);
			}
			if (scale_idx >= 0) {
				animation->remove_track(base_track);
			}
		}
	}

	if (p_import_value_tracks) {
		for (int i = 0; i < valid_animated_properties.size(); i++) {
			int ti = valid_animated_properties[i];

			if (p_clip == -1) {
				if (track_filter.has(ti)) {
					continue;
				}
			} else {
				if (!track_filter.has(ti)) {
					continue;
				}
			}

			const Collada::AnimationTrack &at = collada.state.animation_tracks[ti];

			// take snapshots
			if (!collada.state.scene_map.has(at.target)) {
				continue;
			}

			NodeMap &nm = node_map[at.target];
			String path = scene->get_path_to(nm.node);

			animation->add_track(Animation::TYPE_BLEND_SHAPE);
			int track = animation->get_track_count() - 1;

			path = path + ":" + at.param;
			animation->track_set_path(track, path);
			animation->track_set_imported(track, true); //helps merging later

			for (int j = 0; j < at.keys.size(); j++) {
				float time = at.keys[j].time;
				Variant value;
				Vector<float> data = at.keys[j].data;
				if (data.size() == 1) {
					//push a float
					value = data[0];

				} else if (data.size() == 16) {
					//matrix
					WARN_PRINT("Collada: Value keys for matrices not supported.");
				} else {
					WARN_PRINT("Collada: Unexpected amount of value keys: " + itos(data.size()));
				}

				animation->blend_shape_track_insert_key(track, time, value);
			}

			tracks_found = true;
		}
	}

	if (tracks_found) {
		animations.push_back(animation);
	}
}

/*********************************************************************************/
/*************************************** SCENE ***********************************/
/*********************************************************************************/

uint32_t EditorSceneFormatImporterCollada::get_import_flags() const {
	return IMPORT_SCENE | IMPORT_ANIMATION;
}

void EditorSceneFormatImporterCollada::get_extensions(List<String> *r_extensions) const {
	r_extensions->push_back("dae");
}

Node *EditorSceneFormatImporterCollada::import_scene(const String &p_path, uint32_t p_flags, const HashMap<StringName, Variant> &p_options, List<String> *r_missing_deps, Error *r_err) {
	if (r_err) {
		*r_err = OK;
	}
	ColladaImport state;
	uint32_t flags = Collada::IMPORT_FLAG_SCENE;
	if (p_flags & IMPORT_ANIMATION) {
		flags |= Collada::IMPORT_FLAG_ANIMATION;
	}

	state.use_mesh_builtin_materials = true;
	state.bake_fps = (float)p_options["animation/fps"];

	Error err = state.load(p_path, flags, p_flags & EditorSceneFormatImporter::IMPORT_GENERATE_TANGENT_ARRAYS, false);

	if (r_err) {
		*r_err = err;
	}

	ERR_FAIL_COND_V_MSG(err != OK, nullptr, "Cannot load scene from file '" + p_path + "'.");

	if (state.missing_textures.size()) {
		/*
	for(int i=0;i<state.missing_textures.size();i++) {
		EditorNode::add_io_error("Texture Not Found: "+state.missing_textures[i]);
	}
	*/

		if (r_missing_deps) {
			for (int i = 0; i < state.missing_textures.size(); i++) {
				//EditorNode::add_io_error("Texture Not Found: "+state.missing_textures[i]);
				r_missing_deps->push_back(state.missing_textures[i]);
			}
		}
	}

	if (p_flags & IMPORT_ANIMATION) {
		state.create_animations(true);
		AnimationPlayer *ap = memnew(AnimationPlayer);
		for (int i = 0; i < state.animations.size(); i++) {
			String name;
			if (state.animations[i]->get_name().is_empty()) {
				name = "default";
			} else {
				name = state.animations[i]->get_name();
			}

			Ref<AnimationLibrary> library;
			if (!ap->has_animation_library("")) {
				library.instantiate();
				ap->add_animation_library("", library);
			} else {
				library = ap->get_animation_library("");
			}
			library->add_animation(name, state.animations[i]);
		}
		state.scene->add_child(ap, true);
		ap->set_owner(state.scene);
	}

	return state.scene;
}

EditorSceneFormatImporterCollada::EditorSceneFormatImporterCollada() {
}