-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfxm_keypose.cs
534 lines (390 loc) · 18.5 KB
/
fxm_keypose.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
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
//жжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжж
using System;using System.IO;using System.Linq;using System.Text;using System.Collections;using System.Collections.Generic;using System.Numerics;
//жжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжжж
sealed class fxm_2_dx
{
static void Main()
{
int v1, v2, v3; // грани
float x, y, z; // координаты точки
float w1, w2, w3, w4; // веса
float c1, c2, c3, c4; // индексы/цвет argb/rgba
int bc1, bc2, bc3, bc4;
int bc5, bc6, bc7, bc8;
int bc9, bc10, bc11, bc12;
int bc13, bc14, bc15, bc16;
float vn1, vn2, vn3; // нормали
float u, v; // развёртка
int vertex_count; // количество вершин
int faces__count; // количество граней
List<string> face_list = new List<string>();
List<string> vert_list = new List<string>();
List<string> norm_list = new List<string>();
List<string> uvst_list = new List<string>();
List<VertexTypePNT> vertex_PNT_List = new List<VertexTypePNT>();
List<VertexTypePWCNT> vertex_PWCNT_List = new List<VertexTypePWCNT>();
// точки вместо запятых // хотя для obj это не важно вроде бы // как и табы вместо пробелов
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
// ищем все fmx файлы в папках и подпапках
string[] allFilesName = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.fxm", SearchOption.AllDirectories) ;
// для каждого fmx файла
foreach (var fxmName in allFilesName)
{
// открыли *.fxm файл на чтение
using (BinaryReader br = new BinaryReader(File.Open(fxmName, FileMode.Open)))
{
// TODO // получается слишком много файлов для саб-мешей
// может быть лучше сделать для каждого из них свой каталог и переместить туда нужные текстуры?
// открыли *.x на запись
using (StreamWriter sw = new StreamWriter(Path.GetDirectoryName(fxmName) + "/" + Path.GetFileNameWithoutExtension(fxmName) + ".x"))
{ // Directory.CreateDirectory(Path.GetDirectoryName(fxmName) + "\\" + Path.GetFileNameWithoutExtension(fxmName));
/////////////////////////////////////////////////////////////////////////////////////////
sw.WriteLine(
@"xof 0303txt 0032
");
/////////////////////////////////////////////////////////////////////////////////////////
// Mesh mesh = new Mesh();
/////////////////////////////////////////////////////////////////////////////////////////
// версия модели
br.ReadInt32();
br.ReadInt32();
int frame_count = br.ReadInt32(); // количество "костей"
Console.WriteLine("\n" + frame_count + "\n");
Node[] Frames = new Node[frame_count]; // лист фреймов
Matrix4x4 matrix1111 = new Matrix4x4
(
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
);
// инициализируем их поля null-значениями
for (int subm = 0; subm < frame_count ; subm++)
{
Frames[subm] = new Node(null, matrix1111, null);
}
/////////////////////////////////////////////////////////////////////////////////////////
for (int subm = 0; subm < frame_count ; subm++)
{
// разделитель между всеми костями
// 00 00 00 00 00 00 00 00
br.ReadInt32(); br.ReadInt32();
// читаем имя фрейма
int frame_name_length = br.ReadInt32(); // читаем число символов в строке имени jointa
byte[] frameNameHex = new byte[frame_name_length]; // создаём массив байт под них
br.Read(frameNameHex, 0, frame_name_length); // читаем байты в массив
string frame_name = Encoding.Default.GetString(frameNameHex);
// читаем номер родительской кости
int parent_joint_number = br.ReadInt32();
if (parent_joint_number == -1)
parent_joint_number = 0; // Bip01 "ссылается" сам на себя
// читаем матрицу трансформации
float[] a = new float[16]; for (int i = 0; i < 16 ; i++) a[i] = br.ReadSingle();
Matrix4x4 matrix = new Matrix4x4(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7],
a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15] );
// создаём Фрейм на основе считанных данных
Frames[subm] = new Node(frame_name, matrix, Frames[parent_joint_number]);
// запослняем список дочерних костей
for (int i = 0; i < frame_count; i++) // сравниваем каждый фрейм с каждым
for (int h = 0; h < frame_count; h++) // для проверки наличия родителя
if (Frames[i].Parent == Frames[h] && (!Frames[h].Child.Contains(Frames[i])))
// если поле.Parent.ФреймаX ссылается на ссылку_хранящуюся_в_массиве
// т.е. если поле имеет такого родителя и такого дитя ещё не содержится в списке дитей
Frames[h].Child.Add(Frames[i]); // то добавляем [его] в список детей его родителя
}
// а как их вывести в х-файл?... сложно
/////////////////////////////////////////////////////////////////////////////////////////
for (int sub = 0; sub < 11 ; sub++) // пропускаем 44 нулевых байт
{
br.ReadInt32(); // 00 00 00 00
}
/////////////////////////////////////////////////////////////////////////////////////////
int submesh__count = br.ReadInt32(); // количество саб-мешей // одна
/////////////////////////////////////////////////////////////////////////////////////////
int mesh_name_length = br.ReadInt32(); // читаем число символов в строке имени jointa
byte[] mesh_Name_Hex = new byte[mesh_name_length]; // создаём массив байт под них
br.Read(mesh_Name_Hex, 0, mesh_name_length); // читаем байты в массив
string mesh_name = Encoding.Default.GetString(mesh_Name_Hex);
/////////////////////////////////////////////////////////////////////////////////////////
for (int sub = 0; sub < 6 ; sub++)
{
br.ReadInt32();
// 00 00 00 00---00 00 00 00---00 00 00 00
// 12 01 00 00---20 00 00 00---01 00 00 00
// D3DFVF_NORMAL D3DFVF_XYZ D3DFVF_TEX1
// #define D3DFVF_DIFFUSE 0x040
// #define D3DFVF_SPECULAR 0x080
}
/////////////////////////////////////////////////////////////////////////////////////////
// для каждой сабмеши
for (int subm = 0; subm < 1 ; subm++)
{
sw.WriteLine("Mesh Subset_1 {");
/////////////////////////////////////////////////////////////////////////////////////////
faces__count = br.ReadInt32(); // количество граней
vertex_count = br.ReadInt32(); // количество вершин
/////////////////////////////////////////////////////////////////////////////////////////
// читаем грани в виде строк f v1 v2 v3
for ( int i = 0 ; i < faces__count ; i++ )
{
v1 = br.ReadInt16();
v2 = br.ReadInt16();
v3 = br.ReadInt16();
face_list.Add("3;" + v1 + "," + v2 + "," + v3 + ";" );
}
/////////////////////////////////////////////////////////////////////////////////////////
// считываем информацию по каждой вершине v vn vt
for ( int i = 0 ; i < vertex_count ; i++ )
{
x = br.ReadSingle();
y = br.ReadSingle();
z = br.ReadSingle();
vn1 = br.ReadSingle();
vn2 = br.ReadSingle();
vn3 = br.ReadSingle();
u = br.ReadSingle();
v = br.ReadSingle();
vertex_PNT_List.Add(new VertexTypePNT(new Vector3 (x, y, z), new Vector3 (vn1, vn2, vn3), new Vector2 (u, v)));
vert_list.Add(x + ";" + y + ";" + z + ";");
uvst_list.Add(u + ";" + v + ";");
norm_list.Add(vn1 + ";" + vn2 + ";" + vn3 + ";");
}
/////////////////////////////////////////////////////////////////////////////////////////
// записываем всю информацию в *.x файл
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
sw.WriteLine(vert_list.Count + ";");
for (int q = 0; q < vert_list.Count; q++)
{
sw.Write(vert_list[q]);
if (q == vert_list.Count - 1 ) { sw.Write(";"); break;}
else sw.Write(",\n");
} sw.WriteLine();
/////////////////////////////////////////////////////////////////////////////////////////
sw.WriteLine(face_list.Count + ";");
for (int q = 0; q < face_list.Count; q++)
{
sw.Write(face_list[q]);
if (q == face_list.Count - 1 ) { sw.Write(";"); break;}
else sw.Write(",\n");
} sw.WriteLine();
/////////////////////////////////////////////////////////////////////////////////////////
sw.WriteLine("MeshMaterialList {");
sw.WriteLine("1;");
sw.WriteLine(face_list.Count + ";");
for (int q = 0; q < face_list.Count; q++)
{
sw.Write("0");
if (q == face_list.Count - 1 ) { sw.Write(";"); break;}
else sw.Write(",");
}
sw.WriteLine(@"
Material {
1.0; 1.0; 1.0; 1.000000;;
1.000000;
0.000000; 0.000000; 0.000000;;
0.000000; 0.000000; 0.000000;;
TextureFilename {
""" + "P01_FamilyDog.jpg" + @"""" + @"; // текстуру для этих моделей ищи в FXLibrary.fxf
}
}
}
");
/////////////////////////////////////////////////////////////////////////////////////////
sw.WriteLine("MeshNormals {");
sw.WriteLine(norm_list.Count + ";");
for (int q = 0; q < norm_list.Count; q++)
{
sw.Write(norm_list[q]);
if (q == norm_list.Count - 1 ) { sw.Write(";"); break;}
else sw.Write(",\n");
} sw.WriteLine();
/////////////////////////////////////////////////////////////////////////////////////////
sw.WriteLine(face_list.Count + ";");
for (int q = 0; q < face_list.Count; q++)
{
sw.Write(face_list[q]);
if (q == face_list.Count - 1 ) { sw.Write(";"); break;}
else sw.Write(",\n");
} sw.WriteLine();
sw.WriteLine("}"); // закрыли MeshNormals
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////
sw.WriteLine("MeshTextureCoords {");
sw.WriteLine(uvst_list.Count + ";");
for (int q = 0; q < uvst_list.Count; q++)
{
sw.Write(uvst_list[q]);
if (q == uvst_list.Count - 1 ) { sw.Write(";"); break;}
else sw.Write(",\n");
} sw.WriteLine();
sw.WriteLine("} // закрыли MeshTextureCoords");
//???????????????????????????????????????????????????????????????????????????????????????
//???????????????????????????????????????????????????????????????????????????????????????
//???????????????????????????????????????????????????????????????????????????????????????
/*
sw.WriteLine();
sw.WriteLine("MeshVertexColors {");
sw.WriteLine();
sw.WriteLine(vertex_count + ";");
sw.WriteLine();
sw.WriteLine();
*/
// считываем информацию по каждой вершине v w c vn vt
w1 = w2 = w3 = w4 = 0.0f; // присвоим нули
c1 = c2 = c3 = c4 = 0.0f; // а то ругается :)
for ( int i = 0 ; i < vertex_count ; i++ )
{
x = br.ReadSingle(); y = br.ReadSingle(); z = br.ReadSingle();
byte weightCount = br.ReadByte();
byte b2 = br.ReadByte();
byte b3 = br.ReadByte();
byte b4 = br.ReadByte();
if ( weightCount == 0 )
{
w1 = br.ReadSingle(); // 1 вес // 00 00 80 3F или FF FF 7F 3F
bc1 = br.ReadByte(); bc2 = br.ReadByte(); bc3 = br.ReadByte(); bc4 = br.ReadByte();
bc5 = br.ReadByte(); bc6 = br.ReadByte(); bc7 = br.ReadByte(); bc8 = br.ReadByte();
bc9 = br.ReadByte(); bc10= br.ReadByte(); bc11= br.ReadByte(); bc12 = br.ReadByte();
bc13= br.ReadByte(); bc14= br.ReadByte(); bc15= br.ReadByte(); bc16 = br.ReadByte();
c1 = bc13; // 1 индекс
}
if ( weightCount == 2 )
{
w1 = br.ReadSingle();
w2 = br.ReadSingle();
bc1 = br.ReadByte(); bc2 = br.ReadByte(); bc3 = br.ReadByte(); bc4 = br.ReadByte();
bc5 = br.ReadByte(); bc6 = br.ReadByte(); bc7 = br.ReadByte(); bc8 = br.ReadByte();
bc9 = br.ReadByte(); bc10= br.ReadByte(); bc11= br.ReadByte(); bc12= br.ReadByte();
c1 = bc9 ;
c2 = bc10;
}
if ( weightCount == 3 )
{
w1 = br.ReadSingle();
w2 = br.ReadSingle();
w3 = br.ReadSingle();
bc1 = br.ReadByte(); bc2 = br.ReadByte(); bc3 = br.ReadByte(); bc4 = br.ReadByte();
bc5 = br.ReadByte(); bc6 = br.ReadByte(); bc7 = br.ReadByte(); bc8 = br.ReadByte();
c1 = bc5;
c2 = bc6;
c3 = bc7;
}
if ( weightCount == 4 )
{
w1 = br.ReadSingle();
w2 = br.ReadSingle();
w3 = br.ReadSingle();
w4 = br.ReadSingle();
c1 = br.ReadByte();
c2 = br.ReadByte();
c3 = br.ReadByte();
c4 = br.ReadByte();
}
vn1 = br.ReadSingle(); vn2 = br.ReadSingle(); vn3 = br.ReadSingle();
u = br.ReadSingle(); v = br.ReadSingle();
vertex_PWCNT_List.Add
(
new VertexTypePWCNT
(
new Vector3 (x, y, z),
new Vector4 (w1, w2, w3, w4),
new Vector4 (c1, c2, c3, c4),
new Vector3 (vn1, vn2, vn3),
new Vector2 (u, v)
)
);
// if (i < vertex_count-1) sw.WriteLine( i + ";" + c1 + ";" + c2 + ";" + c3 + ";" + c4 + ";;," );
}
// sw.WriteLine( (vertex_count-1) + ";" + c1 + ";" + c2 + ";" + c3 + ";" + c4 + ";;;" );
// sw.WriteLine("} // закрыли MeshVertexColors");
//???????????????????????????????????????????????????????????????????????????????????????
//???????????????????????????????????????????????????????????????????????????????????????
//???????????????????????????????????????????????????????????????????????????????????????
sw.WriteLine(@"
} // закрыли Mesh
");
/////////////////////////////////////////////////////////////////////////////////////////
sw.WriteLine(@"
} // закрыли Frame Body
");
/////////////////////////////////////////////////////////////////////////////////////////
sw.WriteLine(@"
} // закрыли Frame SceneRoot
");
/////////////////////////////////////////////////////////////////////////////////////////
vert_list.Clear();
uvst_list.Clear();
norm_list.Clear();
face_list.Clear();
vertex_PNT_List.Clear();
vertex_PWCNT_List.Clear();
/////////////////////////////////////////////////////////////////////////////////////////
} // проход по сабмешам
} // using StreamWriter // закрываем *.x файл на запись // sw.Close(); // sw.Dispose();
} // using BinaryReader // закрываем файл на чтение
} // foreach (var fxmName in allFilesName)
} // static void Main()
} // class Program
//OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
//OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
//OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
public class Node // XFile frame
{
public string Name;
public Matrix4x4 TrafoMatrix;
public Node Parent;
public List<Node> Child = new List<Node>();
// public List<Mesh> Meshes = new List<Mesh>();
public Node(string name, Matrix4x4 mtr, Node parent = null)
{
this.Name = name;
this.TrafoMatrix = mtr;
this.Parent = parent;
}
}
//OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
//OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
//OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
/*public class Mesh
{
public string fileTexture;
public Vertex[] MeshVertex;
public Vertex[] MeshNormals;
public int[] MeshFace;
public int[] MeshFaceNormals;
public float[,] TextCoords;
}*/
//OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
class VertexTypePNT
{
public Vector3 Position; // x y z
public Vector3 Normal; // n1 n2 n3
public Vector2 ST; // u v
public VertexTypePNT(Vector3 position, Vector3 normal, Vector2 uv)
{
Position = position;
Normal = normal;
ST = uv;
}
}
//OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
class VertexTypePWCNT
{
public Vector3 position; // x y z
public Vector4 weight; // ...
public Vector4 color; // RGBA
public Vector3 normal; // n1 n2 n3
public Vector2 textCoord; // u v
public VertexTypePWCNT(Vector3 position, Vector4 weight, Vector4 color, Vector3 normal, Vector2 uv )
{
this.position = position;
this.weight = weight;
this.color = color;
this.normal = normal;
this.textCoord = uv;
}
}
//OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO