Created
January 30, 2021 23:42
-
-
Save XakazukinX/d17410f4c2360386cac221275e856455 to your computer and use it in GitHub Desktop.
uLipSyncをVRM BlendShape Proxyで受けるやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using VRM; | |
namespace uLipSync | |
{ | |
[System.Serializable] | |
public class BlendShapeProxyLipSyncInfo | |
{ | |
public string clipName = ""; | |
public BlendShapeClip blendShapeClip; | |
public int index = -1; | |
public float factor = 1f; | |
public float blend = 0f; | |
} | |
public class uLipSyncVRMBlendShapeProxy : MonoBehaviour | |
{ | |
public VRMBlendShapeProxy vrmBlendShapeProxy; | |
/*[SerializeField] private BlendShapeClip[] targetPreset;*/ | |
public List<BlendShapeProxyLipSyncInfo> blendShapeInfoList = new List<BlendShapeProxyLipSyncInfo>() | |
{ | |
new BlendShapeProxyLipSyncInfo(), | |
new BlendShapeProxyLipSyncInfo(), | |
new BlendShapeProxyLipSyncInfo(), | |
new BlendShapeProxyLipSyncInfo(), | |
new BlendShapeProxyLipSyncInfo(), | |
new BlendShapeProxyLipSyncInfo(), | |
}; | |
private float _cacheVolume = 0f; | |
private Dictionary<BlendShapeKey, float> blendShapeWeight = new Dictionary<BlendShapeKey, float>(); | |
private void Start() | |
{ | |
blendShapeWeight = new Dictionary<BlendShapeKey, float>(); | |
var clips = vrmBlendShapeProxy.BlendShapeAvatar.Clips; | |
for (int i = 0; i < clips.Count; i++) | |
{ | |
blendShapeWeight.Add(clips[i].Key, 0); | |
} | |
} | |
public void OnLipSyncUpdate(LipSyncInfo info) | |
{ | |
foreach (var kv in info.vowels) | |
{ | |
int i = (int) kv.Key; | |
blendShapeInfoList[i].blend = kv.Value; | |
} | |
_cacheVolume = info.volume; | |
} | |
private void LateUpdate() | |
{ | |
foreach (var info in blendShapeInfoList) | |
{ | |
var clip = info.blendShapeClip; | |
if (clip == null) continue; | |
if (string.IsNullOrEmpty(info.clipName)) continue; | |
float blend = info.blend * info.factor * _cacheVolume; | |
blendShapeWeight[clip.Key] = blend; | |
vrmBlendShapeProxy.AccumulateValue(clip.Key, blend); | |
} | |
vrmBlendShapeProxy.Apply(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using Unity.Collections.LowLevel.Unsafe; | |
using UnityEditor; | |
using UnityEngine; | |
using VRM; | |
namespace uLipSync | |
{ | |
[CustomEditor(typeof(uLipSyncVRMBlendShapeProxy))] | |
public class uLipSyncVRMBlendShapeProxyEditor : Editor | |
{ | |
uLipSyncVRMBlendShapeProxy _uLipSyncVrmBlendShapeProxy | |
{ | |
get { return target as uLipSyncVRMBlendShapeProxy; } | |
} | |
private bool IsValidProxy() | |
{ | |
//proxyがアサインされてなかったらアタッチしたオブジェクトから探す | |
if (_uLipSyncVrmBlendShapeProxy.vrmBlendShapeProxy == null) | |
{ | |
_uLipSyncVrmBlendShapeProxy.vrmBlendShapeProxy = | |
_uLipSyncVrmBlendShapeProxy.gameObject.GetComponentInParent<VRMBlendShapeProxy>(); | |
} | |
//それでも見つからなければ中断 | |
if (_uLipSyncVrmBlendShapeProxy.vrmBlendShapeProxy == null) | |
{ | |
Debug.LogError("VRMBlendShapeProxy is not found"); | |
return false; | |
} | |
return true; | |
} | |
private void AutoSetPresetClip(VRMBlendShapeProxy proxy) | |
{ | |
void Set(Vowel vowel, BlendShapePreset preset) | |
{ | |
var info = _uLipSyncVrmBlendShapeProxy.blendShapeInfoList[(int) vowel]; | |
var clip = proxy.BlendShapeAvatar.GetClip(preset); | |
info.index = proxy.BlendShapeAvatar.Clips.IndexOf(clip); | |
info.clipName = clip.name; | |
info.blendShapeClip = clip; | |
} | |
Set(Vowel.A, BlendShapePreset.A); | |
Set(Vowel.I, BlendShapePreset.I); | |
Set(Vowel.U, BlendShapePreset.U); | |
Set(Vowel.E, BlendShapePreset.E); | |
Set(Vowel.O, BlendShapePreset.O); | |
Debug.Log("Auto Set VRM BlendShapeProxy Presets."); | |
} | |
public override void OnInspectorGUI() | |
{ | |
serializedObject.Update(); | |
//uLipSyncのエディタ拡張をそのまま使わせてもらう | |
EditorUtil.DrawProperty(serializedObject, nameof(_uLipSyncVrmBlendShapeProxy.vrmBlendShapeProxy)); | |
if (GUILayout.Button("Auto Set")) | |
{ | |
if (IsValidProxy()) | |
{ | |
var proxy = _uLipSyncVrmBlendShapeProxy.vrmBlendShapeProxy; | |
AutoSetPresetClip(proxy); | |
EditorUtility.SetDirty(target); | |
} | |
} | |
for (int i = (int) Vowel.A; | |
i <= (int) Vowel.None && i < _uLipSyncVrmBlendShapeProxy.blendShapeInfoList.Count; | |
++i) | |
{ | |
var info = _uLipSyncVrmBlendShapeProxy.blendShapeInfoList[i]; | |
DrawBlendShape((Vowel) i, info); | |
} | |
serializedObject.ApplyModifiedProperties(); | |
} | |
private void DrawBlendShape(Vowel vowel, BlendShapeProxyLipSyncInfo info) | |
{ | |
EditorGUILayout.BeginHorizontal(); | |
EditorGUILayout.PrefixLabel(vowel.ToString()); | |
DrawBlendShapePopup(info); | |
DrawFactor(info); | |
EditorGUILayout.EndHorizontal(); | |
} | |
private void DrawBlendShapePopup(BlendShapeProxyLipSyncInfo info) | |
{ | |
var clipNames = GetBlendShapeClipNameArray(); | |
var newIndex = | |
EditorGUILayout.Popup(info.index + 1, clipNames, GUILayout.MinWidth(200f)); | |
if (newIndex != info.index + 1) | |
{ | |
Undo.RecordObject(target, "Change Blend Shape"); | |
info.index = newIndex - 1; | |
info.clipName = (info.index == -1 ? "" : clipNames[info.index]); | |
if (info.index == -1) | |
{ | |
info.clipName = ""; | |
info.blendShapeClip = null; | |
} | |
else | |
{ | |
info.clipName = clipNames[info.index]; | |
info.blendShapeClip = | |
_uLipSyncVrmBlendShapeProxy.vrmBlendShapeProxy.BlendShapeAvatar.Clips[info.index]; | |
} | |
} | |
} | |
private string[] GetBlendShapeClipNameArray() | |
{ | |
var proxy = _uLipSyncVrmBlendShapeProxy.vrmBlendShapeProxy; | |
if (proxy == null) | |
{ | |
return new string[0]; | |
} | |
var clips = proxy.BlendShapeAvatar.Clips; | |
var names = new List<string>(); | |
names.Add("None"); | |
for (int i = 0; i < clips.Count; ++i) | |
{ | |
var clipName = clips[i].BlendShapeName; | |
names.Add(clipName); | |
} | |
return names.ToArray(); | |
} | |
// uLipSyncBlendShapeのエディタ拡張を引用 | |
private void DrawFactor(BlendShapeProxyLipSyncInfo info) | |
{ | |
float factor = EditorGUILayout.Slider(info.factor, 0f, 2f); | |
if (Math.Abs(factor - info.factor) > 0.01f) | |
{ | |
Undo.RecordObject(target, "Change Blend Factor"); | |
info.factor = factor; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ライセンス
このコード
MIT
依存物
uLipSync
https://github.com/hecomi/uLipSync
http://tips.hecomi.com/entry/2021/01/10/001929
UniVRM
https://github.com/vrm-c/UniVRM