summaryrefslogtreecommitdiff
path: root/modules/fbx/data/fbx_mesh_data.cpp
blob: 8f32c523f9db08493356b4664af325b5ffb7b912 (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
/*************************************************************************/
/*  fbx_mesh_data.cpp                                                    */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* 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 "fbx_mesh_data.h"

#include "core/templates/local_vector.h"
#include "scene/resources/mesh.h"
#include "scene/resources/surface_tool.h"

#include "thirdparty/misc/polypartition.h"

template <class T>
T collect_first(const Vector<VertexData<T>> *p_data, T p_fall_back) {
	if (p_data->is_empty()) {
		return p_fall_back;
	}

	return (*p_data)[0].data;
}

template <class T>
HashMap<int, T> collect_all(const Vector<VertexData<T>> *p_data, HashMap<int, T> p_fall_back) {
	if (p_data->is_empty()) {
		return p_fall_back;
	}

	HashMap<int, T> collection;
	for (int i = 0; i < p_data->size(); i += 1) {
		const VertexData<T> &vd = (*p_data)[i];
		collection[vd.polygon_index] = vd.data;
	}
	return collection;
}

template <class T>
T collect_average(const Vector<VertexData<T>> *p_data, T p_fall_back) {
	if (p_data->is_empty()) {
		return p_fall_back;
	}

	T combined = (*p_data)[0].data; // Make sure the data is always correctly initialized.
	print_verbose("size of data: " + itos(p_data->size()));
	for (int i = 1; i < p_data->size(); i += 1) {
		combined += (*p_data)[i].data;
	}
	combined = combined / real_t(p_data->size());

	return combined.normalized();
}

HashMap<int, Vector3> collect_normal(const Vector<VertexData<Vector3>> *p_data, HashMap<int, Vector3> p_fall_back) {
	if (p_data->is_empty()) {
		return p_fall_back;
	}

	HashMap<int, Vector3> collection;
	for (int i = 0; i < p_data->size(); i += 1) {
		const VertexData<Vector3> &vd = (*p_data)[i];
		collection[vd.polygon_index] = vd.data;
	}
	return collection;
}

HashMap<int, Vector2> collect_uv(const Vector<VertexData<Vector2>> *p_data, HashMap<int, Vector2> p_fall_back) {
	if (p_data->is_empty()) {
		return p_fall_back;
	}

	HashMap<int, Vector2> collection;
	for (int i = 0; i < p_data->size(); i += 1) {
		const VertexData<Vector2> &vd = (*p_data)[i];
		collection[vd.polygon_index] = vd.data;
	}
	return collection;
}

EditorSceneImporterMeshNode3D *FBXMeshData::create_fbx_mesh(const ImportState &state, const FBXDocParser::MeshGeometry *p_mesh_geometry, const FBXDocParser::Model *model, bool use_compression) {
	mesh_geometry = p_mesh_geometry;
	// todo: make this just use a uint64_t FBX ID this is a copy of our original materials unfortunately.
	const std::vector<const FBXDocParser::Material *> &material_lookup = model->GetMaterials();

	// TODO: perf hotspot on large files
	// this can be a very large copy
	std::vector<int> polygon_indices = mesh_geometry->get_polygon_indices();
	std::vector<Vector3> vertices = mesh_geometry->get_vertices();

	// Phase 1. Parse all FBX data.
	HashMap<int, Vector3> normals;
	HashMap<int, HashMap<int, Vector3>> normals_raw = extract_per_vertex_data(
			vertices.size(),
			mesh_geometry->get_edge_map(),
			polygon_indices,
			mesh_geometry->get_normals(),
			&collect_all,
			HashMap<int, Vector3>());

	HashMap<int, Vector2> uvs_0;
	HashMap<int, HashMap<int, Vector2>> uvs_0_raw = extract_per_vertex_data(
			vertices.size(),
			mesh_geometry->get_edge_map(),
			polygon_indices,
			mesh_geometry->get_uv_0(),
			&collect_all,
			HashMap<int, Vector2>());

	HashMap<int, Vector2> uvs_1;
	HashMap<int, HashMap<int, Vector2>> uvs_1_raw = extract_per_vertex_data(
			vertices.size(),
			mesh_geometry->get_edge_map(),
			polygon_indices,
			mesh_geometry->get_uv_1(),
			&collect_all,
			HashMap<int, Vector2>());

	HashMap<int, Color> colors;
	HashMap<int, HashMap<int, Color>> colors_raw = extract_per_vertex_data(
			vertices.size(),
			mesh_geometry->get_edge_map(),
			polygon_indices,
			mesh_geometry->get_colors(),
			&collect_all,
			HashMap<int, Color>());

	// TODO what about tangents?
	// TODO what about bi-nomials?
	// TODO there is other?

	HashMap<int, SurfaceId> polygon_surfaces = extract_per_polygon(
			vertices.size(),
			polygon_indices,
			mesh_geometry->get_material_allocation_id(),
			-1);

	HashMap<String, MorphVertexData> morphs;
	extract_morphs(mesh_geometry, morphs);

	// TODO please add skinning.
	//mesh_id = mesh_geometry->ID();

	sanitize_vertex_weights(state);

	// Re organize polygon vertices to to correctly take into account strange
	// UVs.
	reorganize_vertices(
			polygon_indices,
			vertices,
			normals,
			uvs_0,
			uvs_1,
			colors,
			morphs,
			normals_raw,
			colors_raw,
			uvs_0_raw,
			uvs_1_raw);

	const int color_count = colors.size();
	print_verbose("Vertex color count: " + itos(color_count));

	// Make sure that from this moment on the mesh_geometry is no used anymore.
	// This is a safety step, because the mesh_geometry data are no more valid
	// at this point.

	const int vertex_count = vertices.size();

	print_verbose("Vertex count: " + itos(vertex_count));

	// The map key is the material allocator id that is also used as surface id.
	HashMap<SurfaceId, SurfaceData> surfaces;

	// Phase 2. For each material create a surface tool (So a different mesh).
	{
		if (polygon_surfaces.is_empty()) {
			// No material, just use the default one with index -1.
			// Set -1 to all polygons.
			const int polygon_count = count_polygons(polygon_indices);
			for (int p = 0; p < polygon_count; p += 1) {
				polygon_surfaces[p] = -1;
			}
		}

		// Create the surface now.
		for (const int *polygon_id = polygon_surfaces.next(nullptr); polygon_id != nullptr; polygon_id = polygon_surfaces.next(polygon_id)) {
			const int surface_id = polygon_surfaces[*polygon_id];
			if (surfaces.has(surface_id) == false) {
				SurfaceData sd;
				sd.surface_tool.instance();
				sd.surface_tool->begin(Mesh::PRIMITIVE_TRIANGLES);

				if (surface_id < 0) {
					// nothing to do
				} else if (surface_id < (int)material_lookup.size()) {
					const FBXDocParser::Material *mat_mapping = material_lookup.at(surface_id);
					const uint64_t mapping_id = mat_mapping->ID();
					if (state.cached_materials.has(mapping_id)) {
						sd.material = state.cached_materials[mapping_id];
					}
				} else {
					WARN_PRINT("out of bounds surface detected, FBX file has corrupt material data");
				}

				surfaces.set(surface_id, sd);
			}
		}
	}

	// Phase 3. Map the vertices relative to each surface, in this way we can
	// just insert the vertices that we need per each surface.
	{
		PolygonId polygon_index = -1;
		SurfaceId surface_id = -1;
		SurfaceData *surface_data = nullptr;

		for (size_t polygon_vertex = 0; polygon_vertex < polygon_indices.size(); polygon_vertex += 1) {
			if (is_start_of_polygon(polygon_indices, polygon_vertex)) {
				polygon_index += 1;
				ERR_FAIL_COND_V_MSG(polygon_surfaces.has(polygon_index) == false, nullptr, "The FBX file is corrupted, This surface_index is not expected.");
				surface_id = polygon_surfaces[polygon_index];
				surface_data = surfaces.getptr(surface_id);
				CRASH_COND(surface_data == nullptr); // Can't be null.
			}

			const int vertex = get_vertex_from_polygon_vertex(polygon_indices, polygon_vertex);

			// The vertex position in the surface
			// Uses a lookup table for speed with large scenes
			bool has_polygon_vertex_index = surface_data->lookup_table.has(vertex);
			int surface_polygon_vertex_index = -1;

			if (has_polygon_vertex_index) {
				surface_polygon_vertex_index = surface_data->lookup_table[vertex];
			} else {
				surface_polygon_vertex_index = surface_data->vertices_map.size();
				surface_data->lookup_table[vertex] = surface_polygon_vertex_index;
				surface_data->vertices_map.push_back(vertex);
			}

			surface_data->surface_polygon_vertex[polygon_index].push_back(surface_polygon_vertex_index);
		}
	}

	//print_verbose("[debug UV 1] UV1: " + itos(uvs_0.size()));
	//print_verbose("[debug UV 2] UV2: " + itos(uvs_1.size()));

	// Phase 4. Per each surface just insert the vertices and add the indices.
	for (const SurfaceId *surface_id = surfaces.next(nullptr); surface_id != nullptr; surface_id = surfaces.next(surface_id)) {
		SurfaceData *surface = surfaces.getptr(*surface_id);

		// Just add the vertices data.
		for (unsigned int i = 0; i < surface->vertices_map.size(); i += 1) {
			const Vertex vertex = surface->vertices_map[i];

			// This must be done before add_vertex because the surface tool is
			// expecting this before the st->add_vertex() call
			add_vertex(state,
					surface->surface_tool,
					state.scale,
					vertex,
					vertices,
					normals,
					uvs_0,
					uvs_1,
					colors);
		}

		// Triangulate the various polygons and add the indices.
		for (const PolygonId *polygon_id = surface->surface_polygon_vertex.next(nullptr); polygon_id != nullptr; polygon_id = surface->surface_polygon_vertex.next(polygon_id)) {
			const Vector<DataIndex> *indices = surface->surface_polygon_vertex.getptr(*polygon_id);
			triangulate_polygon(
					surface,
					*indices,
					vertices);
		}
	}

	// Phase 5. Compose the morphs if any.
	for (const SurfaceId *surface_id = surfaces.next(nullptr); surface_id != nullptr; surface_id = surfaces.next(surface_id)) {
		SurfaceData *surface = surfaces.getptr(*surface_id);

		for (const String *morph_name = morphs.next(nullptr); morph_name != nullptr; morph_name = morphs.next(morph_name)) {
			MorphVertexData *morph_data = morphs.getptr(*morph_name);

			// As said by the docs, this is not supposed to be different than
			// vertex_count.
			CRASH_COND(morph_data->vertices.size() != vertex_count);
			CRASH_COND(morph_data->normals.size() != vertex_count);

			Vector3 *vertices_ptr = morph_data->vertices.ptrw();
			Vector3 *normals_ptr = morph_data->normals.ptrw();

			Ref<SurfaceTool> morph_st;
			morph_st.instance();
			morph_st->begin(Mesh::PRIMITIVE_TRIANGLES);

			for (unsigned int vi = 0; vi < surface->vertices_map.size(); vi += 1) {
				const Vertex &vertex = surface->vertices_map[vi];
				add_vertex(
						state,
						morph_st,
						state.scale,
						vertex,
						vertices,
						normals,
						uvs_0,
						uvs_1,
						colors,
						vertices_ptr[vertex],
						normals_ptr[vertex]);
			}

			if (state.is_blender_fbx) {
				morph_st->generate_normals();
			}
			morph_st->generate_tangents();
			surface->morphs.push_back(morph_st->commit_to_arrays());
		}
	}

	// Phase 6. Compose the mesh and return it.
	Ref<EditorSceneImporterMesh> mesh;
	mesh.instance();

	// Add blend shape info.
	for (const String *morph_name = morphs.next(nullptr); morph_name != nullptr; morph_name = morphs.next(morph_name)) {
		mesh->add_blend_shape(*morph_name);
	}

	// TODO always normalized, Why?
	mesh->set_blend_shape_mode(Mesh::BLEND_SHAPE_MODE_NORMALIZED);

	// Add surfaces.
	int in_mesh_surface_id = 0;
	for (const SurfaceId *surface_id = surfaces.next(nullptr); surface_id != nullptr; surface_id = surfaces.next(surface_id)) {
		SurfaceData *surface = surfaces.getptr(*surface_id);

		if (state.is_blender_fbx) {
			surface->surface_tool->generate_normals();
		}
		// you can't generate them without a valid uv map.
		if (uvs_0_raw.size() > 0) {
			surface->surface_tool->generate_tangents();
		}

		Array mesh_array = surface->surface_tool->commit_to_arrays();
		Array blend_shapes = surface->morphs;

		if (surface->material.is_valid()) {
			mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES, mesh_array, blend_shapes, Dictionary(), surface->material, surface->material->get_name());
		} else {
			mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES, mesh_array, blend_shapes);
		}

		in_mesh_surface_id += 1;
	}

	EditorSceneImporterMeshNode3D *godot_mesh = memnew(EditorSceneImporterMeshNode3D);
	godot_mesh->set_mesh(mesh);
	const String name = ImportUtils::FBXNodeToName(model->Name());
	godot_mesh->set_name(name); // hurry up compiling >.<
	mesh->set_name("mesh3d-" + name);
	return godot_mesh;
}

