BlendShapeKeyのインタフェースを厳格化、整理

#399 #400

* BlendShapeClip.Key 追加
* BlendShapeKey(string name, BlendShapePreset preset) を private に。代わりに、CreateFromPreset または CreateUnknown を使用してください
* BlendShapeKey.CreateFromClip(BlendShapeKey.CreateFrom からリネーム)
* BlendShapeKey.CreateFromPreset(new BlendShpaeKey(BlendShapePreset)からリネーム)
* BlendShapeKey.CreateUnknown(new BlendShpaeKey(string)からリネーム)
* BlendShapeKey.m_name, Preset を readonly
This commit is contained in:
ousttrue
2020-06-09 13:29:19 +09:00
parent 1bd6cdab75
commit d4c17839a3
9 changed files with 127 additions and 63 deletions

View File

@@ -165,7 +165,7 @@ namespace VRM
public override string GetInfoString()
{
return BlendShapeKey.CreateFrom((BlendShapeClip)target).ToString();
return BlendShapeKey.CreateFromClip((BlendShapeClip)target).ToString();
}
}
}

View File

@@ -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);

View File

@@ -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()
;
}

View File

@@ -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<BlendShapeKey, float>();
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)));
}
}
}
}

View File

@@ -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));
}
}
}

View File

@@ -117,6 +117,12 @@ namespace VRM
[SerializeField]
public BlendShapePreset Preset;
/// <summary>
/// BlendShapeClipに対応するBlendShapeKey
/// </summary>
/// <returns></returns>
public BlendShapeKey Key => BlendShapeKey.CreateFromClip(this);
/// <summary>
/// BlendShapeに対する参照(index ベース)
/// </summary>

View File

@@ -18,14 +18,14 @@ namespace VRM
/// </summary>
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)
/// <summary>
/// name と preset のペアからBlendShapeKeyを生成するが、
/// BlendShapePreset.Unknown のときと、それ以外のときで挙動が異なることを知っている必要があって、わかりにくいので private に変更。
/// v0.56
///
/// 代わりに、public static 関数を使って生成します
///
/// CreateFromPreset(BlendShapePreset)
/// CreateFromClip(BlendShapeClip)
/// CreateUnknown(string)
///
/// TODO ?
/// 旧仕様(GC発生などでパフォーマンスは、あまりよろしくない)
/// CreateLegacyFromString(string);
///
/// </summary>
/// <param name="name"></param>
/// <param name="preset"></param>
private BlendShapeKey(string name, BlendShapePreset preset)
{
m_name = name;
Preset = preset;
@@ -84,6 +98,41 @@ namespace VRM
}
}
/// <summary>
/// PresetからBlendShapeKeyを生成
/// </summary>
/// <param name="preset"></param>
/// <returns></returns>
public static BlendShapeKey CreateFromPreset(BlendShapePreset preset)
{
return new BlendShapeKey(preset.ToString(), preset);
}
/// <summary>
/// BlendShapeClipからBlendShapeKeyを生成
/// </summary>
/// <param name="clip"></param>
/// <returns></returns>
public static BlendShapeKey CreateFromClip(BlendShapeClip clip)
{
if (clip == null)
{
return default(BlendShapeKey);
}
return new BlendShapeKey(clip.BlendShapeName, clip.Preset);
}
/// <summary>
/// BlendShapePreset.Unknown である BlendShapeKey を name から作成する
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
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)

View File

@@ -30,7 +30,7 @@ namespace VRM
public BlendShapeMerger(IEnumerable<BlendShapeClip> 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<BlendShapeKey, float>();

View File

@@ -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<BlendShapeKey, float>(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);
}
}