MouthMovement
最終更新: 2020年1月30日
概述
MouthMovement是针对口形同步参数的当前值,应用开/关状态值的功能。
可以将动态中设置的口形同步曲线和从正在播放的音频中实时采样的值等应用到模型中。
参阅 此处 了解如何在模型上设置口形同步参数。
MouthMovement中唯一设置的是嘴巴的开合状态。
无法执行将嘴形与元音匹配等操作。
如果要在Unity上指定口形同步的参数,可以在Cubism Editor中的模型进行设置,也可以在Unity上由用户任意指定。
Cubism SDK for Unity中的MouthMovement包含三种类型的元素。
- 参数指定用组件
- 将值应用于各参数的组件
- 操作2中应用的值
1. 参数指定用组件
使用CubismMouthParameter指定要用于MouthMovement的参数。
CubismMouthParameter是一个继承MonoBehaviour的组件,
它通过附加到置入[Prefab根]/Parameters/下的GameObject来使用。
这会将与附加的GameObject具有相同ID的参数视为口形同步的参数。
如果模型本身设置有口形同步参数,那么CubismMouthParameter将在载入时附加到该参数的GameObject上。
由于CubismMouthParameter用作获取参考目的地的标记,因此它内部不处理任何内容,也没有数据。
2. 将值应用于各参数的组件
使用CubismMouthController将打开/关闭值应用于各参数。
这是一个继承自MonoBehaviour的组件,使用时会附加到Cubism的Prefab的根。
在原始化期间获取附加到Prefab的所有CubismMouthParameter的参考。
如果在执行过程中追加/移除眨眼参数,调用CubismMouthController.Refresh()再次获取参考。
/// <summary>
/// Refreshes controller. Call this method after adding and/or removing <see cref="CubismMouthParameter"/>s.
/// </summary>
public void Refresh()
{
var model = this.FindCubismModel();
// Fail silently...
if (model == null)
{
return;
}
// Cache destinations.
var tags = model
.Parameters
.GetComponentsMany<CubismMouthParameter>();
Destinations = new CubismParameter[tags.Length];
for (var i = 0; i < tags.Length; ++i)
{
Destinations[i] = tags[i].GetComponent<CubismParameter>();
}
// Get cubism update controller.
HasUpdateController = (GetComponent<CubismUpdateController>() != null);
}
...
/// <summary>
/// Called by Unity. Makes sure cache is initialized.
/// </summary>
private void Start()
{
// Initialize cache.
Refresh();
}
CubismMouthController在每帧的LateUpdate()时机,将CubismMouthController.MouthOpening的值应用于CubismMouthParameter标记的参数。
/// <summary>
/// Called by cubism update controller. Updates controller.
/// </summary>
/// <remarks>
/// Make sure this method is called after any animations are evaluated.
/// </remarks>
public void OnLateUpdate()
{
// Fail silently.
if (!enabled || Destinations == null)
{
return;
}
// Apply value.
Destinations.BlendToValue(BlendMode, MouthOpening);
}
MouthOpening的设置值在0.0f~1.0f的范围内。
CubismMouthController使用CubismMouthController.BlendMode中设置的计算方法将此值应用于对象参数。
通过从外部操纵此MouthOpening值,您可以使模型的嘴巴张开或合上。
/// <summary>
/// The opening of the mouth.
/// </summary>
[SerializeField, Range(0f, 1f)]
public float MouthOpening = 1f;
3. 操纵2中应用的值
如“2. 将值应用于各参数的组件”中所述,您可以通过操作CubismMouthController.MouthOpening的值,将值应用于口形同步的参数。
Cubism SDK for Unity可以通过三种方式操作此值:
- 通过动态操纵值
- 周期性操作值
- 通过从AudioClip中采样来操作值
此外,通过在用户端实装对该值的操作处理,可以独立自定义口形同步速度、时间等。
Tips
如果操作CubismMouthController.MouthOpening的组件在CubismMouthController之后执行,它可能不会按预期动作。
如果动作出现问题,可以通过在用户端显式控制组件的执行顺序来避免。
在Cubism SDK for Unity中,各组件的执行顺序由CubismUpdateController控制,因此您也可以使用它。
此外,由于上述三种设置方法操作相同的值,很难在不作任何努力的情况下将它们共存于一个模型中。
通过动态操纵值
当使用设置有眨眼参数的模型通过Cubism的Animator创建动态时,可以设置眨眼曲线。
如果将带有眨眼曲线的.motion3.json载入Unity项目,AnimationClip将以CubismMouthController.MouthOpening的值为对象生成该曲线。
因此,CubismMouthController.MouthOpening的值是通过以Animator组件等播放AnimationClip来操作的。
周期性操作值
使用CubismAutoMouthInput周期性操作口形同步的值。
CubismAutoMouthInput是一个使用正弦波计算和设置口形同步值的组件。
要使用CubismAutoMouthInput,请将其附加到Cubism的Prefab的根目录。
CubismAutoMouthInput有一个设置项目。
- Timescale
正弦波的周期发生变化。
/// <summary>
/// Timescale.
/// </summary>
[SerializeField]
public float Timescale = 10f;
/// <summary>
/// Called by Unity. Updates controller.
/// </summary>
/// <remarks>
/// Make sure this method is called after any animations are evaluated.
/// </remarks>
private void LateUpdate()
{
// Fail silently.
if (Controller == null)
{
return;
}
// Progress time.
T += (Time.deltaTime * Timescale);
// Evaluate.
Controller.MouthOpening = Mathf.Abs(Mathf.Sin(T));
}
通过从AudioClip中采样来操作值
使用CubismAudioMouthInput,从Unity上播放的音频中设置口形同步值。
CubismAudioMouthInput通过从AudioSource播放的音频信息中采样,实时生成和设置口形同步值。
要使用CubismAudioMouthInput,请将其附加到Cubism的Prefab的根目录。
/// <summary>
/// Samples audio input and applies it to mouth controller.
/// </summary>
private void Update()
{
// 'Fail' silently.
if (AudioInput == null)
{
return;
}
// Sample audio.
var total = 0f;
AudioInput.GetOutputData(Samples, 0);
for (var i = 0; i < Samples.Length; ++i)
{
var sample = Samples[i];
total += (sample * sample);
}
// Compute root mean square over samples.
var rms = Mathf.Sqrt(total / Samples.Length) * Gain;
// Clamp root mean square.
rms = Mathf.Clamp(rms, 0.0f, 1.0f);
// Smooth rms.
rms = Mathf.SmoothDamp(LastRms, rms, ref VelocityBuffer, Smoothing * 0.1f);
// Set rms as mouth opening and store it for next evaluation.
Target.MouthOpening = rms;
LastRms = rms;
}
CubismAudioMouthInput有四个设置项目。
/// <summary>
/// Audio source to sample.
/// </summary>
[SerializeField]
public AudioSource AudioInput;
/// <summary>
/// Sampling quality.
/// </summary>
[SerializeField]
public CubismAudioSamplingQuality SamplingQuality;
/// <summary>
/// Audio gain.
/// </summary>
[Range(1.0f, 10.0f)]
public float Gain = 1.0f;
/// <summary>
/// Smoothing.
/// </summary>
[Range(0.0f, 1.0f)]
public float Smoothing;
- AudioInput
要采样的AudioSource的参考。
- SamplingQuality
要采样的音频的精度。
- Gain
采样值的倍率。
1是同样大小,数值越大,口形同步值越大。
- Smoothing
采样值的平滑化量。
该值越高,口形同步值的变化就越平滑。