void FBXMeshData::sanitize_vertex_weights(const ImportState &state) {
	const int max_vertex_influence_count = RS::ARRAY_WEIGHTS_SIZE;
	Map<int, int> skeleton_to_skin_bind_id;
	// TODO: error's need added
	const FBXDocParser::Skin *fbx_skin = mesh_geometry->DeformerSkin();

	if (fbx_skin == nullptr || fbx_skin->Clusters().size() == 0) {
		return; // do nothing
	}

	//
	// Precalculate the skin cluster mapping
	//

	int bind_id = 0;
	for (const FBXDocParser::Cluster *cluster : fbx_skin->Clusters()) {
		ERR_CONTINUE_MSG(!state.fbx_bone_map.has(cluster->TargetNode()->ID()), "Missing bone map for cluster target node with id " + uitos(cluster->TargetNode()->ID()) + ".");
		Ref<FBXBone> bone = state.fbx_bone_map[cluster->TargetNode()->ID()];
		skeleton_to_skin_bind_id.insert(bone->godot_bone_id, bind_id);
		bind_id++;
	}

	for (const Vertex *v = vertex_weights.next(nullptr); v != nullptr; v = vertex_weights.next(v)) {
		VertexWeightMapping *vm = vertex_weights.getptr(*v);
		ERR_CONTINUE(vm->bones.size() != vm->weights.size()); // No message, already checked.
		ERR_CONTINUE(vm->bones_ref.size() != vm->weights.size()); // No message, already checked.

		const int initial_size = vm->weights.size();
		{
			// Init bone id
			int *bones_ptr = vm->bones.ptrw();
			Ref<FBXBone> *bones_ref_ptr = vm->bones_ref.ptrw();

			for (int i = 0; i < vm->weights.size(); i += 1) {
				// At this point this is not possible because the skeleton is already initialized.
				CRASH_COND(bones_ref_ptr[i]->godot_bone_id == -2);
				bones_ptr[i] = skeleton_to_skin_bind_id[bones_ref_ptr[i]->godot_bone_id];
			}

			// From this point on the data is no more valid.
			vm->bones_ref.clear();
		}

		{
			// Sort
			real_t *weights_ptr = vm->weights.ptrw();
			int *bones_ptr = vm->bones.ptrw();
			for (int i = 0; i < vm->weights.size(); i += 1) {
				for (int x = i + 1; x < vm->weights.size(); x += 1) {
					if (weights_ptr[i] < weights_ptr[x]) {
						SWAP(weights_ptr[i], weights_ptr[x]);
						SWAP(bones_ptr[i], bones_ptr[x]);
					}
				}
			}
		}

		{
			// Resize
			vm->weights.resize(max_vertex_influence_count);
			vm->bones.resize(max_vertex_influence_count);
			real_t *weights_ptr = vm->weights.ptrw();
			int *bones_ptr = vm->bones.ptrw();
			for (int i = initial_size; i < max_vertex_influence_count; i += 1) {
				weights_ptr[i] = 0.0;
				bones_ptr[i] = 0;
			}

			// Normalize
			real_t sum = 0.0;
			for (int i = 0; i < max_vertex_influence_count; i += 1) {
				sum += weights_ptr[i];
			}
			if (sum > 0.0) {
				for (int i = 0; i < vm->weights.size(); i += 1) {
					weights_ptr[i] = weights_ptr[i] / sum;
				}
			}
		}
	}
}

