-
Notifications
You must be signed in to change notification settings - Fork 0
/
BHDFile.cs
300 lines (287 loc) · 9.31 KB
/
BHDFile.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using SharpDX;
namespace LOMNTool
{
/// <summary>
/// A file that stores a model's skeleton.
/// </summary>
public class BHDFile
{
/// <summary>
/// The biped bone names as defined in the Legend of Mata Nui binary.
/// </summary>
public static string[] BipedBoneNames =
{
"Bip01",
"Bip01_Pelvis",
"Bip01_Spine",
"Bip01_Spine1",
"Bip01_Spine2",
"Bip01_Spine3",
"Bip01_R_Thigh",
"Bip01_L_Thigh",
"Bip01_R_Calf",
"Bip01_L_Calf",
"Bip01_R_Foot",
"Bip01_L_Foot",
"Bip01_R_Clavicle",
"Bip01_L_Clavicle",
"Bip01_R_UpperArm",
"Bip01_L_UpperArm",
"Bip01_R_Forearm",
"Bip01_L_Forearm",
"Bip01_R_Hand",
"Bip01_L_Hand",
"Bip01_Neck",
"Bip01_Neck1",
"Bip01_Head",
"Bip01_R_Finger0",
"Bip01_L_Finger0",
"Bip01_R_Finger1",
"Bip01_L_Finger1",
"Bip01_R_Finger2",
"Bip01_L_Finger2",
"Bip01_R_Finger3",
"Bip01_L_Finger3",
"Bip01_R_Finger4",
"Bip01_L_Finger4",
"Bip01_R_Finger01",
"Bip01_L_Finger01",
"Bip01_R_Finger11",
"Bip01_L_Finger11",
"Bip01_R_Finger21",
"Bip01_L_Finger21",
"Bip01_R_Finger31",
"Bip01_L_Finger31",
"Bip01_R_Finger41",
"Bip01_L_Finger41",
"Bip01_R_Finger02",
"Bip01_L_Finger02",
"Bip01_R_Finger12",
"Bip01_L_Finger12",
"Bip01_R_Finger22",
"Bip01_L_Finger22",
"Bip01_R_Finger32",
"Bip01_L_Finger32",
"Bip01_R_Finger42",
"Bip01_L_Finger42",
"Bip01_R_Toe0",
"Bip01_L_Toe0",
"Bip01_R_Toe1",
"Bip01_L_Toe1",
"Bip01_R_Toe2",
"Bip01_L_Toe2",
"Bip01_R_Toe01",
"Bip01_L_Toe01",
"Bip01_R_Toe11",
"Bip01_L_Toe11",
"Bip01_R_Toe21",
"Bip01_L_Toe21",
"Bip01_R_Toe3",
"Bip01_L_Toe3",
"Bip01_R_Toe31",
"Bip01_L_Toe31",
"Bip01_R_Toe4",
"Bip01_L_Toe4",
"Bip01_R_Toe41",
"Bip01_L_Toe41",
"MiscBone0",
"MiscBone1",
"MiscBone2",
"MiscBone3",
"MiscBone4",
"MiscBone5",
"MiscBone6",
"MiscBone7",
"MiscBone8",
"MiscBone9",
"MiscBone10",
"MiscBone11",
"MiscBone12",
"MiscBone13",
"MiscBone14",
"MiscBone15",
"MiscBone16",
"MiscBone17",
"MiscBone18",
"Bip01_Tail4",
"Bip01_Tail3",
"Bip01_Tail2",
"Bip01_Neck4",
"Bip01_Neck3",
"Bip01_Neck2",
"Bip01_Tail",
"Bip01_Tail1"
};
/// <summary>
/// The non-biped bone names as defined in the Legend of Mata Nui binary.
/// </summary>
public static string[] NonBipedBoneNames =
{
"root",
"Dummy01",
"Dummy02",
"Dummy03",
"Dummy04",
"Dummy05",
"Dummy06",
"Dummy07",
"Dummy08",
"Dummy09",
"Dummy10",
"Dummy11",
"Dummy12",
"Dummy13",
"Dummy14",
"Dummy15",
"Dummy16",
"Dummy17",
"Dummy18",
"Dummy19",
"Dummy20",
"Dummy21",
"Dummy22",
"Dummy23",
"Dummy24",
"Dummy25",
"Dummy26",
"Dummy27",
"Dummy28",
"Dummy29",
"Dummy30",
"Dummy31",
"Dummy32",
"Dummy33",
"Dummy34",
"Dummy35",
"Dummy36",
"Dummy37",
"Dummy38",
"Dummy39",
"Dummy40",
"Dummy41",
"Dummy42",
"Dummy43",
"Dummy44",
"Dummy45",
"Dummy46",
"Dummy47",
"Dummy48",
"Dummy49",
"Dummy50",
"Dummy51",
"Dummy52",
"Dummy53",
"Dummy54",
"Dummy55",
"Dummy56",
"Dummy57",
"Dummy58",
"Dummy59"
};
/// <summary>
/// A bone in the skeleton.
/// </summary>
public class Bone
{
/// <summary>
/// Which bone this is in the file, and also the index into the name arrays (<see cref="BipedBoneNames"/> and <see cref="NonBipedBoneNames"/>)
/// </summary>
public uint Index;
/// <summary>
/// The index of the parent bone (or equal to <see cref="Index"/> if this bone is a root bone, or 0xFFFFFFFF if the name corresponding to <see cref="Index"/> isn't used in the skeleton.)
/// </summary>
public uint ParentIndex;
/// <summary>
/// The transform of this Bone relative to its parent Bone.
/// </summary>
public Matrix Transform;
/// <summary>
/// A List of all the bones that had this Bone's <see cref="Index"/> as their <see cref="ParentIndex"/> when the <see cref="BHDFile"/> was loaded.
/// </summary>
public List<Bone> Children = new List<Bone>(); // Not stored in the file, just computed to be helpful.
}
/// <summary>
/// The <see cref="Bone"/>s in this BHDFile.
/// </summary>
public List<Bone> Bones = new List<Bone>();
/// <summary>
/// The possible names of the bones in this BHDFile.
/// </summary>
public string[] NameSlots = null;
/// <summary>
/// Creates an empty BHDFile.
/// </summary>
public BHDFile()
{
}
/// <summary>
/// Creates a BHDFile by reading the data from the given file.
/// </summary>
/// <param name="filename">The name of the file that data should be read from.</param>
public BHDFile(string filename)
{
using (FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
using (BinaryReader reader = new BinaryReader(stream))
{
uint count = reader.ReadUInt32();
for (uint i = 0; i < count; i++)
{
Bone b = new Bone();
b.ParentIndex = reader.ReadUInt32();
b.Index = i;
b.Transform = Matrix.Identity;
Bones.Add(b);
}
for (int i = 0; i < count; i++)
{
Bone b = Bones[i];
if (b.ParentIndex != 0xFFFFFFFF && b.ParentIndex != i)
{
Bones[(int)b.ParentIndex].Children.Add(b);
}
b.Transform.Column1 = new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), 0.0f);
b.Transform.Column2 = new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), 0.0f);
b.Transform.Column3 = new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), 0.0f);
b.Transform.Column4 = new Vector4(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle(), 1.0f);
}
}
}
/// <summary>
/// Writes this BHDFile to the given file.
/// </summary>
/// <param name="filename">The filename to write to.</param>
public void Write(string filename)
{
using (FileStream stream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Read))
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write((uint)Bones.Count);
foreach (Bone b in Bones)
{
writer.Write(b.ParentIndex);
}
foreach (Bone b in Bones)
{
writer.Write(b.Transform.Column1.X);
writer.Write(b.Transform.Column1.Y);
writer.Write(b.Transform.Column1.Z);
writer.Write(b.Transform.Column2.X);
writer.Write(b.Transform.Column2.Y);
writer.Write(b.Transform.Column2.Z);
writer.Write(b.Transform.Column3.X);
writer.Write(b.Transform.Column3.Y);
writer.Write(b.Transform.Column3.Z);
writer.Write(b.Transform.Column4.X);
writer.Write(b.Transform.Column4.Y);
writer.Write(b.Transform.Column4.Z);
}
}
}
}
}