From 6d5e3f871d3c8a351d428099b306ec738a3f4dd0 Mon Sep 17 00:00:00 2001 From: TORISOUP Date: Fri, 28 Dec 2018 20:25:26 +0900 Subject: [PATCH] optimize comparing struct in dictionary key --- Scripts/BlendShape/BlendShapeBindingMerger.cs | 9 ++-- Scripts/BlendShape/BlendShapeClip.cs | 51 +++++++++++++++++-- .../BlendShape/MaterialValueBindingMerger.cs | 20 +++++++- 3 files changed, 72 insertions(+), 8 deletions(-) diff --git a/Scripts/BlendShape/BlendShapeBindingMerger.cs b/Scripts/BlendShape/BlendShapeBindingMerger.cs index 9de217d35..fdd41b71f 100644 --- a/Scripts/BlendShape/BlendShapeBindingMerger.cs +++ b/Scripts/BlendShape/BlendShapeBindingMerger.cs @@ -9,7 +9,7 @@ namespace VRM /// class BlendShapeBindingMerger { - class BlendShapeBindingComparer : IEqualityComparer + class DictionaryKeyBlendShapeBindingComparer : IEqualityComparer { public bool Equals(BlendShapeBinding x, BlendShapeBinding y) { @@ -22,19 +22,22 @@ namespace VRM return obj.RelativePath.GetHashCode() + obj.Index; } } + + private static DictionaryKeyBlendShapeBindingComparer comparer = new DictionaryKeyBlendShapeBindingComparer(); + /// /// BlendShapeの適用値を蓄積する /// /// /// /// - Dictionary m_blendShapeValueMap = new Dictionary(new BlendShapeBindingComparer()); + Dictionary m_blendShapeValueMap = new Dictionary(comparer); /// /// /// /// - Dictionary> m_blendShapeSetterMap = new Dictionary>(); + Dictionary> m_blendShapeSetterMap = new Dictionary>(comparer); public BlendShapeBindingMerger(Dictionary clipMap, Transform root) { diff --git a/Scripts/BlendShape/BlendShapeClip.cs b/Scripts/BlendShape/BlendShapeClip.cs index 6f3a5f3a2..4902902d7 100644 --- a/Scripts/BlendShape/BlendShapeClip.cs +++ b/Scripts/BlendShape/BlendShapeClip.cs @@ -4,8 +4,8 @@ using UnityEngine; namespace VRM { - [Serializable] - public struct BlendShapeBinding + [Serializable] + public struct BlendShapeBinding : IEquatable { public String RelativePath; public int Index; @@ -15,15 +15,60 @@ namespace VRM { return string.Format("{0}[{1}]=>{2}", RelativePath, Index, Weight); } + + public bool Equals(BlendShapeBinding other) + { + return string.Equals(RelativePath, other.RelativePath) && Index == other.Index && Weight.Equals(other.Weight); + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is BlendShapeBinding && Equals((BlendShapeBinding)obj); + } + + public override int GetHashCode() + { + unchecked + { + var hashCode = (RelativePath != null ? RelativePath.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ Index; + hashCode = (hashCode * 397) ^ Weight.GetHashCode(); + return hashCode; + } + } } [Serializable] - public struct MaterialValueBinding + public struct MaterialValueBinding : IEquatable { public String MaterialName; public String ValueName; public Vector4 TargetValue; public Vector4 BaseValue; + + public bool Equals(MaterialValueBinding other) + { + return string.Equals(MaterialName, other.MaterialName) && string.Equals(ValueName, other.ValueName) && TargetValue.Equals(other.TargetValue) && BaseValue.Equals(other.BaseValue); + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MaterialValueBinding && Equals((MaterialValueBinding)obj); + } + + public override int GetHashCode() + { + unchecked + { + var hashCode = (MaterialName != null ? MaterialName.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ (ValueName != null ? ValueName.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ TargetValue.GetHashCode(); + hashCode = (hashCode * 397) ^ BaseValue.GetHashCode(); + return hashCode; + } + } } [CreateAssetMenu(menuName = "VRM/BlendShapeClip")] diff --git a/Scripts/BlendShape/MaterialValueBindingMerger.cs b/Scripts/BlendShape/MaterialValueBindingMerger.cs index f626e483d..0ec9e5b36 100644 --- a/Scripts/BlendShape/MaterialValueBindingMerger.cs +++ b/Scripts/BlendShape/MaterialValueBindingMerger.cs @@ -11,6 +11,22 @@ namespace VRM /// class MaterialValueBindingMerger { + + struct DictionaryKeyMaterialValueBindingComparer : IEqualityComparer + { + public bool Equals(MaterialValueBinding x, MaterialValueBinding y) + { + return x.TargetValue == y.TargetValue && x.BaseValue == y.BaseValue && x.MaterialName == y.MaterialName && x.ValueName == y.ValueName; + } + + public int GetHashCode(MaterialValueBinding obj) + { + return obj.GetHashCode(); + } + } + + static DictionaryKeyMaterialValueBindingComparer comparer = new DictionaryKeyMaterialValueBindingComparer(); + /// /// 名前とmaterialのマッピング /// @@ -24,9 +40,9 @@ namespace VRM /// /// /// - Dictionary m_materialValueMap = new Dictionary(); + Dictionary m_materialValueMap = new Dictionary(comparer); - Dictionary m_materialSetterMap = new Dictionary(); + Dictionary m_materialSetterMap = new Dictionary(comparer); //BlendShapeClip[] m_clips;