diff --git a/Assets/VRM/UniVRM/Editor/BlendShape/BlendShapeClipEditor.cs b/Assets/VRM/UniVRM/Editor/BlendShape/BlendShapeClipEditor.cs index 2821163e2..4186b68cb 100644 --- a/Assets/VRM/UniVRM/Editor/BlendShape/BlendShapeClipEditor.cs +++ b/Assets/VRM/UniVRM/Editor/BlendShape/BlendShapeClipEditor.cs @@ -165,7 +165,7 @@ namespace VRM public override string GetInfoString() { - return BlendShapeKey.CreateFrom((BlendShapeClip)target).ToString(); + return BlendShapeKey.CreateFromClip((BlendShapeClip)target).ToString(); } } } diff --git a/Assets/VRM/UniVRM/Editor/BlendShape/BlendShapeClipSelector.cs b/Assets/VRM/UniVRM/Editor/BlendShape/BlendShapeClipSelector.cs index c3853c35e..2865ed6ac 100644 --- a/Assets/VRM/UniVRM/Editor/BlendShape/BlendShapeClipSelector.cs +++ b/Assets/VRM/UniVRM/Editor/BlendShape/BlendShapeClipSelector.cs @@ -62,7 +62,7 @@ namespace VRM EditorGUILayout.LabelField("Select BlendShapeClip", EditorStyles.boldLabel); var array = m_avatar.Clips .Select(x => x != null - ? BlendShapeKey.CreateFrom(x).ToString() + ? BlendShapeKey.CreateFromClip(x).ToString() : "null" ).ToArray(); SelectedIndex = GUILayout.SelectionGrid(SelectedIndex, array, 4); @@ -88,7 +88,7 @@ namespace VRM public void DuplicateWarn() { - var key = BlendShapeKey.CreateFrom(Selected); + var key = BlendShapeKey.CreateFromClip(Selected); if (m_avatar.Clips.Where(x => key.Match(x)).Count() > 1) { EditorGUILayout.HelpBox("duplicate clip: " + key, MessageType.Error); diff --git a/Assets/VRM/UniVRM/Editor/BlendShape/VRMBlendShapeProxyEditor.cs b/Assets/VRM/UniVRM/Editor/BlendShape/VRMBlendShapeProxyEditor.cs index 92a26a833..743bb5eb3 100644 --- a/Assets/VRM/UniVRM/Editor/BlendShape/VRMBlendShapeProxyEditor.cs +++ b/Assets/VRM/UniVRM/Editor/BlendShape/VRMBlendShapeProxyEditor.cs @@ -42,7 +42,7 @@ namespace VRM { m_sliders = m_target.BlendShapeAvatar.Clips .Where(x => x != null) - .Select(x => new BlendShapeSlider(m_target, BlendShapeKey.CreateFrom(x))) + .Select(x => new BlendShapeSlider(m_target, BlendShapeKey.CreateFromClip(x))) .ToList() ; } diff --git a/Assets/VRM/UniVRM/Editor/Tests/VRMBlendShapeKeyTest.cs b/Assets/VRM/UniVRM/Editor/Tests/VRMBlendShapeKeyTest.cs index 8262333cc..f72a18d20 100644 --- a/Assets/VRM/UniVRM/Editor/Tests/VRMBlendShapeKeyTest.cs +++ b/Assets/VRM/UniVRM/Editor/Tests/VRMBlendShapeKeyTest.cs @@ -1,42 +1,51 @@ using NUnit.Framework; +using System; using System.Collections.Generic; -using VRM; +using System.Reflection; namespace VRM { public class VRMBlendShapeKeyTest { + static BlendShapeKey CreateBlendShapeKey(string name, BlendShapePreset preset) + { + var argTypes = new Type[] { typeof(string), typeof(BlendShapePreset) }; + // private constructor + var constructor = typeof(BlendShapeKey).GetConstructor( + BindingFlags.Instance | BindingFlags.NonPublic, + null, argTypes, null); + return (BlendShapeKey)constructor.Invoke(new object[] { name, preset }); + } + [Test] public void KeyTest() { - var key = new BlendShapeKey("Blink", BlendShapePreset.Blink); - - Assert.AreEqual(key, new BlendShapeKey("Blink", BlendShapePreset.Blink)); - Assert.AreEqual(key, new BlendShapeKey(BlendShapePreset.Blink)); - Assert.AreEqual(key, new BlendShapeKey("xxx", BlendShapePreset.Blink)); + var key = CreateBlendShapeKey("Blink", BlendShapePreset.Blink); + Assert.AreEqual(key, CreateBlendShapeKey("Blink", BlendShapePreset.Blink)); + Assert.AreEqual(key, BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink)); + Assert.AreEqual(key, CreateBlendShapeKey("xxx", BlendShapePreset.Blink)); var dict = new Dictionary(); dict[key] = 1.0f; - Assert.IsTrue(dict.ContainsKey(new BlendShapeKey("Blink",BlendShapePreset.Blink))); - Assert.IsTrue(dict.ContainsKey(new BlendShapeKey(BlendShapePreset.Blink))); - Assert.IsTrue(dict.ContainsKey(new BlendShapeKey("xxx", BlendShapePreset.Blink))); - + Assert.IsTrue(dict.ContainsKey(CreateBlendShapeKey("Blink", BlendShapePreset.Blink))); + Assert.IsTrue(dict.ContainsKey(BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink))); + Assert.IsTrue(dict.ContainsKey(CreateBlendShapeKey("xxx", BlendShapePreset.Blink))); + dict.Clear(); - - var key2 = new BlendShapeKey("Blink"); // name: Blink, Preset: Unknown + + var key2 = BlendShapeKey.CreateUnknown("Blink"); // name: Blink, Preset: Unknown dict[key2] = 1.0f; - - Assert.AreEqual( key2, new BlendShapeKey("Blink", BlendShapePreset.Unknown)); - Assert.AreNotEqual(key2, new BlendShapeKey("blink")); - Assert.AreNotEqual(key2, new BlendShapeKey("Blink", BlendShapePreset.Blink)); - Assert.AreNotEqual(key2, new BlendShapeKey(BlendShapePreset.Blink)); - - Assert.IsFalse(dict.ContainsKey(new BlendShapeKey("blink"))); - Assert.IsFalse(dict.ContainsKey(new BlendShapeKey("Blink",BlendShapePreset.Blink))); - Assert.IsFalse(dict.ContainsKey(new BlendShapeKey(BlendShapePreset.Blink))); - + + Assert.AreEqual(key2, CreateBlendShapeKey("Blink", BlendShapePreset.Unknown)); + Assert.AreNotEqual(key2, BlendShapeKey.CreateUnknown("blink")); + Assert.AreNotEqual(key2, CreateBlendShapeKey("Blink", BlendShapePreset.Blink)); + Assert.AreNotEqual(key2, BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink)); + + Assert.IsFalse(dict.ContainsKey(BlendShapeKey.CreateUnknown("blink"))); + Assert.IsFalse(dict.ContainsKey(CreateBlendShapeKey("Blink", BlendShapePreset.Blink))); + Assert.IsFalse(dict.ContainsKey(BlendShapeKey.CreateFromPreset(BlendShapePreset.Blink))); } } -} \ No newline at end of file +} diff --git a/Assets/VRM/UniVRM/Scripts/BlendShape/BlendShapeAvatar.cs b/Assets/VRM/UniVRM/Scripts/BlendShape/BlendShapeAvatar.cs index 62b164b5c..6129050e3 100644 --- a/Assets/VRM/UniVRM/Scripts/BlendShape/BlendShapeAvatar.cs +++ b/Assets/VRM/UniVRM/Scripts/BlendShape/BlendShapeAvatar.cs @@ -57,7 +57,7 @@ namespace VRM Debug.LogFormat("{0}", clip.name); } - Clips = Clips.OrderBy(x => BlendShapeKey.CreateFrom(x)).ToList(); + Clips = Clips.OrderBy(x => BlendShapeKey.CreateFromClip(x)).ToList(); } static public BlendShapeClip CreateBlendShapeClip(string path) @@ -138,12 +138,12 @@ namespace VRM public BlendShapeClip GetClip(BlendShapePreset preset) { - return GetClip(new BlendShapeKey(preset)); + return GetClip(BlendShapeKey.CreateFromPreset(preset)); } public BlendShapeClip GetClip(String name) { - return GetClip(new BlendShapeKey(name)); + return GetClip(BlendShapeKey.CreateUnknown(name)); } } } diff --git a/Assets/VRM/UniVRM/Scripts/BlendShape/BlendShapeClip.cs b/Assets/VRM/UniVRM/Scripts/BlendShape/BlendShapeClip.cs index 69ede711e..ab560ca8e 100644 --- a/Assets/VRM/UniVRM/Scripts/BlendShape/BlendShapeClip.cs +++ b/Assets/VRM/UniVRM/Scripts/BlendShape/BlendShapeClip.cs @@ -117,6 +117,12 @@ namespace VRM [SerializeField] public BlendShapePreset Preset; + /// + /// BlendShapeClipに対応するBlendShapeKey + /// + /// + public BlendShapeKey Key => BlendShapeKey.CreateFromClip(this); + /// /// BlendShapeに対する参照(index ベース) /// diff --git a/Assets/VRM/UniVRM/Scripts/BlendShape/BlendShapeKey.cs b/Assets/VRM/UniVRM/Scripts/BlendShape/BlendShapeKey.cs index a03f9a1fe..d8b5611b0 100644 --- a/Assets/VRM/UniVRM/Scripts/BlendShape/BlendShapeKey.cs +++ b/Assets/VRM/UniVRM/Scripts/BlendShape/BlendShapeKey.cs @@ -18,14 +18,14 @@ namespace VRM /// private static readonly string UnknownPresetPrefix = "Unknown_"; - private string m_name; + private readonly string m_name; public string Name { get { return m_name; } } - public BlendShapePreset Preset; + public readonly BlendShapePreset Preset; string m_id; @@ -57,11 +57,25 @@ namespace VRM } } - public BlendShapeKey(BlendShapePreset preset) : this(preset.ToString(), preset) - { - } - - public BlendShapeKey(string name, BlendShapePreset preset = BlendShapePreset.Unknown) + /// + /// name と preset のペアからBlendShapeKeyを生成するが、 + /// BlendShapePreset.Unknown のときと、それ以外のときで挙動が異なることを知っている必要があって、わかりにくいので private に変更。 + /// v0.56 + /// + /// 代わりに、public static 関数を使って生成します + /// + /// CreateFromPreset(BlendShapePreset) + /// CreateFromClip(BlendShapeClip) + /// CreateUnknown(string) + /// + /// TODO ? + /// 旧仕様(GC発生などでパフォーマンスは、あまりよろしくない) + /// CreateLegacyFromString(string); + /// + /// + /// + /// + private BlendShapeKey(string name, BlendShapePreset preset) { m_name = name; Preset = preset; @@ -84,6 +98,41 @@ namespace VRM } } + /// + /// PresetからBlendShapeKeyを生成 + /// + /// + /// + public static BlendShapeKey CreateFromPreset(BlendShapePreset preset) + { + return new BlendShapeKey(preset.ToString(), preset); + } + + /// + /// BlendShapeClipからBlendShapeKeyを生成 + /// + /// + /// + public static BlendShapeKey CreateFromClip(BlendShapeClip clip) + { + if (clip == null) + { + return default(BlendShapeKey); + } + + return new BlendShapeKey(clip.BlendShapeName, clip.Preset); + } + + /// + /// BlendShapePreset.Unknown である BlendShapeKey を name から作成する + /// + /// + /// + public static BlendShapeKey CreateUnknown(string name) + { + return new BlendShapeKey(name, BlendShapePreset.Unknown); + } + public override string ToString() { return ID.Replace(UnknownPresetPrefix, "").ToUpper(); @@ -98,7 +147,7 @@ namespace VRM { if (obj is BlendShapeKey) { - return Equals((BlendShapeKey) obj); + return Equals((BlendShapeKey)obj); } else { @@ -111,19 +160,9 @@ namespace VRM return ID.GetHashCode(); } - public static BlendShapeKey CreateFrom(BlendShapeClip clip) - { - if (clip == null) - { - return default(BlendShapeKey); - } - - return new BlendShapeKey(clip.BlendShapeName, clip.Preset); - } - public bool Match(BlendShapeClip clip) { - return this.Equals(CreateFrom(clip)); + return this.Equals(CreateFromClip(clip)); } public int CompareTo(BlendShapeKey other) diff --git a/Assets/VRM/UniVRM/Scripts/BlendShape/BlendShapeMerger.cs b/Assets/VRM/UniVRM/Scripts/BlendShape/BlendShapeMerger.cs index 5cbd30c3f..02e60ab7b 100644 --- a/Assets/VRM/UniVRM/Scripts/BlendShape/BlendShapeMerger.cs +++ b/Assets/VRM/UniVRM/Scripts/BlendShape/BlendShapeMerger.cs @@ -30,7 +30,7 @@ namespace VRM public BlendShapeMerger(IEnumerable clips, Transform root) { - m_clipMap = clips.ToDictionary(x => BlendShapeKey.CreateFrom(x), x => x); + m_clipMap = clips.ToDictionary(x => BlendShapeKey.CreateFromClip(x), x => x); m_valueMap = new Dictionary(); diff --git a/Assets/VRM/UniVRM/Scripts/BlendShape/VRMBlendShapeProxy.cs b/Assets/VRM/UniVRM/Scripts/BlendShape/VRMBlendShapeProxy.cs index 9b750cc74..1721c6dab 100644 --- a/Assets/VRM/UniVRM/Scripts/BlendShape/VRMBlendShapeProxy.cs +++ b/Assets/VRM/UniVRM/Scripts/BlendShape/VRMBlendShapeProxy.cs @@ -83,7 +83,7 @@ namespace VRM { foreach (var clip in BlendShapeAvatar.Clips) { - var key = BlendShapeKey.CreateFrom(clip); + var key = BlendShapeKey.CreateFromClip(clip); yield return new KeyValuePair(key, m_merger.GetValue(key)); } } @@ -115,42 +115,52 @@ namespace VRM public static class VRMBlendShapeProxyExtensions { + [Obsolete("Use BlendShapeKey.CreateFromPreset")] public static float GetValue(this VRMBlendShapeProxy proxy, BlendShapePreset key) { - return proxy.GetValue(new BlendShapeKey(key)); + return proxy.GetValue(BlendShapeKey.CreateFromPreset(key)); } + [Obsolete("Use BlendShapeKey.CreateUnknown")] public static float GetValue(this VRMBlendShapeProxy proxy, String key) { - return proxy.GetValue(new BlendShapeKey(key)); + return proxy.GetValue(BlendShapeKey.CreateUnknown(key)); } [Obsolete("Use ImmediatelySetValue")] public static void SetValue(this VRMBlendShapeProxy proxy, BlendShapePreset key, float value) { - proxy.ImmediatelySetValue(new BlendShapeKey(key), value); + proxy.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(key), value); } + + [Obsolete("Use BlendShapeKey.CreateFromPreset")] public static void ImmediatelySetValue(this VRMBlendShapeProxy proxy, BlendShapePreset key, float value) { - proxy.ImmediatelySetValue(new BlendShapeKey(key), value); + proxy.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(key), value); } + + [Obsolete("Use BlendShapeKey.CreateFromPreset")] public static void AccumulateValue(this VRMBlendShapeProxy proxy, BlendShapePreset key, float value) { - proxy.AccumulateValue(new BlendShapeKey(key), value); + proxy.AccumulateValue(BlendShapeKey.CreateFromPreset(key), value); } [Obsolete("Use ImmediatelySetValue")] public static void SetValue(this VRMBlendShapeProxy proxy, String key, float value) { - proxy.ImmediatelySetValue(new BlendShapeKey(key), value); + proxy.ImmediatelySetValue(BlendShapeKey.CreateUnknown(key), value); } + + [Obsolete("Use BlendShapeKey.CreateUnknown")] public static void ImmediatelySetValue(this VRMBlendShapeProxy proxy, String key, float value) { - proxy.ImmediatelySetValue(new BlendShapeKey(key), value); + proxy.ImmediatelySetValue(BlendShapeKey.CreateUnknown(key), value); } + + [Obsolete("Use BlendShapeKey.CreateUnknown")] public static void AccumulateValue(this VRMBlendShapeProxy proxy, String key, float value) { - proxy.AccumulateValue(new BlendShapeKey(key), value); + proxy.AccumulateValue(BlendShapeKey.CreateUnknown(key), value); } [Obsolete("Use ImmediatelySetValue")] @@ -164,11 +174,11 @@ namespace VRM { if (apply) { - proxy.ImmediatelySetValue(new BlendShapeKey(key), value); + proxy.ImmediatelySetValue(BlendShapeKey.CreateFromPreset(key), value); } else { - proxy.AccumulateValue(new BlendShapeKey(key), value); + proxy.AccumulateValue(BlendShapeKey.CreateFromPreset(key), value); } } @@ -177,11 +187,11 @@ namespace VRM { if (apply) { - proxy.ImmediatelySetValue(new BlendShapeKey(key), value); + proxy.ImmediatelySetValue(BlendShapeKey.CreateUnknown(key), value); } else { - proxy.AccumulateValue(new BlendShapeKey(key), value); + proxy.AccumulateValue(BlendShapeKey.CreateUnknown(key), value); } }