-
Notifications
You must be signed in to change notification settings - Fork 145
/
OpenSeeVRMDriver.cs
2082 lines (1914 loc) · 96.1 KB
/
OpenSeeVRMDriver.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
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
#define WINDOWS_BUILD
#endif
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using UnityEngine;
using OpenSee;
using VRM;
// This is a more comprehensive VRM avatar animator than the OpenSeeVRMExpression example.
// To use the lip sync functionality, place the OVRLipSync 1.28 component somewhere in the scene.
// No other OVR related components are required.
public class OpenSeeVRMDriver : MonoBehaviour {
#if WINDOWS_BUILD
#region DllImport
[DllImport("user32.dll", SetLastError = true)]
static extern ushort GetAsyncKeyState(int vKey);
#endregion
#else
static ushort GetAsyncKeyState(int vKey) { return 0; }
#endif
[Header("Settings")]
[Tooltip("This is the OpenSeeExpression module used for expression prediction.")]
public OpenSeeExpression openSeeExpression;
[Tooltip("This is the OpenSeeIKTarget module used to move the avatar.")]
public OpenSeeIKTarget openSeeIKTarget;
[Tooltip("This is the target VRM avatar's blend shape proxy.")]
public VRMBlendShapeProxy vrmBlendShapeProxy;
[Tooltip("When this is enabled, no blendshapes are set.")]
public bool skipApply = false;
[Tooltip("When this is enabled, no gaze tracking (bone or blendshape) is applied.")]
public bool skipApplyEyes = false;
[Tooltip("When this is enabled, no jaw bone animation is applied.")]
public bool skipApplyJaw = false;
[Tooltip("When this is enabled in addition to skipApply, expressions from hotkeys or expression detection are still applied.")]
public bool stillApplyExpressions = false;
[Tooltip("This factor is multiplied to all blendshape weights relating to camera tracking, as well as gaze and jaw tracking.")]
public float blendShapeWeight = 1f;
[Tooltip("This needs to be enabled when tracking with the fast 30 point model.")]
public bool only30Points = false;
[Tooltip("When enabled, if a model has perfect sync blendshapes, they will be used for more detailed tracking. (Experimental!)")]
public bool useCameraPerfectSync = false;
//[Tooltip("When set to true, expression and audio data will be processed in FixedUpdate, otherwise it will be processed in Update.")]
//public bool fixedUpdate = true;
[Tooltip("When enabled, the avatar will blink automatically. Otherwise, blinks will be applied according to the face tracking's blink detection, allowing it to wink. Even for tracked eye blinks, the eye blink setting of the current expression is used.")]
public bool autoBlink = true;
[Tooltip("When automatic eye blinks are disabled, this sets the threshold for how closed the eyes have to be to be registered as fully closed. At 0 the eyes will never fully close.")]
[Range(0, 1)]
public float eyeClosedThreshold = 0.2f;
[Tooltip("When automatic eye blinks are disabled, this sets the threshold for how open the eyes have to be to be registered as fully open. At 1 the eyes will never fully open.")]
[Range(0, 1)]
public float eyeOpenedThreshold = 0.55f;
[Tooltip("This is the blink smoothing factor for camera based blink tracking, with 0 being no smoothing and 1 being a fixed blink state.")]
[Range(0, 1)]
public float blinkSmoothing = 0.75f;
[Tooltip("With tracked eye blinking, the eyes will close when looking down. Enable this to compensate this by modifying thresholds.")]
public bool lookDownCompensation = false;
[Tooltip("This is an angle value that can be used to adjust the angle from which a face is considered to be looking downwards.")]
[Range(-10, 20)]
public float lookDownAdjustment = 0f;
[Tooltip("When enabled, auto blinking will be enabled while looking down.")]
public bool autoBlinkLookingDown = false;
[Tooltip("When enabled, the blink state for both eyes is permanently linked together.")]
public bool linkBlinks = true;
[Tooltip("When enabled, it becomes possible to wink even with link blinks enabled.")]
public bool allowWinking = false;
[Tooltip("If allowWinking is enabled, if the eye blink values are within this distance, the blink is linked.")]
public float smartWinkThreshold = 0.85f;
[Tooltip("When enabled, the avatar's eye will move according to the face tracker's gaze tracking.")]
public bool gazeTracking = true;
[Tooltip("This is the right eye bone. When either eye bone is not set, the VRM look direction blendshapes are used instead.")]
public Transform rightEye;
[Tooltip("This is the left eye bone. When either eye bone is not set, the VRM look direction blendshapes are used instead.")]
public Transform leftEye;
[Tooltip("This is the eyebrow tracking strength, with 0 meaning no eyebrow tracking and 1 meaning full strength.")]
[Range(0, 1)]
public float eyebrowStrength = 1.0f;
[Tooltip("This is the eyebrow smoothing factor, with 0 being no smoothing and 1 being fixed eyebrows.")]
public float eyebrowSmoothing = 0.65f;
[Tooltip("This is the zero point of the eyebrow value. It can be used to offset the eyebrow tracking value.")]
[Range(-1, 1)]
public float eyebrowZero = 0.0f;
[Tooltip("This is the sensitivity of the eyebrow tracking, with 0 meaning no eyebrow movement.")]
[Range(0, 2)]
public float eyebrowSensitivity = 1.0f;
[Tooltip("This is the gaze smoothing factor, with 0 being no smoothing and 1 being a fixed gaze.")]
[Range(0, 1)]
public float gazeSmoothing = 0.6f;
[Tooltip("This is the gaze stabilization factor, with 0 being no stabilization and 1 being a fixed gaze.")]
[Range(0, 1)]
public float gazeStabilizer = 0.1f;
[Tooltip("For bone based gaze tracking, the conversion factor from [-1, 1] to degrees should be entered here. For blendshapes, sometimes gaze tracking does not follow the gaze strongly enough. With these factors, its effect can be strengthened, but usually it should be set to 1.")]
public Vector2 gazeFactor = new Vector2(5f, 10f);
[Tooltip("This factor is applied on top of the gazeFactor field.")]
[Range(0, 5)]
public float gazeStrength = 1f;
[Tooltip("This this adds an offset with values in the range of -1 to 1 to the gaze coordinates.")]
public Vector2 gazeCenter = Vector2.zero;
[Tooltip("This component lets you customize the automatic eye blinking.")]
public OpenSeeEyeBlink eyeBlinker = new OpenSeeEyeBlink();
[Tooltip("When enabled, expressions will respond to global hotkeys.")]
public bool hotkeys = true;
[Tooltip("This component lets configure your VRM expressions.")]
public OpenSeeVRMExpression[] expressions = new OpenSeeVRMExpression[]{
new OpenSeeVRMExpression("neutral", BlendShapePreset.Neutral, true, 1f, 1f, 1f, true, true, 0x70, true, true, false, true, 50f, 120f),
new OpenSeeVRMExpression("fun", BlendShapePreset.Fun, true, 1f, 1f, 1f, true, true, 0x71, true, true, false, true, 1f, 120f),
new OpenSeeVRMExpression("joy", BlendShapePreset.Joy, true, 1f, 1f, 1f, true, true, 0x72, true, true, false, true, 1f, 120f),
new OpenSeeVRMExpression("angry", BlendShapePreset.Angry, true, 1f, 1f, 0f, true, true, 0x73, true, true, false, true, 1f, 120f),
new OpenSeeVRMExpression("sorrow", BlendShapePreset.Sorrow, true, 1f, 1f, 0f, true, true, 0x74, true, true, false, true, 1f, 120f),
new OpenSeeVRMExpression("surprise", "Surprised", true, 1f, 1f, 0f, true, true, 0x75, true, true, false, true, 1f, 120f)
};
[Tooltip("The expression configuration is initialized on startup. If it is changed and needs to be reinitialized, this can be triggered by using this flag or calling InitExpressionMap. This flag is reset to false afterwards.")]
public bool reloadExpressions = false;
[Header("Lip sync settings")]
[Tooltip("This allows you to enable and disable lip sync. When disabled, mouth tracking is used instead.")]
public bool lipSync = true;
[Tooltip("When enabled, camera based mouth tracking will be used when no viseme is detected.")]
public bool hybridLipSync = false;
[Tooltip("When enabled, viseme size will not depend on the OVR Lip Sync weight.")]
public bool maximizeLipSync = false;
[Tooltip("When enabled, mouth blendshapes are normalized if they add up to a value above 1.")]
public bool visemeNormalization = true;
[Tooltip("When enabled and the animator of the avatar has a float parameter called JawMovement, this parameter will be set to values from 0 to 1, corresponding to a closed to open mouth. This can be used for animating jaw bones.")]
public bool animateJawBone = false;
[Tooltip("This should be set to the included mecanim animation, which sets the jaw bone to closed on the first frame and to open on the second frame.")]
public AnimationClip jawBoneAnimation;
[Tooltip("This is the mouth tracking smoothing factor, with 0 being no smoothing and 1 being a fixed mouth.")]
[Range(0, 1)]
public float mouthSmoothing = 0.6f;
[Tooltip("This is the mouth tracking stabilization factor, with 0 being no stabilization and 1 being a fixed mouth.")]
[Range(0, 1)]
public float mouthStabilizer = 0.2f;
[Tooltip("This is the mouth tracking stabilization factor used for wide mouth expressions, with 0 being no stabilization and 1 being a fixed mouth.")]
[Range(0, 1)]
public float mouthStabilizerWide = 0.3f;
[Tooltip("When the detected audio volume is lower than this, the mouth will be considered closed for camera based mouth tracking.")]
[Range(0, 1)]
public float mouthSquelch = 0.01f;
[Tooltip("With this, the squelch threshold above can be disabled.")]
public bool mouthUseSquelch = false;
#if WINDOWS_BUILD
[Tooltip("This allows you to select the OVRLipSync provider.")]
public OVRLipSync.ContextProviders provider = OVRLipSync.ContextProviders.Enhanced;
#endif
[Tooltip("When enabled, audio will be read from the given audio source, otherwise it will be read from the given mic.")]
public bool useCanned = false;
[Tooltip("This is the audio source for canned audio.")]
public AudioSource audioSource = null;
[Tooltip("This is the microphone audio will be captured from. A list can be retrieved from Microphone.devices.")]
public string mic = null;
[Tooltip("When enabled, the lip sync function will be initialized or reinitialized in the next Update or FixedUpdate call, according to the IK target's fixedUpdate flag. This flag is reset to false afterwards. It is also possible to call InitializeLipSync instead.")]
public bool initializeLipSync = false;
[Tooltip("This sets the viseme detection smoothing in a range from 1 to 100, where 1 means no smoothing and values above 90 mean pretty much no visemes.")]
[Range(1, 100)]
public int smoothAmount = 50;
[Tooltip("This is the viseme animation smoothing factor, with 0 being no smoothing and 1 being no animation.")]
[Range(0, 1)]
public float visemeSmoothing = 0.75f;
[Tooltip("Often, the A, I, U, E, O expressions are too strong, so you can scale them down using this value.")]
[Range(0, 1)]
public float visemeFactor = 0.6f;
[Tooltip("This is the gain applied to audio read from the microphone. If your input level is high, set this back to 1. If your recording is very quiet and the lip sync has trouble picking up your speech, you can try increasing it.")]
public float gain = 2f;
[Header("Lip sync information")]
[Tooltip("This shows if the lip sync system is currently active.")]
public bool active = false;
[Tooltip("This shows the current audio volume.")]
public float audioVolume = 0f;
[HideInInspector]
public OpenSee.OpenSee.OpenSeeData openSeeData { get; private set; } = null;
#if WINDOWS_BUILD
private OVRLipSync.Frame visemeData = new OVRLipSync.Frame();
#endif
private float fakeTimeTime = 0f;
private float volume;
static private bool inited = false;
private int lastSmoothing = 50;
private uint context = 0;
private float[] partialAudio;
private int partialPos;
private bool isCanned = false;
private string lastMic;
private int freq;
private int lastPos = 0;
private int channels = 0;
private AudioClip clip = null;
private BlendShapeKey[] visemePresetMap;
private Dictionary<string, OpenSeeVRMExpression> expressionMap;
private OpenSeeVRMExpression currentExpression = null;
private OpenSeeVRMExpression lastExpression = null;
private bool haveFullVisemeSet = false;
private BlendShapeKey[] fullVisemePresets;
#if WINDOWS_BUILD
private Dictionary<OVRLipSync.Viseme, int> ovrMap;
#endif
private float turnLeftBoundaryAngle = -30f;
private float turnRightBoundaryAngle = 20f;
private float turnDownBoundaryAngle = 205f;
private float wasLookingDown = 0f;
private double lastGaze = 0f;
private float lastLookUpDown = 0f;
private float lastLookLeftRight = 0f;
private float currentLookUpDown = 0f;
private float currentLookLeftRight = 0f;
private TimeInterpolate gazeInterpolate;
private float[] lastVisemeValues;
private double lastMouth = 0f;
private float[] lastMouthStates;
private float[] currentMouthStates;
private TimeInterpolate mouthInterpolate;
private double startedSilVisemes = -10;
private double silVisemeHybridThreshold = 0.3;
private bool wasSilViseme = true;
private double lastBlink = 0f;
private float lastBlinkLeft = 0f;
private float lastBlinkRight = 0f;
private float currentBlinkLeft = 0f;
private float currentBlinkRight = 0f;
private TimeInterpolate blinkInterpolate;
private bool overridden = false;
private OpenSeeVRMExpression toggledExpression = null;
private HashSet<OpenSeeVRMExpression> continuedPress = new HashSet<OpenSeeVRMExpression>();
private bool currentExpressionEnableBlinking;
private bool currentExpressionEnableVisemes;
private float currentExpressionEyebrowWeight;
private float currentExpressionVisemeFactor;
private OpenSeeBlendShapeProxy proxy = null;
private Animator animator;
private bool haveJawParameter;
private Transform jawBone;
private Quaternion jawRotation = Quaternion.identity;
private HumanBodyBones[] humanBodyBones;
private Transform[] humanBodyBoneTransforms;
private Quaternion[] humanBodyBoneRotations;
private VRMBlendShapeProxy lastAvatar = null;
private int eyebrowIsMoving = 0;
private double lastBrows = 0f;
private SkinnedMeshRenderer faceMesh;
private int faceType = -1;
private int browUpIndex = -1;
private int browDownIndex = -1;
private int browAngryIndex = -1;
private int browSorrowIndex = -1;
private float lastBrowUpDown = 0f;
private float[] lastBrowStates;
private float[] currentBrowStates;
private TimeInterpolate browInterpolate;
private BlendShapeKey[] browClips;
private bool trackMouth = false;
private double lastPerfectSync = -1;
private float lastAudioTime = -1f;
private class TimeInterpolate {
private float interpolateT = 0f;
private float updateT = 0f;
private double lastT = 0f;
private double currentT = 0f;
private float clamp = 1f;
private int gotData = 0;
public TimeInterpolate() {}
public TimeInterpolate(float clamp) {
this.clamp = clamp;
}
public void UpdateTime(double nowT) {
updateT = Time.time;
lastT = currentT;
currentT = nowT;
if (gotData < 2)
gotData++;
}
public float Interpolate() {
interpolateT = Time.time;
if (interpolateT == updateT || gotData < 2)
return 0f;
return Mathf.Min((interpolateT - updateT) / (float)(currentT - lastT), clamp);
}
}
// This class is used to ensure max() based instead of add() based blendshape accumulation and to allow setting animator parameters from blendshape values
private class OpenSeeBlendShapeProxy {
private HashSet<string> browsBlendShapes = new HashSet<string>() { "BROWS UP", "BROWS DOWN", "BROWINNERUP", "BROWDOWNLEFT", "BROWDOWNRIGHT", "BROWOUTERUPLEFT", "BROWOUTERUPRIGHT", "NOSESNEERLEFT", "NOSESNEERRIGHT" };
private HashSet<string> eyesBlendShapes = new HashSet<string>() { "BLINK", "BLINK_L", "BLINK_R", "EYELOOKUPLEFT", "EYELOOKUPRIGHT", "EYELOOKDOWNLEFT", "EYELOOKDOWNRIGHT", "EYELOOKINLEFT", "EYELOOKINRIGHT", "EYELOOKOUTLEFT", "EYELOOKOUTRIGHT", "EYEBLINKLEFT", "EYEBLINKRIGHT", "EYESQUINTRIGHT", "EYESQUINTLEFT", "EYEWIDELEFT", "EYEWIDERIGHT", "NOSESNEERLEFT", "NOSESNEERRIGHT", "CHEEKSQUINTLEFT", "CHEEKSQUINTRIGHT" };
private HashSet<string> mouthBlendShapes = new HashSet<string>() { "A", "I", "U", "E", "O", "SIL", "CH", "DD", "FF", "KK", "NN", "PP", "RR", "SS", "TH", "JAWOPEN", "JAWFORWARD", "JAWLEFT", "JAWRIGHT", "MOUTHFUNNEL", "MOUTHPUCKER", "MOUTHLEFT", "MOUTHRIGHT", "MOUTHROLLUPPER", "MOUTHROLLLOWER", "MOUTHSHRUGUPPER", "MOUTHSHRUGLOWER", "MOUTHCLOSE", "MOUTHSMILELEFT", "MOUTHSMILERIGHT", "MOUTHFROWNLEFT", "MOUTHFROWNRIGHT", "MOUTHDIMPLELEFT", "MOUTHDIMPLERIGHT", "MOUTHUPPERUPLEFT", "MOUTHUPPERUPRIGHT", "MOUTHLOWERDOWNLEFT", "MOUTHLOWERDOWNRIGHT", "MOUTHPRESSLEFT", "MOUTHPRESSRIGHT", "MOUTHSTRETCHLEFT", "MOUTHSTRETCHRIGHT", "TONGUEOUT", "CHEEKSQUINTLEFT", "CHEEKSQUINTRIGHT" };
private bool clearPSBrows = false;
private bool clearPSEyes = false;
private bool clearPSMouth = false;
private Dictionary<BlendShapeKey, float> clearKeys = new Dictionary<BlendShapeKey, float>();
private bool perfectSync = false;
private HashSet<string> visemeBlendShapes = new HashSet<string>() { "A", "I", "U", "E", "O", "SIL", "CH", "DD", "FF", "KK", "NN", "PP", "RR", "SS", "TH" };
private string[] perfectSyncNames = new string[] { "BROWINNERUP", "BROWDOWNLEFT", "BROWDOWNRIGHT", "BROWOUTERUPLEFT", "BROWOUTERUPRIGHT", "EYELOOKUPLEFT", "EYELOOKUPRIGHT", "EYELOOKDOWNLEFT", "EYELOOKDOWNRIGHT", "EYELOOKINLEFT", "EYELOOKINRIGHT", "EYELOOKOUTLEFT", "EYELOOKOUTRIGHT", "EYEBLINKLEFT", "EYEBLINKRIGHT", "EYESQUINTRIGHT", "EYESQUINTLEFT", "EYEWIDELEFT", "EYEWIDERIGHT", "CHEEKPUFF", "CHEEKSQUINTLEFT", "CHEEKSQUINTRIGHT", "NOSESNEERLEFT", "NOSESNEERRIGHT", "JAWOPEN", "JAWFORWARD", "JAWLEFT", "JAWRIGHT", "MOUTHFUNNEL", "MOUTHPUCKER", "MOUTHLEFT", "MOUTHRIGHT", "MOUTHROLLUPPER", "MOUTHROLLLOWER", "MOUTHSHRUGUPPER", "MOUTHSHRUGLOWER", "MOUTHCLOSE", "MOUTHSMILELEFT", "MOUTHSMILERIGHT", "MOUTHFROWNLEFT", "MOUTHFROWNRIGHT", "MOUTHDIMPLELEFT", "MOUTHDIMPLERIGHT", "MOUTHUPPERUPLEFT", "MOUTHUPPERUPRIGHT", "MOUTHLOWERDOWNLEFT", "MOUTHLOWERDOWNRIGHT", "MOUTHPRESSLEFT", "MOUTHPRESSRIGHT", "MOUTHSTRETCHLEFT", "MOUTHSTRETCHRIGHT", "TONGUEOUT" };
private InterpolatedMap<string, InterpolatedFloat, float> perfectSyncMap = new InterpolatedMap<string, InterpolatedFloat, float>();
private Dictionary<string, BlendShapeKey> clipMap = new Dictionary<string, BlendShapeKey>();
private VRMBlendShapeProxy proxy = null;
private Animator animator = null;
private RuntimeAnimatorController animatorController = null;
private Dictionary<BlendShapeKey, float> clearMap = new Dictionary<BlendShapeKey, float>();
private Dictionary<BlendShapeKey, float> values = new Dictionary<BlendShapeKey, float>();
private Dictionary<BlendShapeKey, string> clipNames = new Dictionary<BlendShapeKey, string>();
private Dictionary<string, Tuple<string, AnimatorControllerParameterType>> parameters = new Dictionary<string, Tuple<string, AnimatorControllerParameterType>>();
private bool interpolatedPerfectSync = false;
private float defaultWeight = 1f;
private OpenSeeVRMDriver vrmDriver = null;
public OpenSeeBlendShapeProxy(OpenSeeVRMDriver vrmDriver) {
this.vrmDriver = vrmDriver;
}
void InterpolatePerfectSync() {
if (!HasPerfectSync() || interpolatedPerfectSync)
return;
foreach (var name in perfectSyncNames) {
if (!perfectSyncMap.Check(name))
continue;
float weight = perfectSyncMap.Get(name);
AccumulateValue(name, weight, 1f);
}
interpolatedPerfectSync = true;
}
public void ClearPerfectSync() {
perfectSyncMap.Clear();
}
public void SetPerfectSync(string name, float weight, double nowT, float factor) {
perfectSyncMap.Store(name.ToUpper(), Mathf.Clamp(weight * factor, 0f, 1f), nowT);
}
public void SetPerfectSync(string name, float weight, double nowT) {
SetPerfectSync(name, weight, nowT, defaultWeight);
}
public bool HasPerfectSync() {
if (proxy == null || animator == null)
return false;
return perfectSync;
}
public void SetWeight(float v) {
defaultWeight = v;
}
public void DisableFaceParts(bool brows, bool eyes, bool mouth) {
clearPSBrows = brows;
clearPSEyes = eyes;
clearPSMouth = mouth;
}
private void SetFloat(BlendShapeKey key, float weight) {
SetFloat(key, weight, defaultWeight);
}
private void SetFloat(BlendShapeKey key, float weight, float factor) {
if (!clipNames.ContainsKey(key))
return;
string name = clipNames[key];
weight *= factor;
if (animator != null && parameters.ContainsKey(name)) {
var parameter = parameters[name];
if (parameter.Item2 == AnimatorControllerParameterType.Float)
animator.SetFloat(parameter.Item1, weight);
if (parameter.Item2 == AnimatorControllerParameterType.Bool)
animator.SetBool(parameter.Item1, weight > 0.5f);
}
}
private void CheckAnimatorController() {
if (animator == null)
return;
if (animatorController != animator.runtimeAnimatorController) {
animatorController = animator.runtimeAnimatorController;
parameters.Clear();
foreach (var parameter in animator.parameters) {
string key = parameter.name.ToUpper();
if (!parameters.ContainsKey(key) && (parameter.type == AnimatorControllerParameterType.Float || parameter.type == AnimatorControllerParameterType.Bool))
parameters.Add(key, new Tuple<string, AnimatorControllerParameterType>(parameter.name, parameter.type));
else
parameters[key] = new Tuple<string, AnimatorControllerParameterType>(parameter.name, parameter.type);
}
}
}
public void Clear() {
CheckAnimatorController();
interpolatedPerfectSync = false;
if (proxy != null) {
foreach (var pair in proxy.GetValues()) {
SetFloat(pair.Key, 0f);
}
foreach (var pair in values) {
SetFloat(pair.Key, 0f);
}
proxy.SetValues(clearMap);
}
values.Clear();
}
void NormalizeVisemes() {
float total = 0f;
foreach (var pair in values) {
string name = pair.Key.Name.ToUpper();
if (visemeBlendShapes.Contains(name))
total += pair.Value;
}
if (total > 1f) {
var keys = new List<BlendShapeKey>(values.Keys);
foreach (var key in keys) {
string name = key.Name.ToUpper();
if (visemeBlendShapes.Contains(name))
values[key] = values[key] / total;
}
}
}
public void Apply() {
if (proxy == null)
return;
CheckAnimatorController();
if (perfectSync)
InterpolatePerfectSync();
if (vrmDriver != null && vrmDriver.visemeNormalization)
NormalizeVisemes();
if (clearPSBrows || clearPSEyes || clearPSMouth) {
clearKeys.Clear();
foreach (var pair in proxy.GetValues()) {
string name = pair.Key.Name.ToUpper();
if (clearPSBrows && browsBlendShapes.Contains(name)) {
clearKeys[pair.Key] = 0f;
values[pair.Key] = 0f;
} else if (clearPSEyes && eyesBlendShapes.Contains(name)) {
clearKeys[pair.Key] = 0f;
values[pair.Key] = 0f;
} else if (clearPSMouth && mouthBlendShapes.Contains(name)) {
clearKeys[pair.Key] = 0f;
values[pair.Key] = 0f;
}
}
clearPSBrows = false;
clearPSEyes = false;
clearPSMouth = false;
}
proxy.SetValues(values);
foreach (var pair in values) {
SetFloat(pair.Key, pair.Value, 1f);
}
proxy.Apply();
}
public void AccumulateValue(BlendShapeKey key, float weight, float factor) {
if (!values.ContainsKey(key))
values.Add(key, weight * factor);
else
values[key] = Mathf.Max(values[key], weight * factor);
}
public void AccumulateValue(BlendShapeKey key, float weight) {
AccumulateValue(key, weight, defaultWeight);
}
public void AccumulateValue(string key, float weight, float factor) {
if (!clipMap.ContainsKey(key.ToUpper()))
return;
AccumulateValue(clipMap[key.ToUpper()], weight, factor);
}
public void AccumulateValue(string key, float weight) {
AccumulateValue(key, weight, defaultWeight);
}
public void UpdateAvatar(VRMBlendShapeProxy vrmBlendShapeProxy, Animator animator) {
proxy = vrmBlendShapeProxy;
this.animator = animator;
perfectSync = false;
if (proxy == null)
return;
HashSet<string> perfectSyncSet = new HashSet<string>(perfectSyncNames);
clipMap.Clear();
clipNames.Clear();
clearMap.Clear();
perfectSyncMap.Clear();
//perfectSyncMap.SetSmoothing(0.5f);
foreach (BlendShapeClip clip in vrmBlendShapeProxy.BlendShapeAvatar.Clips) {
if (clip.Preset == BlendShapePreset.Unknown && clip.BlendShapeName != null) {
string name = clip.BlendShapeName.ToUpper();
clipMap.Add(name, BlendShapeKey.CreateUnknown(clip.BlendShapeName));
clipNames.Add(BlendShapeKey.CreateUnknown(clip.BlendShapeName), name);
clearMap.Add(BlendShapeKey.CreateUnknown(clip.BlendShapeName), 0f);
if (perfectSyncSet.Contains(name)) {
perfectSyncSet.Remove(name);
}
} else if (clip.Preset != BlendShapePreset.Unknown) {
string name = clip.BlendShapeName.ToUpper();
clipNames.Add(BlendShapeKey.CreateFromPreset(clip.Preset), name);
clearMap.Add(BlendShapeKey.CreateFromPreset(clip.Preset), 0f);
}
}
if (perfectSyncSet.Count < 1)
perfectSync = true;
}
}
#if WINDOWS_BUILD
private Dictionary<OVRLipSync.Viseme, float[]> catsData = null;
#endif
void InitCatsData() {
#if WINDOWS_BUILD
catsData = new Dictionary<OVRLipSync.Viseme, float[]>();
// This is similar to what the Blender CATS plugin does, but with A, I, U, E, O, JawOpen
catsData.Add(OVRLipSync.Viseme.sil, new float[]{0f, 0f, 0f, 0f, 0f, 0f});
catsData.Add(OVRLipSync.Viseme.aa, new float[]{0.9998f, 0f, 0f, 0f, 0f, 1f});
catsData.Add(OVRLipSync.Viseme.CH, new float[]{0f, 0.9996f, 0f, 0f, 0f, 1f});
catsData.Add(OVRLipSync.Viseme.DD, new float[]{0.3f, 0.7f, 0f, 0f, 0f, 0.7f});
catsData.Add(OVRLipSync.Viseme.E, new float[]{0f, 0f, 0f, 0.9997f, 0f, 0.6f});
catsData.Add(OVRLipSync.Viseme.FF, new float[]{0.2f, 0.4f, 0f, 0f, 0f, 0.1f});
catsData.Add(OVRLipSync.Viseme.ih, new float[]{0.5f, 0.2f, 0f, 0f, 0f, 0.5f});
catsData.Add(OVRLipSync.Viseme.kk, new float[]{0.7f, 0.4f, 0f, 0f, 0f, 0.0f});
catsData.Add(OVRLipSync.Viseme.nn, new float[]{0.2f, 0.7f, 0f, 0f, 0f, 0.1f});
catsData.Add(OVRLipSync.Viseme.oh, new float[]{0f, 0f, 0f, 0f, 0.9999f, 1f});
catsData.Add(OVRLipSync.Viseme.ou, new float[]{0f, 0f, 0.9995f, 0f, 0f, 1f});
catsData.Add(OVRLipSync.Viseme.PP, new float[]{0.4f, 0f, 0f, 0f, 0.4f, 0f});
catsData.Add(OVRLipSync.Viseme.RR, new float[]{0f, 0.5f, 0f, 0f, 0.3f, 0.4f});
catsData.Add(OVRLipSync.Viseme.SS, new float[]{0f, 0.8f, 0f, 0f, 0f, 0.3f});
catsData.Add(OVRLipSync.Viseme.TH, new float[]{0.4f, 0f, 0f, 0f, 0.15f, 0.5f});
#endif
visemePresetMap = new BlendShapeKey[6] {
BlendShapeKey.CreateFromPreset(BlendShapePreset.A),
BlendShapeKey.CreateFromPreset(BlendShapePreset.I),
BlendShapeKey.CreateFromPreset(BlendShapePreset.U),
BlendShapeKey.CreateFromPreset(BlendShapePreset.E),
BlendShapeKey.CreateFromPreset(BlendShapePreset.O),
BlendShapeKey.CreateFromPreset(BlendShapePreset.Unknown)
};
lastVisemeValues = new float[] {0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f};
#if WINDOWS_BUILD
ovrMap = new Dictionary<OVRLipSync.Viseme, int>() {
[OVRLipSync.Viseme.sil] = 0,
[OVRLipSync.Viseme.aa] = 1,
[OVRLipSync.Viseme.CH] = 2,
[OVRLipSync.Viseme.DD] = 3,
[OVRLipSync.Viseme.E] = 4,
[OVRLipSync.Viseme.FF] = 5,
[OVRLipSync.Viseme.ih] = 6,
[OVRLipSync.Viseme.kk] = 7,
[OVRLipSync.Viseme.nn] = 8,
[OVRLipSync.Viseme.oh] = 9,
[OVRLipSync.Viseme.ou] = 10,
[OVRLipSync.Viseme.PP] = 11,
[OVRLipSync.Viseme.RR] = 12,
[OVRLipSync.Viseme.SS] = 13,
[OVRLipSync.Viseme.TH] = 14
};
#endif
fullVisemePresets = new BlendShapeKey[16] {
BlendShapeKey.CreateUnknown("SIL"),
BlendShapeKey.CreateFromPreset(BlendShapePreset.A),
BlendShapeKey.CreateUnknown("CH"),
BlendShapeKey.CreateUnknown("DD"),
BlendShapeKey.CreateFromPreset(BlendShapePreset.E),
BlendShapeKey.CreateUnknown("FF"),
BlendShapeKey.CreateFromPreset(BlendShapePreset.I),
BlendShapeKey.CreateUnknown("KK"),
BlendShapeKey.CreateUnknown("NN"),
BlendShapeKey.CreateFromPreset(BlendShapePreset.O),
BlendShapeKey.CreateFromPreset(BlendShapePreset.U),
BlendShapeKey.CreateUnknown("PP"),
BlendShapeKey.CreateUnknown("RR"),
BlendShapeKey.CreateUnknown("SS"),
BlendShapeKey.CreateUnknown("TH"),
BlendShapeKey.CreateFromPreset(BlendShapePreset.Unknown)
};
}
#if WINDOWS_BUILD
OVRLipSync.Viseme GetActiveViseme(out float bestValue) {
var visemes = Enum.GetValues(typeof(OVRLipSync.Viseme));
bestValue = -1f;
OVRLipSync.Viseme bestViseme = OVRLipSync.Viseme.sil;
foreach (var viseme in visemes) {
if (visemeData.Visemes[(int)viseme] > bestValue) {
bestViseme = (OVRLipSync.Viseme)viseme;
bestValue = visemeData.Visemes[(int)viseme];
}
}
return bestViseme;
}
#endif
bool ApplyVisemes() {
#if WINDOWS_BUILD
trackMouth = false;
if (vrmBlendShapeProxy == null || catsData == null)
return true;
float expressionFactor = 1f;
if (currentExpression != null)
expressionFactor = currentExpressionVisemeFactor;
if (currentExpression != null && !currentExpressionEnableVisemes) {
return true;
}
float weight;
OVRLipSync.Viseme current = GetActiveViseme(out weight);
if (current == OVRLipSync.Viseme.sil) {
if (wasSilViseme) {
if (hybridLipSync && Time.time - startedSilVisemes > silVisemeHybridThreshold) {
ApplyMouthShape();
return false;
}
} else {
startedSilVisemes = Time.time;
}
wasSilViseme = true;
} else {
if (wasSilViseme)
startedSilVisemes = Time.time;
wasSilViseme = false;
if (hybridLipSync && Time.time - startedSilVisemes < silVisemeHybridThreshold)
ApplyMouthShape(true);
}
if (!maximizeLipSync)
weight = Mathf.Clamp(weight * 1.5f, 0f, 1f);
else
weight = 1f;
float[] values = new float[16];
int lastIdx = 5;
if (haveFullVisemeSet) {
lastIdx = 15;
values[ovrMap[current]] = weight;
values[lastIdx] = catsData[current][5];
} else {
Array.Copy(catsData[current], values, 6);
}
for (int i = 0; i <= lastIdx; i++) {
lastVisemeValues[i] = values[i] * weight * (1f - visemeSmoothing) + lastVisemeValues[i] * visemeSmoothing;
if (lastVisemeValues[i] < 0f) {
lastVisemeValues[i] = 0f;
}
if (lastVisemeValues[i] > 1f) {
lastVisemeValues[i] = 1f;
}
float result = lastVisemeValues[i] * visemeFactor * expressionFactor;
if (result > 0f && i < lastIdx) {
if (haveFullVisemeSet)
proxy.AccumulateValue(fullVisemePresets[i], result, 1f);
else
proxy.AccumulateValue(visemePresetMap[i], result, 1f);
}
if (i == lastIdx) {
if (animateJawBone)
SetJaw(result * 0.99999f);
else
SetJaw(0f);
}
}
#endif
return false;
}
void SetJaw(float v) {
if (haveJawParameter) {
animator.SetFloat("JawMovement", v);
return;
}
if (jawBoneAnimation != null && jawBone != null) {
for (int i = 0; i < humanBodyBones.Length; i++) {
if (humanBodyBoneTransforms[i] != null)
humanBodyBoneRotations[i] = humanBodyBoneTransforms[i].localRotation;
}
jawBoneAnimation.SampleAnimation(lastAvatar.gameObject, v / jawBoneAnimation.frameRate);
jawRotation = jawBone.localRotation;
for (int i = 0; i < humanBodyBones.Length; i++) {
if (humanBodyBoneTransforms[i] != null)
humanBodyBoneTransforms[i].localRotation = humanBodyBoneRotations[i];
}
return;
}
}
void ApplyMouthShape(bool fadeOnly) {
if (vrmBlendShapeProxy == null)
return;
if (visemePresetMap == null)
InitCatsData();
if (openSeeData == null || openSeeData.features == null)
return;
if (lastMouthStates == null)
lastMouthStates = new float[]{0f, 0f, 0f, 0f, 0f, 0f};
if (currentMouthStates == null)
currentMouthStates = new float[]{0f, 0f, 0f, 0f, 0f, 0f};
if (mouthInterpolate == null)
mouthInterpolate = new TimeInterpolate();
float t = mouthInterpolate.Interpolate();
if (currentExpression != null && !currentExpressionEnableVisemes)
return;
trackMouth = !fadeOnly;
float expressionFactor = 1f;
if (currentExpression != null)
expressionFactor = currentExpressionVisemeFactor;
for (int i = 0; i < 6; i++) {
float interpolated = Mathf.Lerp(lastMouthStates[i], currentMouthStates[i], t);
float result = interpolated * expressionFactor * visemeFactor;
if (i < 5 && result > 0f)
if (!useCameraPerfectSync || !proxy.HasPerfectSync())
proxy.AccumulateValue(visemePresetMap[i], result);
if (i == 5) {
if (animateJawBone)
SetJaw(result * 0.99999f * blendShapeWeight);
else
SetJaw(0f);
}
}
if (lastMouth < openSeeData.time) {
lastMouth = openSeeData.time;
mouthInterpolate.UpdateTime(lastMouth);
float open = openSeeData.features.MouthOpen;
float wide = openSeeData.features.MouthWide;
float[] mouthStates = new float[]{0f, 0f, 0f, 0f, 0f, 0f};
float stabilizer = mouthStabilizer;
float stabilizerWide = mouthStabilizerWide;
if (openSeeData.rawEuler.y < turnLeftBoundaryAngle || openSeeData.rawEuler.y > turnRightBoundaryAngle) {
stabilizer *= 2.5f;
stabilizerWide *= 2.5f;
}
do {
if (fadeOnly)
break;
if (mouthUseSquelch && audioVolume < mouthSquelch)
break;
mouthStates[5] = Mathf.Clamp(open / 0.55f - 0.1f, 0f, 1f);
if (open < stabilizer && Mathf.Abs(wide) < stabilizer)
break;
if (wide > stabilizer && open < stabilizerWide)
break;
if (open > 0.5f) {
// O
mouthStates[4] = open;
} else if (open >= 0f) {
// A
mouthStates[0] = open;
}
if (wide >= 0f && open > stabilizer * 0.5f) {
if (wide > 0.5f) {
// I
mouthStates[1] = wide;
} else {
// E
mouthStates[3] = wide;
}
} else if (wide < Mathf.Clamp(stabilizerWide * 1.5f, 0f, 0.8f) && open > stabilizer) {
// U
mouthStates[2] = -wide;
}
float total = 0f;
float max = 0f;
for (int i = 0; i < 5; i++) {
total += mouthStates[i];
if (mouthStates[i] > max)
max = mouthStates[i];
}
max = Mathf.Clamp(max * 3f, 0f, 1f);
if (total < 0.0001f)
break;
for (int i = 0; i < 5; i++) {
mouthStates[i] = max * (mouthStates[i] / total);
}
} while (false);
for (int i = 0; i < 6; i++) {
lastMouthStates[i] = Mathf.Lerp(lastMouthStates[i], currentMouthStates[i], t);
mouthStates[i] = Mathf.Lerp(lastMouthStates[i], mouthStates[i], 1f - mouthSmoothing);
}
currentMouthStates = mouthStates;
}
}
void ApplyMouthShape() {
ApplyMouthShape(false);
}
void UpdatePerfectSync() {
if (openSeeData == null || lastPerfectSync >= openSeeData.time)
return;
lastPerfectSync = openSeeData.time;
if (useCameraPerfectSync && proxy.HasPerfectSync()) {
float upDownStrength = (openSeeData.features.EyebrowUpDownLeft + openSeeData.features.EyebrowUpDownRight) / 2f;
if (upDownStrength > 0f)
proxy.SetPerfectSync("BROWINNERUP", upDownStrength * 0.8f, lastPerfectSync);
else
proxy.SetPerfectSync("BROWINNERUP", 0, lastPerfectSync);
if (openSeeData.features.EyebrowUpDownLeft < 0.2f) {
proxy.SetPerfectSync("BROWDOWNLEFT", -openSeeData.features.EyebrowUpDownLeft * 0.5f, lastPerfectSync);
if (openSeeData.features.EyeLeft < 0.1f && openSeeData.features.EyeLeft > -0.6f) {
proxy.SetPerfectSync("EYESQUINTLEFT", -openSeeData.features.EyeLeft, lastPerfectSync);
proxy.SetPerfectSync("EYEBLINKLEFT", 0, lastPerfectSync);
} else if (openSeeData.features.EyeLeft <= -0.6f) {
proxy.SetPerfectSync("EYEBLINKLEFT", -openSeeData.features.EyeLeft * 1.5f, lastPerfectSync);
proxy.SetPerfectSync("EYESQUINTLEFT", 0, lastPerfectSync);
} else {
proxy.SetPerfectSync("EYESQUINTLEFT", 0, lastPerfectSync);
proxy.SetPerfectSync("EYEBLINKLEFT", 0, lastPerfectSync);
}
} else {
proxy.SetPerfectSync("BROWDOWNLEFT", 0, lastPerfectSync);
proxy.SetPerfectSync("EYESQUINTLEFT", 0, lastPerfectSync);
if (openSeeData.features.EyeLeft <= -0.3f)
proxy.SetPerfectSync("EYEBLINKLEFT", -openSeeData.features.EyeLeft * 1.5f, lastPerfectSync);
else
proxy.SetPerfectSync("EYEBLINKLEFT", 0, lastPerfectSync);
}
if (openSeeData.features.EyebrowUpDownRight < 0.2f) {
proxy.SetPerfectSync("BROWDOWNRIGHT", -openSeeData.features.EyebrowUpDownRight * 0.5f, lastPerfectSync);
if (openSeeData.features.EyeRight < 0.1f && openSeeData.features.EyeRight > -0.6f) {
proxy.SetPerfectSync("EYESQUINTRIGHT", -openSeeData.features.EyeRight, lastPerfectSync);
proxy.SetPerfectSync("EYEBLINKRIGHT", 0, lastPerfectSync);
} else if (openSeeData.features.EyeRight <= -0.6f) {
proxy.SetPerfectSync("EYEBLINKRIGHT", -openSeeData.features.EyeRight * 1.5f, lastPerfectSync);
proxy.SetPerfectSync("EYESQUINTRIGHT", 0, lastPerfectSync);
} else {
proxy.SetPerfectSync("EYESQUINTRIGHT", 0, lastPerfectSync);
proxy.SetPerfectSync("EYEBLINKRIGHT", 0, lastPerfectSync);
}
} else {
proxy.SetPerfectSync("BROWDOWNRIGHT", 0, lastPerfectSync);
proxy.SetPerfectSync("EYESQUINTRIGHT", 0, lastPerfectSync);
if (openSeeData.features.EyeRight <= -0.3f)
proxy.SetPerfectSync("EYEBLINKRIGHT", -openSeeData.features.EyeRight * 1.5f, lastPerfectSync);
else
proxy.SetPerfectSync("EYEBLINKRIGHT", 0, lastPerfectSync);
}
if (openSeeData.features.EyebrowSteepnessLeft < 0.2f)
proxy.SetPerfectSync("BROWOUTERUPLEFT", -openSeeData.features.EyebrowSteepnessLeft, lastPerfectSync);
else
proxy.SetPerfectSync("BROWOUTERUPLEFT", 0, lastPerfectSync);
if (openSeeData.features.EyebrowSteepnessRight < 0.2f)
proxy.SetPerfectSync("BROWOUTERUPRIGHT", -openSeeData.features.EyebrowSteepnessRight, lastPerfectSync);
else
proxy.SetPerfectSync("BROWOUTERUPRIGHT", 0, lastPerfectSync);
if (openSeeData.features.EyeLeft > 0.5f)
proxy.SetPerfectSync("EYEWIDELEFT", openSeeData.features.EyeLeft * 0.7f, lastPerfectSync);
else
proxy.SetPerfectSync("EYEWIDELEFT", 0, lastPerfectSync);
if (openSeeData.features.EyeRight > 0.5f)
proxy.SetPerfectSync("EYEWIDERIGHT", openSeeData.features.EyeRight * 0.7f, lastPerfectSync);
else
proxy.SetPerfectSync("EYEWIDERIGHT", 0, lastPerfectSync);
if (trackMouth) {
if (openSeeData.features.MouthWide < 0.5f)
proxy.SetPerfectSync("MOUTHPUCKER", -openSeeData.features.MouthWide * 0.3f, lastPerfectSync);
else
proxy.SetPerfectSync("MOUTHPUCKER", 0, lastPerfectSync);
if (openSeeData.features.MouthOpen > 0.0f) {
proxy.SetPerfectSync("JAWOPEN", openSeeData.features.MouthOpen, lastPerfectSync);
proxy.SetPerfectSync("MOUTHCLOSE", 0, lastPerfectSync);
} else if (openSeeData.features.MouthOpen < -0.0f) {
proxy.SetPerfectSync("MOUTHCLOSE", openSeeData.features.MouthOpen, lastPerfectSync);
proxy.SetPerfectSync("JAWOPEN", 0, lastPerfectSync);
} else {
proxy.SetPerfectSync("JAWOPEN", 0, lastPerfectSync);
proxy.SetPerfectSync("MOUTHCLOSE", 0, lastPerfectSync);
}
if (openSeeData.features.MouthCornerInOutLeft > 0.3f)
proxy.SetPerfectSync("MOUTHLEFT", openSeeData.features.MouthCornerInOutLeft * 0.5f, lastPerfectSync);
else
proxy.SetPerfectSync("MOUTHLEFT", 0, lastPerfectSync);
if (openSeeData.features.MouthCornerInOutRight > 0.3f)
proxy.SetPerfectSync("MOUTHRIGHT", openSeeData.features.MouthCornerInOutRight * 0.5f, lastPerfectSync);
else
proxy.SetPerfectSync("MOUTHRIGHT", 0, lastPerfectSync);
if (openSeeData.features.MouthCornerUpDownLeft > 0.3f)
proxy.SetPerfectSync("MOUTHSMILELEFT", openSeeData.features.MouthCornerUpDownLeft * 0.5f, lastPerfectSync);
else if (openSeeData.features.MouthCornerUpDownLeft < -0.3f)
proxy.SetPerfectSync("MOUTHFROWNLEFT", -openSeeData.features.MouthCornerUpDownLeft, lastPerfectSync);
else {
proxy.SetPerfectSync("MOUTHSMILELEFT", 0, lastPerfectSync);
proxy.SetPerfectSync("MOUTHFROWNLEFT", 0, lastPerfectSync);
}
if (openSeeData.features.MouthCornerUpDownRight > 0.3f)
proxy.SetPerfectSync("MOUTHSMILERIGHT", openSeeData.features.MouthCornerUpDownRight * 0.5f, lastPerfectSync);
else if (openSeeData.features.MouthCornerUpDownLeft < -0.3f)
proxy.SetPerfectSync("MOUTHFROWNRIGHT", -openSeeData.features.MouthCornerUpDownRight, lastPerfectSync);
else {
proxy.SetPerfectSync("MOUTHSMILERIGHT", 0, lastPerfectSync);
proxy.SetPerfectSync("MOUTHFROWNRIGHT", 0, lastPerfectSync);
}
}
}
}
void UpdateBrows() {
if ((browClips == null && (faceMesh == null || faceType < 0)) || openSeeData == null) {
return;
}
if (browInterpolate == null)
browInterpolate = new TimeInterpolate();
float t = browInterpolate.Interpolate();
float strength = eyebrowStrength;
if (currentExpression != null)
strength *= currentExpressionEyebrowWeight;
if (!useCameraPerfectSync || !proxy.HasPerfectSync()) {
if (!skipApply && browClips == null) {
if (browUpIndex > -1 && strength > 0f)
faceMesh.SetBlendShapeWeight(browUpIndex, strength * Mathf.Lerp(lastBrowStates[0], currentBrowStates[0], t));
if (browDownIndex > -1 && strength > 0f)
faceMesh.SetBlendShapeWeight(browDownIndex, strength * Mathf.Lerp(lastBrowStates[1], currentBrowStates[1], t));
if (browAngryIndex > -1 && strength > 0f)
faceMesh.SetBlendShapeWeight(browAngryIndex, strength * Mathf.Lerp(lastBrowStates[2], currentBrowStates[2], t));
if (browSorrowIndex > -1 && strength > 0f)
faceMesh.SetBlendShapeWeight(browSorrowIndex, strength * Mathf.Lerp(lastBrowStates[3], currentBrowStates[3], t));
}
if (browClips != null && strength > 0f) {
if (lastBrowUpDown > 0f)
proxy.AccumulateValue(browClips[0], strength * Mathf.Lerp(lastBrowStates[0], currentBrowStates[0], t));
else if (lastBrowUpDown < 0f)
proxy.AccumulateValue(browClips[1], strength * Mathf.Lerp(lastBrowStates[1], currentBrowStates[1], t));
}
}
if (lastBrows < openSeeData.time) {
lastBrows = openSeeData.time;
float upDownStrength = (openSeeData.features.EyebrowUpDownLeft + openSeeData.features.EyebrowUpDownRight) / 2f;
float stabilizer = 0.3f;
if (eyebrowZero > 0) {
if (upDownStrength > eyebrowZero)
upDownStrength = (upDownStrength - eyebrowZero) / (1f - eyebrowZero);
else
upDownStrength = (upDownStrength - eyebrowZero) / (1f + eyebrowZero);
} else if (eyebrowZero < 0) {
if (upDownStrength < eyebrowZero)
upDownStrength = (upDownStrength - eyebrowZero) / (1f + eyebrowZero);
else
upDownStrength = (upDownStrength - eyebrowZero) / (1f - eyebrowZero);
}
float magnitude = Mathf.Clamp(Mathf.Abs(upDownStrength - lastBrowUpDown) * eyebrowSensitivity, -1f, 1f);
float factor = 1f;
if (openSeeData.rawEuler.y < turnLeftBoundaryAngle || openSeeData.rawEuler.y > turnRightBoundaryAngle) {
factor = 0.7f;
stabilizer *= 2;
}
if (upDownStrength > 0 && (eyebrowIsMoving > 0 || magnitude > stabilizer)) {
upDownStrength = factor * Mathf.Clamp(upDownStrength / 0.6f, 0f, 1f);
if (magnitude > stabilizer)
eyebrowIsMoving = 5;
} else if (upDownStrength < 0 && (eyebrowIsMoving > 0 || magnitude > stabilizer)) {
upDownStrength = -factor * Mathf.Clamp(upDownStrength / -0.2f, 0f, 1f);
if (magnitude > stabilizer)
eyebrowIsMoving = 5;
} else if (upDownStrength > stabilizer || upDownStrength < stabilizer) {
upDownStrength = lastBrowUpDown;
} else {
upDownStrength = 0f;
}
if (eyebrowIsMoving > 0)
eyebrowIsMoving--;
upDownStrength = Mathf.LerpUnclamped(lastBrowUpDown, upDownStrength, 1f - eyebrowSmoothing);