-
-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathExporter.cs
More file actions
644 lines (594 loc) · 26.4 KB
/
Copy pathExporter.cs
File metadata and controls
644 lines (594 loc) · 26.4 KB
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace AnimeStudio.GUI
{
internal static class Exporter
{
public static bool ExportTexture2D(AssetItem item, string exportPath)
{
var m_Texture2D = (Texture2D)item.Asset;
if (Properties.Settings.Default.convertTexture)
{
bool isHDR = m_Texture2D.m_TextureFormat == TextureFormat.RGBAHalf && Properties.Settings.Default.enableHDR;
var type = Properties.Settings.Default.convertType;
if (isHDR)
{
type = ImageFormat.Hdr;
}
if (!TryExportFile(exportPath, item, "." + type.ToString().ToLower(), out var exportFullPath))
return false;
if (isHDR)
{
var buff = ArrayPool<Byte>.Shared.Rent((int)m_Texture2D.image_data.Size);
m_Texture2D.image_data.GetData(buff);
int outPutSize = m_Texture2D.m_Width * m_Texture2D.m_Height * 4;
var stream = ArrayPool<Half>.Shared.Rent(outPutSize);
for (var i = 0; i < outPutSize; i += 4)
{
stream[i] = Half.ToHalf(buff, i * 2);
stream[i + 1] = Half.ToHalf(buff, i * 2 + 2);
stream[i + 2] = Half.ToHalf(buff, i * 2 + 4);
stream[i + 3] = Half.ToHalf(buff, i * 2 + 6);
}
var flippedStream = ArrayPool<Half>.Shared.Rent(outPutSize);
for (int y = 0; y < m_Texture2D.m_Height; y++)
{
int rowSize = m_Texture2D.m_Width * 4;
int srcRowStart = y * rowSize;
int destRowStart = (m_Texture2D.m_Height - y - 1) * rowSize;
Array.Copy(stream, srcRowStart, flippedStream, destRowStart, rowSize);
}
String header =
"#?RADIANCE\n" +
"PRIMARIES=0 0 0 0 0 0 0 0\nFORMAT=32-bit_rle_rgbe\n\n" +
"-Y " + m_Texture2D.m_Height.ToString() + " +X " +
m_Texture2D.m_Width.ToString() + "\n";
using (var file = new BinaryWriter(File.OpenWrite(exportFullPath)))
{
file.Write(System.Text.Encoding.ASCII.GetBytes(header));
// from https://github.com/Opioid/rgbe/blob/master/encode.go
for (int i = 0; i != outPutSize; i += 4)
{
float maxComponent = Math.Max(flippedStream[i], System.Math.Max(flippedStream[i + 1], flippedStream[i + 2]));
byte[] rgbe = new byte[4];
if (maxComponent < 1e-32f)
{
rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0;
} else
{
int exponent;
float mantissa = (float)Double.Frexp((double)maxComponent, out exponent);
mantissa *= 256.0f / maxComponent;
rgbe[0] = (byte)(flippedStream[i] * mantissa);
rgbe[1] = (byte)(flippedStream[i + 1] * mantissa);
rgbe[2] = (byte)(flippedStream[i + 2] * mantissa);
rgbe[3] = (byte)(exponent + 128);
}
foreach (byte channel in rgbe)
{
file.Write(channel);
}
}
}
return true;
} else
{
var image = m_Texture2D.ConvertToImage(true);
if (image == null)
{
return false;
}
using (image)
{
using (var file = File.OpenWrite(exportFullPath))
{
image.WriteToStream(file, type);
}
return true;
}
}
}
else
{
if (!TryExportFile(exportPath, item, ".tex", out var exportFullPath))
return false;
File.WriteAllBytes(exportFullPath, m_Texture2D.image_data.GetData());
return true;
}
}
public static bool ExportAudioClip(AssetItem item, string exportPath)
{
var m_AudioClip = (AudioClip)item.Asset;
var m_AudioData = m_AudioClip.m_AudioData.GetData();
if (m_AudioData == null || m_AudioData.Length == 0)
return false;
var converter = new AudioClipConverter(m_AudioClip);
if (Properties.Settings.Default.convertAudio && converter.IsSupport)
{
if (!TryExportFile(exportPath, item, ".wav", out var exportFullPath))
return false;
var buffer = converter.ConvertToWav();
if (buffer == null)
return false;
File.WriteAllBytes(exportFullPath, buffer);
}
else
{
if (!TryExportFile(exportPath, item, converter.GetExtensionName(), out var exportFullPath))
return false;
File.WriteAllBytes(exportFullPath, m_AudioData);
}
return true;
}
public static bool ExportShader(AssetItem item, string exportPath)
{
if (!TryExportFile(exportPath, item, ".shader", out var exportFullPath))
return false;
var m_Shader = (Shader)item.Asset;
var str = m_Shader.Convert();
File.WriteAllText(exportFullPath, str);
return true;
}
public static bool ExportTextAsset(AssetItem item, string exportPath)
{
var m_TextAsset = (TextAsset)(item.Asset);
var extension = ".txt";
if (Properties.Settings.Default.restoreExtensionName)
{
if (!string.IsNullOrEmpty(item.Container))
{
extension = Path.GetExtension(item.Container);
}
}
if (!TryExportFile(exportPath, item, extension, out var exportFullPath))
return false;
File.WriteAllBytes(exportFullPath, m_TextAsset.m_Script);
return true;
}
public static bool ExportMonoBehaviour(AssetItem item, string exportPath)
{
if (!TryExportFile(exportPath, item, ".json", out var exportFullPath))
return false;
var m_MonoBehaviour = (MonoBehaviour)item.Asset;
var type = m_MonoBehaviour.ToType();
if (type == null)
{
var m_Type = Studio.MonoBehaviourToTypeTree(m_MonoBehaviour);
type = m_MonoBehaviour.ToType(m_Type);
}
var str = JsonConvert.SerializeObject(type, Formatting.Indented);
File.WriteAllText(exportFullPath, str);
return true;
}
public static bool ExportMiHoYoBinData(AssetItem item, string exportPath)
{
string exportFullPath;
if (item.Asset is MiHoYoBinData m_MiHoYoBinData)
{
switch (m_MiHoYoBinData.Type)
{
case MiHoYoBinDataType.JSON:
if (!TryExportFile(exportPath, item, ".json", out exportFullPath))
return false;
var json = m_MiHoYoBinData.Dump() as string;
if (json.Length != 0)
{
File.WriteAllText(exportFullPath, json);
return true;
}
break;
case MiHoYoBinDataType.Bytes:
var extension = ".bin";
if (Properties.Settings.Default.restoreExtensionName)
{
if (!string.IsNullOrEmpty(item.Container))
{
extension = Path.GetExtension(item.Container);
}
}
if (!TryExportFile(exportPath, item, extension, out exportFullPath))
return false;
var bytes = m_MiHoYoBinData.Dump() as byte[];
if (!bytes.IsNullOrEmpty())
{
File.WriteAllBytes(exportFullPath, bytes);
return true;
}
break;
}
}
return false;
}
public static bool ExportFont(AssetItem item, string exportPath)
{
var m_Font = (Font)item.Asset;
if (m_Font.m_FontData != null)
{
var extension = ".ttf";
if (m_Font.m_FontData[0] == 79 && m_Font.m_FontData[1] == 84 && m_Font.m_FontData[2] == 84 && m_Font.m_FontData[3] == 79)
{
extension = ".otf";
}
if (!TryExportFile(exportPath, item, extension, out var exportFullPath))
return false;
File.WriteAllBytes(exportFullPath, m_Font.m_FontData);
return true;
}
return false;
}
public static bool ExportMesh(AssetItem item, string exportPath)
{
var m_Mesh = (Mesh)item.Asset;
if (m_Mesh.m_VertexCount <= 0)
return false;
if (!TryExportFile(exportPath, item, ".obj", out var exportFullPath))
return false;
var sb = new StringBuilder();
sb.AppendLine("g " + m_Mesh.m_Name);
#region Vertices
if (m_Mesh.m_Vertices == null || m_Mesh.m_Vertices.Length == 0)
{
return false;
}
int c = 3;
if (m_Mesh.m_Vertices.Length == m_Mesh.m_VertexCount * 4)
{
c = 4;
}
for (int v = 0; v < m_Mesh.m_VertexCount; v++)
{
sb.AppendFormat("v {0} {1} {2}\r\n", -m_Mesh.m_Vertices[v * c], m_Mesh.m_Vertices[v * c + 1], m_Mesh.m_Vertices[v * c + 2]);
}
#endregion
#region UV
if (m_Mesh.m_UV0?.Length > 0)
{
c = 4;
if (m_Mesh.m_UV0.Length == m_Mesh.m_VertexCount * 2)
{
c = 2;
}
else if (m_Mesh.m_UV0.Length == m_Mesh.m_VertexCount * 3)
{
c = 3;
}
for (int v = 0; v < m_Mesh.m_VertexCount; v++)
{
sb.AppendFormat("vt {0} {1}\r\n", m_Mesh.m_UV0[v * c], m_Mesh.m_UV0[v * c + 1]);
}
}
#endregion
#region Normals
if (m_Mesh.m_Normals?.Length > 0)
{
if (m_Mesh.m_Normals.Length == m_Mesh.m_VertexCount * 3)
{
c = 3;
}
else if (m_Mesh.m_Normals.Length == m_Mesh.m_VertexCount * 4)
{
c = 4;
}
for (int v = 0; v < m_Mesh.m_VertexCount; v++)
{
sb.AppendFormat("vn {0} {1} {2}\r\n", -m_Mesh.m_Normals[v * c], m_Mesh.m_Normals[v * c + 1], m_Mesh.m_Normals[v * c + 2]);
}
}
#endregion
#region Face
int sum = 0;
for (var i = 0; i < m_Mesh.m_SubMeshes.Count; i++)
{
sb.AppendLine($"g {m_Mesh.m_Name}_{i}");
int indexCount = (int)m_Mesh.m_SubMeshes[i].indexCount;
var end = sum + indexCount / 3;
for (int f = sum; f < end; f++)
{
sb.AppendFormat("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\r\n", m_Mesh.m_Indices[f * 3 + 2] + 1, m_Mesh.m_Indices[f * 3 + 1] + 1, m_Mesh.m_Indices[f * 3] + 1);
}
sum = end;
}
#endregion
sb.Replace("NaN", "0");
File.WriteAllText(exportFullPath, sb.ToString());
return true;
}
public static bool ExportVideoClip(AssetItem item, string exportPath)
{
var m_VideoClip = (VideoClip)item.Asset;
if (m_VideoClip.m_ExternalResources.m_Size > 0)
{
if (!TryExportFile(exportPath, item, Path.GetExtension(m_VideoClip.m_OriginalPath), out var exportFullPath))
return false;
m_VideoClip.m_VideoData.WriteData(exportFullPath);
return true;
}
return false;
}
public static bool ExportMovieTexture(AssetItem item, string exportPath)
{
var m_MovieTexture = (MovieTexture)item.Asset;
if (!TryExportFile(exportPath, item, ".ogv", out var exportFullPath))
return false;
File.WriteAllBytes(exportFullPath, m_MovieTexture.m_MovieData);
return true;
}
public static bool ExportSprite(AssetItem item, string exportPath)
{
var type = Properties.Settings.Default.convertType;
if (!TryExportFile(exportPath, item, "." + type.ToString().ToLower(), out var exportFullPath))
return false;
var image = ((Sprite)item.Asset).GetImage();
if (image != null)
{
using (image)
{
using (var file = File.OpenWrite(exportFullPath))
{
image.WriteToStream(file, type);
}
return true;
}
}
return false;
}
public static bool ExportRawFile(AssetItem item, string exportPath)
{
if (!TryExportFile(exportPath, item, ".dat", out var exportFullPath))
return false;
File.WriteAllBytes(exportFullPath, item.Asset.GetRawData());
return true;
}
private static bool TryExportFile(string dir, AssetItem item, string extension, out string fullPath)
{
var fileName = FixFileName(item.Text);
fullPath = Path.Combine(dir, $"{fileName}{extension}");
if (!File.Exists(fullPath))
{
Directory.CreateDirectory(dir);
return true;
}
if (Properties.Settings.Default.allowDuplicates)
{
for (int i = 1; i < int.MaxValue; i++)
{
fullPath = Path.Combine(dir, $"{fileName} ({i}){extension}");
if (!File.Exists(fullPath))
{
return true;
}
}
}
return false;
}
private static bool TryExportFolder(string dir, AssetItem item, out string fullPath)
{
var fileName = FixFileName(item.Text);
fullPath = Path.Combine(dir, fileName);
if (!Directory.Exists(fullPath))
{
return true;
}
if (Properties.Settings.Default.allowDuplicates)
{
for (int i = 1; i < int.MaxValue; i++)
{
fullPath = Path.Combine(dir, $"{fileName} ({i})");
if (!Directory.Exists(fullPath))
{
return true;
}
}
}
return false;
}
public static bool ExportAnimationClip(AssetItem item, string exportPath)
{
if (!TryExportFile(exportPath, item, ".anim", out var exportFullPath))
return false;
var m_AnimationClip = (AnimationClip)item.Asset;
var str = m_AnimationClip.Convert();
if (string.IsNullOrEmpty(str))
return false;
File.WriteAllText(exportFullPath, str);
return true;
}
public static bool ExportAnimator(AssetItem item, string exportPath, List<AssetItem> animationList = null)
{
if (!TryExportFolder(exportPath, item, out var exportFullPath))
return false;
exportFullPath = Path.Combine(exportFullPath, item.Text + ".fbx");
var m_Animator = (Animator)item.Asset;
var options = new ModelConverter.Options()
{
imageFormat = Properties.Settings.Default.convertType,
game = Studio.Game,
collectAnimations = Properties.Settings.Default.collectAnimations,
exportMaterials = Properties.Settings.Default.exportMaterials,
materials = new HashSet<Material>(),
uvs = JsonConvert.DeserializeObject<Dictionary<string, (bool, int)>>(Properties.Settings.Default.uvs),
texs = JsonConvert.DeserializeObject<Dictionary<string, int>>(Properties.Settings.Default.texs),
};
var convert = animationList != null
? new ModelConverter(m_Animator, options, animationList.Select(x => (AnimationClip)x.Asset).ToArray())
: new ModelConverter(m_Animator, options);
if (options.exportMaterials)
{
var materialExportPath = Path.Combine(Path.GetDirectoryName(exportFullPath), "Materials");
Directory.CreateDirectory(materialExportPath);
foreach (var material in options.materials)
{
var matItem = new AssetItem(material);
ExportJSONFile(matItem, materialExportPath);
}
}
ExportFbx(convert, exportFullPath);
return true;
}
public static bool ExportGameObject(AssetItem item, string exportPath, List <AssetItem> animationList = null)
{
if (!TryExportFolder(exportPath, item, out var exportFullPath))
return false;
var m_GameObject = (GameObject)item.Asset;
return ExportGameObject(m_GameObject, exportFullPath + Path.DirectorySeparatorChar, animationList);
}
public static bool ExportGameObject(GameObject gameObject, string exportPath, List<AssetItem> animationList = null)
{
var options = new ModelConverter.Options()
{
imageFormat = Properties.Settings.Default.convertType,
game = Studio.Game,
collectAnimations = Properties.Settings.Default.collectAnimations,
exportMaterials = Properties.Settings.Default.exportMaterials,
materials = new HashSet<Material>(),
uvs = JsonConvert.DeserializeObject<Dictionary<string, (bool, int)>>(Properties.Settings.Default.uvs),
texs = JsonConvert.DeserializeObject<Dictionary<string, int>>(Properties.Settings.Default.texs),
};
var convert = animationList != null
? new ModelConverter(gameObject, options, animationList.Select(x => (AnimationClip)x.Asset).ToArray())
: new ModelConverter(gameObject, options);
if (convert.MeshList.Count == 0)
{
Logger.Info($"GameObject {gameObject.m_Name} has no mesh, skipping...");
return false;
}
if (options.exportMaterials)
{
var materialExportPath = Path.Combine(exportPath, "Materials");
Directory.CreateDirectory(materialExportPath);
foreach (var material in options.materials)
{
var matItem = new AssetItem(material);
ExportJSONFile(matItem, materialExportPath);
}
}
exportPath = exportPath + FixFileName(gameObject.m_Name) + ".fbx";
ExportFbx(convert, exportPath);
return true;
}
public static void ExportGameObjectMerge(List<GameObject> gameObject, string exportPath, List<AssetItem> animationList = null)
{
var rootName = Path.GetFileNameWithoutExtension(exportPath);
var options = new ModelConverter.Options()
{
imageFormat = Properties.Settings.Default.convertType,
game = Studio.Game,
collectAnimations = Properties.Settings.Default.collectAnimations,
exportMaterials = Properties.Settings.Default.exportMaterials,
materials = new HashSet<Material>(),
uvs = JsonConvert.DeserializeObject<Dictionary<string, (bool, int)>>(Properties.Settings.Default.uvs),
texs = JsonConvert.DeserializeObject<Dictionary<string, int>>(Properties.Settings.Default.texs),
};
var convert = animationList != null
? new ModelConverter(rootName, gameObject, options, animationList.Select(x => (AnimationClip)x.Asset).ToArray())
: new ModelConverter(rootName, gameObject, options);
if (options.exportMaterials)
{
var materialExportPath = Path.Combine(Path.GetDirectoryName(exportPath), "Materials");
Directory.CreateDirectory(materialExportPath);
foreach (var material in options.materials)
{
var matItem = new AssetItem(material);
ExportJSONFile(matItem, materialExportPath);
}
}
ExportFbx(convert, exportPath);
}
private static void ExportFbx(IImported convert, string exportPath)
{
var exportOptions = new Fbx.ExportOptions()
{
eulerFilter = Properties.Settings.Default.eulerFilter,
filterPrecision = (float)Properties.Settings.Default.filterPrecision,
exportAllNodes = Properties.Settings.Default.exportAllNodes,
exportSkins = Properties.Settings.Default.exportSkins,
exportAnimations = Properties.Settings.Default.exportAnimations,
exportBlendShape = Properties.Settings.Default.exportBlendShape,
castToBone = Properties.Settings.Default.castToBone,
boneSize = (int)Properties.Settings.Default.boneSize,
scaleFactor = (float)Properties.Settings.Default.scaleFactor,
fbxVersion = Properties.Settings.Default.fbxVersion,
fbxFormat = Properties.Settings.Default.fbxFormat
};
ModelExporter.ExportFbx(exportPath, convert, exportOptions);
}
public static bool ExportDumpFile(AssetItem item, string exportPath)
{
if (!TryExportFile(exportPath, item, ".txt", out var exportFullPath))
return false;
var str = item.Asset.Dump();
if (str == null && item.Asset is MonoBehaviour m_MonoBehaviour)
{
var m_Type = Studio.MonoBehaviourToTypeTree(m_MonoBehaviour);
str = m_MonoBehaviour.Dump(m_Type);
}
if (str != null)
{
File.WriteAllText(exportFullPath, str);
return true;
}
return false;
}
public static bool ExportConvertFile(AssetItem item, string exportPath)
{
switch (item.Type)
{
case ClassIDType.GameObject:
return ExportGameObject(item, exportPath);
case ClassIDType.Texture2D:
return ExportTexture2D(item, exportPath);
case ClassIDType.AudioClip:
return ExportAudioClip(item, exportPath);
case ClassIDType.Shader:
return ExportShader(item, exportPath);
case ClassIDType.TextAsset:
return ExportTextAsset(item, exportPath);
case ClassIDType.MonoBehaviour:
return ExportMonoBehaviour(item, exportPath);
case ClassIDType.Font:
return ExportFont(item, exportPath);
case ClassIDType.Mesh:
return ExportMesh(item, exportPath);
case ClassIDType.VideoClip:
return ExportVideoClip(item, exportPath);
case ClassIDType.MovieTexture:
return ExportMovieTexture(item, exportPath);
case ClassIDType.Sprite:
return ExportSprite(item, exportPath);
case ClassIDType.Animator:
return ExportAnimator(item, exportPath);
case ClassIDType.AnimationClip:
return ExportAnimationClip(item, exportPath);
case ClassIDType.MiHoYoBinData:
return ExportMiHoYoBinData(item, exportPath);
case ClassIDType.Material:
return ExportJSONFile(item, exportPath);
case ClassIDType.NapAssetBundleIndexAsset:
return ExportJSONFile(item, exportPath);
default:
return ExportRawFile(item, exportPath);
}
}
public static bool ExportJSONFile(AssetItem item, string exportPath)
{
if (!TryExportFile(exportPath, item, ".json", out var exportFullPath))
return false;
var settings = new JsonSerializerSettings();
settings.Converters.Add(new StringEnumConverter());
var str = JsonConvert.SerializeObject(item.Asset, Formatting.Indented, settings);
File.WriteAllText(exportFullPath, str);
return true;
}
public static string FixFileName(string str)
{
if (str.Length >= 260) return Path.GetRandomFileName();
return Path.GetInvalidFileNameChars().Aggregate(str, (current, c) => current.Replace(c, '_'));
}
}
}