やりたいこと
自分で作ったvroidをCandy Rock Starのステージで踊らせたい
環境
unity version 2020.3.18f1
参考にした記事
とてもとても分かりやすい記事があります。
うまくいかなったところを解消
FocusObj設定
この設定がないとエラーが出て実行できなかった。とりあえずモデルの先頭に設定。
MusicStartar追加
この設定がないとエラーが出て実行できなかった。さくっと追加。
ソース修正
こちらはエラーが出ないのにちゃんと動かなかった。バージョンが上がって書き方が変わった模様。
FaceChanger.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRM;
public class FaceChanger : MonoBehaviour
{
VRMBlendShapeProxy proxy;
void Start()
{
proxy = GetComponent<VRMBlendShapeProxy>();
}
//表情を変えるイベントをここで受け取る.
public void OnCallChangeFace(string str)
{
switch (str)
{ //受け取ったメッセージが
case "eye_close@unitychan": //「目を閉じる」であれば
proxy.SetValues(new Dictionary<BlendShapeKey, float>
{ {BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink), 1f } }); // 瞬き
break;
case "smile3@unitychan": //「笑顔」であれば
proxy.SetValues(new Dictionary<BlendShapeKey, float>
{ {BlendShapeKey.CreateFromPreset(BlendShapePreset.Joy), 1f } }); // 喜
break;
case "conf@unitychan": //「困惑」であれば
proxy.SetValues(new Dictionary<BlendShapeKey, float>
{ {BlendShapeKey.CreateFromPreset(BlendShapePreset.Angry), 1f } }); // 怒
break;
case "default@unitychan": //それ以外なら
proxy.SetValues(new Dictionary<BlendShapeKey, float>
{
{BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink), 0f } ,
{BlendShapeKey.CreateFromPreset(BlendShapePreset.Joy), 0f } ,
{BlendShapeKey.CreateFromPreset(BlendShapePreset.Angry), 0f } ,
}); //すべてリセット
break;
}
}
}
LipSyncController.cs
using UnityEngine;
using System.Collections;
using VRM;
public class LipSyncController : MonoBehaviour
{
public string targetName;
public Transform nodeA;
public Transform nodeE;
public Transform nodeI;
public Transform nodeO;
public Transform nodeU;
public AnimationCurve weightCurve;
VRMBlendShapeProxy target;
void Start()
{
target = GameObject.Find(targetName).GetComponent<VRMBlendShapeProxy>();
}
float GetWeight(Transform tr)
{
return weightCurve.Evaluate(tr.localPosition.z);
}
void LateUpdate()
{
Debug.Log("LipSyncController#LateUpdate:");
var total = 1.0f;
var w = total * GetWeight(nodeA);
target.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(BlendShapePreset.A), w);
total -= w;
w = total * GetWeight(nodeI);
target.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(BlendShapePreset.I), w);
total -= w;
w = total * GetWeight(nodeU);
target.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(BlendShapePreset.U), w);
total -= w;
w = total * GetWeight(nodeE);
target.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(BlendShapePreset.E), w);
total -= w;
w = total * GetWeight(nodeO);
target.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(BlendShapePreset.O), w);
target.Apply();
}
}
これで無事に踊れました!