-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomOVRLipSyncContextMorphTarget.cs
271 lines (233 loc) · 8.69 KB
/
CustomOVRLipSyncContextMorphTarget.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
/************************************************************************************
Filename : OVRLipSyncContextMorphTarget.cs
Content : This bridges the viseme output to the morph targets
Created : August 7th, 2015
Copyright : Copyright Facebook Technologies, LLC and its affiliates.
All rights reserved.
Licensed under the Oculus Audio SDK License Version 3.3 (the "License");
you may not use the Oculus Audio SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.
You may obtain a copy of the License at
https://developer.oculus.com/licenses/audio-3.3/
Unless required by applicable law or agreed to in writing, the Oculus Audio SDK
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************************/
using UnityEngine;
using System.Linq;
using System;
using static UnityEngine.GraphicsBuffer;
using UnityEditor;
public class CustomOVRLipSyncContextMorphTarget : MonoBehaviour
{
// PUBLIC
// Manually assign the skinned mesh renderer to this script
[Tooltip("Skinned Mesh Rendered target to be driven by Oculus Lipsync")]
public SkinnedMeshRenderer[] skinnedMeshRenderer = null;
public Transform jawBone;
private Quaternion jawBoneRotation;
private float jawAngleInc = 0f;
// Set the blendshape index to go to (-1 means there is not one assigned)
[Tooltip("Blendshape index to trigger for each viseme.")]
public VisemeBlendMapping[] visemeToBlendTargets;
[Serializable] public class VisemeBlendMapping
{
public int[] visemeToBlendTargets = Enumerable.Range(0, OVRLipSync.VisemeCount).ToArray();
public VisemeBlendMapping()
{
visemeToBlendTargets = Enumerable.Range(0, OVRLipSync.VisemeCount).ToArray();
}
}
// enable/disable sending signals to viseme engine
[Tooltip("Enable using the test keys defined below to manually trigger each viseme.")]
public bool enableVisemeTestKeys = false;
[Tooltip("Test keys used to manually trigger an individual viseme - by " +
"default the QWERTY row of a US keyboard.")]
public KeyCode[] visemeTestKeys =
{
KeyCode.BackQuote,
KeyCode.Tab,
KeyCode.Q,
KeyCode.W,
KeyCode.E,
KeyCode.R,
KeyCode.T,
KeyCode.Y,
KeyCode.U,
KeyCode.I,
KeyCode.O,
KeyCode.P,
KeyCode.LeftBracket,
KeyCode.RightBracket,
KeyCode.Backslash,
};
[Tooltip("Test key used to manually trigger laughter and visualise the results")]
public KeyCode laughterKey = KeyCode.CapsLock;
[Tooltip("Blendshape index to trigger for laughter")]
public int laughterBlendTarget = OVRLipSync.VisemeCount;
[Range(0.0f, 1.0f)]
[Tooltip("Laughter probability threshold above which the laughter blendshape will be activated")]
public float laughterThreshold = 0.5f;
[Range(0.0f, 3.0f)]
[Tooltip("Laughter animation linear multiplier, the final output will be clamped to 1.0")]
public float laughterMultiplier = 1.5f;
// smoothing amount
[Range(1, 100)]
[Tooltip("Smoothing of 1 will yield only the current predicted viseme, 100 will yield an extremely smooth viseme response.")]
public int smoothAmount = 70;
// PRIVATE
// Look for a lip-sync Context (should be set at the same level as this component)
private OVRLipSyncContextBase lipsyncContext = null;
/// <summary>
/// Start this instance.
/// </summary>
void Start()
{
// morph target needs to be set manually; possibly other components will need the same
if (skinnedMeshRenderer == null)
{
Debug.LogError("LipSyncContextMorphTarget.Start Error: " +
"Please set the target Skinned Mesh Renderer to be controlled!");
return;
}
// make sure there is a phoneme context assigned to this object
lipsyncContext = GetComponent<OVRLipSyncContextBase>();
if (lipsyncContext == null)
{
Debug.LogError("LipSyncContextMorphTarget.Start Error: " +
"No OVRLipSyncContext component on this object!");
}
else
{
// Send smoothing amount to context
lipsyncContext.Smoothing = smoothAmount;
}
if(jawBone != null)
{
jawBoneRotation = jawBone.localRotation;
}
}
/// <summary>
/// Update this instance.
/// </summary>
void Update()
{
if ((lipsyncContext != null) && (skinnedMeshRenderer != null))
{
// get the current viseme frame
OVRLipSync.Frame frame = lipsyncContext.GetCurrentPhonemeFrame();
if (frame != null)
{
SetVisemeToMorphTarget(frame);
SetLaughterToMorphTarget(frame);
}
// TEST visemes by capturing key inputs and sending a signal
CheckForKeys();
// Update smoothing value
if (smoothAmount != lipsyncContext.Smoothing)
{
lipsyncContext.Smoothing = smoothAmount;
}
}
}
/// <summary>
/// Sends the signals.
/// </summary>
void CheckForKeys()
{
if (enableVisemeTestKeys)
{
for (int i = 0; i < OVRLipSync.VisemeCount; ++i)
{
CheckVisemeKey(visemeTestKeys[i], i, 100);
}
}
CheckLaughterKey();
}
/// <summary>
/// Sets the viseme to morph target.
/// </summary>
void SetVisemeToMorphTarget(OVRLipSync.Frame frame)
{
float javOpen = Mathf.Max(frame.Visemes[11] * 0.5f, frame.Visemes[12] * 0.3f, frame.Visemes[10] * 0.9f, frame.Visemes[13] * 0.9f, frame.Visemes[14] * 0.7f);
jawAngleInc = Mathf.Lerp(0, 15, javOpen);
for (int j = 0; j < visemeToBlendTargets.Length; j++)
{
for (int i = 0; i < visemeToBlendTargets.Length; i++)
{
if (visemeToBlendTargets[j].visemeToBlendTargets[i] != -1)
{
// Viseme blend weights are in range of 0->1.0, we need to make range 100
skinnedMeshRenderer[j].SetBlendShapeWeight(
visemeToBlendTargets[j].visemeToBlendTargets[i],
frame.Visemes[i] * 100.0f);
}
}
}
if (jawBone != null)
{
jawBone.localRotation = jawBoneRotation * Quaternion.Euler(new Vector3(0, 0, -jawAngleInc));
}
}
/// <summary>
/// Sets the laughter to morph target.
/// </summary>
void SetLaughterToMorphTarget(OVRLipSync.Frame frame)
{
if (laughterBlendTarget != -1)
{
// Laughter score will be raw classifier output in [0,1]
float laughterScore = frame.laughterScore;
// Threshold then re-map to [0,1]
laughterScore = laughterScore < laughterThreshold ? 0.0f : laughterScore - laughterThreshold;
laughterScore = Mathf.Min(laughterScore * laughterMultiplier, 1.0f);
laughterScore *= 1.0f / laughterThreshold;
foreach (SkinnedMeshRenderer smr in skinnedMeshRenderer)
{
smr.SetBlendShapeWeight(
laughterBlendTarget,
laughterScore * 100.0f);
}
}
}
/// <summary>
/// Sends the viseme signal.
/// </summary>
/// <param name="key">Key.</param>
/// <param name="viseme">Viseme.</param>
/// <param name="arg1">Arg1.</param>
void CheckVisemeKey(KeyCode key, int viseme, int amount)
{
if (Input.GetKeyDown(key))
{
foreach (VisemeBlendMapping vbm in visemeToBlendTargets)
{
lipsyncContext.SetVisemeBlend(vbm.visemeToBlendTargets[viseme], amount);
}
}
if (Input.GetKeyUp(key))
{
foreach (VisemeBlendMapping vbm in visemeToBlendTargets)
{
lipsyncContext.SetVisemeBlend(vbm.visemeToBlendTargets[viseme], 0);
}
}
}
/// <summary>
/// Sends the laughter signal.
/// </summary>
void CheckLaughterKey()
{
if (Input.GetKeyDown(laughterKey))
{
lipsyncContext.SetLaughterBlend(100);
}
if (Input.GetKeyUp(laughterKey))
{
lipsyncContext.SetLaughterBlend(0);
}
}
}