Skip to content

Commit 2bcb964

Browse files
Greatly speed up large model loading times
1 parent 1090b81 commit 2bcb964

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

xivModdingFramework/Models/Helpers/ModelModifiers.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,6 @@ public static void MergeGeometryData(TTModel ttModel, XivMdl rawMdl, Action<bool
421421

422422
for (var pi = 0; pi < totalParts; pi++)
423423
{
424-
425424
var ttPart = new TTMeshPart();
426425
ttMesh.Parts.Add(ttPart);
427426
ttPart.Name = "Part " + partIdx;
@@ -431,24 +430,21 @@ public static void MergeGeometryData(TTModel ttModel, XivMdl rawMdl, Action<bool
431430
var indexStart = fakePart == false ? basePart.IndexOffset - baseMesh.MeshInfo.IndexDataOffset : 0;
432431
var indexCount = fakePart == false ? basePart.IndexCount : baseMesh.MeshInfo.IndexCount;
433432

434-
var indices = baseMesh.VertexData.Indices.GetRange(indexStart, indexCount);
433+
var indices = baseMesh.VertexData.Indices.Skip(indexStart).Take(indexCount);
435434

436435
// Get the Vertices unique to this part.
437-
var uniqueVertexIdSet = new SortedSet<int>(indices); // Maximum possible amount is # of indices, though likely it is less.
438-
439-
foreach (var ind in indices)
440-
{
441-
uniqueVertexIdSet.Add(ind);
442-
}
436+
var uniqueVertexIdSet = new HashSet<int>(indices);
443437

444438
// Need it as a list to have index access to it.
445-
var uniqueVertexIds = uniqueVertexIdSet.ToList();
439+
var uniqueVertexIds = new List<int>(uniqueVertexIdSet);
440+
uniqueVertexIds.Sort();
446441

447442
// Maps old vertex ID to new vertex ID.
448-
var vertDict = new Dictionary<int, int>(uniqueVertexIds.Count);
443+
var vertMap = new int[uniqueVertexIds.Max() + 1];
449444

450445
// Now we need to loop through, copy over the vertex data, keeping track of the new vertex IDs.
451446
ttPart.Vertices = new List<TTVertex>(uniqueVertexIds.Count);
447+
452448
for (var i = 0; i < uniqueVertexIds.Count; i++)
453449
{
454450
var oldVertexId = uniqueVertexIds[i];
@@ -556,14 +552,14 @@ public static void MergeGeometryData(TTModel ttModel, XivMdl rawMdl, Action<bool
556552
}
557553

558554
ttPart.Vertices.Add(ttVert);
559-
vertDict.Add(oldVertexId, ttPart.Vertices.Count - 1);
555+
vertMap[oldVertexId] = ttPart.Vertices.Count - 1;
560556
}
561557

562558
// Now we need to copy in the triangle indices, pointing to the new, part-level vertex IDs.
563-
ttPart.TriangleIndices = new List<int>(indices.Count);
559+
ttPart.TriangleIndices = new List<int>(indexCount);
564560
foreach (var oldVertexId in indices)
565561
{
566-
ttPart.TriangleIndices.Add(vertDict[oldVertexId]);
562+
ttPart.TriangleIndices.Add(vertMap[oldVertexId]);
567563
}
568564

569565
// Ok, gucci now.

0 commit comments

Comments
 (0)