-
Notifications
You must be signed in to change notification settings - Fork 22
/
PokemonImporter.cs
1213 lines (1059 loc) · 71 KB
/
PokemonImporter.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
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using CurveExtended;
using P3DS2U.Editor.SPICA;
using P3DS2U.Editor.SPICA.COLLADA;
using P3DS2U.Editor.SPICA.Commands;
using P3DS2U.Editor.SPICA.H3D;
using P3DS2U.Editor.SPICA.H3D.Animation;
using P3DS2U.Editor.SPICA.H3D.Model;
using P3DS2U.Editor.SPICA.H3D.Model.Mesh;
using UnityEditor;
using UnityEngine;
using Random = UnityEngine.Random;
//using UnityEditor.CustomUnityFbxExporter;
/*
*TODO: Separate the model and the container prefabs, to have the model drag-droppable in the animation previews.
*TODO: Autogenerate emmision maps and emission shaders
*TODO: Shaders in ase format (UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset != null && UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset.GetType().ToString().Contains("UniversalRenderPipelineAsset"))
*TODO: AssetBundles/Addressable build size optimizations
*TODO: Environments/Characters
*/
namespace P3DS2U.Editor
{
public class PokemonImporter : EditorWindow
{
public const string ImportPath = "Assets/Bin3DS/";
public const string ExportPath = "Assets/Exported/";
private static int _processedCount;
private delegate void OnAnimationImported (AnimationClip clip);
private static OnAnimationImported OnAnimationImportedEvent;
public static void RaiseOnAnimationImportedEvent (AnimationClip clip) => OnAnimationImportedEvent?.Invoke (clip);
private static H3D h3DScene = null;
private static int CurrentAnimationIndex = 0;
private static string CurrentAnimationExportFolder = "";
public static P3ds2USettingsScriptableObject CurrentImportSettings { get; private set; }
public static List<AnimationImportOptions> AnimationImportOptions = new List<AnimationImportOptions>();
[MenuItem ("3DStoUnity/Find Settings Object")]
private static void ImportPokemonAction ()
{
if (!Directory.Exists (ImportPath)) {
Directory.CreateDirectory (ImportPath);
EditorUtility.DisplayDialog ("Created Folder " + ImportPath,
"Created Folder" + ImportPath +
" \nPlease place .bin files to be imported in that directory or subdirectories, Files with the same name will be merged together",
"ok");
}
SettingsUtils.GetOrCreateSettings(true);
}
public static void StartImportingBinaries (P3ds2USettingsScriptableObject importSettings, Dictionary<string, List<string>> scenesDict)
{
CurrentImportSettings = importSettings;
try {
string ExportPath = importSettings.ExportPath;
AnimationImportOptions.Add(importSettings.ImporterSettings.FightAnimationsToImport);
AnimationImportOptions.Add(importSettings.ImporterSettings.PetAnimationsToImport);
AnimationImportOptions.Add(importSettings.ImporterSettings.MovementAnimationsToImport);
_processedCount = 0;
for (int i = importSettings.ImporterSettings.StartIndex; i <= importSettings.ImporterSettings.EndIndex; i++)
{
var kvp = scenesDict.ElementAt(i);
EditorUtility.ClearProgressBar ();
EditorUtility.DisplayProgressBar ("Importing", kvp.Key.Replace (".bin", ""),
(float) _processedCount / scenesDict.Count);
h3DScene = new H3D ();
//Add all non-animation binaries first
foreach (var singleFileToBeMerged in kvp.Value) {
var fileType = BinaryUtils.GetBinaryFileType (singleFileToBeMerged);
if (fileType == BinaryUtils.FileType.Animation) continue;
H3DDict<H3DBone> skeleton = null;
if (h3DScene.Models.Count > 0) skeleton = h3DScene.Models[0].Skeleton;
var data = FormatIdentifier.IdentifyAndOpen (singleFileToBeMerged, skeleton);
if (data != null) h3DScene.Merge (data);
}
int animFilesCount = 0;
//Merge animation binaries
foreach (var singleFileToBeMerged in kvp.Value) {
var fileType = BinaryUtils.GetBinaryFileType (singleFileToBeMerged);
if (fileType != BinaryUtils.FileType.Animation) continue;
H3DDict<H3DBone> skeleton = null;
if (h3DScene.Models.Count > 0) skeleton = h3DScene.Models[0].Skeleton;
var data = FormatIdentifier.IdentifyAndOpen (singleFileToBeMerged, skeleton, animFilesCount);
animFilesCount++;
if (data != null) h3DScene.Merge (data);
}
var combinedExportFolder = ExportPath + kvp.Key.Replace (".bin", "") + "/Files/";
if (!Directory.Exists (combinedExportFolder)) {
Directory.CreateDirectory (combinedExportFolder);
} else {
Directory.Delete (ExportPath + kvp.Key.Replace (".bin", "") + "/", true);
Directory.CreateDirectory (combinedExportFolder);
}
if (importSettings.ImporterSettings.ImportTextures) {
GenerateTextureFiles (h3DScene, combinedExportFolder);
}
var meshDict = new Dictionary<string, SkinnedMeshRenderer> ();
if (importSettings.ImporterSettings.ImportModel) {
try {
meshDict = GenerateMeshInUnityScene (h3DScene, combinedExportFolder, importSettings);
}
catch (Exception e) {
Debug.LogError (
"Check your settings! Are you sure, you are using the correct input format, is each pokemon's binaries in a separate folder?\n" +
e.Message + "\n" + e.StackTrace);
}
}
var matDict = new Dictionary<string, Material> ();
if (importSettings.ImporterSettings.ImportShinyMaterials) {
matDict = GenerateShinyMaterialFiles (h3DScene, combinedExportFolder, importSettings.customShaderSettings);
}
if (importSettings.ImporterSettings.ImportMaterials) {
matDict = GenerateMaterialFiles (h3DScene, combinedExportFolder, importSettings.customShaderSettings);
}
if (importSettings.ImporterSettings.ApplyMaterials) {
AddMaterialsToGeneratedMeshes (meshDict, matDict, h3DScene);
} else if (importSettings.ImporterSettings.ApplyShinyMaterials) {
AddMaterialsToGeneratedMeshes (meshDict, matDict, h3DScene);
}
if (importSettings.ImporterSettings.SkeletalAnimations) {
GenerateSkeletalAnimations (h3DScene, combinedExportFolder);
}
if (importSettings.ImporterSettings.VisibilityAnimations) {
GenerateVisibilityAnimations (h3DScene, combinedExportFolder);
}
if (importSettings.ImporterSettings.MaterialAnimations) {
GenerateMaterialAnimations (h3DScene, combinedExportFolder, importSettings);
}
var modelGo = GameObject.Find ("GeneratedUnityObject");
if (modelGo) {
var modelName = kvp.Key.Replace (".bin", "");
if (importSettings.ImporterSettings.SkeletalAnimations) {
if (importSettings.customAnimatorSettings.baseController == null)
{ GenerateAnimationController(modelGo, combinedExportFolder, modelName); }
else
{ GenerateAnimationOverrideController(modelGo, combinedExportFolder, modelName, importSettings.customAnimatorSettings); }
}
modelGo.transform.parent = null;
modelGo.name = modelName;
modelGo.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
var prefabPath =
AssetDatabase.GenerateUniqueAssetPath (ExportPath + kvp.Key.Replace (".bin", "") + "/" + kvp.Key.Replace (".bin", "") + ".prefab");
PrefabUtility.SaveAsPrefabAssetAndConnect (modelGo, prefabPath, InteractionMode.UserAction);
// Export model as FBX?
//ModelExporter.ExportObject(Application.dataPath + "/Model.fbx", modelGo);
if (importSettings.spawnModelsIntoScene)
modelGo.transform.position = importSettings.spawnAtRandomPosition
? new Vector3(Random.Range(-100f, 100f), 0f, Random.Range(-100f, 100f))
: Vector3.zero;
else
DestroyImmediate(modelGo);
}
}
}
catch (Exception e) {
Debug.LogError ("Something went horribly wrong! Hmu, I'll try to fix it.\n" + e.Message + "\n" +
e.StackTrace);
}
CurrentImportSettings = null;
EditorUtility.ClearProgressBar();
}
private static void GenerateAnimationController(GameObject modelGo, string combinedExportFolder,
string modelName)
{
var animationsFolderPath = combinedExportFolder + "Animations/";
var animator = modelGo.AddComponent<Animator> ();
var animatorController = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(animationsFolderPath + $"animController-{modelName}.controller");
var files = Directory.GetFiles (animationsFolderPath);
// animatorController.layers[0].stateMachine.AddState ("1");
foreach (var animationFilePath in files) {
var animationClip = AssetDatabase.LoadAssetAtPath<AnimationClip> (animationFilePath);
if (animationClip != null) {
animatorController.AddMotion (animationClip);
}
}
animator.runtimeAnimatorController = animatorController;
AssetDatabase.Refresh();
}
private static void GenerateAnimationOverrideController(GameObject modelGo, string combinedExportFolder,
string modelName, P3ds2UAnimatorProperties animatorImportSettings)
{
var animationsFolderPath = combinedExportFolder + "Animations/";
var animator = modelGo.AddComponent<Animator>();
var animatorOverrideController = AssetHelper.GetAsset<AnimatorOverrideController>($"animController-{modelName}", animationsFolderPath + $"animController-{modelName}.overrideController");
animatorOverrideController.runtimeAnimatorController = animatorImportSettings.baseController;
var files = Directory.GetFiles(animationsFolderPath);
var overrides = new List<KeyValuePair<string, string>>();
#region Animation Key Pairs
overrides.Add(new KeyValuePair<string, string>("Fight_" + "Idle".ToLower(), animatorImportSettings.Fight_Idle));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "Appear".ToLower(), animatorImportSettings.Appear));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "Transform".ToLower(), animatorImportSettings.Transform));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "Release".ToLower(), animatorImportSettings.Release));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "Dropping".ToLower(), animatorImportSettings.Dropping));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "Landing".ToLower(), animatorImportSettings.Landing));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "Release_without_Landing".ToLower(), animatorImportSettings.Release_without_Landing));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "Mega_Upgraded".ToLower(), animatorImportSettings.Mega_Upgraded));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "Attack".ToLower(), animatorImportSettings.Attack));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "Attack_2".ToLower(), animatorImportSettings.Attack_2));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "Attack_3".ToLower(), animatorImportSettings.Attack_3));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "Attack_4".ToLower(), animatorImportSettings.Attack_4));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "No_Touch_Attack".ToLower(), animatorImportSettings.No_Touch_Attack));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "No_Touch_Attack_2".ToLower(), animatorImportSettings.No_Touch_Attack_2));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "No_Touch_Attack_3".ToLower(), animatorImportSettings.No_Touch_Attack_3));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "No_Touch_Attack_4".ToLower(), animatorImportSettings.No_Touch_Attack_4));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "Be_Attacked".ToLower(), animatorImportSettings.Be_Attacked));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "Lost".ToLower(), animatorImportSettings.Lost));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "Empty".ToLower(), animatorImportSettings.Fight_Empty));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "Eye_Emotion".ToLower(), animatorImportSettings.Fight_Eye_Emotion));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "Eye_2_Emotion".ToLower(), animatorImportSettings.Fight_Eye_2_Emotion));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "Eye_3_Emotion".ToLower(), animatorImportSettings.Fight_Eye_3_Emotion));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "Mouth_Emotion".ToLower(), animatorImportSettings.Fight_Mouth_Emotion));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "Mouth_2_Emotion".ToLower(), animatorImportSettings.Fight_Mouth_2_Emotion));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "Mouth_3_Emotion".ToLower(), animatorImportSettings.Fight_Mouth_3_Emotion));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "State".ToLower(), animatorImportSettings.Fight_State));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "State_2".ToLower(), animatorImportSettings.Fight_State_2));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "State_3".ToLower(), animatorImportSettings.Fight_State_3));
overrides.Add(new KeyValuePair<string, string>("Fight_" + "State_4".ToLower(), animatorImportSettings.Fight_State_4));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Idle".ToLower(), animatorImportSettings.Pet_Idle));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Turn".ToLower(), animatorImportSettings.Turn));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Look_Back".ToLower(), animatorImportSettings.Look_Back));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Look_Back_Happily".ToLower(), animatorImportSettings.Look_Back_Happily));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Falling_Aasleep".ToLower(), animatorImportSettings.Falling_Asleep));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Sleepy".ToLower(), animatorImportSettings.Sleepy));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Sleepy_Awaken".ToLower(), animatorImportSettings.Sleepy_Awaken));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Sleeping".ToLower(), animatorImportSettings.Sleeping));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Awaken".ToLower(), animatorImportSettings.Awaken));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Refuse".ToLower(), animatorImportSettings.Refuse));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Thinking".ToLower(), animatorImportSettings.Thinking));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Agree".ToLower(), animatorImportSettings.Agree));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Happy".ToLower(), animatorImportSettings.Happy));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Very_Happy".ToLower(), animatorImportSettings.Very_Happy));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Look_Around".ToLower(), animatorImportSettings.Look_Around));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Rub_Eyes".ToLower(), animatorImportSettings.Rub_Eyes));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Comfortable".ToLower(), animatorImportSettings.Comfortable));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Sad".ToLower(), animatorImportSettings.Sad));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Salutate".ToLower(), animatorImportSettings.Salutate));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Happy_2".ToLower(), animatorImportSettings.Happy_2));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Angry".ToLower(), animatorImportSettings.Angry));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Begin_Eating".ToLower(), animatorImportSettings.Begin_Eating));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Eating".ToLower(), animatorImportSettings.Eating));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Eating_Finished".ToLower(), animatorImportSettings.Eating_Finished));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "No_Eating".ToLower(), animatorImportSettings.No_Eating));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Empty".ToLower(), animatorImportSettings.Pet_Empty));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Eye_Emotion".ToLower(), animatorImportSettings.Pet_Eye_Emotion));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Eye_2_Emotion".ToLower(), animatorImportSettings.Pet_Eye_2_Emotion));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Eye_3_Emotion".ToLower(), animatorImportSettings.Pet_Eye_3_Emotion));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Mouth_Emotion".ToLower(), animatorImportSettings.Pet_Mouth_Emotion));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Mouth_2_Emotion".ToLower(), animatorImportSettings.Pet_Mouth_2_Emotion));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "Mouth_3_Emotion".ToLower(), animatorImportSettings.Pet_Mouth_3_Emotion));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "State".ToLower(), animatorImportSettings.Pet_State));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "State_2".ToLower(), animatorImportSettings.Pet_State_2));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "State_3".ToLower(), animatorImportSettings.Pet_State_3));
overrides.Add(new KeyValuePair<string, string>("Pet_" + "State_4".ToLower(), animatorImportSettings.Pet_State_4));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "Idle".ToLower(), animatorImportSettings.Movement_Idle));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "Empty".ToLower(), animatorImportSettings.Movement_Empty));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "Walk".ToLower(), animatorImportSettings.Walk));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "Run".ToLower(), animatorImportSettings.Run));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "Empty_2".ToLower(), animatorImportSettings.Empty_2));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "Start_Walk".ToLower(), animatorImportSettings.Start_Walk));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "End_Walk".ToLower(), animatorImportSettings.End_Walk));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "Empty_3".ToLower(), animatorImportSettings.Empty_3));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "Start_Run".ToLower(), animatorImportSettings.Start_Run));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "End_Run".ToLower(), animatorImportSettings.End_Run));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "Empty_4".ToLower(), animatorImportSettings.Empty_4));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "Start_Run_2".ToLower(), animatorImportSettings.Start_Run_2));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "End_Run_2".ToLower(), animatorImportSettings.End_Run_2));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "Empty_5".ToLower(), animatorImportSettings.Empty_5));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "Eye_Emotion".ToLower(), animatorImportSettings.Movement_Eye_Emotion));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "Eye_2_Emotion".ToLower(), animatorImportSettings.Movement_Eye_2_Emotion));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "Eye_3_Emotion".ToLower(), animatorImportSettings.Movement_Eye_3_Emotion));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "Mouth_Emotion".ToLower(), animatorImportSettings.Movement_Mouth_Emotion));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "Mouth_2_Emotion".ToLower(), animatorImportSettings.Movement_Mouth_2_Emotion));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "Mouth_3_Emotion".ToLower(), animatorImportSettings.Movement_Mouth_3_Emotion));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "State".ToLower(), animatorImportSettings.Movement_State));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "State_2".ToLower(), animatorImportSettings.Movement_State_2));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "State_3".ToLower(), animatorImportSettings.Movement_State_3));
overrides.Add(new KeyValuePair<string, string>("Movement_" + "State_4".ToLower(), animatorImportSettings.Movement_State_4));
#endregion
Debug.Log("Fight_" + "Idle".ToLower());
foreach (var animationFilePath in files)
{
var animationClip = AssetDatabase.LoadAssetAtPath<AnimationClip>(animationFilePath);
if (animationClip != null)
{
foreach (KeyValuePair<string, string> anim in overrides)
{
Debug.Log("Animation Clip name is: " + animationClip.name + " and Key is: " + anim.Key);
if (animationClip.name == anim.Key)
{
try
{ animatorOverrideController[anim.Value] = animationClip; }
catch (Exception e)
{
Debug.LogWarning("Override Animation" + anim.Value + "either does not exist or is written incorrectly.\n" + e.Message + "\n" +
e.StackTrace);
}
break;
}
}
}
}
animatorOverrideController.SaveAsset(animationsFolderPath + $"animController-{modelName}.overrideController");
animator.runtimeAnimatorController = animatorOverrideController;
AssetDatabase.Refresh();
}
private static void GenerateVisibilityAnimations (H3D h3DScene, string combinedExportFolder)
{
var modelTransform = GameObject.Find ("GeneratedUnityObject").transform;
foreach (var currentVisAnim in h3DScene.VisibilityAnimations) {
var fileCreated = false;
var animationClip = AssetDatabase.LoadAssetAtPath<AnimationClip> (combinedExportFolder + "/Animations/" + currentVisAnim.Name + ".anim");
if (animationClip == null) {
animationClip = new AnimationClip {name = currentVisAnim.Name};
var clipSettings = AnimationUtility.GetAnimationClipSettings (animationClip);
clipSettings.loopTime = currentVisAnim.AnimationFlags == H3DAnimationFlags.IsLooping;
AnimationUtility.SetAnimationClipSettings (animationClip, clipSettings);
fileCreated = true;
}
foreach (var animationElement in currentVisAnim.Elements) {
switch (animationElement.PrimitiveType) {
case H3DPrimitiveType.Boolean:
if (animationElement.TargetType == H3DTargetType.MeshNodeVisibility) {
var visCurve = new AnimationCurve ();
if (animationElement.Content is H3DAnimBoolean h3DAnimBoolean) {
var added = false;
for (var i = 0; i < h3DAnimBoolean.Values.Count; i++) {
visCurve.AddKey (KeyframeUtil.GetNew (
AnimationUtils.GetTimeAtFrame (animationClip, i),
h3DAnimBoolean.Values[i] ? 1f : 0f,
TangentMode.Stepped));
if (h3DAnimBoolean.Values[i] == false) {
added = true;
}
}
if (added) {
var skms = modelTransform.GetComponentsInChildren<Transform> ();
foreach (var skm in skms) {
if (skm.name.Replace (" (Instance)", "").Contains(animationElement.Name)) {
var cbp = AnimationUtility.CalculateTransformPath (skm.transform,
modelTransform);
animationClip.SetCurve (cbp, typeof(GameObject), "m_IsActive", visCurve);
}
}
}
}
}
break;
}
}
if (fileCreated) {
AssetDatabase.CreateAsset (animationClip, combinedExportFolder + "/Animations/" + currentVisAnim.Name + ".anim");
}
AssetDatabase.SaveAssets ();
AssetDatabase.Refresh ();
}
}
private static void GenerateMaterialAnimations (H3D h3DScene, string combinedExportFolder, P3ds2USettingsScriptableObject importerSettings)
{
var modelTransform = GameObject.Find ("GeneratedUnityObject").transform;
for (var index = 0; index < h3DScene.MaterialAnimations.Count; index++) {
var currentMatAnim = h3DScene.MaterialAnimations[index];
var fileCreated = false;
var animationClip =
AssetDatabase.LoadAssetAtPath<AnimationClip> (combinedExportFolder + "/Animations/" +
currentMatAnim.Name + ".anim");
if (animationClip == null) {
animationClip = new AnimationClip {name = currentMatAnim.Name};
var clipSettings = AnimationUtility.GetAnimationClipSettings (animationClip);
clipSettings.loopTime = currentMatAnim.AnimationFlags == H3DAnimationFlags.IsLooping;
AnimationUtility.SetAnimationClipSettings (animationClip, clipSettings);
fileCreated = true;
}
var newCurves = new Dictionary<AnimationUtils.AnimationCurveIndex, AnimationCurve> ();
foreach (var animationElement in currentMatAnim.Elements) {
switch (animationElement.PrimitiveType) {
case H3DPrimitiveType.Vector2D:
var targetType = animationElement.TargetType;
if (targetType != H3DTargetType.MaterialTexCoord0Trans &&
animationElement.TargetType != H3DTargetType.MaterialTexCoord1Trans &&
animationElement.TargetType != H3DTargetType.MaterialTexCoord2Trans) {
continue;
}
AnimationCurve curveY = null;
switch (targetType) {
case H3DTargetType.MaterialTexCoord0Trans:
curveY = AnimationUtils.GetOrAddCurve (newCurves,
AnimationUtils.MatAnimationModifier.Tex0TranslateY,
animationElement.Name);
break;
case H3DTargetType.MaterialTexCoord1Trans:
curveY = AnimationUtils.GetOrAddCurve (newCurves,
AnimationUtils.MatAnimationModifier.Tex1TranslateY,
animationElement.Name);
break;
case H3DTargetType.MaterialTexCoord2Trans:
curveY = AnimationUtils.GetOrAddCurve (newCurves,
AnimationUtils.MatAnimationModifier.Tex2TranslateY,
animationElement.Name);
break;
}
AnimationCurve curveX = null;
switch (targetType) {
case H3DTargetType.MaterialTexCoord0Trans:
curveX = AnimationUtils.GetOrAddCurve (newCurves,
AnimationUtils.MatAnimationModifier.Tex0TranslateX,
animationElement.Name);
break;
case H3DTargetType.MaterialTexCoord1Trans:
curveX = AnimationUtils.GetOrAddCurve (newCurves,
AnimationUtils.MatAnimationModifier.Tex1TranslateX,
animationElement.Name);
break;
case H3DTargetType.MaterialTexCoord2Trans:
curveX = AnimationUtils.GetOrAddCurve (newCurves,
AnimationUtils.MatAnimationModifier.Tex2TranslateX,
animationElement.Name);
break;
}
if (animationElement.Content is H3DAnimVector2D h3DAnimVector2D) {
var interpolateY = false;
foreach (var singleYFrame in h3DAnimVector2D.Y.KeyFrames) {
var lhs = singleYFrame.InSlope;
var rhs = singleYFrame.OutSlope;
TangentMode tangentMode;
if (lhs == 0 && rhs == 0) {
tangentMode = TangentMode.Stepped;
} else {
tangentMode = TangentMode.Linear;
}
if (!importerSettings.ImporterSettings.InterpolateAnimations) {
tangentMode = TangentMode.Stepped;
}
var time = AnimationUtils.GetTimeAtFrame (
animationClip,
(int) singleYFrame.Frame);
var keyFrame = KeyframeUtil.GetNew (time, 1 - singleYFrame.Value,tangentMode);
curveY.AddKey (keyFrame);
if (tangentMode == TangentMode.Linear) {
interpolateY = true;
}
}
if (interpolateY) {
curveY.UpdateAllLinearTangents ();
}
}
if (animationElement.Content is H3DAnimVector2D h3DAnimVector2d) {
var interpolateX = false;
foreach (var singleXFrame in h3DAnimVector2d.X.KeyFrames) {
var lhs = singleXFrame.InSlope;
var rhs = singleXFrame.OutSlope;
TangentMode tangentMode;
if (lhs == 0 && rhs == 0) {
tangentMode = TangentMode.Stepped;
} else {
tangentMode = TangentMode.Linear;
}
if (!importerSettings.ImporterSettings.InterpolateAnimations) {
tangentMode = TangentMode.Stepped;
}
float value;
if (targetType != H3DTargetType.MaterialTexCoord2Trans) {
value = (float) Math.Abs (Math.Round (1 - singleXFrame.Value,
MidpointRounding.AwayFromZero));
} else {
value = 2 - singleXFrame.Value;
}
var time = AnimationUtils.GetTimeAtFrame (animationClip, (int) singleXFrame.Frame);
var keyFrame = KeyframeUtil.GetNew (time, value,tangentMode);
curveX.AddKey (keyFrame);
if (tangentMode == TangentMode.Linear) {
interpolateX = true;
}
}
if (interpolateX) {
curveX.UpdateAllLinearTangents ();
}
}
foreach (var kvp in newCurves) {
var skms = modelTransform.GetComponentsInChildren<SkinnedMeshRenderer> ();
foreach (var skm in skms) {
if (skm.sharedMaterial.name.Replace (" (Instance)", "") == animationElement.Name) {
var cbp = AnimationUtility.CalculateTransformPath (skm.transform,
modelTransform);
var shaderPropName = AnimationUtils.MatModifierToShaderProp (kvp.Key.Modifier);
animationClip.SetCurve (cbp, typeof(SkinnedMeshRenderer),
shaderPropName, kvp.Value);
}
}
}
break;
}
}
if (fileCreated) {
AssetDatabase.CreateAsset (animationClip,
combinedExportFolder + "/Animations/" + currentMatAnim.Name + ".anim");
}
AssetDatabase.SaveAssets ();
AssetDatabase.Refresh ();
}
}
private static void OnAnimationPostprocessed (AnimationClip daeClip)
{
var newClip = new AnimationClip ();
var x = AnimationUtility.GetCurveBindings (daeClip);
foreach (var y in x) {
AnimationUtility.SetEditorCurve (newClip, y,
AnimationUtility.GetEditorCurve (daeClip, y));
}
var clipSettings = AnimationUtility.GetAnimationClipSettings (newClip);
clipSettings.loopTime = h3DScene.SkeletalAnimations[0].AnimationFlags == H3DAnimationFlags.IsLooping;
AnimationUtility.SetAnimationClipSettings(newClip, clipSettings);
if (!string.IsNullOrEmpty (CurrentAnimationExportFolder)) {
PokemonAnimationImporter.IsEnabled = false;
AssetDatabase.CreateAsset (newClip, CurrentAnimationExportFolder + h3DScene.SkeletalAnimations[CurrentAnimationIndex++].Name + ".anim");
AssetDatabase.Refresh ();
}
}
//NOTE: doing it this way because unity does keyframe compression on asset import.
//which won't happen if I generate the animation inside of Unity, also generating animations is hard =/
//Hit me up, if you know a better way of loading the animations from the binaries
private static void GenerateSkeletalAnimations (H3D h3DScene, string combinedExportFolder)
{
CurrentAnimationExportFolder = combinedExportFolder + "/Animations/";
var tempFilePath = CurrentAnimationExportFolder + "/BinInterimAnimation.dae";
if (!Directory.Exists (CurrentAnimationExportFolder)) {
Directory.CreateDirectory (CurrentAnimationExportFolder);
}
CurrentAnimationIndex = 0;
for (var i = 0; i < h3DScene.SkeletalAnimations.Count; i++) {
OnAnimationImportedEvent += OnAnimationPostprocessed;
var dae = new DaeAnimations (h3DScene, 0, i);
dae.Save (tempFilePath);
PokemonAnimationImporter.IsEnabled = true;
AssetDatabase.ImportAsset (tempFilePath);
AssetDatabase.Refresh(); //<- continue from AssetImporter.cs
OnAnimationImportedEvent -= OnAnimationPostprocessed;
}
PokemonAnimationImporter.IsEnabled = false;
if (File.Exists (tempFilePath)) {
File.Delete (tempFilePath);
File.Delete (tempFilePath + ".meta");
}
AssetDatabase.Refresh ();
}
private static void AddMaterialsToGeneratedMeshes (IReadOnlyDictionary<string, SkinnedMeshRenderer> meshDict,
IReadOnlyDictionary<string, Material> matDict, H3D h3dScene)
{
var h3DModel = h3dScene.Models[0];
foreach (var h3DMesh in h3DModel.Meshes)
foreach (var h3DSubMesh in h3DMesh.SubMeshes) {
var h3dSubMeshName = h3DModel.MeshNodesTree.Find (h3DMesh.NodeIndex) + "_" +
h3DModel.Meshes.IndexOf (h3DMesh) + "_" + h3DMesh.SubMeshes.IndexOf (h3DSubMesh);
var h3dMaterial = h3DModel.Materials[h3DMesh.MaterialIndex];
meshDict[h3dSubMeshName].sharedMaterial = matDict[h3dMaterial.Name];
}
AssetDatabase.Refresh ();
}
private static void GenerateTextureFiles (H3D h3DScene, string exportPath)
{
//Get raw texture data from Scene
var textureDict = new Dictionary<string, Texture2D> ();
foreach (var h3DTexture in h3DScene.Textures) {
try {
var width = h3DTexture.Width;
var height = h3DTexture.Height;
var colorArray = new List<Color32> ();
var buffer = h3DTexture.ToRGBA ();
for (var i = 0; i < buffer.Length; i += 4) {
var col = new Color32 (buffer[i + 0], buffer[i + 1], buffer[i + 2],
buffer[i + 3]);
colorArray.Add (col);
}
var texture = new Texture2D (width, height, TextureFormat.ARGB32, false) {name = h3DTexture.Name};
var colorCounter = 0;
for (var y = 0; y < texture.height; y++)
for (var x = 0; x < texture.width; x++)
texture.SetPixel (x, y, colorArray[colorCounter++]);
texture.Apply ();
textureDict.Add (texture.name, texture);
}
catch (Exception e) {
Debug.LogError ("Error importing texture: " + h3DTexture.Name + "\n" + e.Message + "\n" +
e.StackTrace);
}
}
var currentTextureExportFolder = exportPath + "/Textures/";
if (!Directory.Exists (currentTextureExportFolder)) {
Directory.CreateDirectory (currentTextureExportFolder);
}
foreach (var kvp in textureDict)
File.WriteAllBytes (currentTextureExportFolder + kvp.Key + ".png", kvp.Value.EncodeToPNG ());
AssetDatabase.Refresh ();
}
public enum MaterialType
{
Body,
Iris,
Core,
Stencil
}
private static Dictionary<string, Material> GenerateShinyMaterialFiles (H3D h3DScene, string exportPath,
P3ds2UShaderProperties shaderImportSettings)
{
var currentMaterialExportFolder = exportPath + "/Materials/Shiny/";
if (!Directory.Exists (currentMaterialExportFolder)) {
Directory.CreateDirectory (currentMaterialExportFolder);
}
var matDict = new Dictionary<string, Material> ();
foreach (var h3dMaterial in h3DScene.Models[0].Materials) {
var filePath = currentMaterialExportFolder + h3dMaterial.Name + ".mat";
Shader shaderToApply;
MaterialType materialType;
if ((int) h3dMaterial.MaterialParams.MetaData[0].Values[0] == 2) {
shaderToApply = shaderImportSettings.irisShader;
materialType = MaterialType.Iris;
} else {
shaderToApply = shaderImportSettings.bodyShader;
materialType = MaterialType.Body;
}
if (h3dMaterial.MaterialParams.StencilTest.Enabled) {
if (h3dMaterial.MaterialParams.StencilTest.Function == (PICATestFunc) 1) {
shaderToApply = shaderImportSettings.fireCoreShader;
materialType = MaterialType.Core;
}
if (h3dMaterial.MaterialParams.StencilTest.Function == (PICATestFunc) 2) {
shaderToApply = shaderImportSettings.fireStencilShader;
materialType = MaterialType.Stencil;
}
}
Material newMaterial;
if (File.Exists (filePath)) {
newMaterial = AssetDatabase.LoadAssetAtPath<Material> (filePath);
newMaterial.shader = shaderToApply;
} else {
newMaterial = new Material (shaderToApply);
}
newMaterial.shaderKeywords = new[]
{"_MAIN_LIGHT_CALCULATE_SHADOWS", "_MAIN_LIGHT_SHADOW_CASCADE", " _SHADOWS_SOFT"};
var mainTexturePath = exportPath + "/Textures/" + h3dMaterial.Texture0Name + "_1.png";
var mainTexture = (Texture2D) AssetDatabase.LoadAssetAtPath (mainTexturePath, typeof(Texture2D));
if (mainTexture != null) {
var mainTextureRepresentation = new TextureUtils.H3DTextureRepresentation {
TextureCoord = h3dMaterial.MaterialParams.TextureCoords[0],
TextureMapper = h3dMaterial.TextureMappers[0]
};
var importer = (TextureImporter) AssetImporter.GetAtPath (mainTexturePath);
importer.wrapModeU =
TextureUtils.PicaToUnityTextureWrapMode (mainTextureRepresentation.TextureMapper.WrapU);
importer.wrapModeV =
TextureUtils.PicaToUnityTextureWrapMode (mainTextureRepresentation.TextureMapper.WrapV);
importer.maxTextureSize = 256;
AssetDatabase.ImportAsset (mainTexturePath, ImportAssetOptions.ForceUpdate);
newMaterial.SetVector (Shader.PropertyToID(shaderImportSettings.BaseMapTiling),
new Vector4 (mainTextureRepresentation.TextureCoord.Scale.X,
mainTextureRepresentation.TextureCoord.Scale.Y, 0, 0));
newMaterial.SetVector (Shader.PropertyToID (shaderImportSettings.BaseMapOffset),
new Vector4 (0, 0));
newMaterial.SetTexture (Shader.PropertyToID (shaderImportSettings.BaseMap),
mainTexture);
newMaterial.mainTexture = mainTexture;
}
var normalMapPath = exportPath + "/Textures/" + h3dMaterial.Texture2Name + "_1.png";
var normalTexture = (Texture2D) AssetDatabase.LoadAssetAtPath (normalMapPath, typeof(Texture2D));
if (normalTexture != null) {
var normalTextureRepresentation = new TextureUtils.H3DTextureRepresentation {
TextureCoord = h3dMaterial.MaterialParams.TextureCoords[2],
TextureMapper = h3dMaterial.TextureMappers[2]
};
var importer = (TextureImporter) AssetImporter.GetAtPath (normalMapPath);
// importer.textureType = TextureImporterType.NormalMap;
importer.wrapModeU =
TextureUtils.PicaToUnityTextureWrapMode (normalTextureRepresentation.TextureMapper.WrapU);
importer.wrapModeV =
TextureUtils.PicaToUnityTextureWrapMode (normalTextureRepresentation.TextureMapper.WrapV);
importer.maxTextureSize = 256;
AssetDatabase.ImportAsset (normalMapPath, ImportAssetOptions.ForceUpdate);
newMaterial.SetVector (Shader.PropertyToID(shaderImportSettings.NormalMapTiling),
new Vector4 (normalTextureRepresentation.TextureCoord.Scale.X,
normalTextureRepresentation.TextureCoord.Scale.Y, 0, 0));
newMaterial.SetVector (Shader.PropertyToID (shaderImportSettings.NormalMapOffset),
new Vector4 (normalTextureRepresentation.TextureCoord.Translation.X,
normalTextureRepresentation.TextureCoord.Translation.Y));
newMaterial.SetTexture (Shader.PropertyToID (shaderImportSettings.NormalMap),
normalTexture);
}
var occlusionMapPath = exportPath + "/Textures/" + h3dMaterial.Texture1Name + "_1.png";
var occlusionTexture = (Texture2D) AssetDatabase.LoadAssetAtPath (occlusionMapPath, typeof(Texture2D));
if (occlusionTexture != null) {
var occlusionMapRepresentation = new TextureUtils.H3DTextureRepresentation {
TextureCoord = h3dMaterial.MaterialParams.TextureCoords[1],
TextureMapper = h3dMaterial.TextureMappers[1]
};
var importer = (TextureImporter) AssetImporter.GetAtPath (occlusionMapPath);
importer.wrapModeU =
TextureUtils.PicaToUnityTextureWrapMode (occlusionMapRepresentation.TextureMapper.WrapU);
importer.wrapModeV =
TextureUtils.PicaToUnityTextureWrapMode (occlusionMapRepresentation.TextureMapper.WrapV);
importer.maxTextureSize = 256;
AssetDatabase.ImportAsset (occlusionMapPath, ImportAssetOptions.ForceUpdate);
newMaterial.SetVector (Shader.PropertyToID(shaderImportSettings.OcclusionMapTiling),
new Vector4 (occlusionMapRepresentation.TextureCoord.Scale.X,
occlusionMapRepresentation.TextureCoord.Scale.Y, 0, 0));
newMaterial.SetVector (Shader.PropertyToID (shaderImportSettings.OcclusionMapOffset),
new Vector4 (occlusionMapRepresentation.TextureCoord.Translation.X,
occlusionMapRepresentation.TextureCoord.Translation.Y));
newMaterial.SetTexture (Shader.PropertyToID (shaderImportSettings.OcclusionMap),
occlusionTexture);
}
if (materialType == MaterialType.Core) {
newMaterial.SetColor (Shader.PropertyToID (shaderImportSettings.Constant4Color),
new Color32 (
h3dMaterial.MaterialParams.Constant4Color.R,
h3dMaterial.MaterialParams.Constant4Color.G,
h3dMaterial.MaterialParams.Constant4Color.B,
h3dMaterial.MaterialParams.Constant4Color.A
));
// newMaterial.SetVector ("_TestV4", testVector4);
} else if (materialType == MaterialType.Stencil) {
newMaterial.SetColor (Shader.PropertyToID (shaderImportSettings.Constant4Color),
new Color32 (
h3dMaterial.MaterialParams.Constant4Color.R,
h3dMaterial.MaterialParams.Constant4Color.G,
h3dMaterial.MaterialParams.Constant4Color.B,
h3dMaterial.MaterialParams.Constant4Color.A
));
newMaterial.SetColor (Shader.PropertyToID (shaderImportSettings.Constant3Color),
new Color32 (
h3dMaterial.MaterialParams.Constant3Color.R,
h3dMaterial.MaterialParams.Constant3Color.G,
h3dMaterial.MaterialParams.Constant3Color.B,
h3dMaterial.MaterialParams.Constant3Color.A
));
}
if (!File.Exists (filePath)) {
AssetDatabase.CreateAsset (newMaterial, currentMaterialExportFolder + h3dMaterial.Name + ".mat");
}
matDict.Add (h3dMaterial.Name, newMaterial);
}
AssetDatabase.SaveAssets ();
return matDict;
}
private static Dictionary<string, Material> GenerateMaterialFiles (H3D h3DScene, string exportPath,
P3ds2UShaderProperties shaderImportSettings)
{
var currentMaterialExportFolder = exportPath + "/Materials/";
if (!Directory.Exists (currentMaterialExportFolder)) {
Directory.CreateDirectory (currentMaterialExportFolder);
}
var matDict = new Dictionary<string, Material> ();
foreach (var h3dMaterial in h3DScene.Models[0].Materials) {
var filePath = currentMaterialExportFolder + h3dMaterial.Name + ".mat";
Shader shaderToApply;
MaterialType materialType;
if ((int) h3dMaterial.MaterialParams.MetaData[0].Values[0] == 2) {
shaderToApply = shaderImportSettings.irisShader;
materialType = MaterialType.Iris;
} else {
shaderToApply = shaderImportSettings.bodyShader;
materialType = MaterialType.Body;
}
if (h3dMaterial.MaterialParams.StencilTest.Enabled) {
if (h3dMaterial.MaterialParams.StencilTest.Function == (PICATestFunc) 1) {
shaderToApply = shaderImportSettings.fireCoreShader;
materialType = MaterialType.Core;
}
if (h3dMaterial.MaterialParams.StencilTest.Function == (PICATestFunc) 2) {
shaderToApply = shaderImportSettings.fireStencilShader;
materialType = MaterialType.Stencil;
}
}
Material newMaterial;
if (File.Exists (filePath)) {
newMaterial = AssetDatabase.LoadAssetAtPath<Material> (filePath);
newMaterial.shader = shaderToApply;
} else {
newMaterial = new Material (shaderToApply);
}
newMaterial.shaderKeywords = new[]
{"_MAIN_LIGHT_CALCULATE_SHADOWS", "_MAIN_LIGHT_SHADOW_CASCADE", " _SHADOWS_SOFT"};
var mainTexturePath = exportPath + "/Textures/" + h3dMaterial.Texture0Name + ".png";
var mainTexture = (Texture2D) AssetDatabase.LoadAssetAtPath (mainTexturePath, typeof(Texture2D));
if (mainTexture != null) {
var mainTextureRepresentation = new TextureUtils.H3DTextureRepresentation {
TextureCoord = h3dMaterial.MaterialParams.TextureCoords[0],
TextureMapper = h3dMaterial.TextureMappers[0]
};
var importer = (TextureImporter) AssetImporter.GetAtPath (mainTexturePath);
importer.wrapModeU =
TextureUtils.PicaToUnityTextureWrapMode (mainTextureRepresentation.TextureMapper.WrapU);
importer.wrapModeV =
TextureUtils.PicaToUnityTextureWrapMode (mainTextureRepresentation.TextureMapper.WrapV);
importer.maxTextureSize = 256;
AssetDatabase.ImportAsset (mainTexturePath, ImportAssetOptions.ForceUpdate);
newMaterial.SetVector (Shader.PropertyToID(shaderImportSettings.BaseMapTiling),
new Vector4 (mainTextureRepresentation.TextureCoord.Scale.X,
mainTextureRepresentation.TextureCoord.Scale.Y, 0, 0));
newMaterial.SetVector (Shader.PropertyToID (shaderImportSettings.BaseMapOffset),
new Vector4 (0, 0));
newMaterial.SetTexture (Shader.PropertyToID (shaderImportSettings.BaseMap),
mainTexture);
newMaterial.mainTexture = mainTexture;
}
var normalMapPath = exportPath + "/Textures/" + h3dMaterial.Texture2Name + ".png";
var normalTexture = (Texture2D) AssetDatabase.LoadAssetAtPath (normalMapPath, typeof(Texture2D));
if (normalTexture != null) {
var normalTextureRepresentation = new TextureUtils.H3DTextureRepresentation {
TextureCoord = h3dMaterial.MaterialParams.TextureCoords[2],
TextureMapper = h3dMaterial.TextureMappers[2]
};
var importer = (TextureImporter) AssetImporter.GetAtPath (normalMapPath);
// importer.textureType = TextureImporterType.NormalMap;
importer.wrapModeU =
TextureUtils.PicaToUnityTextureWrapMode (normalTextureRepresentation.TextureMapper.WrapU);
importer.wrapModeV =
TextureUtils.PicaToUnityTextureWrapMode (normalTextureRepresentation.TextureMapper.WrapV);
importer.maxTextureSize = 256;
AssetDatabase.ImportAsset (normalMapPath, ImportAssetOptions.ForceUpdate);
newMaterial.SetVector (Shader.PropertyToID(shaderImportSettings.NormalMapTiling),
new Vector4 (normalTextureRepresentation.TextureCoord.Scale.X,
normalTextureRepresentation.TextureCoord.Scale.Y, 0, 0));
newMaterial.SetVector (Shader.PropertyToID (shaderImportSettings.NormalMapOffset),
new Vector4 (normalTextureRepresentation.TextureCoord.Translation.X,
normalTextureRepresentation.TextureCoord.Translation.Y));
newMaterial.SetTexture (Shader.PropertyToID (shaderImportSettings.NormalMap),
normalTexture);
}
var occlusionMapPath = exportPath + "/Textures/" + h3dMaterial.Texture1Name + ".png";
var occlusionTexture = (Texture2D) AssetDatabase.LoadAssetAtPath (occlusionMapPath, typeof(Texture2D));
if (occlusionTexture != null) {
var occlusionMapRepresentation = new TextureUtils.H3DTextureRepresentation {
TextureCoord = h3dMaterial.MaterialParams.TextureCoords[1],
TextureMapper = h3dMaterial.TextureMappers[1]
};
var importer = (TextureImporter) AssetImporter.GetAtPath (occlusionMapPath);
importer.wrapModeU =
TextureUtils.PicaToUnityTextureWrapMode (occlusionMapRepresentation.TextureMapper.WrapU);
importer.wrapModeV =
TextureUtils.PicaToUnityTextureWrapMode (occlusionMapRepresentation.TextureMapper.WrapV);
importer.maxTextureSize = 256;
AssetDatabase.ImportAsset (occlusionMapPath, ImportAssetOptions.ForceUpdate);
newMaterial.SetVector (Shader.PropertyToID(shaderImportSettings.OcclusionMapTiling),
new Vector4 (occlusionMapRepresentation.TextureCoord.Scale.X,
occlusionMapRepresentation.TextureCoord.Scale.Y, 0, 0));
newMaterial.SetVector (Shader.PropertyToID (shaderImportSettings.OcclusionMapOffset),
new Vector4 (occlusionMapRepresentation.TextureCoord.Translation.X,
occlusionMapRepresentation.TextureCoord.Translation.Y));
newMaterial.SetTexture (Shader.PropertyToID (shaderImportSettings.OcclusionMap),
occlusionTexture);
}
if (materialType == MaterialType.Core) {
newMaterial.SetColor (Shader.PropertyToID (shaderImportSettings.Constant4Color),
new Color32 (
h3dMaterial.MaterialParams.Constant4Color.R,
h3dMaterial.MaterialParams.Constant4Color.G,
h3dMaterial.MaterialParams.Constant4Color.B,
h3dMaterial.MaterialParams.Constant4Color.A
));
// newMaterial.SetVector ("_TestV4", testVector4);
} else if (materialType == MaterialType.Stencil) {
newMaterial.SetColor (Shader.PropertyToID (shaderImportSettings.Constant4Color),
new Color32 (
h3dMaterial.MaterialParams.Constant4Color.R,
h3dMaterial.MaterialParams.Constant4Color.G,
h3dMaterial.MaterialParams.Constant4Color.B,
h3dMaterial.MaterialParams.Constant4Color.A
));
newMaterial.SetColor (Shader.PropertyToID (shaderImportSettings.Constant3Color),
new Color32 (
h3dMaterial.MaterialParams.Constant3Color.R,
h3dMaterial.MaterialParams.Constant3Color.G,
h3dMaterial.MaterialParams.Constant3Color.B,
h3dMaterial.MaterialParams.Constant3Color.A
));
}
if (!File.Exists (filePath)) {
AssetDatabase.CreateAsset (newMaterial, currentMaterialExportFolder + h3dMaterial.Name + ".mat");
}
matDict.Add (h3dMaterial.Name, newMaterial);
}
AssetDatabase.SaveAssets ();
return matDict;
}