void FBXMeshData::reorganize_vertices(
		// TODO: perf hotspot on insane files
		std::vector<int> &r_polygon_indices,
		std::vector<Vector3> &r_vertices,
		HashMap<int, Vector3> &r_normals,
		HashMap<int, Vector2> &r_uv_1,
		HashMap<int, Vector2> &r_uv_2,
		HashMap<int, Color> &r_color,
		HashMap<String, MorphVertexData> &r_morphs,
		HashMap<int, HashMap<int, Vector3>> &r_normals_raw,
		HashMap<int, HashMap<int, Color>> &r_colors_raw,
		HashMap<int, HashMap<int, Vector2>> &r_uv_1_raw,
		HashMap<int, HashMap<int, Vector2>> &r_uv_2_raw) {
	// Key: OldVertex; Value: [New vertices];
	HashMap<int, Vector<int>> duplicated_vertices;

	PolygonId polygon_index = -1;
	for (int pv = 0; pv < (int)r_polygon_indices.size(); pv += 1) {
		if (is_start_of_polygon(r_polygon_indices, pv)) {
			polygon_index += 1;
		}
		const Vertex index = get_vertex_from_polygon_vertex(r_polygon_indices, pv);

		bool need_duplication = false;
		Vector2 this_vert_poly_uv1 = Vector2();
		Vector2 this_vert_poly_uv2 = Vector2();
		Vector3 this_vert_poly_normal = Vector3();
		Color this_vert_poly_color = Color();

		// Take the normal and see if we need to duplicate this polygon.
		if (r_normals_raw.has(index)) {
			const HashMap<PolygonId, Vector3> *nrml_arr = r_normals_raw.getptr(index);

			if (nrml_arr->has(polygon_index)) {
				this_vert_poly_normal = nrml_arr->get(polygon_index);
			} else if (nrml_arr->has(-1)) {
				this_vert_poly_normal = nrml_arr->get(-1);
			} else {
				print_error("invalid normal detected: " + itos(index) + " polygon index: " + itos(polygon_index));
				for (const PolygonId *pid = nrml_arr->next(nullptr); pid != nullptr; pid = nrml_arr->next(pid)) {
					print_verbose("debug contents key: " + itos(*pid));

					if (nrml_arr->has(*pid)) {
						print_verbose("contents valid: " + nrml_arr->get(*pid));
					}
				}
			}

			// Now, check if we need to duplicate it.
			for (const PolygonId *pid = nrml_arr->next(nullptr); pid != nullptr; pid = nrml_arr->next(pid)) {
				if (*pid == polygon_index) {
					continue;
				}

				const Vector3 vert_poly_normal = *nrml_arr->getptr(*pid);
				if ((this_vert_poly_normal - vert_poly_normal).length_squared() > CMP_EPSILON) {
					// Yes this polygon need duplication.
					need_duplication = true;
					break;
				}
			}
		}

		// TODO: make me vertex color
		// Take the normal and see if we need to duplicate this polygon.
		if (r_colors_raw.has(index)) {
			const HashMap<PolygonId, Color> *color_arr = r_colors_raw.getptr(index);

			if (color_arr->has(polygon_index)) {
				this_vert_poly_color = color_arr->get(polygon_index);
			} else if (color_arr->has(-1)) {
				this_vert_poly_color = color_arr->get(-1);
			} else {
				print_error("invalid color detected: " + itos(index) + " polygon index: " + itos(polygon_index));
				for (const PolygonId *pid = color_arr->next(nullptr); pid != nullptr; pid = color_arr->next(pid)) {
					print_verbose("debug contents key: " + itos(*pid));

					if (color_arr->has(*pid)) {
						print_verbose("contents valid: " + color_arr->get(*pid));
					}
				}
			}

			// Now, check if we need to duplicate it.
			for (const PolygonId *pid = color_arr->next(nullptr); pid != nullptr; pid = color_arr->next(pid)) {
				if (*pid == polygon_index) {
					continue;
				}

				const Color vert_poly_color = *color_arr->getptr(*pid);
				if (!this_vert_poly_color.is_equal_approx(vert_poly_color)) {
					// Yes this polygon need duplication.
					need_duplication = true;
					break;
				}
			}
		}

		// Take the UV1 and UV2 and see if we need to duplicate this polygon.
		{
			HashMap<int, HashMap<int, Vector2>> *uv_raw = &r_uv_1_raw;
			Vector2 *this_vert_poly_uv = &this_vert_poly_uv1;
			for (int kk = 0; kk < 2; kk++) {
				if (uv_raw->has(index)) {
					const HashMap<PolygonId, Vector2> *uvs = uv_raw->getptr(index);

					if (uvs->has(polygon_index)) {
						// This Polygon has its own uv.
						(*this_vert_poly_uv) = *uvs->getptr(polygon_index);

						// Check if we need to duplicate it.
						for (const PolygonId *pid = uvs->next(nullptr); pid != nullptr; pid = uvs->next(pid)) {
							if (*pid == polygon_index) {
								continue;
							}
							const Vector2 vert_poly_uv = *uvs->getptr(*pid);
							if (((*this_vert_poly_uv) - vert_poly_uv).length_squared() > CMP_EPSILON) {
								// Yes this polygon need duplication.
								need_duplication = true;
								break;
							}
						}
					} else if (uvs->has(-1)) {
						// It has the default UV.
						(*this_vert_poly_uv) = *uvs->getptr(-1);
					} else if (uvs->size() > 0) {
						// No uv, this is strange, just take the first and duplicate.
						(*this_vert_poly_uv) = *uvs->getptr(*uvs->next(nullptr));
						WARN_PRINT("No UVs for this polygon, while there is no default and some other polygons have it. This FBX file may be corrupted.");
					}
				}
				uv_raw = &r_uv_2_raw;
				this_vert_poly_uv = &this_vert_poly_uv2;
			}
		}

		// If we want to duplicate it, Let's see if we already duplicated this
		// vertex.
		if (need_duplication) {
			if (duplicated_vertices.has(index)) {
				Vertex similar_vertex = -1;
				// Let's see if one of the new vertices has the same data of this.
				const Vector<int> *new_vertices = duplicated_vertices.getptr(index);
				for (int j = 0; j < new_vertices->size(); j += 1) {
					const Vertex new_vertex = (*new_vertices)[j];
					bool same_uv1 = false;
					bool same_uv2 = false;
					bool same_normal = false;
					bool same_color = false;

					if (r_uv_1.has(new_vertex)) {
						if ((this_vert_poly_uv1 - (*r_uv_1.getptr(new_vertex))).length_squared() <= CMP_EPSILON) {
							same_uv1 = true;
						}
					}

					if (r_uv_2.has(new_vertex)) {
						if ((this_vert_poly_uv2 - (*r_uv_2.getptr(new_vertex))).length_squared() <= CMP_EPSILON) {
							same_uv2 = true;
						}
					}

					if (r_color.has(new_vertex)) {
						if (this_vert_poly_color.is_equal_approx((*r_color.getptr(new_vertex)))) {
							same_color = true;
						}
					}

					if (r_normals.has(new_vertex)) {
						if ((this_vert_poly_normal - (*r_normals.getptr(new_vertex))).length_squared() <= CMP_EPSILON) {
							same_uv2 = true;
						}
					}

					if (same_uv1 && same_uv2 && same_normal && same_color) {
						similar_vertex = new_vertex;
						break;
					}
				}

				if (similar_vertex != -1) {
					// Update polygon.
					if (is_end_of_polygon(r_polygon_indices, pv)) {
						r_polygon_indices[pv] = ~similar_vertex;
					} else {
						r_polygon_indices[pv] = similar_vertex;
					}
					need_duplication = false;
				}
			}
		}

		if (need_duplication) {
			const Vertex old_index = index;
			const Vertex new_index = r_vertices.size();

			// Polygon index.
			if (is_end_of_polygon(r_polygon_indices, pv)) {
				r_polygon_indices[pv] = ~new_index;
			} else {
				r_polygon_indices[pv] = new_index;
			}

			// Vertex position.
			r_vertices.push_back(r_vertices[old_index]);

			// Normals
			if (r_normals_raw.has(old_index)) {
				r_normals.set(new_index, this_vert_poly_normal);
				r_normals_raw.getptr(old_index)->erase(polygon_index);
				r_normals_raw[new_index][polygon_index] = this_vert_poly_normal;
			}

			// Vertex Color
			if (r_colors_raw.has(old_index)) {
				r_color.set(new_index, this_vert_poly_color);
				r_colors_raw.getptr(old_index)->erase(polygon_index);
				r_colors_raw[new_index][polygon_index] = this_vert_poly_color;
			}

			// UV 0
			if (r_uv_1_raw.has(old_index)) {
				r_uv_1.set(new_index, this_vert_poly_uv1);
				r_uv_1_raw.getptr(old_index)->erase(polygon_index);
				r_uv_1_raw[new_index][polygon_index] = this_vert_poly_uv1;
			}

			// UV 1
			if (r_uv_2_raw.has(old_index)) {
				r_uv_2.set(new_index, this_vert_poly_uv2);
				r_uv_2_raw.getptr(old_index)->erase(polygon_index);
				r_uv_2_raw[new_index][polygon_index] = this_vert_poly_uv2;
			}

			// Morphs
			for (const String *mname = r_morphs.next(nullptr); mname != nullptr; mname = r_morphs.next(mname)) {
				MorphVertexData *d = r_morphs.getptr(*mname);
				// This can't never happen.
				CRASH_COND(d == nullptr);
				if (d->vertices.size() > old_index) {
					d->vertices.push_back(d->vertices[old_index]);
				}
				if (d->normals.size() > old_index) {
					d->normals.push_back(d->normals[old_index]);
				}
			}

			if (vertex_weights.has(old_index)) {
				vertex_weights.set(new_index, vertex_weights[old_index]);
			}

			duplicated_vertices[old_index].push_back(new_index);
		} else {
			if (r_normals_raw.has(index) &&
					r_normals.has(index) == false) {
				r_normals.set(index, this_vert_poly_normal);
			}

			if (r_colors_raw.has(index) && r_color.has(index) == false) {
				r_color.set(index, this_vert_poly_color);
			}

			if (r_uv_1_raw.has(index) &&
					r_uv_1.has(index) == false) {
				r_uv_1.set(index, this_vert_poly_uv1);
			}

			if (r_uv_2_raw.has(index) &&
					r_uv_2.has(index) == false) {
				r_uv_2.set(index, this_vert_poly_uv2);
			}
		}
	}
}

