-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharacterBrain.cs
641 lines (523 loc) · 18.4 KB
/
CharacterBrain.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
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using RogoDigital.Lipsync;
using System;
using Motiviti.Enkidu;
namespace Motiviti.Enkidu
{
public class CharacterBrain : CharacterHead
{
public float talkAngle = 180;
public bool isPlayingCustomAnimation = false;
public CharacterHead conversationPartner = null;
public EyeGestures eyeGesture = EyeGestures.idle;
public CharacterHead.Moods mood;
public PlayerHead[] heads;
public bool interruptFlag = false;
public bool isAnimationPlaying = false;
public AnimationWaitingMethod animationWaitingMethod = AnimationWaitingMethod.UntilAnimationFinish;
public bool alwaysTurnToFacePlayer = false;
protected Transform mainTransform = null;
protected string currentMode = "";
protected ArmGestures currentArmGesture;
protected HeadGestures currentHeadGesture;
protected EyeGestures currentEyeGesture;
protected NeckGestures currentNeckGesture;
protected BodyGestures currentBodyGesture;
[SerializeField]
protected Animator animator;
protected bool isTalking = false;
protected bool lastFrameIsTalking = false;
protected bool holdingRemote = false;
string[] phonemes = { "AI", "E", "U", "O", "CDGKNRSThYZ", "FV", "L", "MBP", "WQ", "Rest" };
public enum AnimationWaitingMethod
{
UntilActionPoint = 0,
UntilAnimationFinish = 1
}
public class VoiceClip
{
public string name;
public string text;
public PhonemePositions phonemes;
public AudioClip audioClip;
public CharacterHead.Moods mood;
public string mode = "idle";
public AudioSource audioSource;
public float volume = 1;
public bool interruptFlag = false;
public float duration = 0;
}
public class PhonemePosition
{
public float position = 0;
public string phoneme;
public PhonemePosition(float pos, string pho)
{
position = pos;
phoneme = pho;
}
public override string ToString()
{
return String.Format("[{0}] {1}", position.ToString(), phoneme);
}
}
public class MoodPosition
{
public float position = 0;
public CharacterHead.Moods mood;
public MoodPosition(float pos, CharacterHead.Moods m)
{
position = pos;
mood = m;
}
}
public class ModePosition
{
public float position = 0;
public string mode;
public ModePosition(float pos, string m)
{
position = pos;
mode = m;
}
}
public class PhonemePositions
{
public LipSyncData lipSyncData;
ArrayList positions = new ArrayList();
ArrayList moodPositions = new ArrayList();
ArrayList modePositions = new ArrayList();
public void Add(PhonemePosition pos) // must be ordered
{
positions.Add(pos);
}
public void AddMood(MoodPosition pos) // must be ordered
{
moodPositions.Add(pos);
}
public void AddMode(ModePosition pos) // must be ordered
{
modePositions.Add(pos);
}
public string GetPhoneme(float position)
{
float currentPosition = -1;
string currentPhoneme = "MBP";
for (int i = 0; i < positions.Count; i++)
{
currentPhoneme = ((PhonemePosition)positions[i]).phoneme;
if (i < positions.Count - 1)
{
currentPosition = ((PhonemePosition)positions[i + 1]).position;
}
else
break;
if (currentPosition >= position) break;
}
int maxI = positions.Count - 1;
var cp = ((PhonemePosition)positions[maxI]).position;
if (position > cp)
{
if (position - cp > 0.1f) currentPhoneme = "MBP";
}
return currentPhoneme;
}
public void GetMood(float position, ref CharacterHead.Moods currentMood, ref bool change)
{
change = false;
float currentPosition = -1;
for (int i = 0; i < moodPositions.Count; i++)
{
currentPosition = ((MoodPosition)moodPositions[i]).position;
if (position >= currentPosition && (i >= moodPositions.Count - 1 || position < ((MoodPosition)moodPositions[i + 1]).position))
{
if (currentMood != ((MoodPosition)moodPositions[i]).mood)
{
currentMood = ((MoodPosition)moodPositions[i]).mood;
change = true;
}
return;
}
}
currentMood = CharacterHead.Moods.Neutral; // will not get picked up unless change==true
}
public void GetMode(float position, ref string currentMode, ref bool change)
{
change = false;
float currentPosition = -1;
for (int i = 0; i < modePositions.Count; i++)
{
currentPosition = ((ModePosition)modePositions[i]).position;
if (position >= currentPosition && (i >= modePositions.Count - 1 || position < ((ModePosition)modePositions[i + 1]).position))
{
if (currentMode != ((ModePosition)modePositions[i]).mode)
{
currentMode = ((ModePosition)modePositions[i]).mode;
change = true;
}
return;
}
}
currentMode = "idle"; // will not get picked up unless change==true
}
}
public enum GestureConfigurations
{
idle = 0,
explaining = 1,
explainingExactly = 2,
extremelyExcited = 3,
proud = 4,
embarrassed = 5
}
public enum BodyGestures
{
idle = 0,
frontLeanSlight = 1,
frontLean = 2,
backLeanSlight = 3,
backLean = 4
}
public enum HeadGestures
{
idle = 0,
talk1 = 1,
talk2 = 2,
slowTalk = 3,
lookAtCamera = 4,
embarrassed = 5
}
public enum NeckGestures
{
idle = 0,
leanFront = 1,
leanBack = 2,
lookBack = 3
}
public enum EyeGestures
{
idle = 0,
talk = 1,
up = 2,
down = 3,
left = 4,
right = 5,
back = 6,
side = 7,
follow = 8
}
public enum ArmGestures
{
idle = 0,
me1 = 1,
me2 = 2,
me3 = 3,
you1 = 5,
you2 = 6,
you3 = 7,
excitedFists = 8,
stop = 9,
explain1 = 10,
explain2 = 11,
explain3 = 12,
explain4 = 13,
explain5 = 14,
explain6 = 15,
explain7 = 16,
idea = 17,
why1 = 20,
why2 = 21,
oops1 = 25,
oops2 = 26,
thinking1 = 30,
thinking2 = 31,
proud = 35,
crossedArms = 36,
armsUp = 40,
holdingRemote = 50
}
public virtual void ChangeState(int newState)
{
Debug.LogWarning("TODO: ChangeState not implemented.");
}
public virtual void ChangeTalkMode(string mode)
{
Debug.LogWarning("TODO: Implement ChangeTalkMode.");
}
public bool IsTalking
{
get
{
return isTalking;
}
set
{
isTalking = value;
}
}
public virtual bool PlayCustomAnimation(string name, AnimationWaitingMethod method)
{
this.animationWaitingMethod = method;
return false;
}
public virtual void PlayStandardAnimation(string clip, string dir)
{
}
protected virtual void Awake()
{
foreach (PlayerHead h in heads)
{
if (h != null)
h.elroyBrain = this;
}
if (mainTransform == null)
{
if (animator != null)
mainTransform = animator.transform.parent;
else
mainTransform = transform;
}
}
public override void Start()
{
base.Start();
}
void OnEnable()
{
StartCoroutine(BlinkProc());
}
public EyeGestures GetCurrentEyeGesture()
{
return currentEyeGesture;
}
protected virtual IEnumerator BlinkProc()
{
while (true)
{
if (IsTalking)
{
yield return new WaitForSeconds(0.5f + UnityEngine.Random.value * 1);
}
else
{
yield return new WaitForSeconds(1.5f + UnityEngine.Random.value * 3);
}
foreach (PlayerHead h in heads)
{
if (h != null) h.Blink();
}
}
}
public override void SetMood(int moodIndex)
{
mood = (CharacterHead.Moods)moodIndex;
foreach (PlayerHead head in heads)
{
if (head && head.isActiveAndEnabled) head.SetMood(moodIndex);
}
}
public override void ChangeMood(CharacterHead.Moods mood)
{
this.mood = mood;
foreach (PlayerHead head in heads)
{
if (head /*&& head.isActiveAndEnabled*/) head.SetMood((int)mood);
}
}
public override void StopTalking()
{
ChangeTalkMode("idle");
ShowPhoneme("MBP");
SetTalkMode(false);
base.StopTalking();
interruptFlag = true;
foreach (PlayerHead head in heads)
{
if (head /*&& head.isActiveAndEnabled*/) head.StopTalking();
}
ChangeMood(Moods.Neutral);
IsTalking = false;
}
public override void ShowPhoneme(string phoneme)
{
foreach (PlayerHead head in heads)
{
if (head && head.isActiveAndEnabled) head.ShowPhoneme(phoneme);
}
}
public virtual void RunLipSync(string charName, AudioSource src, AudioClip audioClip, int lineID, string message)
{
StartCoroutine(RunLipSyncProc(charName, src, audioClip, lineID, message));
}
CharacterHead.Moods GetMoodFromString(string name, out string restOfText)
{
if (string.IsNullOrEmpty(name))
{
restOfText = name;
return CharacterHead.Moods.Neutral;
}
restOfText = name;
int moodPos = name.IndexOf(">");
int moodId = 0;
if (moodPos != -1)
{
string moodStr = name.Substring(0, moodPos);
moodId = int.Parse(moodStr);
name = name.Substring(moodPos + 1);
restOfText = name;
}
return (CharacterHead.Moods)moodId;
}
CharacterHead.Moods GetMoodFromString(string moodString)
{
CharacterHead.Moods mood = CharacterHead.Moods.Neutral;
switch (moodString)
{
case "Neutral":
mood = CharacterHead.Moods.Neutral;
break;
case "Angry":
mood = CharacterHead.Moods.Angry;
break;
case "Sad":
mood = CharacterHead.Moods.Sad;
break;
case "Happy":
mood = CharacterHead.Moods.Happy;
break;
case "Scared":
mood = CharacterHead.Moods.Scared;
break;
case "Determined":
mood = CharacterHead.Moods.Determined;
break;
}
return mood;
}
protected virtual void SwitchToIdleIfNotAlready()
{
// TODO
Debug.LogWarning("TODO: Implement SwitchToIdleIfNotAlready");
}
public void SetTalkMode(bool tm)
{
if (tm) SwitchToIdleIfNotAlready();
animator.SetBool("talking", tm);
IsTalking = tm;
ResetArmAnimationStatus();
}
protected virtual void ResetToIdle()
{
}
protected virtual IEnumerator RunLipSyncProc(string charName, AudioSource src, AudioClip audioClip, int lineID, string message)
{
this.interruptFlag = false;
IsTalking = true;
string currentMode = "idle";
LipSyncData lipSyncData = Resources.Load<LipSyncData>("Lipsync/" + charName + "/" + charName + lineID.ToString());
if (audioClip == null || lipSyncData == null)
{
float fakeClipLength = 3f;
float startTime = Time.time;
fakeClipLength = message.Length * 0.1f;
// Simulate a talk cycle
string[] talkCycle = { "AI", "AI", "O", "AI", "E", "AI", "O", "U" };
int i = 0;
while (Time.time - startTime < fakeClipLength && !interruptFlag)
{
//if(character != null && !character.isTalking) interruptFlag = true;
string ph = talkCycle[i % talkCycle.Length];
i++;
ShowPhoneme(ph);
yield return new WaitForSeconds(.09f);
}
ShowPhoneme("MBP");
}
else
{
VoiceClip voiceClip = new VoiceClip();
voiceClip.name = audioClip.name;
voiceClip.text = "text not needed here";
CharacterHead.Moods mood = CharacterHead.Moods.Neutral;
SetArmAnimationEnabled(true);
if (lipSyncData != null)
{
voiceClip.phonemes = GetPhonemesFromLipSyncData(lipSyncData, audioClip.length);
voiceClip.audioClip = audioClip;
voiceClip.mood = mood;
CharacterHead.Moods currentMood = CharacterHead.Moods.Neutral;
while (src.isPlaying && !interruptFlag)
{
//if(character != null && !character.isTalking) interruptFlag = true;
float time = src.time;
bool change = false;
voiceClip.phonemes.GetMode(time /*+ pause*/, ref currentMode, ref change);
if (change)
{
ChangeTalkMode(currentMode);
}
change = false;
voiceClip.phonemes.GetMood(time /*+ pause*/, ref currentMood, ref change);
if (change)
{
ChangeMood(currentMood);
}
string ph = voiceClip.phonemes.GetPhoneme(time /*+ pause*/ ).Trim();
ShowPhoneme(ph);
yield return new WaitForSeconds(1 / 60f);
}
}
}
ChangeTalkMode("idle");
ShowPhoneme("MBP");
ChangeMood(CharacterHead.Moods.Neutral);
IsTalking = false;
animator.SetBool("talking", false);
ResetArmAnimationStatus();
yield return null;
}
protected virtual void ResetArmAnimationStatus()
{
Debug.LogWarning("not implemented");
}
protected virtual void SetArmAnimationEnabled(bool b)
{
Debug.LogWarning("not implemented");
}
protected PhonemePositions GetPhonemesFromLipSyncData(LipSyncData data, float audioClipLength)
{
PhonemePositions pos = new PhonemePositions();
foreach (var phonemeMarker in data.phonemeData)
{
pos.Add(new PhonemePosition(phonemeMarker.time * audioClipLength, phonemes[phonemeMarker.phonemeNumber]));
}
foreach (var emotionMarker in data.emotionData)
{
pos.AddMood(new MoodPosition(emotionMarker.startTime * audioClipLength, GetMoodFromString(emotionMarker.emotion)));
pos.AddMood(new MoodPosition(emotionMarker.endTime * audioClipLength, CharacterHead.Moods.Neutral));
}
foreach (var gestureMarker in data.gestureData)
{
pos.AddMode(new ModePosition(gestureMarker.time * audioClipLength, gestureMarker.gesture));
}
return pos;
}
public virtual void AnimationEventEndCutScene()
{
Debug.LogWarning("TODO: Implement AnimationEventEndCutScene");
}
public virtual void AnimationActionPoint(string animationName)
{
Debug.LogWarning("TODO: Implement AnimationActionPoint");
}
public virtual void AnimationFinished(string animationName)
{
Debug.Log("AnimationFinished " + animationName);
isAnimationPlaying = false;
}
}
}