Skip to content

Commit

Permalink
BlendShapeKeyのインタフェースを厳格化、整理
Browse files Browse the repository at this point in the history
vrm-c#399 vrm-c#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
  • Loading branch information
ousttrue committed Jun 9, 2020
1 parent 1bd6cda commit d4c1783
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public override void OnInspectorGUI()

public override string GetInfoString()
{
return BlendShapeKey.CreateFrom((BlendShapeClip)target).ToString();
return BlendShapeKey.CreateFromClip((BlendShapeClip)target).ToString();
}
}
}
4 changes: 2 additions & 2 deletions Assets/VRM/UniVRM/Editor/BlendShape/BlendShapeClipSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void SelectGUI()
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);
Expand All @@ -88,7 +88,7 @@ public void SelectGUI()

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void OnEnable()
{
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()
;
}
Expand Down
55 changes: 32 additions & 23 deletions Assets/VRM/UniVRM/Editor/Tests/VRMBlendShapeKeyTest.cs
Original file line number Diff line number Diff line change
@@ -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)));
}
}
}
}
6 changes: 3 additions & 3 deletions Assets/VRM/UniVRM/Scripts/BlendShape/BlendShapeAvatar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void Restore()

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)
Expand Down Expand Up @@ -138,12 +138,12 @@ public BlendShapeClip GetClip(BlendShapeKey key)

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));
}
}
}
6 changes: 6 additions & 0 deletions Assets/VRM/UniVRM/Scripts/BlendShape/BlendShapeClip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ public GameObject Prefab
[SerializeField]
public BlendShapePreset Preset;

/// <summary>
/// BlendShapeClipに対応するBlendShapeKey
/// </summary>
/// <returns></returns>
public BlendShapeKey Key => BlendShapeKey.CreateFromClip(this);

/// <summary>
/// BlendShapeに対する参照(index ベース)
/// </summary>
Expand Down
77 changes: 58 additions & 19 deletions Assets/VRM/UniVRM/Scripts/BlendShape/BlendShapeKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public struct BlendShapeKey : IEquatable<BlendShapeKey>, IComparable<BlendShapeK
/// </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;

Expand Down Expand Up @@ -57,11 +57,25 @@ string ID
}
}

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;
Expand All @@ -84,6 +98,41 @@ public BlendShapeKey(string name, BlendShapePreset preset = BlendShapePreset.Unk
}
}

/// <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();
Expand All @@ -98,7 +147,7 @@ public override bool Equals(object obj)
{
if (obj is BlendShapeKey)
{
return Equals((BlendShapeKey) obj);
return Equals((BlendShapeKey)obj);
}
else
{
Expand All @@ -111,19 +160,9 @@ public override int GetHashCode()
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)
Expand Down
2 changes: 1 addition & 1 deletion Assets/VRM/UniVRM/Scripts/BlendShape/BlendShapeMerger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class BlendShapeMerger

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

Expand Down
36 changes: 23 additions & 13 deletions Assets/VRM/UniVRM/Scripts/BlendShape/VRMBlendShapeProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public IEnumerable<KeyValuePair<BlendShapeKey, float>> GetValues()
{
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));
}
}
Expand Down Expand Up @@ -115,42 +115,52 @@ public void Apply()

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")]
Expand All @@ -164,11 +174,11 @@ public static void SetValue(this VRMBlendShapeProxy proxy, BlendShapePreset key,
{
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);
}
}

Expand All @@ -177,11 +187,11 @@ public static void SetValue(this VRMBlendShapeProxy proxy, String key, float val
{
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);
}
}

Expand Down

0 comments on commit d4c1783

Please sign in to comment.