void FBXMeshData::add_vertex(
		const ImportState &state,
		Ref<SurfaceTool> p_surface_tool,
		real_t p_scale,
		Vertex p_vertex,
		const std::vector<Vector3> &p_vertices_position,
		const HashMap<int, Vector3> &p_normals,
		const HashMap<int, Vector2> &p_uvs_0,
		const HashMap<int, Vector2> &p_uvs_1,
		const HashMap<int, Color> &p_colors,
		const Vector3 &p_morph_value,
		const Vector3 &p_morph_normal) {
	ERR_FAIL_INDEX_MSG(p_vertex, (Vertex)p_vertices_position.size(), "FBX file is corrupted, the position of the vertex can't be retrieved.");

	if (p_normals.has(p_vertex) && !state.is_blender_fbx) {
		p_surface_tool->set_normal(p_normals[p_vertex] + p_morph_normal);
	}

	if (p_uvs_0.has(p_vertex)) {
		//print_verbose("uv1: [" + itos(p_vertex) + "] " + p_uvs_0[p_vertex]);
		// Inverts Y UV.
		p_surface_tool->set_uv(Vector2(p_uvs_0[p_vertex].x, 1 - p_uvs_0[p_vertex].y));
	}

	if (p_uvs_1.has(p_vertex)) {
		//print_verbose("uv2: [" + itos(p_vertex) + "] " + p_uvs_1[p_vertex]);
		// Inverts Y UV.
		p_surface_tool->set_uv2(Vector2(p_uvs_1[p_vertex].x, 1 - p_uvs_1[p_vertex].y));
	}

	if (p_colors.has(p_vertex)) {
		p_surface_tool->set_color(p_colors[p_vertex]);
	}

	// TODO what about binormals?
	// TODO there is other?

	if (vertex_weights.has(p_vertex)) {
		// Let's extract the weight info.
		const VertexWeightMapping *vm = vertex_weights.getptr(p_vertex);
		const Vector<int> &bones = vm->bones;

		// the bug is that the bone idx is wrong because it is not ref'ing the skin.

		if (bones.size() > RS::ARRAY_WEIGHTS_SIZE) {
			print_error("[weight overflow detected]");
		}

		p_surface_tool->set_weights(vm->weights);
		// 0 1 2 3 4 5 6 7 < local skeleton / skin for mesh
		// 0 1 2 3 4 5 6 7 8 9 10  < actual skeleton with all joints
		p_surface_tool->set_bones(bones);
	}

	// The surface tool want the vertex position as last thing.
	p_surface_tool->add_vertex((p_vertices_position[p_vertex] + p_morph_value) * p_scale);
}

