-
Notifications
You must be signed in to change notification settings - Fork 6
/
Collision.cs
222 lines (173 loc) · 7.5 KB
/
Collision.cs
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
using Assimp;
using Assimp.Configs;
namespace Marathon.Formats.Mesh
{
/// <summary>
/// File base for the collision.bin format.
/// <para>Used in SONIC THE HEDGEHOG for collision meshes for terrain.</para>
/// </summary>
public class Collision : FileBase
{
public Collision() { }
public Collision(string file, bool serialise = false)
{
switch (Path.GetExtension(file))
{
case ".obj":
case ".fbx":
case ".dae":
{
ImportAssimp(file);
// Save extension-less OBJ (exploiting .NET weirdness, because it doesn't omit all extensions).
if (serialise)
Save(Path.GetFileNameWithoutExtension(file));
break;
}
default:
{
Load(file);
if (serialise)
ExportOBJ($"{Location}.obj");
break;
}
}
}
public override string Extension { get; } = ".bin";
public class FormatData
{
public List<Vector3> Vertices = new();
public List<CollisionFace> Faces = new();
}
public FormatData Data { get; set; } = new();
public override void Load(Stream stream)
{
BINAReader reader = new(stream);
uint unknownUInt32_1 = reader.ReadUInt32(); // TODO: Unknown, is apparently an offset?
uint moppCodeOffset = reader.ReadUInt32(); // Offset to this file's Havok MOPP Code, which '06 doesn't seem to use?
uint vertexTableOffset = reader.ReadUInt32(); // Offset to this file's Vertex Table.
uint faceTableOffset = reader.ReadUInt32(); // Offset to this file's Face Table.
reader.JumpTo(vertexTableOffset, true); // Should already be here but just to be safe.
uint vertexCount = reader.ReadUInt32(); // Get the amount of Vertices in this file.
// Read the Vertices.
for (int i = 0; i < vertexCount; i++)
Data.Vertices.Add(reader.ReadVector3());
reader.JumpTo(faceTableOffset, true); // Should already be here but just to be safe.
uint faceCount = reader.ReadUInt32(); // Get the amount of Faces in this file.
// Read the Faces.
for (int i = 0; i < faceCount; i++)
{
CollisionFace face = new()
{
VertexA = reader.ReadUInt16(),
VertexB = reader.ReadUInt16(),
VertexC = reader.ReadUInt16()
};
reader.FixPadding();
face.Flags = reader.ReadUInt32(); // TODO: Experiment with flags to see what they all do.
Data.Faces.Add(face);
}
}
public override void Save(Stream stream)
{
BINAWriter writer = new(stream);
writer.AddOffset("unknownUInt32_1");
writer.WriteNulls(4); // Ignore MOPP code offset.
writer.FillOffset("unknownUInt32_1", true);
writer.AddOffset("vertexCountOffset");
writer.AddOffset("faceCountOffset");
writer.FillOffset("vertexCountOffset", true);
writer.Write(Data.Vertices.Count);
for (int i = 0; i < Data.Vertices.Count; i++)
writer.Write(Data.Vertices[i]);
writer.FillOffset("faceCountOffset", true);
writer.Write(Data.Faces.Count);
for (int i = 0; i < Data.Faces.Count; i++)
{
writer.Write(Data.Faces[i].VertexA);
writer.Write(Data.Faces[i].VertexB);
writer.Write(Data.Faces[i].VertexC);
writer.FixPadding();
writer.Write(Data.Faces[i].Flags);
}
writer.FinishWrite();
}
public void ExportOBJ(string filePath)
{
// Create a SteamWriter.
StreamWriter obj = new(filePath);
// Vertices.
foreach (Vector3 vertex in Data.Vertices)
obj.WriteLine($"v {vertex.X} {vertex.Y} {vertex.Z}");
// Faces.
// Get all used flags.
List<uint> Flags = new();
foreach (CollisionFace face in Data.Faces)
{
if (!Flags.Contains(face.Flags))
Flags.Add(face.Flags);
}
// Write Faces.
foreach (uint Flag in Flags)
{
obj.WriteLine($"\ng {Flag.ToString("x").PadLeft(8, '0')}");
foreach (CollisionFace face in Data.Faces)
{
if (face.Flags == Flag)
obj.WriteLine($"f {face.VertexA + 1} {face.VertexB + 1} {face.VertexC + 1}");
}
}
// Close the StreamWriter.
obj.Close();
}
public void ImportAssimp(string filePath)
{
// Setup AssimpNet Scene.
AssimpContext assimpImporter = new();
KeepSceneHierarchyConfig config = new(true);
assimpImporter.SetConfig(config);
Scene assimpModel = assimpImporter.ImportFile(filePath, PostProcessSteps.PreTransformVertices);
// Loop through all meshes in the imported file.
foreach (Assimp.Mesh assimpMesh in assimpModel.Meshes)
{
// Mesh Tag.
uint meshNameTag = 0u;
// Assimp seems to attach Mesh to end of DAE imports, lets check for that instead.
// Also seems to have a habit of adding an _ if the name is a number, so factor that in too...
string name = assimpMesh.Name;
if (name.EndsWith("Mesh"))
name = name.Remove(name.LastIndexOf('M'));
if (name.StartsWith('_'))
name = name.Substring(1);
// Try read an @ sign for backwards compatibility.
if (name.Contains("@"))
try { meshNameTag = (uint)Convert.ToInt32(name.Substring(name.LastIndexOf('@') + 1), 16); } catch { }
// Try read the mesh name as the tag instead, leave it at 0 if it's not valid.
else
try { meshNameTag = (uint)Convert.ToInt32(name, 16); } catch { }
// Faces.
foreach (Face assimpFace in assimpMesh.Faces)
{
CollisionFace face = new()
{
VertexA = (ushort)(assimpFace.Indices[0] + Data.Vertices.Count),
VertexB = (ushort)(assimpFace.Indices[1] + Data.Vertices.Count),
VertexC = (ushort)(assimpFace.Indices[2] + Data.Vertices.Count),
Flags = meshNameTag
};
Data.Faces.Add(face);
}
// Vertices.
foreach (Vector3D assimpVertex in assimpMesh.Vertices)
Data.Vertices.Add(new Vector3(assimpVertex.X, assimpVertex.Y, assimpVertex.Z));
}
}
}
public class CollisionFace
{
public ushort VertexA { get; set; }
public ushort VertexB { get; set; }
public ushort VertexC { get; set; }
public uint Flags { get; set; }
public override string ToString() => $"<{VertexA}, {VertexB}, {VertexC}> @{Flags.ToString("X").PadLeft(8, '0')}";
}
}