-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathUnityHumanoidVMDRecorder.cs
913 lines (816 loc) · 48.1 KB
/
UnityHumanoidVMDRecorder.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
using System.Linq;
using System.Threading.Tasks;
//初期ポーズ(T,Aポーズ)の時点でアタッチ、有効化されている必要がある
public class UnityHumanoidVMDRecorder : MonoBehaviour
{
public bool UseParentOfAll = true;
public bool UseCenterAsParentOfAll = true;
/// <summary>
/// 全ての親の座標・回転を絶対座標系で計算する
/// UseParentOfAllがTrueでないと意味がない
/// </summary>
public bool UseAbsoluteCoordinateSystem = true;
public bool IgnoreInitialPosition = false;
public bool IgnoreInitialRotation = false;
/// <summary>
/// 一部のモデルではMMD上ではセンターが足元にある
/// Start前に設定されている必要がある
/// </summary>
public bool UseBottomCenter = false;
/// <summary>
/// Unity上のモーフ名に1.まばたきなど番号が振られている場合、番号を除去する
/// </summary>
public bool TrimMorphNumber = true;
public int KeyReductionLevel = 3;
public bool IsRecording { get; private set; } = false;
public int FrameNumber { get; private set; } = 0;
int frameNumberSaved = 0;
const float FPSs = 0.03333f;
const string CenterNameString = "センター";
const string GrooveNameString = "グルーブ";
public enum BoneNames
{
全ての親, センター, 左足IK, 右足IK, 上半身, 上半身2, 首, 頭,
左肩, 左腕, 左ひじ, 左手首, 右肩, 右腕, 右ひじ, 右手首,
左親指1, 左親指2, 左人指1, 左人指2, 左人指3, 左中指1, 左中指2, 左中指3,
左薬指1, 左薬指2, 左薬指3, 左小指1, 左小指2, 左小指3, 右親指1, 右親指2,
右人指1, 右人指2, 右人指3, 右中指1, 右中指2, 右中指3, 右薬指1, 右薬指2,
右薬指3, 右小指1, 右小指2, 右小指3, 左足, 右足, 左ひざ, 右ひざ,
左足首, 右足首, None
//左つま先, 右つま先は情報付けると足首の回転、位置との矛盾が生じかねない
}
//コンストラクタにて初期化
//全てのボーンを名前で引く辞書
Dictionary<string, Transform> transformDictionary = new Dictionary<string, Transform>();
public Dictionary<BoneNames, Transform> BoneDictionary { get; private set; }
Vector3 parentInitialPosition = Vector3.zero;
Quaternion parentInitialRotation = Quaternion.identity;
Dictionary<BoneNames, List<Vector3>> positionDictionary = new Dictionary<BoneNames, List<Vector3>>();
Dictionary<BoneNames, List<Vector3>> positionDictionarySaved = new Dictionary<BoneNames, List<Vector3>>();
Dictionary<BoneNames, List<Quaternion>> rotationDictionary = new Dictionary<BoneNames, List<Quaternion>>();
Dictionary<BoneNames, List<Quaternion>> rotationDictionarySaved = new Dictionary<BoneNames, List<Quaternion>>();
//ボーン移動量の補正係数
//この値は大体の値、正確ではない
const float DefaultBoneAmplifier = 12.5f;
public Vector3 ParentOfAllOffset = new Vector3(0, 0, 0);
public Vector3 LeftFootIKOffset = Vector3.zero;
public Vector3 RightFootIKOffset = Vector3.zero;
Animator animator;
BoneGhost boneGhost;
MorphRecorder morphRecorder;
MorphRecorder morphRecorderSaved;
// Start is called before the first frame update
void Start()
{
Time.fixedDeltaTime = FPSs;
animator = GetComponent<Animator>();
BoneDictionary = new Dictionary<BoneNames, Transform>()
{
//下半身などというものはUnityにはない
{ BoneNames.全ての親, (transform) },
{ BoneNames.センター, (animator.GetBoneTransform(HumanBodyBones.Hips))},
{ BoneNames.上半身, (animator.GetBoneTransform(HumanBodyBones.Spine))},
{ BoneNames.上半身2, (animator.GetBoneTransform(HumanBodyBones.Chest))},
{ BoneNames.頭, (animator.GetBoneTransform(HumanBodyBones.Head))},
{ BoneNames.首, (animator.GetBoneTransform(HumanBodyBones.Neck))},
{ BoneNames.左肩, (animator.GetBoneTransform(HumanBodyBones.LeftShoulder))},
{ BoneNames.右肩, (animator.GetBoneTransform(HumanBodyBones.RightShoulder))},
{ BoneNames.左腕, (animator.GetBoneTransform(HumanBodyBones.LeftUpperArm))},
{ BoneNames.右腕, (animator.GetBoneTransform(HumanBodyBones.RightUpperArm))},
{ BoneNames.左ひじ, (animator.GetBoneTransform(HumanBodyBones.LeftLowerArm))},
{ BoneNames.右ひじ, (animator.GetBoneTransform(HumanBodyBones.RightLowerArm))},
{ BoneNames.左手首, (animator.GetBoneTransform(HumanBodyBones.LeftHand))},
{ BoneNames.右手首, (animator.GetBoneTransform(HumanBodyBones.RightHand))},
{ BoneNames.左親指1, (animator.GetBoneTransform(HumanBodyBones.LeftThumbProximal))},
{ BoneNames.右親指1, (animator.GetBoneTransform(HumanBodyBones.RightThumbProximal))},
{ BoneNames.左親指2, (animator.GetBoneTransform(HumanBodyBones.LeftThumbIntermediate))},
{ BoneNames.右親指2, (animator.GetBoneTransform(HumanBodyBones.RightThumbIntermediate))},
{ BoneNames.左人指1, (animator.GetBoneTransform(HumanBodyBones.LeftIndexProximal))},
{ BoneNames.右人指1, (animator.GetBoneTransform(HumanBodyBones.RightIndexProximal))},
{ BoneNames.左人指2, (animator.GetBoneTransform(HumanBodyBones.LeftIndexIntermediate))},
{ BoneNames.右人指2, (animator.GetBoneTransform(HumanBodyBones.RightIndexIntermediate))},
{ BoneNames.左人指3, (animator.GetBoneTransform(HumanBodyBones.LeftIndexDistal))},
{ BoneNames.右人指3, (animator.GetBoneTransform(HumanBodyBones.RightIndexDistal))},
{ BoneNames.左中指1, (animator.GetBoneTransform(HumanBodyBones.LeftMiddleProximal))},
{ BoneNames.右中指1, (animator.GetBoneTransform(HumanBodyBones.RightMiddleProximal))},
{ BoneNames.左中指2, (animator.GetBoneTransform(HumanBodyBones.LeftMiddleIntermediate))},
{ BoneNames.右中指2, (animator.GetBoneTransform(HumanBodyBones.RightMiddleIntermediate))},
{ BoneNames.左中指3, (animator.GetBoneTransform(HumanBodyBones.LeftMiddleDistal))},
{ BoneNames.右中指3, (animator.GetBoneTransform(HumanBodyBones.RightMiddleDistal))},
{ BoneNames.左薬指1, (animator.GetBoneTransform(HumanBodyBones.LeftRingProximal))},
{ BoneNames.右薬指1, (animator.GetBoneTransform(HumanBodyBones.RightRingProximal))},
{ BoneNames.左薬指2, (animator.GetBoneTransform(HumanBodyBones.LeftRingIntermediate))},
{ BoneNames.右薬指2, (animator.GetBoneTransform(HumanBodyBones.RightRingIntermediate))},
{ BoneNames.左薬指3, (animator.GetBoneTransform(HumanBodyBones.LeftRingDistal))},
{ BoneNames.右薬指3, (animator.GetBoneTransform(HumanBodyBones.RightRingDistal))},
{ BoneNames.左小指1, (animator.GetBoneTransform(HumanBodyBones.LeftLittleProximal))},
{ BoneNames.右小指1, (animator.GetBoneTransform(HumanBodyBones.RightLittleProximal))},
{ BoneNames.左小指2, (animator.GetBoneTransform(HumanBodyBones.LeftLittleIntermediate))},
{ BoneNames.右小指2, (animator.GetBoneTransform(HumanBodyBones.RightLittleIntermediate))},
{ BoneNames.左小指3, (animator.GetBoneTransform(HumanBodyBones.LeftLittleDistal))},
{ BoneNames.右小指3, (animator.GetBoneTransform(HumanBodyBones.RightLittleDistal))},
{ BoneNames.左足IK, (animator.GetBoneTransform(HumanBodyBones.LeftFoot))},
{ BoneNames.右足IK, (animator.GetBoneTransform(HumanBodyBones.RightFoot))},
{ BoneNames.左足, (animator.GetBoneTransform(HumanBodyBones.LeftUpperLeg))},
{ BoneNames.右足, (animator.GetBoneTransform(HumanBodyBones.RightUpperLeg))},
{ BoneNames.左ひざ, (animator.GetBoneTransform(HumanBodyBones.LeftLowerLeg))},
{ BoneNames.右ひざ, (animator.GetBoneTransform(HumanBodyBones.RightLowerLeg))},
{ BoneNames.左足首, (animator.GetBoneTransform(HumanBodyBones.LeftFoot))},
{ BoneNames.右足首, (animator.GetBoneTransform(HumanBodyBones.RightFoot))},
//左つま先, 右つま先は情報付けると足首の回転、位置との矛盾が生じかねない
//{ BoneNames.左つま先, (animator.GetBoneTransform(HumanBodyBones.LeftToes))},
//{ BoneNames.右つま先, (animator.GetBoneTransform(HumanBodyBones.RightToes))}
};
makeTransformDictionary(transform, transformDictionary);
void makeTransformDictionary(Transform rootBone, Dictionary<string, Transform> dictionary)
{
if (dictionary.ContainsKey(rootBone.name)) { return; }
dictionary.Add(rootBone.name, rootBone);
foreach (Transform childT in rootBone)
{
makeTransformDictionary(childT, dictionary);
}
}
EnforceInitialPose(animator, true);
SetInitialPositionAndRotation();
foreach (BoneNames boneName in BoneDictionary.Keys)
{
if (BoneDictionary[boneName] == null) { continue; }
positionDictionary.Add(boneName, new List<Vector3>());
rotationDictionary.Add(boneName, new List<Quaternion>());
}
if (BoneDictionary[BoneNames.左足IK] != null)
{
LeftFootIKOffset = Quaternion.Inverse(transform.rotation) * (BoneDictionary[BoneNames.左足IK].position - transform.position);
}
if (BoneDictionary[BoneNames.右足IK] != null)
{
RightFootIKOffset = Quaternion.Inverse(transform.rotation) * (BoneDictionary[BoneNames.右足IK].position - transform.position);
}
boneGhost = new BoneGhost(animator, BoneDictionary, UseBottomCenter);
morphRecorder = new MorphRecorder(transform);
}
void EnforceInitialPose(Animator animator, bool aPose = false)
{
if (animator == null)
{
UnityEngine.Debug.Log("EnforceInitialPose");
UnityEngine.Debug.Log("Animatorがnullです");
return;
}
const int APoseDegree = 30;
Vector3 position = animator.transform.position;
Quaternion rotation = animator.transform.rotation;
animator.transform.position = Vector3.zero;
animator.transform.rotation = Quaternion.identity;
int count = animator.avatar.humanDescription.skeleton.Length;
for (int i = 0; i < count; i++)
{
if (!transformDictionary.ContainsKey(animator.avatar.humanDescription.skeleton[i].name))
{
continue;
}
transformDictionary[animator.avatar.humanDescription.skeleton[i].name].localPosition
= animator.avatar.humanDescription.skeleton[i].position;
transformDictionary[animator.avatar.humanDescription.skeleton[i].name].localRotation
= animator.avatar.humanDescription.skeleton[i].rotation;
}
animator.transform.position = position;
animator.transform.rotation = rotation;
if (aPose && animator.isHuman)
{
Transform leftUpperArm = animator.GetBoneTransform(HumanBodyBones.LeftUpperArm);
Transform rightUpperArm = animator.GetBoneTransform(HumanBodyBones.RightUpperArm);
if (leftUpperArm == null || rightUpperArm == null) { return; }
leftUpperArm.Rotate(animator.transform.forward, APoseDegree, Space.World);
rightUpperArm.Rotate(animator.transform.forward, -APoseDegree, Space.World);
}
}
private void FixedUpdate()
{
if (IsRecording)
{
SaveFrame();
FrameNumber++;
}
}
void SaveFrame()
{
if (boneGhost != null) { boneGhost.GhostAll(); }
if (morphRecorder != null) { morphRecorder.RecrodAllMorph(); }
foreach (BoneNames boneName in BoneDictionary.Keys)
{
if (BoneDictionary[boneName] == null)
{
continue;
}
if (boneName == BoneNames.右足IK || boneName == BoneNames.左足IK)
{
Vector3 targetVector = Vector3.zero;
if (UseCenterAsParentOfAll)
{
if ((!UseAbsoluteCoordinateSystem && transform.parent != null) && IgnoreInitialPosition )
{
targetVector
= Quaternion.Inverse(transform.parent.rotation)
* (BoneDictionary[boneName].position - transform.parent.position)
- parentInitialPosition;
}
else if ((!UseAbsoluteCoordinateSystem && transform.parent != null) && !IgnoreInitialPosition)
{
targetVector
= Quaternion.Inverse(transform.parent.rotation)
* (BoneDictionary[boneName].position - transform.parent.position);
}
else if ((UseAbsoluteCoordinateSystem || transform.parent == null) && IgnoreInitialPosition)
{
targetVector = BoneDictionary[boneName].position - parentInitialPosition;
}
else if ((UseAbsoluteCoordinateSystem || transform.parent == null) && transform.parent && !IgnoreInitialPosition)
{
targetVector = BoneDictionary[boneName].position;
}
}
else
{
targetVector = BoneDictionary[boneName].position - transform.position;
targetVector = Quaternion.Inverse(transform.rotation) * targetVector;
}
targetVector -= (boneName == BoneNames.左足IK ? LeftFootIKOffset : RightFootIKOffset);
Vector3 ikPosition = new Vector3(-targetVector.x, targetVector.y, -targetVector.z);
positionDictionary[boneName].Add(ikPosition * DefaultBoneAmplifier);
//回転は全部足首に持たせる
Quaternion ikRotation = Quaternion.identity;
rotationDictionary[boneName].Add(ikRotation);
continue;
}
if (boneGhost != null && boneGhost.GhostDictionary.Keys.Contains(boneName))
{
if (boneGhost.GhostDictionary[boneName].ghost == null || !boneGhost.GhostDictionary[boneName].enabled)
{
rotationDictionary[boneName].Add(Quaternion.identity);
positionDictionary[boneName].Add(Vector3.zero);
continue;
}
Vector3 boneVector = boneGhost.GhostDictionary[boneName].ghost.localPosition;
Quaternion boneQuatenion = boneGhost.GhostDictionary[boneName].ghost.localRotation;
rotationDictionary[boneName].Add(new Quaternion(-boneQuatenion.x, boneQuatenion.y, -boneQuatenion.z, boneQuatenion.w));
boneVector -= boneGhost.GhostOriginalLocalPositionDictionary[boneName];
positionDictionary[boneName].Add(new Vector3(-boneVector.x, boneVector.y, -boneVector.z) * DefaultBoneAmplifier);
continue;
}
Quaternion fixedQuatenion = Quaternion.identity;
Quaternion vmdRotation = Quaternion.identity;
if (boneName == BoneNames.全ての親 && UseAbsoluteCoordinateSystem)
{
fixedQuatenion = BoneDictionary[boneName].rotation;
}
else
{
fixedQuatenion = BoneDictionary[boneName].localRotation;
}
if (boneName == BoneNames.全ての親 && IgnoreInitialRotation)
{
fixedQuatenion = BoneDictionary[boneName].localRotation.MinusRotation(parentInitialRotation);
}
vmdRotation = new Quaternion(-fixedQuatenion.x, fixedQuatenion.y, -fixedQuatenion.z, fixedQuatenion.w);
rotationDictionary[boneName].Add(vmdRotation);
Vector3 fixedPosition = Vector3.zero;
Vector3 vmdPosition = Vector3.zero;
if (boneName == BoneNames.全ての親 && UseAbsoluteCoordinateSystem)
{
fixedPosition = BoneDictionary[boneName].position;
}
else
{
fixedPosition = BoneDictionary[boneName].localPosition;
}
if (boneName == BoneNames.全ての親 && IgnoreInitialPosition)
{
fixedPosition -= parentInitialPosition;
}
vmdPosition = new Vector3(-fixedPosition.x, fixedPosition.y, -fixedPosition.z);
if (boneName == BoneNames.全ての親)
{
positionDictionary[boneName].Add(vmdPosition * DefaultBoneAmplifier + ParentOfAllOffset);
}
else
{
positionDictionary[boneName].Add(vmdPosition * DefaultBoneAmplifier);
}
}
}
void SetInitialPositionAndRotation()
{
if (UseAbsoluteCoordinateSystem)
{
parentInitialPosition = transform.position;
parentInitialRotation = transform.rotation;
}
else
{
parentInitialPosition = transform.localPosition;
parentInitialRotation = transform.localRotation;
}
}
public static void SetFPS(int fps)
{
Time.fixedDeltaTime = 1 / (float)fps;
}
/// <summary>
/// レコーディングを開始または再開
/// </summary>
public void StartRecording()
{
SetInitialPositionAndRotation();
IsRecording = true;
}
/// <summary>
/// レコーディングを一時停止
/// </summary>
public void PauseRecording() { IsRecording = false; }
/// <summary>
/// レコーディングを終了
/// </summary>
public void StopRecording()
{
IsRecording = false;
frameNumberSaved = FrameNumber;
morphRecorderSaved = morphRecorder;
FrameNumber = 0;
positionDictionarySaved = positionDictionary;
positionDictionary = new Dictionary<BoneNames, List<Vector3>>();
rotationDictionarySaved = rotationDictionary;
rotationDictionary = new Dictionary<BoneNames, List<Quaternion>>();
foreach (BoneNames boneName in BoneDictionary.Keys)
{
if (BoneDictionary[boneName] == null) { continue; }
positionDictionary.Add(boneName, new List<Vector3>());
rotationDictionary.Add(boneName, new List<Quaternion>());
}
morphRecorder = new MorphRecorder(transform);
}
/// <summary>
/// VMDを作成する
/// 呼び出す際は先にStopRecordingを呼び出すこと
/// </summary>
/// <param name="modelName">VMDファイルに記載される専用モデル名</param>
/// <param name="filePath">保存先の絶対ファイルパス</param>
public async void SaveVMD(string modelName, string filePath)
{
if (IsRecording)
{
Debug.Log(transform.name + "VMD保存前にレコーディングをストップしてください。");
return;
}
if (KeyReductionLevel <= 0) { KeyReductionLevel = 1; }
Debug.Log(transform.name + "VMDファイル作成開始");
await Task.Run(() =>
{
//ファイルの書き込み
using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
using (BinaryWriter binaryWriter = new BinaryWriter(fileStream))
{
try
{
const string ShiftJIS = "shift_jis";
const int intByteLength = 4;
//ファイルタイプの書き込み
const int fileTypeLength = 30;
const string RightFileType = "Vocaloid Motion Data 0002";
byte[] fileTypeBytes = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes(RightFileType);
binaryWriter.Write(fileTypeBytes, 0, fileTypeBytes.Length);
binaryWriter.Write(new byte[fileTypeLength - fileTypeBytes.Length], 0, fileTypeLength - fileTypeBytes.Length);
//モデル名の書き込み、Shift_JISで保存
const int modelNameLength = 20;
byte[] modelNameBytes = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes(modelName);
//モデル名が長すぎたとき
modelNameBytes = modelNameBytes.Take(Mathf.Min(modelNameLength, modelNameBytes.Length)).ToArray();
binaryWriter.Write(modelNameBytes, 0, modelNameBytes.Length);
binaryWriter.Write(new byte[modelNameLength - modelNameBytes.Length], 0, modelNameLength - modelNameBytes.Length);
//全ボーンフレーム数の書き込み
void LoopWithBoneCondition(Action<BoneNames, int> action)
{
for (int i = 0; i < frameNumberSaved; i++)
{
if ((i % KeyReductionLevel) != 0) { continue; }
foreach (BoneNames boneName in Enum.GetValues(typeof(BoneNames)))
{
if (!BoneDictionary.Keys.Contains(boneName)) { continue; }
if (BoneDictionary[boneName] == null) { continue; }
if (!UseParentOfAll && boneName == BoneNames.全ての親) { continue; }
action(boneName, i);
}
}
}
uint allKeyFrameNumber = 0;
LoopWithBoneCondition((a, b) => { allKeyFrameNumber++; });
byte[] allKeyFrameNumberByte = BitConverter.GetBytes(allKeyFrameNumber);
binaryWriter.Write(allKeyFrameNumberByte, 0, intByteLength);
//人ボーンの書き込み
LoopWithBoneCondition((boneName, i) =>
{
const int boneNameLength = 15;
string boneNameString = boneName.ToString();
if (boneName == BoneNames.全ての親 && UseCenterAsParentOfAll)
{
boneNameString = CenterNameString;
}
if (boneName == BoneNames.センター && UseCenterAsParentOfAll)
{
boneNameString = GrooveNameString;
}
byte[] boneNameBytes = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes(boneNameString);
binaryWriter.Write(boneNameBytes, 0, boneNameBytes.Length);
binaryWriter.Write(new byte[boneNameLength - boneNameBytes.Length], 0, boneNameLength - boneNameBytes.Length);
byte[] frameNumberByte = BitConverter.GetBytes((ulong)i);
binaryWriter.Write(frameNumberByte, 0, intByteLength);
Vector3 position = positionDictionarySaved[boneName][i];
byte[] positionX = BitConverter.GetBytes(position.x);
binaryWriter.Write(positionX, 0, intByteLength);
byte[] positionY = BitConverter.GetBytes(position.y);
binaryWriter.Write(positionY, 0, intByteLength);
byte[] positionZ = BitConverter.GetBytes(position.z);
binaryWriter.Write(positionZ, 0, intByteLength);
Quaternion rotation = rotationDictionarySaved[boneName][i];
byte[] rotationX = BitConverter.GetBytes(rotation.x);
binaryWriter.Write(rotationX, 0, intByteLength);
byte[] rotationY = BitConverter.GetBytes(rotation.y);
binaryWriter.Write(rotationY, 0, intByteLength);
byte[] rotationZ = BitConverter.GetBytes(rotation.z);
binaryWriter.Write(rotationZ, 0, intByteLength);
byte[] rotationW = BitConverter.GetBytes(rotation.w);
binaryWriter.Write(rotationW, 0, intByteLength);
byte[] interpolateBytes = new byte[64];
binaryWriter.Write(interpolateBytes, 0, 64);
});
//全モーフフレーム数の書き込み
morphRecorderSaved.DisableIntron();
if (TrimMorphNumber) { morphRecorderSaved.TrimMorphNumber(); }
void LoopWithMorphCondition(Action<string, int> action)
{
for (int i = 0; i < frameNumberSaved; i++)
{
foreach (string morphName in morphRecorderSaved.MorphDrivers.Keys)
{
if (morphRecorderSaved.MorphDrivers[morphName].ValueList.Count == 0) { continue; }
if (i > morphRecorderSaved.MorphDrivers[morphName].ValueList.Count) { continue; }
//変化のない部分は省く
if (!morphRecorderSaved.MorphDrivers[morphName].ValueList[i].enabled) { continue; }
const int boneNameLength = 15;
string morphNameString = morphName.ToString();
byte[] morphNameBytes = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes(morphNameString);
//名前が長過ぎた場合書き込まない
if (boneNameLength - morphNameBytes.Length < 0) { continue; }
action(morphName, i);
}
}
}
uint allMorphNumber = 0;
LoopWithMorphCondition((a, b) => { allMorphNumber++; });
byte[] faceFrameCount = BitConverter.GetBytes(allMorphNumber);
binaryWriter.Write(faceFrameCount, 0, intByteLength);
//モーフの書き込み
LoopWithMorphCondition((morphName, i) =>
{
const int boneNameLength = 15;
string morphNameString = morphName.ToString();
byte[] morphNameBytes = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes(morphNameString);
binaryWriter.Write(morphNameBytes, 0, morphNameBytes.Length);
binaryWriter.Write(new byte[boneNameLength - morphNameBytes.Length], 0, boneNameLength - morphNameBytes.Length);
byte[] frameNumberByte = BitConverter.GetBytes((ulong)i);
binaryWriter.Write(frameNumberByte, 0, intByteLength);
byte[] valueByte = BitConverter.GetBytes(morphRecorderSaved.MorphDrivers[morphName].ValueList[i].value);
binaryWriter.Write(valueByte, 0, intByteLength);
});
//カメラの書き込み
byte[] cameraFrameCount = BitConverter.GetBytes(0);
binaryWriter.Write(cameraFrameCount, 0, intByteLength);
//照明の書き込み
byte[] lightFrameCount = BitConverter.GetBytes(0);
binaryWriter.Write(lightFrameCount, 0, intByteLength);
//セルフシャドウの書き込み
byte[] selfShadowCount = BitConverter.GetBytes(0);
binaryWriter.Write(selfShadowCount, 0, intByteLength);
//IKの書き込み
//0フレームにキーフレーム一つだけ置く
byte[] ikCount = BitConverter.GetBytes(1);
byte[] ikFrameNumber = BitConverter.GetBytes(0);
byte modelDisplay = Convert.ToByte(1);
//右足IKと左足IKと右足つま先IKと左足つま先IKの4つ
byte[] ikNumber = BitConverter.GetBytes(4);
const int IKNameLength = 20;
byte[] leftIKName = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes("左足IK");
byte[] rightIKName = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes("右足IK");
byte[] leftToeIKName = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes("左つま先IK");
byte[] rightToeIKName = System.Text.Encoding.GetEncoding(ShiftJIS).GetBytes("右つま先IK");
byte ikOn = Convert.ToByte(1);
byte ikOff = Convert.ToByte(0);
binaryWriter.Write(ikCount, 0, intByteLength);
binaryWriter.Write(ikFrameNumber, 0, intByteLength);
binaryWriter.Write(modelDisplay);
binaryWriter.Write(ikNumber, 0, intByteLength);
binaryWriter.Write(leftIKName, 0, leftIKName.Length);
binaryWriter.Write(new byte[IKNameLength - leftIKName.Length], 0, IKNameLength - leftIKName.Length);
binaryWriter.Write(ikOff);
binaryWriter.Write(leftToeIKName, 0, leftToeIKName.Length);
binaryWriter.Write(new byte[IKNameLength - leftToeIKName.Length], 0, IKNameLength - leftToeIKName.Length);
binaryWriter.Write(ikOff);
binaryWriter.Write(rightIKName, 0, rightIKName.Length);
binaryWriter.Write(new byte[IKNameLength - rightIKName.Length], 0, IKNameLength - rightIKName.Length);
binaryWriter.Write(ikOff);
binaryWriter.Write(rightToeIKName, 0, rightToeIKName.Length);
binaryWriter.Write(new byte[IKNameLength - rightToeIKName.Length], 0, IKNameLength - rightToeIKName.Length);
binaryWriter.Write(ikOff);
}
catch (Exception ex)
{
Debug.Log("VMD書き込みエラー" + ex.Message);
}
finally
{
binaryWriter.Close();
}
}
});
Debug.Log(transform.name + "VMDファイル作成終了");
}
/// <summary>
/// VMDを作成する
/// 呼び出す際は先にStopRecordingを呼び出すこと
/// </summary>
/// <param name="modelName">VMDファイルに記載される専用モデル名</param>
/// <param name="filePath">保存先の絶対ファイルパス</param>
/// <param name="keyReductionLevel">キーの書き込み頻度を減らして容量を減らす</param>
public void SaveVMD(string modelName, string filePath, int keyReductionLevel)
{
KeyReductionLevel = keyReductionLevel;
SaveVMD(modelName, filePath);
}
//裏で正規化されたモデル
//(初期ポーズで各ボーンのlocalRotationがQuaternion.identityのモデル)を疑似的にアニメーションさせる
class BoneGhost
{
public Dictionary<BoneNames, (Transform ghost, bool enabled)> GhostDictionary { get; private set; } = new Dictionary<BoneNames, (Transform ghost, bool enabled)>();
public Dictionary<BoneNames, Vector3> GhostOriginalLocalPositionDictionary { get; private set; } = new Dictionary<BoneNames, Vector3>();
public Dictionary<BoneNames, Quaternion> GhostOriginalRotationDictionary { get; private set; } = new Dictionary<BoneNames, Quaternion>();
public Dictionary<BoneNames, Quaternion> OriginalRotationDictionary { get; private set; } = new Dictionary<BoneNames, Quaternion>();
public bool UseBottomCenter { get; private set; } = false;
const string GhostSalt = "Ghost";
private Dictionary<BoneNames, Transform> boneDictionary = new Dictionary<BoneNames, Transform>();
float centerOffsetLength = 0;
public BoneGhost(Animator animator, Dictionary<BoneNames, Transform> boneDictionary, bool useBottomCenter)
{
this.boneDictionary = boneDictionary;
UseBottomCenter = useBottomCenter;
Dictionary<BoneNames, (BoneNames optionParent1, BoneNames optionParent2, BoneNames necessaryParent)> boneParentDictionary
= new Dictionary<BoneNames, (BoneNames optionParent1, BoneNames optionParent2, BoneNames necessaryParent)>()
{
{ BoneNames.センター, (BoneNames.None, BoneNames.None, BoneNames.全ての親) },
{ BoneNames.左足, (BoneNames.None, BoneNames.None, BoneNames.センター) },
{ BoneNames.左ひざ, (BoneNames.None, BoneNames.None, BoneNames.左足) },
{ BoneNames.左足首, (BoneNames.None, BoneNames.None, BoneNames.左ひざ) },
{ BoneNames.右足, (BoneNames.None, BoneNames.None, BoneNames.センター) },
{ BoneNames.右ひざ, (BoneNames.None, BoneNames.None, BoneNames.右足) },
{ BoneNames.右足首, (BoneNames.None, BoneNames.None, BoneNames.右ひざ) },
{ BoneNames.上半身, (BoneNames.None, BoneNames.None, BoneNames.センター) },
{ BoneNames.上半身2, (BoneNames.None, BoneNames.None, BoneNames.上半身) },
{ BoneNames.首, (BoneNames.上半身2, BoneNames.None, BoneNames.上半身) },
{ BoneNames.頭, (BoneNames.首, BoneNames.上半身2, BoneNames.上半身) },
{ BoneNames.左肩, (BoneNames.上半身2, BoneNames.None, BoneNames.上半身) },
{ BoneNames.左腕, (BoneNames.左肩, BoneNames.上半身2, BoneNames.上半身) },
{ BoneNames.左ひじ, (BoneNames.None, BoneNames.None, BoneNames.左腕) },
{ BoneNames.左手首, (BoneNames.None, BoneNames.None, BoneNames.左ひじ) },
{ BoneNames.左親指1, (BoneNames.左手首, BoneNames.None, BoneNames.None) },
{ BoneNames.左親指2, (BoneNames.左親指1, BoneNames.None, BoneNames.None) },
{ BoneNames.左人指1, (BoneNames.左手首, BoneNames.None, BoneNames.None) },
{ BoneNames.左人指2, (BoneNames.左人指1, BoneNames.None, BoneNames.None) },
{ BoneNames.左人指3, (BoneNames.左人指2, BoneNames.None, BoneNames.None) },
{ BoneNames.左中指1, (BoneNames.左手首, BoneNames.None, BoneNames.None) },
{ BoneNames.左中指2, (BoneNames.左中指1, BoneNames.None, BoneNames.None) },
{ BoneNames.左中指3, (BoneNames.左中指2, BoneNames.None, BoneNames.None) },
{ BoneNames.左薬指1, (BoneNames.左手首, BoneNames.None, BoneNames.None) },
{ BoneNames.左薬指2, (BoneNames.左薬指1, BoneNames.None, BoneNames.None) },
{ BoneNames.左薬指3, (BoneNames.左薬指2, BoneNames.None, BoneNames.None) },
{ BoneNames.左小指1, (BoneNames.左手首, BoneNames.None, BoneNames.None) },
{ BoneNames.左小指2, (BoneNames.左小指1, BoneNames.None, BoneNames.None) },
{ BoneNames.左小指3, (BoneNames.左小指2, BoneNames.None, BoneNames.None) },
{ BoneNames.右肩, (BoneNames.上半身2, BoneNames.None, BoneNames.上半身) },
{ BoneNames.右腕, (BoneNames.右肩, BoneNames.上半身2, BoneNames.上半身) },
{ BoneNames.右ひじ, (BoneNames.None, BoneNames.None, BoneNames.右腕) },
{ BoneNames.右手首, (BoneNames.None, BoneNames.None, BoneNames.右ひじ) },
{ BoneNames.右親指1, (BoneNames.右手首, BoneNames.None, BoneNames.None) },
{ BoneNames.右親指2, (BoneNames.右親指1, BoneNames.None, BoneNames.None) },
{ BoneNames.右人指1, (BoneNames.右手首, BoneNames.None, BoneNames.None) },
{ BoneNames.右人指2, (BoneNames.右人指1, BoneNames.None, BoneNames.None) },
{ BoneNames.右人指3, (BoneNames.右人指2, BoneNames.None, BoneNames.None) },
{ BoneNames.右中指1, (BoneNames.右手首, BoneNames.None, BoneNames.None) },
{ BoneNames.右中指2, (BoneNames.右中指1, BoneNames.None, BoneNames.None) },
{ BoneNames.右中指3, (BoneNames.右中指2, BoneNames.None, BoneNames.None) },
{ BoneNames.右薬指1, (BoneNames.右手首, BoneNames.None, BoneNames.None) },
{ BoneNames.右薬指2, (BoneNames.右薬指1, BoneNames.None, BoneNames.None) },
{ BoneNames.右薬指3, (BoneNames.右薬指2, BoneNames.None, BoneNames.None) },
{ BoneNames.右小指1, (BoneNames.右手首, BoneNames.None, BoneNames.None) },
{ BoneNames.右小指2, (BoneNames.右小指1, BoneNames.None, BoneNames.None) },
{ BoneNames.右小指3, (BoneNames.右小指2, BoneNames.None, BoneNames.None) },
};
//Ghostの生成
foreach (BoneNames boneName in boneDictionary.Keys)
{
if (boneName == BoneNames.全ての親 || boneName == BoneNames.左足IK || boneName == BoneNames.右足IK)
{
continue;
}
if (boneDictionary[boneName] == null)
{
GhostDictionary.Add(boneName, (null, false));
continue;
}
Transform ghost = new GameObject(boneDictionary[boneName].name + GhostSalt).transform;
if (boneName == BoneNames.センター && UseBottomCenter)
{
ghost.position = boneDictionary[BoneNames.全ての親].position;
}
else
{
ghost.position = boneDictionary[boneName].position;
}
ghost.rotation = animator.transform.rotation;
GhostDictionary.Add(boneName, (ghost, true));
}
//Ghostの親子構造を設定
foreach (BoneNames boneName in boneDictionary.Keys)
{
if (boneName == BoneNames.全ての親 || boneName == BoneNames.左足IK || boneName == BoneNames.右足IK)
{
continue;
}
if (GhostDictionary[boneName].ghost == null || !GhostDictionary[boneName].enabled)
{
continue;
}
if (boneName == BoneNames.センター)
{
GhostDictionary[boneName].ghost.SetParent(animator.transform);
continue;
}
if (boneParentDictionary[boneName].optionParent1 != BoneNames.None && boneDictionary[boneParentDictionary[boneName].optionParent1] != null)
{
GhostDictionary[boneName].ghost.SetParent(GhostDictionary[boneParentDictionary[boneName].optionParent1].ghost);
}
else if (boneParentDictionary[boneName].optionParent2 != BoneNames.None && boneDictionary[boneParentDictionary[boneName].optionParent2] != null)
{
GhostDictionary[boneName].ghost.SetParent(GhostDictionary[boneParentDictionary[boneName].optionParent2].ghost);
}
else if (boneParentDictionary[boneName].necessaryParent != BoneNames.None && boneDictionary[boneParentDictionary[boneName].necessaryParent] != null)
{
GhostDictionary[boneName].ghost.SetParent(GhostDictionary[boneParentDictionary[boneName].necessaryParent].ghost);
}
else
{
GhostDictionary[boneName] = (GhostDictionary[boneName].ghost, false);
}
}
//初期状態を保存
foreach (BoneNames boneName in GhostDictionary.Keys)
{
if (GhostDictionary[boneName].ghost == null || !GhostDictionary[boneName].enabled)
{
GhostOriginalLocalPositionDictionary.Add(boneName, Vector3.zero);
GhostOriginalRotationDictionary.Add(boneName, Quaternion.identity);
OriginalRotationDictionary.Add(boneName, Quaternion.identity);
}
else
{
GhostOriginalRotationDictionary.Add(boneName, GhostDictionary[boneName].ghost.rotation);
OriginalRotationDictionary.Add(boneName, boneDictionary[boneName].rotation);
if (boneName == BoneNames.センター && UseBottomCenter)
{
GhostOriginalLocalPositionDictionary.Add(boneName, Vector3.zero);
continue;
}
GhostOriginalLocalPositionDictionary.Add(boneName, GhostDictionary[boneName].ghost.localPosition);
}
}
centerOffsetLength = Vector3.Distance(boneDictionary[BoneNames.全ての親].position, boneDictionary[BoneNames.センター].position);
}
public void GhostAll()
{
foreach (BoneNames boneName in GhostDictionary.Keys)
{
if (GhostDictionary[boneName].ghost == null || !GhostDictionary[boneName].enabled) { continue; }
Quaternion transQuaternion = boneDictionary[boneName].rotation * Quaternion.Inverse(OriginalRotationDictionary[boneName]);
GhostDictionary[boneName].ghost.rotation = transQuaternion * GhostOriginalRotationDictionary[boneName];
if (boneName == BoneNames.センター && UseBottomCenter)
{
GhostDictionary[boneName].ghost.position = boneDictionary[boneName].position - centerOffsetLength * GhostDictionary[boneName].ghost.up;
continue;
}
GhostDictionary[boneName].ghost.position = boneDictionary[boneName].position;
}
}
}
class MorphRecorder
{
List<SkinnedMeshRenderer> skinnedMeshRendererList;
//キーはunity上のモーフ名
public Dictionary<string, MorphDriver> MorphDrivers { get; private set; } = new Dictionary<string, MorphDriver>();
public MorphRecorder(Transform model)
{
List<SkinnedMeshRenderer> searchBlendShapeSkins(Transform t)
{
List<SkinnedMeshRenderer> skinnedMeshRendererList = new List<SkinnedMeshRenderer>();
Queue queue = new Queue();
queue.Enqueue(t);
while (queue.Count != 0)
{
SkinnedMeshRenderer skinnedMeshRenderer = (queue.Peek() as Transform).GetComponent<SkinnedMeshRenderer>();
if (skinnedMeshRenderer != null && skinnedMeshRenderer.sharedMesh.blendShapeCount != 0)
{
skinnedMeshRendererList.Add(skinnedMeshRenderer);
}
foreach (Transform childT in (queue.Dequeue() as Transform))
{
queue.Enqueue(childT);
}
}
return skinnedMeshRendererList;
}
skinnedMeshRendererList = searchBlendShapeSkins(model);
foreach (SkinnedMeshRenderer skinnedMeshRenderer in skinnedMeshRendererList)
{
int morphCount = skinnedMeshRenderer.sharedMesh.blendShapeCount;
for (int i = 0; i < morphCount; i++)
{
string morphName = skinnedMeshRenderer.sharedMesh.GetBlendShapeName(i);
////モーフ名に重複があれば2コ目以降は無視
if (MorphDrivers.Keys.Contains(morphName)) { continue; }
MorphDrivers.Add(morphName, new MorphDriver(skinnedMeshRenderer, i));
}
}
}
public void RecrodAllMorph()
{
foreach (MorphDriver morphDriver in MorphDrivers.Values)
{
morphDriver.RecordMorph();
}
}
public void TrimMorphNumber()
{
string dot = ".";
Dictionary<string, MorphDriver> morphDriversTemp = new Dictionary<string, MorphDriver>();
foreach (string morphName in MorphDrivers.Keys)
{
//正規表現使うより、dot探して整数か見る
if (morphName.Contains(dot) && int.TryParse(morphName.Substring(0, morphName.IndexOf(dot)), out int dummy))
{
morphDriversTemp.Add(morphName.Substring(morphName.IndexOf(dot) + 1), MorphDrivers[morphName]);
continue;
}
morphDriversTemp.Add(morphName, MorphDrivers[morphName]);
}
MorphDrivers = morphDriversTemp;
}
public void DisableIntron()
{
foreach (string morphName in MorphDrivers.Keys)
{
for (int i = 0; i < MorphDrivers[morphName].ValueList.Count; i++)
{
//情報がなければ次へ
if (MorphDrivers[morphName].ValueList.Count == 0) { continue; }
//今、前、後が同じなら不必要なので無効化
if (i > 0
&& i < MorphDrivers[morphName].ValueList.Count - 1
&& MorphDrivers[morphName].ValueList[i].value == MorphDrivers[morphName].ValueList[i - 1].value
&& MorphDrivers[morphName].ValueList[i].value == MorphDrivers[morphName].ValueList[i + 1].value)
{
MorphDrivers[morphName].ValueList[i] = (MorphDrivers[morphName].ValueList[i].value, false);
}
}
}
}
public class MorphDriver
{
const float MorphAmplifier = 0.01f;
public SkinnedMeshRenderer SkinnedMeshRenderer { get; private set; } = new SkinnedMeshRenderer();
public int MorphIndex { get; private set; }
public List<(float value, bool enabled)> ValueList { get; private set; } = new List<(float value, bool enabled)>();
public MorphDriver(SkinnedMeshRenderer skinnedMeshRenderer, int morphIndex)
{
SkinnedMeshRenderer = skinnedMeshRenderer;
MorphIndex = morphIndex;
}
public void RecordMorph()
{
ValueList.Add((SkinnedMeshRenderer.GetBlendShapeWeight(MorphIndex) * MorphAmplifier, true));
}
}
}
}