void FBXMeshData::triangulate_polygon(SurfaceData *surface, const Vector<int> &p_polygon_vertex, const std::vector<Vector3> &p_vertices) const {
	Ref<SurfaceTool> st(surface->surface_tool);
	const int polygon_vertex_count = p_polygon_vertex.size();
	//const Vector<Vertex>& p_surface_vertex_map
	if (polygon_vertex_count == 1) {
		// point to triangle
		st->add_index(p_polygon_vertex[0]);
		st->add_index(p_polygon_vertex[0]);
		st->add_index(p_polygon_vertex[0]);
		return;
	} else if (polygon_vertex_count == 2) {
		// line to triangle
		st->add_index(p_polygon_vertex[1]);
		st->add_index(p_polygon_vertex[1]);
		st->add_index(p_polygon_vertex[0]);
		return;
	} else if (polygon_vertex_count == 3) {
		// triangle to triangle
		st->add_index(p_polygon_vertex[0]);
		st->add_index(p_polygon_vertex[2]);
		st->add_index(p_polygon_vertex[1]);
		return;
	} else if (polygon_vertex_count == 4) {
		// quad to triangle - this code is awesome for import times
		// it prevents triangles being generated slowly
		st->add_index(p_polygon_vertex[0]);
		st->add_index(p_polygon_vertex[2]);
		st->add_index(p_polygon_vertex[1]);
		st->add_index(p_polygon_vertex[2]);
		st->add_index(p_polygon_vertex[0]);
		st->add_index(p_polygon_vertex[3]);
		return;
	} else {
		// non triangulated - we must run the triangulation algorithm
		bool is_simple_convex = false;
		// this code is 'slow' but required it triangulates all the unsupported geometry.
		// Doesn't allow for bigger polygons because those are unlikely be convex
		if (polygon_vertex_count <= 6) {
			// Start from true, check if it's false.
			is_simple_convex = true;
			Vector3 first_vec;
			for (int i = 0; i < polygon_vertex_count; i += 1) {
				const Vector3 p1 = p_vertices[surface->vertices_map[p_polygon_vertex[i]]];
				const Vector3 p2 = p_vertices[surface->vertices_map[p_polygon_vertex[(i + 1) % polygon_vertex_count]]];
				const Vector3 p3 = p_vertices[surface->vertices_map[p_polygon_vertex[(i + 2) % polygon_vertex_count]]];

				const Vector3 edge1 = p1 - p2;
				const Vector3 edge2 = p3 - p2;

				const Vector3 res = edge1.normalized().cross(edge2.normalized()).normalized();
				if (i == 0) {
					first_vec = res;
				} else {
					if (first_vec.dot(res) < 0.0) {
						// Ok we found an angle that is not the same dir of the
						// others.
						is_simple_convex = false;
						break;
					}
				}
			}
		}

		if (is_simple_convex) {
			// This is a convex polygon, so just triangulate it.
			for (int i = 0; i < (polygon_vertex_count - 2); i += 1) {
				st->add_index(p_polygon_vertex[2 + i]);
				st->add_index(p_polygon_vertex[1 + i]);
				st->add_index(p_polygon_vertex[0]);
			}
			return;
		}
	}

	{
		// This is a concave polygon.

		std::vector<Vector3> poly_vertices(polygon_vertex_count);
		for (int i = 0; i < polygon_vertex_count; i += 1) {
			poly_vertices[i] = p_vertices[surface->vertices_map[p_polygon_vertex[i]]];
		}

		const Vector3 poly_norm = get_poly_normal(poly_vertices);
		if (poly_norm.length_squared() <= CMP_EPSILON) {
			ERR_FAIL_COND_MSG(poly_norm.length_squared() <= CMP_EPSILON, "The normal of this poly was not computed. Is this FBX file corrupted.");
		}

		// Select the plan coordinate.
		int axis_1_coord = 0;
		int axis_2_coord = 1;
		{
			real_t inv = poly_norm.z;

			const real_t axis_x = ABS(poly_norm.x);
			const real_t axis_y = ABS(poly_norm.y);
			const real_t axis_z = ABS(poly_norm.z);

			if (axis_x > axis_y) {
				if (axis_x > axis_z) {
					// For the most part the normal point toward X.
					axis_1_coord = 1;
					axis_2_coord = 2;
					inv = poly_norm.x;
				}
			} else if (axis_y > axis_z) {
				// For the most part the normal point toward Y.
				axis_1_coord = 2;
				axis_2_coord = 0;
				inv = poly_norm.y;
			}

			// Swap projection axes to take the negated projection vector into account
			if (inv < 0.0f) {
				SWAP(axis_1_coord, axis_2_coord);
			}
		}

		TPPLPoly tppl_poly;
		tppl_poly.Init(polygon_vertex_count);
		std::vector<Vector2> projected_vertices(polygon_vertex_count);
		for (int i = 0; i < polygon_vertex_count; i += 1) {
			const Vector2 pv(poly_vertices[i][axis_1_coord], poly_vertices[i][axis_2_coord]);
			projected_vertices[i] = pv;
			tppl_poly.GetPoint(i) = pv;
		}
		tppl_poly.SetOrientation(TPPL_ORIENTATION_CCW);

		List<TPPLPoly> out_poly;

		TPPLPartition tppl_partition;
		if (tppl_partition.Triangulate_OPT(&tppl_poly, &out_poly) == 0) { // Good result.
			if (tppl_partition.Triangulate_EC(&tppl_poly, &out_poly) == 0) { // Medium result.
				if (tppl_partition.Triangulate_MONO(&tppl_poly, &out_poly) == 0) { // Really poor result.
					ERR_FAIL_MSG("The triangulation of this polygon failed, please try to triangulate your mesh or check if it has broken polygons.");
				}
			}
		}

		std::vector<Vector2> tris(out_poly.size());
		for (List<TPPLPoly>::Element *I = out_poly.front(); I; I = I->next()) {
			TPPLPoly &tp = I->get();

			ERR_FAIL_COND_MSG(tp.GetNumPoints() != 3, "The triangulator returned more points, how this is possible?");
			// Find Index
			for (int i = 2; i >= 0; i -= 1) {
				const Vector2 vertex = tp.GetPoint(i);
				bool done = false;
				// Find Index
				for (int y = 0; y < polygon_vertex_count; y += 1) {
					if ((projected_vertices[y] - vertex).length_squared() <= CMP_EPSILON) {
						// This seems the right vertex
						st->add_index(p_polygon_vertex[y]);
						done = true;
						break;
					}
				}
				ERR_FAIL_COND(done == false);
			}
		}
	}
}

void FBXMeshData::gen_weight_info(Ref<SurfaceTool> st, Vertex vertex_id) const {
	if (vertex_weights.is_empty()) {
		return;
	}

	if (vertex_weights.has(vertex_id)) {
		// Let's extract the weight info.
		const VertexWeightMapping *vm = vertex_weights.getptr(vertex_id);
		st->set_weights(vm->weights);
		st->set_bones(vm->bones);
	}
}

int FBXMeshData::get_vertex_from_polygon_vertex(const std::vector<int> &p_polygon_indices, int p_index) const {
	if (p_index < 0 || p_index >= (int)p_polygon_indices.size()) {
		return -1;
	}

	const int vertex = p_polygon_indices[p_index];
	if (vertex >= 0) {
		return vertex;
	} else {
		// Negative numbers are the end of the face, reversing the bits is
		// possible to obtain the positive correct vertex number.
		return ~vertex;
	}
}

bool FBXMeshData::is_end_of_polygon(const std::vector<int> &p_polygon_indices, int p_index) const {
	if (p_index < 0 || p_index >= (int)p_polygon_indices.size()) {
		return false;
	}

	const int vertex = p_polygon_indices[p_index];

	// If the index is negative this is the end of the Polygon.
	return vertex < 0;
}

bool FBXMeshData::is_start_of_polygon(const std::vector<int> &p_polygon_indices, int p_index) const {
	if (p_index < 0 || p_index >= (int)p_polygon_indices.size()) {
		return false;
	}

	if (p_index == 0) {
		return true;
	}

	// If the previous indices is negative this is the begin of a new Polygon.
	return p_polygon_indices[p_index - 1] < 0;
}

int FBXMeshData::count_polygons(const std::vector<int> &p_polygon_indices) const {
	// The negative numbers define the end of the polygon. Counting the amount of
	// negatives the numbers of polygons are obtained.
	int count = 0;
	for (size_t i = 0; i < p_polygon_indices.size(); i += 1) {
		if (p_polygon_indices[i] < 0) {
			count += 1;
		}
	}
	return count;
}

