UniVRM v0.58.0
SetValues
ImmediatelySetValue
AccumulateValue
Apply
SetValues
関数のみを使用します。
そのフレームで必要な表情の weight 値をすべて集めてから SetValues
を 1 回だけ呼んで設定します。
var proxy = GetComponent<VRMBlendShapeProxy>();
proxy.SetValues(new Dictionary<BlendShapeKey, float>
{
{BlendShapeKey.CreateFromPreset(BlendShapePreset.A), 1f}, // [0, 1] の範囲で Weight を指定
{BlendShapeKey.CreateFromPreset(BlendShapePreset.Joy), 1f}, // システム定義の表情は enum で指定
{BlendShapeKey.CreateUnknown("USER_DEFINED_FACIAL"), 1f}, // ユーザ定義の表情は string で指定
});
この節では、なぜ SetValues
を使わなければならないのかという疑問に回答します。
たとえば 2 つの VRMBlendShape Blink_L
と Blink_R
が
VRMBlendShape Blink_L
A
の Blendshape eye_close_L
の weight 値 100
A
の Blendshape eye_close_R
の weight 値 1
VRMBlendShape Blink_R
A
の Blendshape eye_close_L
の weight 値 1
A
の Blendshape eye_close_R
の weight 値 100
で定義されているとします。 このとき両目を閉じたいモチベーションから、両方を有効にする意図で下記のように実行します。
proxy.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink_L), 1.0f);
proxy.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink_R), 1.0f);
すると、左目だけが開いてしまいます。
これは後から ImmediateSetValue
した Blink_R
が Blink_L
と競合して weight を上書きしてしまうからです。
したがって VRM の表情制御においては下記の 2 通りのどちらかの方法で書くことが求められます。
これらの方法はこの競合の問題を解決して表情を設定することができます。
proxy.SetValues(new Dictionary<BlendShapeKey, float>
{
{BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink_L), 1.0f},
{BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink_R), 1.0f},
});
または
proxy.AccumulateValue(BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink_L), 1.0f); // すぐに適用せずにたくわえる
proxy.AccumulateValue(BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink_R), 1.0f);
proxy.Apply(); // 蓄積した値をまとめて適用する