mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-05-31 23:42:28 -05:00
BlendShapeProxyのEditorモード廃止とMaterialMorph対応
This commit is contained in:
parent
c9bec38fea
commit
bdd80e112e
|
|
@ -1,62 +0,0 @@
|
||||||
using UnityEditor;
|
|
||||||
using UnityEngine;
|
|
||||||
|
|
||||||
namespace VRM
|
|
||||||
{
|
|
||||||
class BlendShapeSlider
|
|
||||||
{
|
|
||||||
VRMBlendShapeProxy m_target;
|
|
||||||
BlendShapeKey m_key;
|
|
||||||
float? m_editorValue;
|
|
||||||
|
|
||||||
public BlendShapeSlider(VRMBlendShapeProxy target, BlendShapeKey key)
|
|
||||||
{
|
|
||||||
m_target = target;
|
|
||||||
m_key = key;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Slider()
|
|
||||||
{
|
|
||||||
if (m_target.BlendShapeAvatar == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Application.isPlaying)
|
|
||||||
{
|
|
||||||
var oldValue = m_target.GetValue(m_key);
|
|
||||||
var newValue = EditorGUILayout.Slider(m_key.ToString(), oldValue, 0, 1.0f);
|
|
||||||
if (oldValue != newValue)
|
|
||||||
{
|
|
||||||
m_target.SetValue(m_key, newValue);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var oldValue = m_editorValue.HasValue ? m_editorValue.Value : 0.0f;
|
|
||||||
var newValue = EditorGUILayout.Slider(m_key.ToString(), oldValue, 0, 1.0f);
|
|
||||||
if (oldValue != newValue)
|
|
||||||
{
|
|
||||||
var clip = m_target.BlendShapeAvatar.GetClip(m_key);
|
|
||||||
if (clip != null)
|
|
||||||
{
|
|
||||||
clip.Apply(m_target.transform, newValue);
|
|
||||||
m_editorValue = newValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void RestoreEditorValue()
|
|
||||||
{
|
|
||||||
if (m_editorValue.HasValue)
|
|
||||||
{
|
|
||||||
var clip = m_target.BlendShapeAvatar.GetClip(m_key);
|
|
||||||
if (clip != null)
|
|
||||||
{
|
|
||||||
clip.Apply(m_target.transform, m_editorValue.Value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
||||||
fileFormatVersion: 2
|
|
||||||
guid: 84dd38a83c8fb594484159a563312a26
|
|
||||||
timeCreated: 1520579905
|
|
||||||
licenseType: Free
|
|
||||||
MonoImporter:
|
|
||||||
serializedVersion: 2
|
|
||||||
defaultReferences: []
|
|
||||||
executionOrder: 0
|
|
||||||
icon: {instanceID: 0}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
using System.Collections.Generic;
|
using UnityEditor;
|
||||||
using System.Linq;
|
|
||||||
using UnityEditor;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -11,29 +9,19 @@ namespace VRM
|
||||||
{
|
{
|
||||||
VRMBlendShapeProxy m_target;
|
VRMBlendShapeProxy m_target;
|
||||||
SkinnedMeshRenderer[] m_renderers;
|
SkinnedMeshRenderer[] m_renderers;
|
||||||
List<BlendShapeSlider> m_sliders;
|
|
||||||
|
|
||||||
void OnEnable()
|
void OnEnable()
|
||||||
{
|
{
|
||||||
m_target = (VRMBlendShapeProxy)target;
|
m_target = (VRMBlendShapeProxy)target;
|
||||||
|
|
||||||
if (m_target.BlendShapeAvatar != null)
|
|
||||||
{
|
|
||||||
m_sliders = m_target.BlendShapeAvatar.Clips
|
|
||||||
.Where(x => x != null)
|
|
||||||
.Select(x => new BlendShapeSlider(m_target, BlendShapeKey.CreateFrom(x)))
|
|
||||||
.ToList()
|
|
||||||
;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void OnInspectorGUI()
|
public override void OnInspectorGUI()
|
||||||
{
|
{
|
||||||
base.OnInspectorGUI();
|
base.OnInspectorGUI();
|
||||||
|
|
||||||
if (m_sliders != null)
|
if (m_target.Sliders != null)
|
||||||
{
|
{
|
||||||
foreach (var slider in m_sliders)
|
foreach (var slider in m_target.Sliders)
|
||||||
{
|
{
|
||||||
slider.Slider();
|
slider.Slider();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,10 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UniGLTF;
|
||||||
|
#if UNITY_EDITOR
|
||||||
|
using UnityEditor;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
namespace VRM
|
namespace VRM
|
||||||
|
|
@ -13,6 +16,51 @@ namespace VRM
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
public BlendShapeAvatar BlendShapeAvatar;
|
public BlendShapeAvatar BlendShapeAvatar;
|
||||||
|
|
||||||
|
#if UNITY_EDITOR
|
||||||
|
public class BlendShapeSlider
|
||||||
|
{
|
||||||
|
VRMBlendShapeProxy m_target;
|
||||||
|
BlendShapeKey m_key;
|
||||||
|
|
||||||
|
public BlendShapeSlider(VRMBlendShapeProxy target, BlendShapeKey key)
|
||||||
|
{
|
||||||
|
m_target = target;
|
||||||
|
m_key = key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Slider()
|
||||||
|
{
|
||||||
|
if (m_target.BlendShapeAvatar == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var oldValue = m_target.GetValue(m_key);
|
||||||
|
var newValue = EditorGUILayout.Slider(m_key.ToString(), oldValue, 0, 1.0f);
|
||||||
|
if (oldValue != newValue)
|
||||||
|
{
|
||||||
|
m_target.SetValue(m_key, newValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
List<BlendShapeSlider> m_sliders;
|
||||||
|
public List<BlendShapeSlider> Sliders
|
||||||
|
{
|
||||||
|
get { return m_sliders; }
|
||||||
|
}
|
||||||
|
private void SetupSliders()
|
||||||
|
{
|
||||||
|
if (BlendShapeAvatar != null && BlendShapeAvatar.Clips != null)
|
||||||
|
{
|
||||||
|
m_sliders = BlendShapeAvatar.Clips
|
||||||
|
.Where(x => x != null)
|
||||||
|
.Select(x => new BlendShapeSlider(this, BlendShapeKey.CreateFrom(x)))
|
||||||
|
.ToList()
|
||||||
|
;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
struct BlendShapePath
|
struct BlendShapePath
|
||||||
{
|
{
|
||||||
public String RelativePath;
|
public String RelativePath;
|
||||||
|
|
@ -69,7 +117,8 @@ namespace VRM
|
||||||
m_valueMap = new Dictionary<BlendShapeKey, float>();
|
m_valueMap = new Dictionary<BlendShapeKey, float>();
|
||||||
foreach (var kv in m_clipMap)
|
foreach (var kv in m_clipMap)
|
||||||
{
|
{
|
||||||
foreach (var binding in kv.Value.Values) {
|
foreach (var binding in kv.Value.Values)
|
||||||
|
{
|
||||||
|
|
||||||
if (!m_setterMap.ContainsKey(binding))
|
if (!m_setterMap.ContainsKey(binding))
|
||||||
{
|
{
|
||||||
|
|
@ -78,7 +127,7 @@ namespace VRM
|
||||||
if (_target != null)
|
if (_target != null)
|
||||||
{
|
{
|
||||||
target = _target.GetComponent<SkinnedMeshRenderer>();
|
target = _target.GetComponent<SkinnedMeshRenderer>();
|
||||||
}
|
}
|
||||||
if (target != null)
|
if (target != null)
|
||||||
{
|
{
|
||||||
m_setterMap.Add(binding, new BlendShapePathHandler
|
m_setterMap.Add(binding, new BlendShapePathHandler
|
||||||
|
|
@ -100,42 +149,42 @@ namespace VRM
|
||||||
|
|
||||||
public void Clear()
|
public void Clear()
|
||||||
{
|
{
|
||||||
foreach(var kv in m_setterMap)
|
foreach (var kv in m_setterMap)
|
||||||
{
|
{
|
||||||
kv.Value.Clear();
|
kv.Value.Clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Restore()
|
public void Restore(Dictionary<string, Material> materialMap)
|
||||||
{
|
{
|
||||||
foreach (var kv in m_valueMap.ToArray())
|
foreach (var kv in m_valueMap.ToArray())
|
||||||
{
|
{
|
||||||
SetValue(kv.Key, kv.Value, false);
|
SetValue(kv.Key, kv.Value, false, materialMap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Apply()
|
public void Apply()
|
||||||
{
|
{
|
||||||
foreach(var kv in m_setterMap)
|
foreach (var kv in m_setterMap)
|
||||||
{
|
{
|
||||||
kv.Value.Apply();
|
kv.Value.Apply();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetValue(BlendShapeKey key, float value, bool replace)
|
public void SetValue(BlendShapeKey key, float value, bool replace, Dictionary<string, Material> materialMap)
|
||||||
{
|
{
|
||||||
m_valueMap[key] = value;
|
m_valueMap[key] = value;
|
||||||
|
|
||||||
BlendShapeClip clip;
|
BlendShapeClip clip;
|
||||||
if(!m_clipMap.TryGetValue(key, out clip))
|
if (!m_clipMap.TryGetValue(key, out clip))
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach(var binding in clip.Values)
|
foreach (var binding in clip.Values)
|
||||||
{
|
{
|
||||||
BlendShapePathHandler handler;
|
BlendShapePathHandler handler;
|
||||||
if(m_setterMap.TryGetValue(binding, out handler))
|
if (m_setterMap.TryGetValue(binding, out handler))
|
||||||
{
|
{
|
||||||
if (replace)
|
if (replace)
|
||||||
{
|
{
|
||||||
|
|
@ -147,13 +196,20 @@ namespace VRM
|
||||||
{
|
{
|
||||||
// 積算
|
// 積算
|
||||||
handler.AddValue(binding.Weight * value);
|
handler.AddValue(binding.Weight * value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Debug.LogWarningFormat("'{0}' not found", binding);
|
Debug.LogWarningFormat("'{0}' not found", binding);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// materialの更新
|
||||||
|
foreach(var binding in clip.MaterialValues)
|
||||||
|
{
|
||||||
|
var propValue = binding.BaseValue + (binding.TargetValue - binding.BaseValue) * value;
|
||||||
|
materialMap[binding.MaterialName].SetColor(binding.ValueName, propValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public float GetValue(BlendShapeKey key)
|
public float GetValue(BlendShapeKey key)
|
||||||
|
|
@ -167,6 +223,47 @@ namespace VRM
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BlendShapeMerger m_merger;
|
BlendShapeMerger m_merger;
|
||||||
|
Dictionary<string, Material> m_materialMap;
|
||||||
|
private void Awake()
|
||||||
|
{
|
||||||
|
m_materialMap = new Dictionary<string, Material>();
|
||||||
|
foreach(var x in transform.Traverse())
|
||||||
|
{
|
||||||
|
var renderer = x.GetComponent<Renderer>();
|
||||||
|
if (renderer != null)
|
||||||
|
{
|
||||||
|
foreach (var y in renderer.sharedMaterials.Where(y => y != null))
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(y.name))
|
||||||
|
{
|
||||||
|
if (!m_materialMap.ContainsKey(y.name))
|
||||||
|
{
|
||||||
|
m_materialMap.Add(y.name, y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#if UNITY_EDITOR
|
||||||
|
SetupSliders();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDestroy()
|
||||||
|
{
|
||||||
|
if (m_materialMap != null)
|
||||||
|
{
|
||||||
|
foreach (var x in BlendShapeAvatar.Clips)
|
||||||
|
{
|
||||||
|
foreach (var y in x.MaterialValues)
|
||||||
|
{
|
||||||
|
// restore values
|
||||||
|
m_materialMap[y.MaterialName].SetColor(y.ValueName, y.BaseValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void Update()
|
private void Update()
|
||||||
{
|
{
|
||||||
|
|
@ -179,7 +276,7 @@ namespace VRM
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetValue(BlendShapePreset key, float value, bool apply=true)
|
public void SetValue(BlendShapePreset key, float value, bool apply = true)
|
||||||
{
|
{
|
||||||
SetValue(new BlendShapeKey(key), value, apply);
|
SetValue(new BlendShapeKey(key), value, apply);
|
||||||
}
|
}
|
||||||
|
|
@ -187,7 +284,7 @@ namespace VRM
|
||||||
{
|
{
|
||||||
return GetValue(new BlendShapeKey(key));
|
return GetValue(new BlendShapeKey(key));
|
||||||
}
|
}
|
||||||
public void SetValue(String key, float value, bool apply=true)
|
public void SetValue(String key, float value, bool apply = true)
|
||||||
{
|
{
|
||||||
SetValue(new BlendShapeKey(key), value, apply);
|
SetValue(new BlendShapeKey(key), value, apply);
|
||||||
}
|
}
|
||||||
|
|
@ -195,11 +292,11 @@ namespace VRM
|
||||||
{
|
{
|
||||||
return GetValue(new BlendShapeKey(key));
|
return GetValue(new BlendShapeKey(key));
|
||||||
}
|
}
|
||||||
public void SetValue(BlendShapeKey key, float value, bool apply=true)
|
public void SetValue(BlendShapeKey key, float value, bool apply = true)
|
||||||
{
|
{
|
||||||
if (m_merger != null)
|
if (m_merger != null)
|
||||||
{
|
{
|
||||||
m_merger.SetValue(key, value, apply);
|
m_merger.SetValue(key, value, apply, m_materialMap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public float GetValue(BlendShapeKey key)
|
public float GetValue(BlendShapeKey key)
|
||||||
|
|
@ -223,7 +320,7 @@ namespace VRM
|
||||||
{
|
{
|
||||||
if (m_merger != null)
|
if (m_merger != null)
|
||||||
{
|
{
|
||||||
m_merger.Restore();
|
m_merger.Restore(m_materialMap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -341,7 +341,8 @@ namespace VRM
|
||||||
{
|
{
|
||||||
MaterialName = x.materialName,
|
MaterialName = x.materialName,
|
||||||
ValueName = x.propertyName,
|
ValueName = x.propertyName,
|
||||||
TargetValue = value
|
TargetValue = value,
|
||||||
|
BaseValue = context.Materials.First(y => y.name==x.materialName).GetColor(x.propertyName),
|
||||||
};
|
};
|
||||||
})
|
})
|
||||||
.ToArray();
|
.ToArray();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user