template <class R, class T>
HashMap<int, R> FBXMeshData::extract_per_vertex_data(
		int p_vertex_count,
		const std::vector<FBXDocParser::MeshGeometry::Edge> &p_edge_map,
		const std::vector<int> &p_mesh_indices,
		const FBXDocParser::MeshGeometry::MappingData<T> &p_mapping_data,
		R (*collector_function)(const Vector<VertexData<T>> *p_vertex_data, R p_fall_back),
		R p_fall_back) const {
	/* When index_to_direct is set
	 * index size is 184 ( contains index for the data array [values 0, 96] )
	 * data size is 96 (contains uv coordinates)
	 * this means index is simple data reduction basically
	 */
	////
	if (p_mapping_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::index_to_direct && p_mapping_data.index.size() == 0) {
		print_verbose("debug count: index size: " + itos(p_mapping_data.index.size()) + ", data size: " + itos(p_mapping_data.data.size()));
		print_verbose("vertex indices count: " + itos(p_mesh_indices.size()));
		print_verbose("Edge map size: " + itos(p_edge_map.size()));
	}

	ERR_FAIL_COND_V_MSG(p_mapping_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::index_to_direct && p_mapping_data.index.size() == 0, (HashMap<int, R>()), "FBX importer needs to map correctly to this field, please specify the override index name to fix this problem!");
	ERR_FAIL_COND_V_MSG(p_mapping_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::index && p_mapping_data.index.size() == 0, (HashMap<int, R>()), "The FBX seems corrupted");

	// Aggregate vertex data.
	HashMap<Vertex, Vector<VertexData<T>>> aggregate_vertex_data;

	switch (p_mapping_data.map_type) {
		case FBXDocParser::MeshGeometry::MapType::none: {
			// No data nothing to do.
			return (HashMap<int, R>());
		}
		case FBXDocParser::MeshGeometry::MapType::vertex: {
			ERR_FAIL_COND_V_MSG(p_mapping_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::index_to_direct, (HashMap<int, R>()), "We will support in future");

			if (p_mapping_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::direct) {
				// The data is mapped per vertex directly.
				ERR_FAIL_COND_V_MSG((int)p_mapping_data.data.size() != p_vertex_count, (HashMap<int, R>()), "FBX file corrupted: #ERR01");
				for (size_t vertex_index = 0; vertex_index < p_mapping_data.data.size(); vertex_index += 1) {
					aggregate_vertex_data[vertex_index].push_back({ -1, p_mapping_data.data[vertex_index] });
				}
			} else {
				// The data is mapped per vertex using a reference.
				// The indices array, contains a *reference_id for each vertex.
				// * Note that the reference_id is the id of data into the data array.
				//
				// https://help.autodesk.com/view/FBX/2017/ENU/?guid=__cpp_ref_class_fbx_layer_element_html
				ERR_FAIL_COND_V_MSG((int)p_mapping_data.index.size() != p_vertex_count, (HashMap<int, R>()), "FBX file corrupted: #ERR02");
				for (size_t vertex_index = 0; vertex_index < p_mapping_data.index.size(); vertex_index += 1) {
					ERR_FAIL_INDEX_V_MSG(p_mapping_data.index[vertex_index], (int)p_mapping_data.data.size(), (HashMap<int, R>()), "FBX file seems corrupted: #ERR03.");
					aggregate_vertex_data[vertex_index].push_back({ -1, p_mapping_data.data[p_mapping_data.index[vertex_index]] });
				}
			}
		} break;
		case FBXDocParser::MeshGeometry::MapType::polygon_vertex: {
			if (p_mapping_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::index_to_direct) {
				// The data is mapped using each index from the indexes array then direct to the data (data reduction algorithm)
				ERR_FAIL_COND_V_MSG((int)p_mesh_indices.size() != (int)p_mapping_data.index.size(), (HashMap<int, R>()), "FBX file seems corrupted: #ERR04");
				int polygon_id = -1;
				for (size_t polygon_vertex_index = 0; polygon_vertex_index < p_mapping_data.index.size(); polygon_vertex_index += 1) {
					if (is_start_of_polygon(p_mesh_indices, polygon_vertex_index)) {
						polygon_id += 1;
					}
					const int vertex_index = get_vertex_from_polygon_vertex(p_mesh_indices, polygon_vertex_index);
					ERR_FAIL_COND_V_MSG(vertex_index < 0, (HashMap<int, R>()), "FBX file corrupted: #ERR05");
					ERR_FAIL_COND_V_MSG(vertex_index >= p_vertex_count, (HashMap<int, R>()), "FBX file corrupted: #ERR06");
					const int index_to_direct = p_mapping_data.index[polygon_vertex_index];
					T value = p_mapping_data.data[index_to_direct];
					aggregate_vertex_data[vertex_index].push_back({ polygon_id, value });
				}
			} else if (p_mapping_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::direct) {
				// The data are mapped per polygon vertex directly.
				ERR_FAIL_COND_V_MSG((int)p_mesh_indices.size() != (int)p_mapping_data.data.size(), (HashMap<int, R>()), "FBX file seems corrupted: #ERR04");
				int polygon_id = -1;
				for (size_t polygon_vertex_index = 0; polygon_vertex_index < p_mapping_data.data.size(); polygon_vertex_index += 1) {
					if (is_start_of_polygon(p_mesh_indices, polygon_vertex_index)) {
						polygon_id += 1;
					}
					const int vertex_index = get_vertex_from_polygon_vertex(p_mesh_indices, polygon_vertex_index);
					ERR_FAIL_COND_V_MSG(vertex_index < 0, (HashMap<int, R>()), "FBX file corrupted: #ERR05");
					ERR_FAIL_COND_V_MSG(vertex_index >= p_vertex_count, (HashMap<int, R>()), "FBX file corrupted: #ERR06");

					aggregate_vertex_data[vertex_index].push_back({ polygon_id, p_mapping_data.data[polygon_vertex_index] });
				}
			} else {
				// The data is mapped per polygon_vertex using a reference.
				// The indices array, contains a *reference_id for each polygon_vertex.
				// * Note that the reference_id is the id of data into the data array.
				//
				// https://help.autodesk.com/view/FBX/2017/ENU/?guid=__cpp_ref_class_fbx_layer_element_html
				ERR_FAIL_COND_V_MSG(p_mesh_indices.size() != p_mapping_data.index.size(), (HashMap<int, R>()), "FBX file corrupted: #ERR7");
				int polygon_id = -1;
				for (size_t polygon_vertex_index = 0; polygon_vertex_index < p_mapping_data.index.size(); polygon_vertex_index += 1) {
					if (is_start_of_polygon(p_mesh_indices, polygon_vertex_index)) {
						polygon_id += 1;
					}
					const int vertex_index = get_vertex_from_polygon_vertex(p_mesh_indices, polygon_vertex_index);
					ERR_FAIL_COND_V_MSG(vertex_index < 0, (HashMap<int, R>()), "FBX file corrupted: #ERR8");
					ERR_FAIL_COND_V_MSG(vertex_index >= p_vertex_count, (HashMap<int, R>()), "FBX file seems corrupted: #ERR9.");
					ERR_FAIL_COND_V_MSG(p_mapping_data.index[polygon_vertex_index] < 0, (HashMap<int, R>()), "FBX file seems corrupted: #ERR10.");
					ERR_FAIL_COND_V_MSG(p_mapping_data.index[polygon_vertex_index] >= (int)p_mapping_data.data.size(), (HashMap<int, R>()), "FBX file seems corrupted: #ERR11.");
					aggregate_vertex_data[vertex_index].push_back({ polygon_id, p_mapping_data.data[p_mapping_data.index[polygon_vertex_index]] });
				}
			}
		} break;
		case FBXDocParser::MeshGeometry::MapType::polygon: {
			if (p_mapping_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::direct) {
				// The data are mapped per polygon directly.
				const int polygon_count = count_polygons(p_mesh_indices);
				ERR_FAIL_COND_V_MSG(polygon_count != (int)p_mapping_data.data.size(), (HashMap<int, R>()), "FBX file seems corrupted: #ERR12");

				// Advance each polygon vertex, each new polygon advance the polygon index.
				int polygon_index = -1;
				for (size_t polygon_vertex_index = 0;
						polygon_vertex_index < p_mesh_indices.size();
						polygon_vertex_index += 1) {
					if (is_start_of_polygon(p_mesh_indices, polygon_vertex_index)) {
						polygon_index += 1;
						ERR_FAIL_INDEX_V_MSG(polygon_index, (int)p_mapping_data.data.size(), (HashMap<int, R>()), "FBX file seems corrupted: #ERR13");
					}

					const int vertex_index = get_vertex_from_polygon_vertex(p_mesh_indices, polygon_vertex_index);
					ERR_FAIL_INDEX_V_MSG(vertex_index, p_vertex_count, (HashMap<int, R>()), "FBX file corrupted: #ERR14");

					aggregate_vertex_data[vertex_index].push_back({ polygon_index, p_mapping_data.data[polygon_index] });
				}
				ERR_FAIL_COND_V_MSG((polygon_index + 1) != polygon_count, (HashMap<int, R>()), "FBX file seems corrupted: #ERR16. Not all Polygons are present in the file.");
			} else {
				// The data is mapped per polygon using a reference.
				// The indices array, contains a *reference_id for each polygon.
				// * Note that the reference_id is the id of data into the data array.
				//
				// https://help.autodesk.com/view/FBX/2017/ENU/?guid=__cpp_ref_class_fbx_layer_element_html
				const int polygon_count = count_polygons(p_mesh_indices);
				ERR_FAIL_COND_V_MSG(polygon_count != (int)p_mapping_data.index.size(), (HashMap<int, R>()), "FBX file seems corrupted: #ERR17");

				// Advance each polygon vertex, each new polygon advance the polygon index.
				int polygon_index = -1;
				for (size_t polygon_vertex_index = 0;
						polygon_vertex_index < p_mesh_indices.size();
						polygon_vertex_index += 1) {
					if (is_start_of_polygon(p_mesh_indices, polygon_vertex_index)) {
						polygon_index += 1;
						ERR_FAIL_INDEX_V_MSG(polygon_index, (int)p_mapping_data.index.size(), (HashMap<int, R>()), "FBX file seems corrupted: #ERR18");
						ERR_FAIL_INDEX_V_MSG(p_mapping_data.index[polygon_index], (int)p_mapping_data.data.size(), (HashMap<int, R>()), "FBX file seems corrupted: #ERR19");
					}

					const int vertex_index = get_vertex_from_polygon_vertex(p_mesh_indices, polygon_vertex_index);
					ERR_FAIL_INDEX_V_MSG(vertex_index, p_vertex_count, (HashMap<int, R>()), "FBX file corrupted: #ERR20");

					aggregate_vertex_data[vertex_index].push_back({ polygon_index, p_mapping_data.data[p_mapping_data.index[polygon_index]] });
				}
				ERR_FAIL_COND_V_MSG((polygon_index + 1) != polygon_count, (HashMap<int, R>()), "FBX file seems corrupted: #ERR22. Not all Polygons are present in the file.");
			}
		} break;
		case FBXDocParser::MeshGeometry::MapType::edge: {
			if (p_mapping_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::direct) {
				// The data are mapped per edge directly.
				ERR_FAIL_COND_V_MSG(p_edge_map.size() != p_mapping_data.data.size(), (HashMap<int, R>()), "FBX file seems corrupted: #ERR23");
				for (size_t edge_index = 0; edge_index < p_mapping_data.data.size(); edge_index += 1) {
					const FBXDocParser::MeshGeometry::Edge edge = FBXDocParser::MeshGeometry::get_edge(p_edge_map, edge_index);
					ERR_FAIL_INDEX_V_MSG(edge.vertex_0, p_vertex_count, (HashMap<int, R>()), "FBX file corrupted: #ERR24");
					ERR_FAIL_INDEX_V_MSG(edge.vertex_1, p_vertex_count, (HashMap<int, R>()), "FBX file corrupted: #ERR25");
					ERR_FAIL_INDEX_V_MSG(edge.vertex_0, (int)p_mapping_data.data.size(), (HashMap<int, R>()), "FBX file corrupted: #ERR26");
					ERR_FAIL_INDEX_V_MSG(edge.vertex_1, (int)p_mapping_data.data.size(), (HashMap<int, R>()), "FBX file corrupted: #ERR27");
					aggregate_vertex_data[edge.vertex_0].push_back({ -1, p_mapping_data.data[edge_index] });
					aggregate_vertex_data[edge.vertex_1].push_back({ -1, p_mapping_data.data[edge_index] });
				}
			} else {
				// The data is mapped per edge using a reference.
				// The indices array, contains a *reference_id for each polygon.
				// * Note that the reference_id is the id of data into the data array.
				//
				// https://help.autodesk.com/view/FBX/2017/ENU/?guid=__cpp_ref_class_fbx_layer_element_html
				ERR_FAIL_COND_V_MSG(p_edge_map.size() != p_mapping_data.index.size(), (HashMap<int, R>()), "FBX file seems corrupted: #ERR28");
				for (size_t edge_index = 0; edge_index < p_mapping_data.data.size(); edge_index += 1) {
					const FBXDocParser::MeshGeometry::Edge edge = FBXDocParser::MeshGeometry::get_edge(p_edge_map, edge_index);
					ERR_FAIL_INDEX_V_MSG(edge.vertex_0, p_vertex_count, (HashMap<int, R>()), "FBX file corrupted: #ERR29");
					ERR_FAIL_INDEX_V_MSG(edge.vertex_1, p_vertex_count, (HashMap<int, R>()), "FBX file corrupted: #ERR30");
					ERR_FAIL_INDEX_V_MSG(edge.vertex_0, (int)p_mapping_data.index.size(), (HashMap<int, R>()), "FBX file corrupted: #ERR31");
					ERR_FAIL_INDEX_V_MSG(edge.vertex_1, (int)p_mapping_data.index.size(), (HashMap<int, R>()), "FBX file corrupted: #ERR32");
					ERR_FAIL_INDEX_V_MSG(p_mapping_data.index[edge.vertex_0], (int)p_mapping_data.data.size(), (HashMap<int, R>()), "FBX file corrupted: #ERR33");
					ERR_FAIL_INDEX_V_MSG(p_mapping_data.index[edge.vertex_1], (int)p_mapping_data.data.size(), (HashMap<int, R>()), "FBX file corrupted: #ERR34");
					aggregate_vertex_data[edge.vertex_0].push_back({ -1, p_mapping_data.data[p_mapping_data.index[edge_index]] });
					aggregate_vertex_data[edge.vertex_1].push_back({ -1, p_mapping_data.data[p_mapping_data.index[edge_index]] });
				}
			}
		} break;
		case FBXDocParser::MeshGeometry::MapType::all_the_same: {
			// No matter the mode, no matter the data size; The first always win
			// and is set to all the vertices.
			ERR_FAIL_COND_V_MSG(p_mapping_data.data.size() <= 0, (HashMap<int, R>()), "FBX file seems corrupted: #ERR35");
			if (p_mapping_data.data.size() > 0) {
				for (int vertex_index = 0; vertex_index < p_vertex_count; vertex_index += 1) {
					aggregate_vertex_data[vertex_index].push_back({ -1, p_mapping_data.data[0] });
				}
			}
		} break;
	}

	if (aggregate_vertex_data.size() == 0) {
		return (HashMap<int, R>());
	}

	// A map is used because turns out that the some FBX file are not well organized
	// with vertices well compacted. Using a map allows avoid those issues.
	HashMap<Vertex, R> result;

	// Aggregate the collected data.
	for (const Vertex *index = aggregate_vertex_data.next(nullptr); index != nullptr; index = aggregate_vertex_data.next(index)) {
		Vector<VertexData<T>> *aggregated_vertex = aggregate_vertex_data.getptr(*index);
		// This can't be null because we are just iterating.
		CRASH_COND(aggregated_vertex == nullptr);

		ERR_FAIL_INDEX_V_MSG(0, aggregated_vertex->size(), (HashMap<int, R>()), "The FBX file is corrupted, No valid data for this vertex index.");
		result[*index] = collector_function(aggregated_vertex, p_fall_back);
	}

	// Sanitize the data now, if the file is broken we can try import it anyway.
	bool problem_found = false;
	for (size_t i = 0; i < p_mesh_indices.size(); i += 1) {
		const Vertex vertex = get_vertex_from_polygon_vertex(p_mesh_indices, i);
		if (result.has(vertex) == false) {
			result[vertex] = p_fall_back;
			problem_found = true;
		}
	}
	if (problem_found) {
		WARN_PRINT("Some data is missing, this FBX file may be corrupted: #WARN0.");
	}

	return result;
}

