From bdd80e112e549f581142fb4e2230d577f1033cb4 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 9 Apr 2018 20:24:52 +0900 Subject: [PATCH] =?UTF-8?q?BlendShapeProxy=E3=81=AEEditor=E3=83=A2?= =?UTF-8?q?=E3=83=BC=E3=83=89=E5=BB=83=E6=AD=A2=E3=81=A8MaterialMorph?= =?UTF-8?q?=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Scripts/BlendShape/Editor/BlendShapeSlider.cs | 62 --------- .../Editor/BlendShapeSlider.cs.meta | 12 -- .../Editor/VRMBlnedShapeProxyEditor.cs | 18 +-- Scripts/BlendShape/VRMBlendShapeProxy.cs | 131 +++++++++++++++--- Scripts/Format/VRMImporter.cs | 3 +- 5 files changed, 119 insertions(+), 107 deletions(-) delete mode 100644 Scripts/BlendShape/Editor/BlendShapeSlider.cs delete mode 100644 Scripts/BlendShape/Editor/BlendShapeSlider.cs.meta diff --git a/Scripts/BlendShape/Editor/BlendShapeSlider.cs b/Scripts/BlendShape/Editor/BlendShapeSlider.cs deleted file mode 100644 index ba220befe..000000000 --- a/Scripts/BlendShape/Editor/BlendShapeSlider.cs +++ /dev/null @@ -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); - } - } - } - } -} diff --git a/Scripts/BlendShape/Editor/BlendShapeSlider.cs.meta b/Scripts/BlendShape/Editor/BlendShapeSlider.cs.meta deleted file mode 100644 index ef59fb868..000000000 --- a/Scripts/BlendShape/Editor/BlendShapeSlider.cs.meta +++ /dev/null @@ -1,12 +0,0 @@ -fileFormatVersion: 2 -guid: 84dd38a83c8fb594484159a563312a26 -timeCreated: 1520579905 -licenseType: Free -MonoImporter: - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Scripts/BlendShape/Editor/VRMBlnedShapeProxyEditor.cs b/Scripts/BlendShape/Editor/VRMBlnedShapeProxyEditor.cs index 49be8d1ab..6a7408b31 100644 --- a/Scripts/BlendShape/Editor/VRMBlnedShapeProxyEditor.cs +++ b/Scripts/BlendShape/Editor/VRMBlnedShapeProxyEditor.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; -using System.Linq; -using UnityEditor; +using UnityEditor; using UnityEngine; @@ -11,29 +9,19 @@ namespace VRM { VRMBlendShapeProxy m_target; SkinnedMeshRenderer[] m_renderers; - List m_sliders; void OnEnable() { 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() { 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(); } diff --git a/Scripts/BlendShape/VRMBlendShapeProxy.cs b/Scripts/BlendShape/VRMBlendShapeProxy.cs index 43155003d..111bbe870 100644 --- a/Scripts/BlendShape/VRMBlendShapeProxy.cs +++ b/Scripts/BlendShape/VRMBlendShapeProxy.cs @@ -2,7 +2,10 @@ using System.Collections.Generic; using System.Linq; using UnityEngine; - +using UniGLTF; +#if UNITY_EDITOR +using UnityEditor; +#endif namespace VRM @@ -13,6 +16,51 @@ namespace VRM [SerializeField] 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 m_sliders; + public List 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 { public String RelativePath; @@ -69,7 +117,8 @@ namespace VRM m_valueMap = new Dictionary(); 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)) { @@ -78,7 +127,7 @@ namespace VRM if (_target != null) { target = _target.GetComponent(); - } + } if (target != null) { m_setterMap.Add(binding, new BlendShapePathHandler @@ -100,42 +149,42 @@ namespace VRM public void Clear() { - foreach(var kv in m_setterMap) + foreach (var kv in m_setterMap) { kv.Value.Clear(); } } - public void Restore() + public void Restore(Dictionary materialMap) { foreach (var kv in m_valueMap.ToArray()) { - SetValue(kv.Key, kv.Value, false); + SetValue(kv.Key, kv.Value, false, materialMap); } } public void Apply() { - foreach(var kv in m_setterMap) + foreach (var kv in m_setterMap) { kv.Value.Apply(); } } - public void SetValue(BlendShapeKey key, float value, bool replace) + public void SetValue(BlendShapeKey key, float value, bool replace, Dictionary materialMap) { m_valueMap[key] = value; BlendShapeClip clip; - if(!m_clipMap.TryGetValue(key, out clip)) + if (!m_clipMap.TryGetValue(key, out clip)) { return; } - foreach(var binding in clip.Values) + foreach (var binding in clip.Values) { BlendShapePathHandler handler; - if(m_setterMap.TryGetValue(binding, out handler)) + if (m_setterMap.TryGetValue(binding, out handler)) { if (replace) { @@ -147,13 +196,20 @@ namespace VRM { // 積算 handler.AddValue(binding.Weight * value); - } + } } else { 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) @@ -167,6 +223,47 @@ namespace VRM } } BlendShapeMerger m_merger; + Dictionary m_materialMap; + private void Awake() + { + m_materialMap = new Dictionary(); + foreach(var x in transform.Traverse()) + { + var renderer = x.GetComponent(); + 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() { @@ -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); } @@ -187,7 +284,7 @@ namespace VRM { 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); } @@ -195,11 +292,11 @@ namespace VRM { 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) { - m_merger.SetValue(key, value, apply); + m_merger.SetValue(key, value, apply, m_materialMap); } } public float GetValue(BlendShapeKey key) @@ -223,7 +320,7 @@ namespace VRM { if (m_merger != null) { - m_merger.Restore(); + m_merger.Restore(m_materialMap); } } diff --git a/Scripts/Format/VRMImporter.cs b/Scripts/Format/VRMImporter.cs index 549a30f34..c1374b52c 100644 --- a/Scripts/Format/VRMImporter.cs +++ b/Scripts/Format/VRMImporter.cs @@ -341,7 +341,8 @@ namespace VRM { MaterialName = x.materialName, ValueName = x.propertyName, - TargetValue = value + TargetValue = value, + BaseValue = context.Materials.First(y => y.name==x.materialName).GetColor(x.propertyName), }; }) .ToArray();