-
-
Save XakazukinX/d17410f4c2360386cac221275e856455 to your computer and use it in GitHub Desktop.
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(); | |
} | |
} | |
} |
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; | |
} | |
} | |
} | |
} |
ライセンス
このコード
MIT
依存物
uLipSync
https://github.com/hecomi/uLipSync
http://tips.hecomi.com/entry/2021/01/10/001929
The MIT License (MIT)
Copyright (c) 2021 hecomi
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
UniVRM
https://github.com/vrm-c/UniVRM
MIT License
Copyright (c) 2020 VRM Consortium
Copyright (c) 2018 Masataka SUMI for MToonPermission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
uLipSyncVRMBlendShapeProxyEditor.cs はEditorフォルダに入れるか #if UNITY_EDITOR ~#endif で囲う