template <class T>
HashMap<int, T> FBXMeshData::extract_per_polygon(
		int p_vertex_count,
		const std::vector<int> &p_polygon_indices,
		const FBXDocParser::MeshGeometry::MappingData<T> &p_fbx_data,
		T p_fallback_value) const {
	ERR_FAIL_COND_V_MSG(p_fbx_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::index_to_direct && p_fbx_data.data.size() == 0, (HashMap<int, T>()), "invalid index to direct array");
	ERR_FAIL_COND_V_MSG(p_fbx_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::index && p_fbx_data.index.size() == 0, (HashMap<int, T>()), "The FBX seems corrupted");

	const int polygon_count = count_polygons(p_polygon_indices);

	// Aggregate vertex data.
	HashMap<int, Vector<T>> aggregate_polygon_data;

	switch (p_fbx_data.map_type) {
		case FBXDocParser::MeshGeometry::MapType::none: {
			// No data nothing to do.
			return (HashMap<int, T>());
		}
		case FBXDocParser::MeshGeometry::MapType::vertex: {
			ERR_FAIL_V_MSG((HashMap<int, T>()), "This data can't be extracted and organized per polygon, since into the FBX is mapped per vertex. This should not happen.");
		} break;
		case FBXDocParser::MeshGeometry::MapType::polygon_vertex: {
			ERR_FAIL_V_MSG((HashMap<int, T>()), "This data can't be extracted and organized per polygon, since into the FBX is mapped per polygon vertex. This should not happen.");
		} break;
		case FBXDocParser::MeshGeometry::MapType::polygon: {
			if (p_fbx_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::index_to_direct) {
				// The data is stored efficiently index_to_direct allows less data in the FBX file.
				for (int polygon_index = 0;
						polygon_index < polygon_count;
						polygon_index += 1) {
					if (p_fbx_data.index.size() == 0) {
						ERR_FAIL_INDEX_V_MSG(polygon_index, (int)p_fbx_data.data.size(), (HashMap<int, T>()), "FBX file is corrupted: #ERR62");
						aggregate_polygon_data[polygon_index].push_back(p_fbx_data.data[polygon_index]);
					} else {
						ERR_FAIL_INDEX_V_MSG(polygon_index, (int)p_fbx_data.index.size(), (HashMap<int, T>()), "FBX file is corrupted: #ERR62");

						const int index_to_direct = p_fbx_data.index[polygon_index];
						T value = p_fbx_data.data[index_to_direct];
						aggregate_polygon_data[polygon_index].push_back(value);
					}
				}
			} else if (p_fbx_data.ref_type == FBXDocParser::MeshGeometry::ReferenceType::direct) {
				// The data are mapped per polygon directly.
				ERR_FAIL_COND_V_MSG(polygon_count != (int)p_fbx_data.data.size(), (HashMap<int, T>()), "FBX file is corrupted: #ERR51");

				// Advance each polygon vertex, each new polygon advance the polygon index.
				for (int polygon_index = 0;
						polygon_index < polygon_count;
						polygon_index += 1) {
					ERR_FAIL_INDEX_V_MSG(polygon_index, (int)p_fbx_data.data.size(), (HashMap<int, T>()), "FBX file is corrupted: #ERR52");
					aggregate_polygon_data[polygon_index].push_back(p_fbx_data.data[polygon_index]);
				}
			} else {
				// The data is mapped per polygon using a reference.
				// The indices array, contains a *reference_id for each polygon.
				// * Note that the reference_id is the id of data into the data array.
				//
				// https://help.autodesk.com/view/FBX/2017/ENU/?guid=__cpp_ref_class_fbx_layer_element_html
				ERR_FAIL_COND_V_MSG(polygon_count != (int)p_fbx_data.index.size(), (HashMap<int, T>()), "FBX file seems corrupted: #ERR52");

				// Advance each polygon vertex, each new polygon advance the polygon index.
				for (int polygon_index = 0;
						polygon_index < polygon_count;
						polygon_index += 1) {
					ERR_FAIL_INDEX_V_MSG(polygon_index, (int)p_fbx_data.index.size(), (HashMap<int, T>()), "FBX file is corrupted: #ERR53");
					ERR_FAIL_INDEX_V_MSG(p_fbx_data.index[polygon_index], (int)p_fbx_data.data.size(), (HashMap<int, T>()), "FBX file is corrupted: #ERR54");
					aggregate_polygon_data[polygon_index].push_back(p_fbx_data.data[p_fbx_data.index[polygon_index]]);
				}
			}
		} break;
		case FBXDocParser::MeshGeometry::MapType::edge: {
			ERR_FAIL_V_MSG((HashMap<int, T>()), "This data can't be extracted and organized per polygon, since into the FBX is mapped per edge. This should not happen.");
		} break;
		case FBXDocParser::MeshGeometry::MapType::all_the_same: {
			// No matter the mode, no matter the data size; The first always win
			// and is set to all the vertices.
			ERR_FAIL_COND_V_MSG(p_fbx_data.data.size() <= 0, (HashMap<int, T>()), "FBX file seems corrupted: #ERR55");
			if (p_fbx_data.data.size() > 0) {
				for (int polygon_index = 0; polygon_index < polygon_count; polygon_index += 1) {
					aggregate_polygon_data[polygon_index].push_back(p_fbx_data.data[0]);
				}
			}
		} break;
	}

	if (aggregate_polygon_data.size() == 0) {
		return (HashMap<int, T>());
	}

	// A map is used because turns out that the some FBX file are not well organized
	// with vertices well compacted. Using a map allows avoid those issues.
	HashMap<int, T> polygons;

	// Take the first value for each vertex.
	for (const Vertex *index = aggregate_polygon_data.next(nullptr); index != nullptr; index = aggregate_polygon_data.next(index)) {
		Vector<T> *aggregated_polygon = aggregate_polygon_data.getptr(*index);
		// This can't be null because we are just iterating.
		CRASH_COND(aggregated_polygon == nullptr);

		ERR_FAIL_INDEX_V_MSG(0, (int)aggregated_polygon->size(), (HashMap<int, T>()), "The FBX file is corrupted, No valid data for this polygon index.");

		// Validate the final value.
		polygons[*index] = (*aggregated_polygon)[0];
	}

	// Sanitize the data now, if the file is broken we can try import it anyway.
	bool problem_found = false;
	for (int polygon_i = 0; polygon_i < polygon_count; polygon_i += 1) {
		if (polygons.has(polygon_i) == false) {
			polygons[polygon_i] = p_fallback_value;
			problem_found = true;
		}
	}
	if (problem_found) {
		WARN_PRINT("Some data is missing, this FBX file may be corrupted: #WARN1.");
	}

	return polygons;
}

