Skip to content

Commit d589f6c

Browse files
committed
Merge branch 'master' of https://github.com/FabianFG/CUE4Parse
2 parents 7b7e3d8 + cce937b commit d589f6c

File tree

4 files changed

+335
-189
lines changed

4 files changed

+335
-189
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using CUE4Parse.UE4.Objects.Core.Math;
4+
using CUE4Parse.UE4.Readers;
5+
6+
namespace CUE4Parse.GameTypes.FF7.Assets.Objects;
7+
8+
public static class FF7FStaticMeshLODResources
9+
{
10+
public static bool SerializeIndexBuffer(FArchive Ar, out int[] sectionTrianglesCount, out uint[] indexBuffer)
11+
{
12+
indexBuffer = [];
13+
sectionTrianglesCount = [];
14+
var version = Ar.Read<int>();
15+
var tempAr = new FByteArchive("FF7Rebirth_SM", Ar.ReadArray<byte>(), Ar.Versions);
16+
tempAr.Position += 28; // skip header, idk what that data is
17+
var batches = tempAr.ReadArray<FF7Batch>();
18+
if (batches.Length == 0) return false;
19+
20+
var indicesBuffer = tempAr.ReadArray<uint>();
21+
var batchesBuffer = tempAr.ReadArray<uint>();
22+
var batchInfos = tempAr.ReadArray(() => new FF7BatchInfo(tempAr));
23+
24+
var lods = tempAr.ReadArray(() => new FF7Lod(tempAr));
25+
var batchesIndices = tempAr.ReadArray<int>(); // batches reordering ???
26+
var somestructs = tempAr.ReadArray(() => new Ff7SomeStruct(tempAr));
27+
28+
var index = tempAr.Read<int>(); // always 0
29+
var sectionsIndices = tempAr.ReadArray<uint>();
30+
var lodinfos = tempAr.ReadArray(() => new FF7LodInfo(tempAr));
31+
32+
var ib = new List<uint>(batchesBuffer.Length * 3);
33+
foreach (var batch in batches)
34+
{
35+
var vertices = new HashSet<uint>();
36+
var startIndex = batch.TotalVertices;
37+
var triangleStartIndex = batch.TotatTriangles;
38+
for (var i = 0; i < batch.TrianglesCount; i++)
39+
{
40+
uint x = batchesBuffer[triangleStartIndex+i];
41+
var iv = indicesBuffer[startIndex + (x & 0x3ff)];
42+
var jv = indicesBuffer[startIndex + ((x >> 10) & 0x3ff)];
43+
var kv = indicesBuffer[startIndex + ((x >> 20) & 0x3ff)];
44+
ib.Add(iv);
45+
ib.Add(jv);
46+
ib.Add(kv);
47+
vertices.Add(iv);
48+
vertices.Add(jv);
49+
vertices.Add(kv);
50+
}
51+
}
52+
var fullIndexBuffer = ib.ToArray();
53+
54+
List<uint> indexbuffer = [];
55+
sectionTrianglesCount = new int[sectionsIndices.Length];
56+
for (int i = 0; i < sectionsIndices.Length; i++)
57+
{
58+
var section = lodinfos[sectionsIndices[i]];
59+
sectionTrianglesCount[i] = section.BatchesCount;
60+
indexbuffer.AddRange(fullIndexBuffer.AsSpan()[(section.BatchesOffset * 3)..((section.BatchesOffset + section.BatchesCount) * 3)]);
61+
}
62+
63+
var sections1 = tempAr.ReadArray<uint>();
64+
var lodInfos1 = tempAr.ReadArray(() => new FF7LodInfo(tempAr));
65+
var anotherBuffer = tempAr.ReadArray<uint>();
66+
67+
tempAr.Dispose();
68+
indexBuffer = indexbuffer.ToArray();
69+
return true;
70+
}
71+
}
72+
73+
public struct FF7Batch
74+
{
75+
public int VerticesCount;
76+
public int TotalVertices;
77+
public int TrianglesCount;
78+
public int TotatTriangles;
79+
};
80+
81+
public struct FF7BatchInfo(FArchive Ar)
82+
{
83+
public FVector4 Position1 = Ar.Read<FVector4>();
84+
public FVector4 Position2 = Ar.Read<FVector4>();
85+
public FVector SomeVector1= Ar.Read<FHalfVector>();
86+
public FVector SomeVector2= Ar.Read<FHalfVector>();
87+
public float Distance = Ar.Read<float>();
88+
public FVector MinVertexPosition = Ar.Read<FVector>();
89+
public float SomeFloat = Ar.Read<float>();
90+
public FVector MaxVertexPosition = Ar.Read<FVector>();
91+
public uint Hash = Ar.Read<uint>();
92+
}
93+
94+
public struct Ff7SomeStruct(FArchive Ar)
95+
{
96+
public FVector4[][] Vectors = Ar.ReadArray(3, () => Ar.ReadArray<FVector4>(8));
97+
public int[] Flags = Ar.ReadArray<int>(8);
98+
}
99+
100+
public struct FF7Lod(FArchive Ar)
101+
{
102+
public int Offset = Ar.Read<int>();
103+
public int Count = Ar.Read<int>();
104+
public int NextOffset = Ar.Read<int>();
105+
public int NextCount = Ar.Read<int>();
106+
107+
public FVector4 Position1 = Ar.Read<FVector4>();
108+
public FVector4 Position2 = Ar.Read<FVector4>();
109+
public FVector4 Position3 = Ar.Read<FVector4>();
110+
}
111+
112+
public struct FF7LodInfo(FArchive Ar)
113+
{
114+
public int Index = Ar.Read<int>();
115+
public int idk = Ar.Read<int>();
116+
public int Offset = Ar.Read<int>();
117+
public int Count = Ar.Read<int>();
118+
public int IndicesOffset = Ar.Read<int>();
119+
public int IndicesCount = Ar.Read<int>();
120+
public int BatchesOffset = Ar.Read<int>();
121+
public int BatchesCount = Ar.Read<int>();
122+
public int VerticesOffset = Ar.Read<int>();
123+
public int VerticesCount = Ar.Read<int>();
124+
public ushort[] SomeInts1 = Ar.ReadArray<ushort>(4);
125+
public float[] SomeFloats = Ar.ReadArray<float>(5);
126+
public ushort[] SomeInts2 = Ar.ReadArray<ushort>(14);
127+
}

0 commit comments

Comments
 (0)