HarmonicMotion
最終更新: 2020年1月30日
概述
HarmonicMotion是一种定期重复指定参数值的功能。
它主要用于呼吸等不断动作的事物。
单击 此处 了解如何设置HarmonicMotion。
Cubism SDK for Unity中的HarmonicMotion包含两个元素。
- 用于指定要动作的参数的组件
- 操作各参数值的组件
1. 用于指定要动作的参数的组件
使用CubismHarmonicMotionParameter指定用于HarmonicMotion的参数。
CubismHarmonicMotionParameter是一个继承MonoBehaviour的组件,
它通过附加到置入[Prefab根]/Parameters/下的GameObject来使用。
这将使与附加GameObject具有相同ID的参数值定期动作。
CubismHarmonicMotionParameter有5个设置项目。
/// <summary>
/// Timescale channel.
/// </summary>
[SerializeField]
public int Channel;
/// <summary>
/// Motion direction.
/// </summary>
[SerializeField]
public CubismHarmonicMotionDirection Direction;
/// <summary>
/// Normalized origin of motion.
/// </summary>
/// <remarks>
/// The actual origin used for evaluating the motion depends limits of the <see cref="CubismParameter"/>.
/// </remarks>
[SerializeField, Range(0f, 1f)]
public float NormalizedOrigin = 0.5f;
/// <summary>
/// Normalized range of motion.
/// </summary>
/// <remarks>
/// The actual origin used for evaluating the motion depends limits of the <see cref="CubismParameter"/>.
/// </remarks>
[SerializeField, Range(0f, 1f)]
public float NormalizedRange = 0.5f;
/// <summary>
/// Duration of one motion cycle in seconds.
/// </summary>
[SerializeField, Range(0.01f, 10f)]
public float Duration = 3f;
- Channel
指定在CubismHarmonicMotionController中设置的正弦波周期的倍率。
在HarmonicMotion中,您可以为一个模型设置多个周期,且可以在CubismHarmonicMotionController中设置它们。
在此处设置CubismHarmonicMotionController.ChannelTimescales的索引。
- Direction
以参数的中心为基准,设置周期动作的范围。
有以下三个设置项目。
– Left : 它仅在参数中心的左半部分动作。
– Right:它仅在参数中心的右半部分动作。
– Centric:它在参数全体动作。
- NormalizedOrigin
在Direction中设置要用作基准的参数中心。
以该参数最小值为0、最大值为1时的值为中心进行设置。
- NormalizedRange
设置使值从NormalizedOrigin中设置的值的中心动作的幅度。
设置该参数最小值为0、最大值为1时,从中心移动的距离。
该值只能设置在NormalizedOrigin设置的中心位置到参数的最小值或最大值的范围内。
- Duration
调整参数周期。
/// <summary>
/// Evaluates the parameter.
/// </summary>
/// <returns>Parameter value.</returns>
internal float Evaluate()
{
// Lazily initialize.
if (!IsInitialized)
{
Initialize();
}
// Restore origin and range.
var origin = MinimumValue + (NormalizedOrigin * ValueRange);
var range = NormalizedRange * ValueRange;
// Clamp the range so that it stays within the limits.
Clamp(ref origin, ref range);
// Return result.
return origin + (range * Mathf.Sin(T * (2 * Mathf.PI) / Duration));
}
/// <summary>
/// Clamp origin and range based on <see cref="Direction"/>.
/// </summary>
/// <param name="origin">Origin to clamp.</param>
/// <param name="range">Range to clamp.</param>
private void Clamp(ref float origin, ref float range)
{
switch (Direction)
{
case CubismHarmonicMotionDirection.Left:
{
if ((origin - range) >= MinimumValue)
{
range /= 2;
origin -= range;
}
else
{
range = (origin - MinimumValue) / 2f;
origin = MinimumValue + range;
NormalizedRange = (range * 2f)/ValueRange;
}
break;
}
case CubismHarmonicMotionDirection.Right:
{
if ((origin + range) <= MaximumValue)
{
range /= 2f;
origin += range;
}
else
{
range = (MaximumValue - origin) / 2f;
origin = MaximumValue - range;
NormalizedRange = (range * 2f)/ValueRange;
}
break;
}
default:
{
break;
}
}
// Clamp both range and NormalizedRange.
if ((origin - range) < MinimumValue)
{
range = origin - MinimumValue;
NormalizedRange = range / ValueRange;
}
else if ((origin + range) > MaximumValue)
{
range = MaximumValue - origin;
NormalizedRange = range / ValueRange;
}
}
此外,CubismHarmonicMotionParameter也被用作CubismHarmonicMotionController获取参考目标的标记。
2. 操作各参数值的组件
使用CubismHarmonicMotionController将打开/关闭值应用于各参数。
这是一个继承自MonoBehaviour的组件,使用时会附加到Cubism的Prefab的根。
获取在原始化时附加到Prefab的所有CubismHarmonicMotionParameter的参考。
如果在执行过程中追加/删除周期性运行值的参数,将调用CubismHarmonicMotionController.Refresh()再次获取参考。
/// <summary>
/// Refreshes the controller. Call this method after adding and/or removing <see cref="CubismHarmonicMotionParameter"/>.
/// </summary>
public void Refresh()
{
var model = this.FindCubismModel();
// Catch sources and destinations.
Sources = model
.Parameters
.GetComponentsMany<CubismHarmonicMotionParameter>();
Destinations = new CubismParameter[Sources.Length];
for (var i = 0; i < Sources.Length; ++i)
{
Destinations[i] = Sources[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();
}
CubismHarmonicMotionController在每帧的LateUpdate()时,应用为CubismHarmonicMotionParameter标记的参数计算的值。
/// <summary>
/// Called by Unity. Makes sure cache is initialized.
/// </summary>
private void Start()
{
// Initialize cache.
Refresh();
}
CubismHarmonicMotionController有两个设置项目。
/// <summary>
/// Blend mode.
/// </summary>
[SerializeField]
public CubismParameterBlendMode BlendMode = CubismParameterBlendMode.Additive;
/// <summary>
/// The timescales for each channel.
/// </summary>
[SerializeField]
public float[] ChannelTimescales;
- BlendMode
用于将值应用于到参数的混合模式。
可以设置以下三个值。
– Override:覆盖当前值。
– Additive:加算到当前值。
– Multiply : 正片叠底当前值。
- ChannelTimescales
设置正弦波的周期。
您可以设置多个周期。