void FBXMeshData::extract_morphs(const FBXDocParser::MeshGeometry *mesh_geometry, HashMap<String, MorphVertexData> &r_data) {
	r_data.clear();

	const int vertex_count = mesh_geometry->get_vertices().size();

	for (const FBXDocParser::BlendShape *blend_shape : mesh_geometry->get_blend_shapes()) {
		for (const FBXDocParser::BlendShapeChannel *blend_shape_channel : blend_shape->BlendShapeChannels()) {
			const std::vector<const FBXDocParser::ShapeGeometry *> &shape_geometries = blend_shape_channel->GetShapeGeometries();
			for (const FBXDocParser::ShapeGeometry *shape_geometry : shape_geometries) {
				String morph_name = ImportUtils::FBXAnimMeshName(shape_geometry->Name()).c_str();
				if (morph_name.is_empty()) {
					morph_name = "morph";
				}

				// TODO we have only these??
				const std::vector<unsigned int> &morphs_vertex_indices = shape_geometry->GetIndices();
				const std::vector<Vector3> &morphs_vertices = shape_geometry->GetVertices();
				const std::vector<Vector3> &morphs_normals = shape_geometry->GetNormals();

				ERR_FAIL_COND_MSG((int)morphs_vertex_indices.size() > vertex_count, "The FBX file is corrupted: #ERR103");
				ERR_FAIL_COND_MSG(morphs_vertex_indices.size() != morphs_vertices.size(), "The FBX file is corrupted: #ERR104");
				ERR_FAIL_COND_MSG((int)morphs_vertices.size() > vertex_count, "The FBX file is corrupted: #ERR105");
				ERR_FAIL_COND_MSG(morphs_normals.size() != 0 && morphs_normals.size() != morphs_vertices.size(), "The FBX file is corrupted: #ERR106");

				if (r_data.has(morph_name) == false) {
					// This morph doesn't exist yet.
					// Create it.
					MorphVertexData md;
					md.vertices.resize(vertex_count);
					md.normals.resize(vertex_count);
					r_data.set(morph_name, md);
				}

				MorphVertexData *data = r_data.getptr(morph_name);
				Vector3 *data_vertices_ptr = data->vertices.ptrw();
				Vector3 *data_normals_ptr = data->normals.ptrw();

				for (int i = 0; i < (int)morphs_vertex_indices.size(); i += 1) {
					const Vertex vertex = morphs_vertex_indices[i];

					ERR_FAIL_INDEX_MSG(vertex, vertex_count, "The blend shapes of this FBX file are corrupted. It has a not valid vertex.");

					data_vertices_ptr[vertex] = morphs_vertices[i];

					if (morphs_normals.size() != 0) {
						data_normals_ptr[vertex] = morphs_normals[i];
					}
				}
			}
		}
	}
}